<script type="text/javascript">
    function tables() {
        var $this = $("#database");
        var $table = $("#table");

        $table.html('<option value=""></option>');
        $.getJSON(prefix + "sql/tables/" + $this.val(), function (data) {
            $.each(data, function (key, value) {
                $table.append('<option' + ($table.data("selected") === value.TABLE_NAME ? ' selected' : '') + ' value="' + value.TABLE_NAME + '">' + value.TABLE_NAME + '</option>')
            });

            columns();
        });
    }

    function columns() {
        var $this = $("#table");
        var $database = $("#database");
        var $column = $("#column-code");
        var $name = $("#column-name");

        if ($this.val() === '') return;

        $column.html('');
        $name.html('');
        $.getJSON(prefix + "sql/columns/" + $this.val() + "/" + $database.val(), function (data) {
            $.each(data, function (key, value) {
                $column.append('<option' + ($column.data("selected") === value.COLUMN_NAME ? ' selected' : '') + ' value="' + value.COLUMN_NAME + '">' + value.COLUMN_NAME + '</option>')
                $name.append('<option' + ($name.data("selected") === value.COLUMN_NAME ? ' selected' : '') + ' value="' + value.COLUMN_NAME + '">' + value.COLUMN_NAME + '</option>')
            });

            if ($this.val() === 'Character') {
                $name.val('Name');
            }
        });
    }

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

        tables();
        $(document).on("change", "#table", function () {
            columns();
        });

        $(document).on("change", "#database", function () {
            tables();
        });
    });
</script>


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

<div class="modal" id="modal-add-patent"></div>
<div class="modal" id="modal-edit-patent"></div>

<section class="content">

    <div class="box box-default">
        <form data-ajax class="form-focus" action="<?= admin_url_to('/patents/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-6">
                                    <div class="form-group">
                                        <label class="control-label" for="database"><?= __('Database') ?> <span class="required">*</span></label>
                                        <select class="form-control" id="database" name="database">
                                            <option value=""></option>
                                            <?php foreach (sql_databases() as $database): ?>
                                                <option<?= config('patent.database', config('conn.default.dbname')) === $database['name'] ? ' selected' : '' ?> value="<?= $database['name'] ?>"><?= $database['name'] ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="table"><?= __('Table') ?> <span class="required">*</span></label>
                                        <select class="form-control" id="table" name="table" data-selected="<?= config('patent.table') ?>">
                                            <option value=""></option>
                                            <?php foreach (sql_tables() as $table): ?>
                                                <option<?= $table['TABLE_NAME'] === config('patent.table') ? ' selected' : '' ?> value="<?= $table['TABLE_NAME'] ?>"><?= $table['TABLE_NAME'] ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="column-code"><?= __('Patent column code') ?> <span class="required">*</span></label>
                                        <select data-selected="<?= config('patent.column_code') ?>" class="form-control" id="column-code" name="column_code">
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="column-name"><?= __('Patent column name') ?> <span class="required">*</span></label>
                                        <select data-selected="<?= config('patent.column_name') ?>" class="form-control" id="column-name" name="column_name">
                                        </select>
                                    </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><?= __('Patents') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <?php if (empty($patents)): ?>
                                <div style="margin:0;margin-top:10px" class="callout callout-warning">
                                    <p><?= __('No patents created') ?></p>
                                </div>
                            <?php else: ?>
                                <table id="patents-table" class="table table-condensed table-hover table-striped">
                                    <thead>
                                    <tr>
                                        <th><?= __('Title') ?></th>
                                        <?php if (!config('patent.table')): ?>
                                            <th class="text-center"><?= __('Levels') ?></th>
                                            <th class="text-center"><?= __('Master Level') ?></th>
                                            <th class="text-center"><?= __('Resets') ?></th>
                                            <th class="text-center"><?= __('Master Resets') ?></th>
                                            <th class="text-center"><?= __('SQL') ?></th>
                                        <?php endif ?>
                                        <th width="10"></th>
                                    </tr>
                                    </thead>

                                    <tbody>
                                    <?php foreach ($patents as $patent): ?>
                                        <tr id="sequence_<?= $patent['id'] ?>">
                                            <td>
                                                <i class="fa fa-bars"></i> <?= $patent['title'] ?>
                                            </td>
                                            <?php if (!config('patent.table')): ?>
                                                <td class="text-center"><?= $patent['levels'] ?></td>
                                                <td class="text-center"><?= $patent['master_levels'] ?></td>
                                                <td class="text-center"><?= $patent['resets'] ?></td>
                                                <td class="text-center"><?= $patent['master_resets'] ?></td>
                                                <td class="text-center"><?= __(empty($patent['command']) ? 'No' : 'Yes') ?></td>
                                            <?php endif ?>
                                            <td class="record-actions">
                                                <a data-load-with-modal data-modal="modal-edit-patent" class="btn btn-xs btn-default" href="<?= admin_url_to('/patents/edit/' . $patent['id']) ?>"><?= __('edit') ?></a>
                                                <a data-ajax-delete class="btn btn-xs btn-danger" href="<?= admin_url_to('/patents/delete/' . $patent['id']) ?>"><?= __('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-patent" class="btn btn-xs btn-success" href="<?= admin_url_to('/patents/add') ?>"><?= __('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>