<?php $coins = config('workshop.coins', []) ?>

<script>
    $(function () {
        $(".coins").on("ifChanged", function () {
            var $this = $(this);
            var $prices = $(".price-coin-" + $this.val());

            if ($this.is(":checked")) {
                $prices.attr("disabled", false);
            } else {
                $prices.attr("disabled", true);
            }
        });
    });
</script>

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

<section class="content">
    <form data-ajax class="form-focus" action="<?= admin_url_to('/workshop/config') ?>" method="post">
        <?= csrf_field() ?>

        <div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#general" data-toggle="tab"><?= __('General') ?></a></li>
                <li><a href="#prices" data-toggle="tab"><?= __('Prices') ?></a></li>
                <li><a href="#blocked" data-toggle="tab"><?= __('Items blocked') ?></a></li>
            </ul>

            <div class="tab-content" style="padding: 10px 10px 0 0">
                <div class="tab-pane active" id="general">
                    <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="shops-max-level"><?= __('Item max level') ?></label>
                                            <select class="form-control" name="max_level" id="shops-max-level">
                                                <?php for ($i = 0; $i <= config('item.max_level', 15); $i++): ?>
                                                    <option<?= config('workshop.max_level') == $i ? ' selected="selected"' : '' ?> value="<?= $i ?>">+<?= $i ?></option>
                                                <?php endfor ?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label class="control-label" for="max-option"><?= __('Item max option') ?></label>
                                            <select class="form-control" name="max_option" id="max-option">
                                                <?php for ($i = 0; $i <= config('item.max_option', 28); $i+=4): ?>
                                                    <option<?= config('workshop.max_option') == $i ? ' selected="selected"' : '' ?> value="<?= $i ?>">+<?= $i ?></option>
                                                <?php endfor ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <fieldset>
                                    <legend><?= __('Max excellent by category') ?></legend>
                                    <div class="row">
                                    <?php foreach (config('item.categories', []) as $index => $category): ?>
                                        <div class="col-md-4">
                                            <div class="form-group">
                                                <label class="control-label" for="max-excellent"><?= __('Max exc. (%s)', [$category]) ?></label>
                                                <select class="form-control" name="max_excellent_category[<?= $index ?>]" id="max-excellent">
                                                    <option value=""></option>

                                                    <?php for ($i = 1; $i <= 6; $i++): ?>
                                                        <option<?= config('workshop.max_excellent_category.' . $index, config('workshop.max_excellent')) == $i ? ' selected="selected"' : '' ?> value="<?= $i ?>"><?= $i ?></option>
                                                    <?php endfor ?>
                                                </select>
                                            </div>
                                        </div>
                                    <?php endforeach ?>
                                    </div>
                                </fieldset>

                                <fieldset>
                                    <legend><?= __('Allow options') ?></legend>

                                    <div class="row">
                                        <?php if ($version >= 1.1): ?>
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-ancient"><?= __('Allow ancient') ?></label>
                                                    <div>
                                                        <input<?= config('workshop.allow_ancient', 0) ? ' checked="checked"' : '' ?> class="form-control toggle" type="checkbox" id="allow-ancient" value="1" name="allow_ancient" />
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-ancient-excellent"><?= __('Allow ancient + excellent') ?></label>
                                                    <div>
                                                        <input<?= config('workshop.allow_ancient_excellent', 0) ? ' checked="checked"' : '' ?> class="form-control toggle" type="checkbox" id="allow-ancient-excellent" value="1" name="allow_ancient_excellent" />
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-ancient-socket"><?= __('Allow ancient + socket') ?></label>
                                                    <div>
                                                        <input<?= config('workshop.allow_ancient_socket', 0) ? ' checked="checked"' : '' ?> class="form-control toggle" type="checkbox" id="allow-ancient-socket" value="1" name="allow_ancient_socket" />
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endif ?>

                                        <?php if ($version > 2): ?>
                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-refine"><?= __('Allow refine') ?></label>
                                                    <div>
                                                        <input<?= config('workshop.allow_refine', 0) ? ' checked="checked"' : '' ?> class="form-control toggle" type="checkbox" id="allow-refine" value="1" name="allow_refine" />
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-harmony"><?= __('Allow harmony') ?></label>
                                                    <div>
                                                        <input<?= config('workshop.allow_harmony', 0) ? ' checked="checked"' : '' ?> class="form-control toggle" type="checkbox" id="allow-harmony" value="1" name="allow_harmony" />
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-mastery"><?= __('Allow mastery') ?></label>
                                                    <div>
                                                        <input<?= config('workshop.allow_mastery', 0) ? ' checked="checked"' : '' ?> class="form-control toggle" type="checkbox" id="allow-mastery" value="1" name="allow_mastery" />
                                                    </div>
                                                </div>
                                            </div>

                                            <div class="col-md-3">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-mastery-bonus"><?= __('Allow mastery bonus') ?></label>
                                                    <div>
                                                        <input<?= config('workshop.allow_mastery_bonus', 0) ? ' checked="checked"' : '' ?> class="form-control toggle" type="checkbox" id="allow-mastery-bonus" value="1" name="allow_mastery_bonus" />
                                                    </div>
                                                </div>
                                            </div>
                                        <?php endif ?>
                                    </div>

                                    <?php if ($version > 2): ?>
                                        <div class="row">
                                            <div class="col-md-4">
                                                <div class="form-group">
                                                    <label class="control-label" for="allow-sockets"><?= __('Allow sockets') ?></label>
                                                    <select class="form-control" name="allow_sockets" id="allow-sockets">
                                                        <option value=""></option>
                                                        <option value="1"<?= config('workshop.allow_sockets', 0) == 1 ? ' selected' : '' ?>><?= __('All') ?></option>
                                                        <option value="2"<?= config('workshop.allow_sockets', 0) == 2 ? ' selected' : '' ?>><?= __('Only empty') ?></option>
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="col-md-4">
                                                <div class="form-group">
                                                    <label class="control-label" for="max-sockets"><?= __('Max socket options') ?></label>
                                                    <select class="form-control" name="max_sockets" id="max-sockets">
                                                        <option value=""></option>

                                                        <?php for ($i = 1; $i <= 5; $i++): ?>
                                                            <option<?= config('workshop.max_sockets') == $i ? ' selected="selected"' : '' ?> value="<?= $i ?>"><?= $i ?></option>
                                                        <?php endfor ?>
                                                    </select>
                                                </div>
                                            </div>

                                            <div class="col-md-4">
                                                <div class="form-group">
                                                    <label class="control-label" for="unique-sockets"><?= __('Unique sockets') ?></label>
                                                    <select class="form-control" name="unique_sockets" id="unique-sockets">
                                                        <option value=""></option>
                                                        <option value="1"<?= config('workshop.unique_sockets', 0) == 1 ? ' selected' : '' ?>><?= __('Per code') ?></option>
                                                        <option value="2"<?= config('workshop.unique_sockets', 0) == 2 ? ' selected' : '' ?>><?= __('Per type') ?></option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    <?php endif ?>
                                </fieldset>
                            </div>
                        </div>
                    </div>
                </div>

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

                            <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                                <?php foreach (config('coins', []) as $coin): ?>
                                    <div style="width:28%;margin-top:8px;float:left">
                                        <input<?= in_array($coin['id'], $coins) ? ' checked="checked"' : '' ?> type="checkbox" name="coins[]" class="coins" id="coin-<?= $coin['id'] ?>" value="<?= $coin['id'] ?>" />
                                        <label for="coin-<?= $coin['id'] ?>"><?= $coin['name'] ?></label>
                                    </div>
                                <?php endforeach ?>
                            </div>
                        </div>
                    </div>

                    <?php foreach (config('coins', []) as $coin): ?>
                        <div class="section">
                            <div class="row">
                                <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                                    <h5><?= $coin['name'] ?></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="price-level"><?= __('Price for level') ?></label>
                                        <table class="table table-condensed table-form">
                                            <thead>
                                                <tr>
                                                    <?php for ($i = 0; $i <= config('item.max_level', 15); $i++): ?>
                                                        <th class="text-center">+<?= $i ?></th>
                                                    <?php endfor ?>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr>
                                                    <?php for ($i = 0; $i <= config('item.max_level', 15); $i++): ?>
                                                        <?php
                                                            $priceLevel = config('workshop.prices.' . $coin['id'] . '.level');
                                                            if (is_array($priceLevel) && isset($priceLevel[$i])) {
                                                                $priceLevel = $priceLevel[$i];
                                                            } else {
                                                                $priceLevel = '';
                                                            }
                                                        ?>
                                                        <td><input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= $priceLevel ?>" type="text" name="price[<?= $coin['id'] ?>][level][<?= $i ?>]" id="price-level" /></td>
                                                    <?php endfor ?>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label" for="price-option"><?= __('Price for option') ?></label>

                                        <table class="table table-condensed table-form">
                                            <thead>
                                                <tr>
                                                    <?php for ($i = 0; $i <= config('item.max_option', 28); $i+=4): ?>
                                                        <th class="text-center">+<?= $i ?></th>
                                                    <?php endfor ?>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <tr>
                                                    <?php for ($i = 0; $i <= config('item.max_option'); $i+=4): ?>
                                                        <?php
                                                            $priceOption = config('workshop.prices.' . $coin['id'] . '.option');
                                                            if (is_array($priceOption) && isset($priceOption[$i])) {
                                                                $priceOption = $priceOption[$i];
                                                            } else {
                                                                $priceOption = '';
                                                            }
                                                        ?>
                                                        <td><input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= $priceOption ?>" type="text" name="price[<?= $coin['id'] ?>][option][<?= $i ?>]" id="price-option" /></td>
                                                    <?php endfor ?>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label" for="price-excellent"><?= __('Price for excellent') ?></label>
                                        <table class="table table-condensed table-form">
                                            <thead>
                                                <tr>
                                                    <?php for ($i = 1; $i <= 6; $i++): ?>
                                                        <th class="text-center">#<?= $i ?></th>
                                                    <?php endfor ?>
                                                    <th class="text-center">
                                                        <?= __('Remove') ?>
                                                    </th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            <tr>
                                                <?php for ($i = 0; $i < 6; $i++): ?>
                                                    <?php
                                                        $priceExcellent = config('workshop.prices.' . $coin['id'] . '.excellent');
                                                        if (is_array($priceExcellent) && isset($priceExcellent['add'][$i])) {
                                                            $priceExcellent = $priceExcellent['add'][$i];
                                                        }
                                                    ?>
                                                    <td><input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= $priceExcellent ?>" type="text" name="price[<?= $coin['id'] ?>][excellent][add][]" id="price-excellent" /></td>
                                                <?php endfor ?>
                                                <td><input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.excellent.remove') ?>" type="text" name="price[<?= $coin['id'] ?>][excellent][remove]" id="price-remove-excellent" /></td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>

                                    <div style="display:flex;flex-wrap:wrap">
                                        <div style="width:30%;margin-right:20px;">
                                            <div class="form-group">
                                                <label class="control-label" for="price-skill"><?= __('Price for skill') ?></label>
                                                <div>
                                                    <input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.skill') ?>" type="text" name="price[<?= $coin['id'] ?>][skill]" id="price-skill" />
                                                </div>
                                            </div>
                                        </div>

                                        <div style="width:30%;margin-right:20px;">
                                            <div class="form-group">
                                                <label class="control-label" for="price-luck"><?= __('Price for luck') ?></label>
                                                <div>
                                                    <input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.luck') ?>" type="text" name="price[<?= $coin['id'] ?>][luck]" id="price-luck" />
                                                </div>
                                            </div>
                                        </div>

                                        <?php if ($version >= 1.1): ?>
                                            <div style="width:31%;margin-right:20px;">
                                                <div class="form-group">
                                                    <label class="control-label" for="price-ancient"><?= __('Price for ancient') ?></label>
                                                    <input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.ancient') ?>" type="text" name="price[<?= $coin['id'] ?>][ancient]" id="price-ancient" />
                                                </div>
                                            </div>
                                        <?php endif ?>

                                        <?php if ($version > 2): ?>
                                            <div style="width:30%;margin-right:20px;">
                                                <div class="form-group">
                                                    <label class="control-label" for="price-harmony"><?= __('Price for harmony') ?></label>
                                                    <input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.harmony') ?>" type="text" name="price[<?= $coin['id'] ?>][harmony]" id="price-harmony" />
                                                </div>
                                            </div>

                                            <div style="width:30%;margin-right:20px;">
                                                <div class="form-group">
                                                    <label class="control-label" for="price-refine"><?= __('Price for refine') ?></label>
                                                    <input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.refine') ?>" type="text" name="price[<?= $coin['id'] ?>][refine]" id="price-refine" />
                                                </div>
                                            </div>

                                            <div style="width:30%;">
                                                <div class="form-group">
                                                    <label class="control-label" for="price-mastery-bonus"><?= __('Price for mastery bonus') ?></label>
                                                    <input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.mastery_bonus') ?>" type="text" name="price[<?= $coin['id'] ?>][mastery_bonus]" id="price-mastery-bonus" />
                                                </div>
                                            </div>
                                        <?php endif ?>
                                    </div>

                                    <?php if ($version > 2): ?>
                                        <div class="form-group">
                                            <label class="control-label" for="price-socket"><?= __('Price for socket') ?></label>
                                            <table class="table table-condensed table-form">
                                                <thead>
                                                    <tr>
                                                        <?php for ($i = 1; $i <= 5; $i++): ?>
                                                            <th class="text-center">#<?= $i ?></th>
                                                        <?php endfor ?>
                                                        <th><?= __('Remove') ?></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                        <?php for ($i = 0; $i < 5; $i++): ?>
                                                            <?php
                                                            $priceSocket = config('workshop.prices.' . $coin['id'] . '.socket');
                                                            if (is_array($priceSocket) && isset($priceSocket['add'][$i])) {
                                                                $priceSocket = $priceSocket['add'][$i];
                                                            }
                                                            ?>
                                                            <td><input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= $priceSocket ?>" type="text" name="price[<?= $coin['id'] ?>][socket][add][]" id="price-socket" /></td>
                                                        <?php endfor ?>
                                                        <td><input<?= !in_array($coin['id'], $coins) ? ' disabled' : '' ?> class="form-control price-coin-<?= $coin['id'] ?>" value="<?= config('workshop.prices.' . $coin['id'] . '.socket.remove') ?>" type="text" name="price[<?= $coin['id'] ?>][socket][remove]" id="price-remove-socket" /></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    <?php endif ?>
                                </div>
                            </div>
                        </div>
                    <?php endforeach ?>
                </div>
                <div class="tab-pane" id="blocked">
                    <div class="section">
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
                                <h5><?= __('Items') ?></h5>
                            </div>

                            <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                                <div class="grouped-items">
                                    <?php $index = 0; foreach ($items as $category => $its): ?>
                                        <p class="title">
                                            <a class="grouped-item-category collapsed" data-toggle="collapse" data-target="#category-<?= $index ?>" href="javascript:void(0)">
                                                <?= $category ?>

                                                <span class="label label-default">
                                                            <?= count($its) ?> <?= __('items') ?>
                                                        </span>

                                                <span class="label label-danger">
                                                            <?php $total = array_reduce($itemsBlocked, function ($carry, $item) use ($index) {
                                                                $carry += strpos($item, $index . '-') !== false ? 1 : 0;

                                                                return $carry;
                                                            }) ?>
                                                            <?= $total ?? 0 ?> <?= __('blocked') ?>
                                                        </span>
                                            </a>

                                            <a class="grouped-item-toggle" href="#" data-select-all="<?= $index ?>"><?= __('Toggle all') ?></a>
                                        </p>

                                        <div class="collapse" id="category-<?= $index ?>">
                                            <?php foreach ($its as $item): ?>
                                                <?php $id = $item['section'] . '-' . $item['index'] ?>
                                                <div class="grouped-item">
                                                    <input<?= in_array($id, $itemsBlocked) ? ' checked="checked"' : '' ?> type="checkbox" data-category="<?= $index ?>" name="blocked[]" id="item-<?= $id ?>" value="<?= $id ?>" />
                                                    <label for="item-<?= $id ?>"><?= $item['name'] ?></label>
                                                </div>
                                            <?php endforeach ?>

                                            <div class="clearfix"></div>
                                        </div>
                                        <?php $index++; endforeach ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

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