<script>
    function extWarehouse() {
        var $ext = $("#config-warehouse-ext");
        var $container = $("#warehouse-ext-container");

        if ($ext.is(":checked")) {
            $container.show();
        } else {
            $container.hide();
        }
    }

    function tables() {
        var $this = $("#warehouse-ext-database");
        var $table = $("#warehouse-ext-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 = $("#warehouse-ext-table");
        var $database = $("#warehouse-ext-database");
        var $account = $("#warehouse-ext-column-account");
        var $items = $("#warehouse-ext-column-items");
        var $number = $("#warehouse-ext-column-number");
        var $money = $("#warehouse-ext-column-money");

        $account.html('');
        $items.html('');
        $number.html('');
        $money.html('');

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

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

    $(function () {
        extWarehouse();

        $("#config-warehouse-ext").change(function () {
            extWarehouse();
        });

        tables();
        $("#warehouse-ext-table").change(function () {
            columns();
        });

        $("#warehouse-ext-database").change(function () {
            tables();
        });

        $(".add-server").click(function (e) {
            var $this = $(this);
            var $container = $this.closest("form").find(".container-add-server");
            var index = ($container.find(":input").size() / 2) + 1;
            $container.append('<div style="display:flex;margin-top:10px;"> <input class="form-control" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('Server') ?>" name="servers[' + index + '][server]"> <input class="form-control" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('Name') ?>" name="servers[' + index + '][name]"> <input class="form-control" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('IP') ?>" name="servers[' + index + '][ip]"> <input class="form-control" style="width:15%;margin-right:2%" type="text" placeholder="<?= __('Port') ?>" name="servers[' + index + '][port]"> <input class="form-control" style="width:11%" type="text" placeholder="<?= __('Max users') ?>" name="servers[' + index + '][max]"> </div>');

            e.preventDefault();
        });
    })
</script>

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

<section class="content">
    <div class="box box-default">
        <form data-ajax data-upload class="form-focus" action="<?= admin_url_to('/configs/general') ?>" 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><?= __('Server') ?></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="config-server-team"><?= __('Team') ?></label>
                                        <select class="form-control" id="config-server-team" name="server_team">
                                            <?php foreach ($teams as $team => $name): ?>
                                                <option<?= $team == config('server.team', '') ? ' selected' : '' ?> value="<?= $team ?>"><?= $name ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label" for="server-season"><?= __('Season') ?></label>
                                        <select class="form-control" id="server-season" name="server_season">
                                            <option value=""></option>
                                            <?php foreach ($seasons as $key => $season): ?>
                                                <option<?= $key == config('server.season') ? ' selected' : '' ?> value="<?= $key ?>"><?= $season ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label" for="config-server-version-name"><?= __('Version name') ?></label>
                                        <input class="form-control" type="text" id="config-server-version-name" value="<?= config('server.version_name') ?>" name="server_version_name" />
                                    </div>

                                </div>
                            </div>

                            <div class="form-group">
                                <label class="control-label" for="config-server-name"><?= __('Server name') ?></label>
                                <input class="form-control" type="text" id="config-server-name" value="<?= config('server.name') ?>" name="server_name" />
                            </div>

                            <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-server-experience"><?= __('Experience') ?></label>
                                        <input class="form-control" type="text" id="config-server-experience" value="<?= config('server.experience') ?>" name="server_experience" />
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-server-drop"><?= __('Drop') ?></label>
                                        <input class="form-control" type="text" id="config-server-drop" value="<?= config('server.drop') ?>" name="server_drop" />
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-server-max-level"><?= __('Max level') ?></label>
                                        <input class="form-control" type="text" id="config-server-max-level" value="<?= config('server.max_level') ?>" name="server_max_level" />
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-server-max-status"><?= __('Max status') ?></label>
                                        <input class="form-control" type="text" id="config-server-max-status" value="<?= config('server.max_status') ?>" name="server_max_status" />
                                    </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><?= __('Servers') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <div class="form-group">
                                <div class="container-add-server">
                                    <?php $i = 0; foreach (config('servers', []) as $name => $server): ?>
                                        <div style="display:flex;margin-bottom:10px;">
                                            <input class="form-control" value="<?= $name ?>" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('Server') ?>" name="servers[<?= $i ?>][server]" />
                                            <input class="form-control" value="<?= $server['name'] ?>" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('Name') ?>" name="servers[<?= $i ?>][name]" />
                                            <input class="form-control" value="<?= isset($server['ip']) ? $server['ip'] : '' ?>" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('IP') ?>" name="servers[<?= $i ?>][ip]" />
                                            <input class="form-control" value="<?= isset($server['port']) ? $server['port'] : '' ?>" style="width:15%;margin-right:2%" type="text" placeholder="<?= __('Port') ?>" name="servers[<?= $i ?>][port]" />
                                            <input class="form-control" value="<?= $server['max'] ?>" style="width:11%" type="text" placeholder="<?= __('Max users') ?>" name="servers[<?= $i ?>][max]" />
                                        </div>
                                        <?php $i++; endforeach ?>
                                    <div style="display:flex">
                                        <input class="form-control" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('Server') ?>" name="servers[<?= $i ?>][server]" />
                                        <input class="form-control" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('Name') ?>" name="servers[<?= $i ?>][name]" />
                                        <input class="form-control" style="width:22%;margin-right:2%" type="text" placeholder="<?= __('IP') ?>" name="servers[<?= $i ?>][ip]" />
                                        <input class="form-control" style="width:15%;margin-right:2%" type="text" placeholder="<?= __('Port') ?>" name="servers[<?= $i ?>][port]" />
                                        <input class="form-control" style="width:11%" type="text" placeholder="<?= __('Max users') ?>" name="servers[<?= $i ?>][max]" />
                                    </div>
                                </div>
                            </div>
                            <div style="text-align:right;margin-top:10px">
                                <a class="btn btn-xs btn-success add-server" href="javascript:void(0)"><?= __('Add') ?></a>
                            </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><?= __('Site') ?></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="config-site-title"><?= __('Title') ?></label>
                                        <input class="form-control" type="text" id="config-site-title" value="<?= config('site.title') ?>" name="site_title" />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="config-site-logo"><?= __('Logo') ?></label>
                                        <input data-upload-field class="form-control" type="file" id="config-site-logo" name="logo" />
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-4">
                                    <div class="form-group">
                                        <label class="control-label" for="players-online-show-type"><?= __('Players online') ?></label>
                                        <select class="form-control" id="players-online-show-type" name="players_online_show_type">
                                            <?php foreach (['none' => __('Show nothing'), 'total' => 'Only total', 'all' => 'Show all'] as $key => $value): ?>
                                                <option<?= config('players-online.show_type', 'all') === $key ? ' selected' : '' ?> value="<?= $key ?>"><?= $value ?></option>
                                            <?php endforeach ?>
                                        </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><?= __('SMTP') ?></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="config-smtp-host"><?= __('Server') ?></label>
                                        <input class="form-control" type="text" id="config-smtp-host" value="<?= config('smtp.host') ?>" name="smtp_host" />
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-smtp-port"><?= __('Port') ?></label>
                                        <input class="form-control" type="text" id="config-smtp-port" value="<?= config('smtp.port') ?>" name="smtp_port" />
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-smtp-secure"><?= __('Secure') ?></label>
                                        <select id="config-smtp-secure" class="form-control" name="smtp_secure">
                                            <option value=""></option>
                                            <?php foreach (['tls' => 'TLS', 'ssl' => 'SSL'] as $code => $secure): ?>
                                                <option<?= config('smtp.secure') === $code ? ' selected' : '' ?> value="<?= $code ?>"><?= $secure ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="config-smtp-username"><?= __('Username') ?></label>
                                        <input class="form-control" type="text" id="config-smtp-username" value="<?= config('smtp.username') ?>" name="smtp_username" />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="config-smtp-password"><?= __('Password') ?></label>
                                        <input class="form-control" type="text" id="config-smtp-password" value="<?= config('smtp.password') ?>" name="smtp_password" />
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="control-label" for="config-smtp-from">From</label>
                                <input class="form-control" type="text" id="config-smtp-from" value="<?= config('smtp.from') ?>" name="smtp_from" />
                            </div>
                        </div>
                    </div>
                </div>

                <?php if (server()->hasCastleSiege()): ?>
                    <div class="section">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                                <h5><?= __('Castle siege') ?></h5>
                            </div>

                            <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                                <div class="form-group">
                                    <label class="control-label" for="config-cs-next-confrontation"><?= __('Next confrontation') ?></label>
                                    <input class="form-control" type="text" id="config-cs-next-confrontation" value="<?= config('castlesiege.next_confrontation') ?>" name="cs_next_confrontation" />
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endif ?>

                <div class="section">
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                            <h5><?= __('Connect Server') ?></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="config-connect-host"><?= __('Server') ?></label>
                                        <input class="form-control" type="text" id="config-connect-host" value="<?= config('connectserver.host') ?>" name="connect_host" />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="config-connect-port"><?= __('Port') ?></label>
                                        <input class="form-control" type="text" id="config-connect-port" value="<?= config('connectserver.port') ?>" name="connect_port" />
                                    </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><?= __('Join Server') ?></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="config-join-host"><?= __('Server') ?></label>
                                        <input class="form-control" type="text" id="config-join-host" value="<?= config('joinserver.host') ?>" name="join_host" />
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label" for="config-join-port"><?= __('Port') ?></label>
                                        <input class="form-control" type="text" id="config-join-port" value="<?= config('joinserver.port') ?>" name="join_port" />
                                    </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><?= __('Item') ?></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="config-db-version"><?= __('DbVersion') ?></label>
                                        <input class="form-control" type="text" id="config-db-version" value="<?= config('item.db_version') ?>" name="item_db_version" />
                                    </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><?= __('Warehouse') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <div class="row">
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-warehouse-extended"><?= __('Warehouse extended') ?></label>
                                        <div>
                                            <input type="checkbox" class="form-control toggle" id="config-warehouse-extended"<?= config('warehouse.extended') ? 'checked="checked"' : '' ?> value="1" name="warehouse_extended" />
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-3">
                                    <div class="form-group">
                                        <label class="control-label" for="config-warehouse-ext"><?= __('Ext Warehouse') ?></label>
                                        <div>
                                            <input type="checkbox" class="form-control toggle" id="config-warehouse-ext"<?= config('warehouse.ext.active') ? 'checked="checked"' : '' ?> value="1" name="ext_active" />
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div id="warehouse-ext-container" style="display:none">
                                <div class="row">
                                    <div class="col-md-6">
                                        <label class="control-label" for="warehouse-ext-database"><?= __('Database') ?></label>
                                        <select class="form-control" id="warehouse-ext-database" name="ext_database">
                                            <?php foreach (sql_databases() as $database): ?>
                                                <option<?= config('warehouse.ext.database', config('conn.default.dbname')) === $database['name'] ? ' selected' : '' ?> value="<?= $database['name'] ?>"><?= $database['name'] ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label" for="warehouse-ext-table"><?= __('Table') ?></label>
                                            <select class="form-control" data-selected="<?= config('warehouse.ext.table', 'ExtWarehouse') ?>" id="warehouse-ext-table" name="ext_table">
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="row">
                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="control-label" for="warehouse-ext-column-account"><?= __('Account column') ?></label>
                                            <select class="form-control" data-selected="<?= config('warehouse.ext.column_account', 'AccountID') ?>" id="warehouse-ext-column-account" name="ext_column_account">
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="control-label" for="warehouse-ext-column-items"><?= __('Items column') ?></label>
                                            <select class="form-control" data-selected="<?= config('warehouse.ext.column_items', 'Items') ?>" id="warehouse-ext-column-items" name="ext_column_items">
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="control-label" for="warehouse-ext-column-number"><?= __('Column number') ?></label>
                                            <select class="form-control" data-selected="<?= config('warehouse.ext.column_number', 'Number') ?>" id="warehouse-ext-column-number" name="ext_column_number">
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        <div class="form-group">
                                            <label class="control-label" for="warehouse-ext-column-money"><?= __('Column money') ?></label>
                                            <select data-selected="<?= config('warehouse.ext.column_money', 'Money') ?>" class="form-control" id="warehouse-ext-column-money" name="ext_column_money">
                                            </select>
                                        </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><?= __('Morpheus') ?></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="config-language"><?= __('Language') ?></label>
                                        <select class="form-control" name="language" id="config-language">
                                            <?php foreach ($languages as $key => $language): ?>
                                                <option<?= $key == config('locale', 'pt_BR') ? ' selected' : '' ?> value="<?= $key ?>"><?= $language ?></option>
                                            <?php endforeach ?>
                                        </select>
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="config-maintenance"><?= __('Maintenance') ?></label>
                                        <div>
                                            <input class="form-control toggle" type="checkbox" id="config-maintenance"<?= config('maintenance') ? 'checked="checked"' : '' ?> value="1" name="maintenance" />
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="config-debug">Debug</label>
                                        <div>
                                            <input class="form-control toggle" type="checkbox" id="config-debug"<?= config('debug') ? 'checked="checked"' : '' ?> value="1" name="debug" />
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="config-rewrite">URL Rewrite</label>
                                        <div>
                                            <input class="form-control toggle" type="checkbox" id="config-rewrite"<?= config('url_rewrite') ? 'checked="checked"' : '' ?> value="1" name="url_rewrite" />
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="config-https"><?= __('Force HTTPS') ?></label>
                                        <div>
                                            <input class="form-control toggle" type="checkbox" id="config-https"<?= config('force_https') ? 'checked="checked"' : '' ?> value="1" name="force_https" />
                                        </div>
                                    </div>
                                </div>
                            </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>