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

<section class="content-header">
    <h1><?= __('Hall of fame plugin settings') ?></h1>
</section>

<section class="content">
    <div class="box box-default">
        <form data-ajax class="form-focus" action="<?= admin_url_to('/hallfame/config') ?>" 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><?= __('Settings') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label" for="max-results"><?= __('Max result') ?></label>
                                        <input class="form-control" type="text" id="max-results" value="<?= config('hallfame.max_results', 5) ?>" name="max_results" />
                                    </div>
                                </div>

                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label" for="group-results"><?= __('Group results') ?></label>

                                        <div>
                                            <input class="form-control toggle"<?= config('hallfame.group_results') ? ' checked' : '' ?> type="checkbox" id="group-results" value="1" name="group_results" />
                                        </div>
                                    </div>
                                </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><?= __('Rankings') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <table id="hallfame-table" class="table table-condensed table-hover table-striped">
                                <thead>
                                    <tr>
                                        <th width="10"></th>
                                        <th width="10"></th>
                                        <th><?= __('Name') ?></th>
                                        <th width="60" style="text-align:center"><?= __('Daily') ?></th>
                                        <th width="60" style="text-align:center"><?= __('Weekly') ?></th>
                                        <th width="60" style="text-align:center"><?= __('Monthly') ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php foreach ($rankings as $id => $ranking): ?>
                                    <?php if (!$ranking['active']) continue; ?>

                                    <?php $hallfame = config('hallfame.rankings.' . $id, null) ?>
                                    <tr id="sequence_<?= $id ?>" class="drag">
                                        <td>
                                            <i class="fa fa-bars"></i>
                                        </td>
                                        <td style="vertical-align:middle">
                                            <input<?= $hallfame && isset($hallfame['active']) && $hallfame['active'] ? ' checked' : '' ?> type="checkbox" name="hallfame[<?= $id ?>][active]" value="1" />
                                        </td>
                                        <td style="vertical-align:bottom">
                                            <input type="text" name="hallfame[<?= $id ?>][name]" value="<?= $hallfame && !empty($hallfame['name']) ? $hallfame['name'] : $ranking['name'] ?>" class="form-control" />
                                        </td>
                                        <td style="vertical-align:middle;text-align:center">
                                            <?php if (isset($ranking['configs']['daily'])): ?>
                                                <input<?= $hallfame['daily'] ? ' checked' : '' ?> type="checkbox" name="hallfame[<?= $id ?>][daily]" value="1" />
                                            <?php endif ?>
                                        </td>
                                        <td style="vertical-align:middle;text-align:center">
                                            <?php if (isset($ranking['configs']['weekly'])): ?>
                                                <input<?= $hallfame['weekly'] ? ' checked' : '' ?> type="checkbox" name="hallfame[<?= $id ?>][weekly]" value="1" />
                                            <?php endif ?>
                                        </td>
                                        <td style="vertical-align:middle;text-align:center">
                                            <?php if (isset($ranking['configs']['monthly'])): ?>
                                                <input<?= $hallfame['monthly'] ? ' checked' : '' ?> type="checkbox" name="hallfame[<?= $id ?>][monthly]" value="1" />
                                            <?php endif ?>
                                        </td>
                                    </tr>
                                <?php endforeach ?>
                                </tbody>
                            </table>
                        </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>