<script>
    $(function () {
        $(".add-status").click(function (e) {
            var $this = $(this);
            var $container = $this.closest("form").find(".container-add-status");
            var id = Math.random().toString(36).substr(2, 9);
            var index = ($container.find(":input").size() / 3);

            $container.append([
                '<div style="display:flex;margin-top:10px" id="' + id + '">',
                '<input class="form-control" style="width:50%;margin-right:30px" type="text" placeholder="<?= __('Name') ?>" name="status[' + index + '][name]" />',
                '<input class="form-control" style="width:30%;margin-right:30px" type="text" placeholder="<?= __('Color') ?>" name="status[' + index + '][color]" />',
                '<div>',
                '<input class="form-control toggle" type="checkbox" value="1" name="status[' + index + '][close]" />',
                '</div>',
                '</div>'
            ].join(''));

            runToggle($('#' + id));

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

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

    <div class="actions">
        <a data-ajax class="btn btn-default btn-sm" href="<?= admin_url_to('/tracker/categories') ?>"><?= __('List') ?></a>
    </div>
</section>

<section class="content">
    <div class="box box-default">
        <form data-ajax class="form-focus" action="<?= admin_url_to('/tracker/categories/edit/' . $category['id']) ?>" 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><?= __('Category') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <div class="row">
                                <div class="col-md-8">
                                    <div class="form-group">
                                        <label class="control-label" for="name"><?= __('Name') ?> <span class="required">*</span></label>
                                        <input class="form-control" value="<?= $category['name'] ?>" type="text" name="name" id="name" />
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="active"><?= __('Active') ?>?</label>
                                        <div>
                                            <input class="form-control toggle"<?= $category['active'] ? ' checked' : '' ?> type="checkbox" id="active" value="1" name="active" />
                                        </div>
                                    </div>
                                </div>

                                <div class="col-md-2">
                                    <div class="form-group">
                                        <label class="control-label" for="is-public"><?= __('Public') ?>?</label>
                                        <div>
                                            <input class="form-control toggle"<?= $category['is_public'] ? ' checked' : '' ?> type="checkbox" id="is-public" value="1" name="is_public" />
                                        </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><?= __('Statuses') ?></h5>
                        </div>

                        <div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
                            <div style="display:flex">
                                <div style="width:50%;margin-right:30px"><strong><?= __('Name') ?></strong></div>
                                <div style="width:30%;margin-right:30px"><strong><?= __('Color') ?></strong></div>
                                <div style="flex:1"><strong><?= __('Closed') ?></strong></div>
                            </div>
                            <div class="container-add-status">
                                <?php foreach (json_decode($category['status'], true) ?? [] as $index => $status): ?>
                                    <div style="display:flex;margin-bottom:10px">
                                        <input class="form-control" value="<?= $status['name'] ?>" style="width:50%;margin-right:30px" type="text" placeholder="<?= __('Name') ?>" name="status[<?= $index ?>][name]" />
                                        <input class="form-control" value="<?= $status['color'] ?>" style="width:30%;margin-right:30px" type="text" placeholder="<?= __('Color') ?>" name="status[<?= $index ?>][color]" />
                                        <div>
                                            <input class="form-control toggle"<?= $status['close'] ? ' checked' : '' ?> type="checkbox" value="1" name="status[<?= $index ?>][close]" />
                                        </div>
                                    </div>
                                <?php endforeach ?>
                                <div style="display:flex">
                                    <input class="form-control" style="width:50%;margin-right:30px" type="text" placeholder="<?= __('Name') ?>" name="status[<?= $index ?? 0 + 1?>][name]" />
                                    <input class="form-control" style="width:30%;margin-right:30px" type="text" placeholder="<?= __('Color') ?>" name="status[<?= $index ?? 0 + 1?>][color]" />
                                    <div>
                                        <input class="form-control toggle" type="checkbox" value="1" name="status[<?= $index ?? 0 + 1?>][close]" />
                                    </div>
                                </div>
                            </div>

                            <div style="text-align:right;margin-top:10px">
                                <a class="btn btn-xs btn-success add-status" href="javascript:void(0)"><?= __('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>