{% extends "admin/admin_base.html" %}

{% block admin_content %}
<div class="container-fluid">
    <div class="admin-toolbar">
        <h1 class="admin-toolbar__title"><i class="bi bi-database-gear" aria-hidden="true"></i> Базы данных сервисов</h1>
        <p class="text-muted mb-0 small">Привязка сервисов к базам данных, настройка и установка через SQL.</p>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Привязки сервисов к БД</h5>
                    <button class="btn btn-primary btn-sm" onclick="loadBindings()">
                        <i class="bi bi-arrow-clockwise"></i> Обновить
                    </button>
                </div>
                <div class="card-body">
                    <div id="bindingsContainer">
                        <div class="text-center py-4">
                            <div class="spinner-border text-primary" role="status"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">Настройка БД для сервиса</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label for="setupServiceName" class="form-label">Сервис</label>
                        <select class="form-select" id="setupServiceName">
                            <option value="">— Выберите сервис —</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="setupDatasourceId" class="form-label">Источник данных</label>
                        <select class="form-select" id="setupDatasourceId">
                            <option value="core">core</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="setupDbType" class="form-label">Тип БД</label>
                        <select class="form-select" id="setupDbType">
                            <option value="mysql">MySQL</option>
                            <option value="postgresql">PostgreSQL</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Действие</label>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="setupAction" id="actionPlatform" value="use_platform" checked>
                            <label class="form-check-label" for="actionPlatform">Использовать БД ядра (основную)</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="setupAction" id="actionCreate" value="create">
                            <label class="form-check-label" for="actionCreate">Создать новую БД</label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="setupAction" id="actionExisting" value="use_existing">
                            <label class="form-check-label" for="actionExisting">Использовать другую существующую</label>
                        </div>
                        <div id="platformDbHint" class="small text-muted mt-1"></div>
                    </div>
                    <div class="mb-3" id="setupNewNameBlock">
                        <label for="setupDatabaseName" class="form-label">Имя новой базы</label>
                        <input type="text" class="form-control" id="setupDatabaseName" placeholder="my_service_db">
                    </div>
                    <div class="mb-3 d-none" id="setupCharsetBlock">
                        <label for="setupCharset" class="form-label">Charset (MySQL)</label>
                        <input type="text" class="form-control form-control-sm" id="setupCharset" placeholder="utf8mb4" value="utf8mb4">
                        <label for="setupCollation" class="form-label mt-2">Collation</label>
                        <input type="text" class="form-control form-control-sm" id="setupCollation" placeholder="utf8mb4_unicode_ci">
                    </div>
                    <div class="mb-3 d-none" id="setupExistingBlock">
                        <label for="setupExistingDb" class="form-label">Существующая база</label>
                        <input type="text" class="form-control form-control-sm mb-2" id="setupDbSearch" placeholder="Поиск...">
                        <div class="form-check mb-2">
                            <input class="form-check-input" type="checkbox" id="showSystemDbs" onchange="toggleShowSystem(this.checked)">
                            <label class="form-check-label" for="showSystemDbs">Показать системные (superadmin)</label>
                        </div>
                        <select class="form-select" id="setupExistingDb">
                            <option value="">— Загрузить список —</option>
                        </select>
                        <div id="setupDbMeta" class="small text-muted mt-1"></div>
                    </div>
                    <div id="installProgress" class="mb-2"></div>
                    <div class="d-flex flex-wrap gap-2">
                        <button class="btn btn-primary" onclick="runSetup()">
                            <i class="bi bi-check2-circle"></i> Настроить
                        </button>
                        <button class="btn btn-outline-secondary" onclick="validateSql()">
                            <i class="bi bi-shield-check"></i> Валидировать SQL
                        </button>
                        <button class="btn btn-outline-success" onclick="installSql()">
                            <i class="bi bi-play-fill"></i> Выполнить SQL
                        </button>
                    </div>
                    <div id="setupMessage" class="mt-3 small"></div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block admin_scripts %}
<script src="{{ url_for('static', filename='js/service_databases.js') }}"></script>
{% endblock %}
