<section class="content-header">
    <h1><?= __('Edit service') ?></h1>

    <div class="actions">
        <a data-ajax class="btn btn-default btn-sm" href="<?= admin_url_to('/services') ?>"><?= __('List') ?></a>
    </div>
</section>

<div class="modal" id="modal-add-rate"></div>

<section class="content">
    <div class="box box-default">
        <form data-ajax class="form-focus" action="<?= admin_url_to('/services/edit/' . $service['id']) ?>" method="post">
            <?= csrf_field() ?>

            <div class="box-body">
                <div class="section">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                            <h5><?= __('Service') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <div class="row">
                                <div class="col-md-8">
                                    <div class="form-group">
                                        <label class="control-label" for="name"><?= __('Name') ?> <span class="required">*</span></label>
                                        <input class="form-control" value="<?= $service['name'] ?>" type="text" name="name" id="name" />
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="allowed"><?= __('Allowed') ?>?</label>
                                        <div>
                                            <input class="form-control toggle"<?= $service['allowed'] ? ' checked' : '' ?> type="checkbox" id="allowed" value="1" name="allowed" />
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="active"><?= __('Active') ?>?</label>
                                        <div>
                                            <input class="form-control toggle"<?= $service['active'] ? ' checked' : '' ?> type="checkbox" id="active" value="1" name="active" />
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="service"><?= __('Service') ?></label>
                                        <input class="form-control" value="<?= $service['service'] ?>" type="text" name="service" id="service" />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="parent-id"><?= __('Parent service') ?></label>
                                        <select class="form-control" name="parent_id" id="parent-id">
                                            <option value=""></option>
                                            <?php foreach ($services as $ser): ?>
                                                <option<?= $service['parent_id'] == $ser['id'] ? ' selected' : '' ?> value="<?= $ser['id'] ?>"><?= $ser['name'] ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="control-label" for="url">URL</label>
                                <input class="form-control" value="<?= $service['url'] ?>" type="text" name="url" id="url" />
                            </div>

                            <div class="form-group">
                                <label class="control-label"><?= __('Allowed to') ?></label>
                                <div>
                                    <?php foreach (config('vip.types', []) as $id => $name): ?>
                                        <div style="width:25%;float:left">
                                            <input type="checkbox"<?= array_key_exists($id, $service['viptypes']) ? ' checked' : '' ?> id="services-vip<?= $id ?>" name="viptype[<?= $id ?>]" />
                                            <label for="services-vip<?= $id ?>"><?= $name ?></label>
                                        </div>
                                    <?php endforeach ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="section">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                            <h5><?= __('Rates') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <?php if (empty($service['rates'])): ?>
                                <div class="callout callout-warning">
                                    <p><?= __('No rates found') ?></p>
                                </div>
                            <?php else: ?>
                                <table id="services-table" class="table table-condensed table-hover table-striped">
                                    <thead>
                                        <tr>
                                            <th><?= __('Type') ?></th>
                                            <th><?= __('Condition') ?></th>
                                            <th><?= __('Amount') ?></th>
                                            <th width="10"></th>
                                        </tr>
                                    </thead>

                                    <tbody>
                                        <?php foreach($service['rates'] as $index => $rate): ?>
                                            <tr>
                                                <td>
                                                    <?= __($rate['type']) ?>
                                                    <?php if ($rate['type'] === 'coin'): ?>
                                                        (<?= config('coins')[$rate['coin']]['name'] ?>)
                                                    <?php else: ?>
                                                        (<?= __('Character')?>)
                                                    <?php endif ?>
                                                </td>
                                                <td><?= util('Text')->toList(array_map(function ($a) {return config('vip.types')[$a];}, $rate['conditions'])) ?></td>
                                                <td><?= util('Number')->format($rate['amount']) ?></td>
                                                <td>
                                                    <a data-ajax-delete class="btn btn-xs btn-danger" href="<?= admin_url_to('/services/delete_rate/' . $service['id'] . '/' . $index) ?>"><?= __('Delete') ?></a>
                                                </td>
                                            </tr>
                                        <?php endforeach ?>
                                    </tbody>
                                </table>
                            <?php endif ?>

                            <div style="text-align:right;margin-top:10px">
                                <a data-load-with-modal data-modal="modal-add-rate" class="btn btn-xs btn-success" href="<?= admin_url_to('/services/add_rate/' . $service['id']) ?>"><?= __('Add') ?></a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="box-footer">
                <div class="row">
                    <div class="col-sm-offset-3" style="padding-left:15px">
                        <input type="submit" class="btn btn-primary" value="<?= __('Save') ?>" />
                    </div>
                </div>
            </div>
        </form>
    </div>
</section>