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

{% block content %}
    <h1>Схемы <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#create_config">+</button></h1>
    <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_schema" 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>
    <table class="table">
        <thead>
            <th>Название</th>
            <th>Действие</th>
        </thead>
        <tbody>
            {% for schema in schemas %}
                <tr>
                    <td>{{ schema.name }}</td>
                    <td><button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#edit_{{ schema.id }}">Редактировать</button></td>
                    <div class="modal fade" id="edit_{{ schema.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="schema" value="{{ schema.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">{{ schema.data }}</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" onclick="return confirm('Подтверди удаление. Это действие необратимо.');">Удалить</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 %}