{% extends 'layouts/base.html' %}

{% block content %}
    <h1>Конфиги <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#create_config">+</button></h1>
    <select class="form-select" style="width: 150px;" onchange="window.location.href='/configs/?stage=' + this.value;">
        <option {% if stage == 'production' %}selected{% endif %}>production</option>
        <option {% if stage == 'development' %}selected{% endif %}>development</option>
    </select>
    <div class="modal fade" id="create_config" tabindex="-1" aria-labelledby="modal-default" style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <form method="POST">
                    {% csrf_token %}
                    <div class="modal-header">
                        <h2 class="h6 modal-title">Создать конфиг</h2>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="text" name="name" placeholder="Название конфига" style="width: 100%;" />
                    </div>
                    <div class="modal-footer">
                        <button type="submit" name="action" value="create_config" class="btn btn-secondary">Создать</button>
                        <button type="button" class="btn btn-link text-gray-600 ms-auto" data-bs-dismiss="modal">Закрыть</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    {% if error %}
        <div class="alert alert-danger" role="alert">
          Конфиг не имеет вид JSON!
        </div>
    {% endif %}
    <table class="table">
        <thead>
            <th>Название</th>
            <th>Действие</th>
        </thead>
        <tbody>
            {% for config in configs %}
                <tr>
                    <td>{{ config.name }}</td>
                    <td><button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#edit_{{ config.id }}">Редактировать</button></td>
                    <div class="modal fade" id="edit_{{ config.id }}" tabindex="-1" aria-labelledby="modal-default" style="display: none;" aria-hidden="true">
                        <div class="modal-dialog modal-dialog-centered" role="document">
                            <div class="modal-content">
                                <form method="POST">
                                    {% csrf_token %}
                                    <input type="hidden" name="config" value="{{ config.id }}" />
                                    <div class="modal-header">
                                        <h2 class="h6 modal-title">Редактировать конфиг {{ config.name }}</h2>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <div class="modal-body">
                                        <textarea style="width: 100%; height: 800px;" name="data">{{ config.data_pretty }}</textarea>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" name="action" value="save" class="btn btn-secondary">Сохранить</button>
                                        <button type="submit" name="action" value="delete" class="btn btn-danger">Удалить</button>
                                        <button type="button" class="btn btn-link text-gray-600 ms-auto" data-bs-dismiss="modal">Закрыть</button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}