<script type="text/javascript">
    $(function () {
        $("#services-table tbody").sortable({
            opacity: 0.6,
            cursor: "move",
            placeholder: "placeholder",
            helper: fixHelper,
            axis: "y",
            items: ".drag",
            update: function() {
                var order = $(this).sortable("serialize");
                $.post(prefix + "services/reorder", order, function(data) {
                });
            }
        });
    });
</script>

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

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

<section class="content">
    <?php if (empty($services)): ?>
        <div class="callout callout-warning">
            <h4>Ops!</h4>
            <p><?= __('No services found') ?></p>
        </div>
    <?php else: ?>

        <div class="box box-default table-responsive">
            <table id="services-table" class="table table-condensed table-hover table-striped">
                <thead>
                    <tr>
                        <th><?= __('Service') ?></th>
                        <th width="120"><?= __('Released to') ?></th>
                        <th></th>
                        <th width="10"></th>
                    </tr>
                </thead>

                <?php foreach ($services as $root): ?>
                    <tbody>
                        <tr class="root <?= $root['active'] ? 'border-success' : 'border-error' ?>">
                            <td colspan="3">
                                <strong><?= $root['name'] ?></strong>
                            </td>
                            <td class="record-actions">
                                <?php if (!empty($root['config_url'])): ?>
                                    <a data-ajax class="btn btn-xs btn-default" href="<?= admin_url_to('' . $root['config_url']) ?>"><?= __('Config') ?></a>
                                <?php endif ?>
                                <a data-ajax class="btn btn-default btn-xs" href="<?= admin_url_to('/services/edit/' . $root['id']) ?>"><?= __('Edit') ?></a>
                                <a data-ajax-delete class="btn btn-xs btn-danger" href="<?= admin_url_to('/services/delete/' . $root['id']) ?>"><?= __('Delete') ?></a>
                            </td>
                        </tr>
                        <?php foreach ($root['children'] as $service): ?>
                            <tr id="sequence_<?= $service['id'] ?>" class="drag <?= $service['active'] ? 'border-success' : 'border-error' ?>">
                                <td><i class="fa fa-bars"></i>&nbsp;&nbsp;&nbsp;&nbsp;<?= $service['name'] ?></td>
                                <td class="record-actions" style="text-align:left">
                                    <?php if ($service['allowed']): ?>
                                        <span class="label label-success"><?= __('Free for all') ?></span>
                                    <?php else: ?>
                                        <?= util('Text')->toList($service['viptypes']) ?>
                                    <?php endif ?>
                                </td>
                                <td></td>
                                <td class="record-actions">
                                    <?php if (!empty($service['config_url'])): ?>
                                        <a data-ajax class="btn btn-xs btn-default" href="<?= admin_url_to('' . $service['config_url']) ?>"><?= __('Config') ?></a>
                                    <?php endif ?>
                                    <a data-ajax class="btn btn-default btn-xs" href="<?= admin_url_to('/services/edit/' . $service['id']) ?>"><?= __('Edit') ?></a>
                                    <a data-ajax-delete class="btn btn-xs btn-danger" href="<?= admin_url_to('/services/delete/' . $service['id']) ?>"><?= __('Delete') ?></a>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    </tbody>
                <?php endforeach ?>
            </table>
        </div>
    <?php endif ?>
</section>