{% extends 'admin/admin_base.html' %}
{% block title %}Настройки{% endblock %}
{% block admin_content %}
<div class="container-fluid">
    <div class="admin-toolbar">
        <h1 class="admin-toolbar__title"><i class="bi bi-gear-fill" aria-hidden="true"></i> Настройки приложения</h1>
    </div>

    {% if settings_sections %}
    <ul class="nav nav-tabs mb-3" id="settingsSectionTabs" role="tablist">
        <li class="nav-item" role="presentation">
            <button type="button" class="nav-link active" data-section-id="general"
                    onclick="switchSettingsSection('general')">
                <i class="bi bi-sliders"></i> Общие
            </button>
        </li>
        {% for section in settings_sections %}
        <li class="nav-item" role="presentation">
            <button type="button" class="nav-link" data-section-id="{{ section.id }}"
                    onclick="switchSettingsSection('{{ section.id }}')">
                {% if section.icon %}<i class="bi {{ section.icon }}"></i>{% endif %}
                {{ section.title }}
            </button>
        </li>
        {% endfor %}
    </ul>
    {% endif %}

    <div id="settings-section-general" class="settings-section-panel">
    <form id="settings-form" class="row g-3">
      <div class="col-lg-6">
        <div class="admin-card">
          <div class="card-header">
            <i class="bi bi-gear"></i> Общие
          </div>
          <div class="card-body">
            <div class="mb-3">
              <label class="form-label">Имя приложения</label>
              <input type="text" class="form-control" name="app_name" id="app_name" placeholder="Имя приложения">
            </div>
            <div class="mb-3">
              <label class="form-label">Порт для API/admin</label>
              <input type="number" class="form-control" name="web_port" id="web_port" min="1" max="65535">
            </div>
          </div>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="admin-card">
          <div class="card-header">
            <i class="bi bi-database"></i> База данных
          </div>
          <div class="card-body">
            <div class="mb-3">
              <label class="form-label">Драйвер</label>
              <select class="form-select" name="db_driver" id="db_driver">
                <option value="mysql">MySQL</option>
                <option value="psql">PostgreSQL</option>
              </select>
            </div>
            <div class="mb-3">
              <label class="form-label">Host</label>
              <input type="text" class="form-control" name="db_host" id="db_host">
            </div>
            <div class="mb-3">
              <label class="form-label">Имя БД</label>
              <input type="text" class="form-control" name="db_name" id="db_name">
            </div>
            <div class="mb-3">
              <label class="form-label">Пользователь</label>
              <input type="text" class="form-control" name="db_user" id="db_user">
            </div>
            <div class="mb-3">
              <label class="form-label">Пароль</label>
              <input type="password" class="form-control" name="db_pass" id="db_pass">
            </div>
          </div>
        </div>
      </div>
      <div class="col-12">
        <div class="admin-card">
          <div class="card-header">
            <i class="bi bi-terminal"></i> Логирование
          </div>
          <div class="card-body row g-3 align-items-end">
            <div class="col-lg-4 mb-3 mb-lg-0">
              <label class="form-label">Уровень логирования</label>
              <select class="form-select" name="log_level" id="log_level">
                <option value="DEBUG">DEBUG</option>
                <option value="INFO">INFO</option>
                <option value="WARNING">WARNING</option>
                <option value="ERROR">ERROR</option>
              </select>
            </div>
            <div class="col-lg-8 text-end">
                <button type="submit" class="btn btn-success"><i class="bi bi-save"></i> Сохранить</button>
            </div>
          </div>
        </div>
      </div>
      <div class="col-12" id="plugin-ports-ops">
        <div class="admin-card">
          <div class="card-header">
            <i class="bi bi-hdd-network"></i> Plugin listeners (ops)
          </div>
          <div class="card-body">
            <p class="text-muted small">Read-only: активные HTTP-listener'ы плагинов.</p>
            <div id="plugin-ports-app"></div>
          </div>
        </div>
      </div>
    </form>
    <div id="settings-save-result" class="alert alert-success d-none mt-3" role="alert">
      Настройки сохранены! Для применения изменений требуется перезапуск приложения.
      <button id="restart-app-btn" class="btn btn-warning btn-sm ms-3"><i class="bi bi-arrow-repeat"></i> Перезапустить сервер</button>
    </div>
    </div>

    {% for section in settings_sections %}
    <div id="settings-section-{{ section.id }}" class="settings-section-panel d-none"></div>
    {% endfor %}
</div>

{% if settings_sections %}
<script type="application/json" id="settings-sections-data">{{ settings_sections | tojson }}</script>
{% endif %}
{% endblock %}
{% block admin_scripts %}
<script>window.__CAN_MANAGE_MAIL_TEMPLATES__ = {{ (can_manage_mail_templates | default(false)) | tojson }};</script>
<script src="{{ url_for('static', filename='js/settings.js') }}"></script>
<script src="{{ url_for('static', filename='js/settings_plugin_sections.js') }}"></script>
<script src="{{ url_for('static', filename='js/vue.global.prod.js') }}"></script>
<script src="{{ url_for('static', filename='js/admin-plugin-ports-vue.js') }}"></script>
{% endblock %}
