<!doctype html>
<html lang="ru">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Admin Matrix</title>
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/admin.css') }}" />
</head>
<body class="{% if scope == 'ib' %}ib{% endif %}">
  <main class="wrap">
    <section class="top">
      <div>
        <strong>Админ-панель матрицы</strong>
        <div class="scope-switch" style="margin-top:8px;">
          {% if 'infra' in allowed_scopes %}
          <a class="scope-chip {% if scope == 'infra' %}active{% endif %}" href="{{ request.path }}?scope=infra">Инфраструктура</a>
          {% endif %}
          {% if 'ib' in allowed_scopes %}
          <a class="scope-chip {% if scope == 'ib' %}active{% endif %}" href="{{ request.path }}?scope=ib">ИБ</a>
          {% endif %}
        </div>
      </div>
      <div class="top-actions">
        <button class="pri" type="submit" form="matrixForm">Сохранить</button>
        <a href="/" style="text-decoration:none;"><button class="warn" type="button">На сайт</button></a>
        <form method="post" style="margin:0;">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <input type="hidden" name="action" value="logout" />
          <button class="danger" type="submit">Выйти</button>
        </form>
      </div>
    </section>

    {% with messages = get_flashed_messages(with_categories=true) %}
      {% if messages %}
        <section class="alerts">
          {% for category, message in messages %}
            <div class="alert {{ category }}">{{ message }}</div>
          {% endfor %}
        </section>
      {% endif %}
    {% endwith %}

    {% if pending_changes %}
    <section class="box pending-box">
      <div class="pending-head">
        <h3>{% if is_super_admin %}Ожидают утверждения{% else %}Мои изменения на проверке{% endif %}</h3>
        {% if is_super_admin %}
        <form method="post">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <input type="hidden" name="action" value="approve_all_changes" />
          <button class="pri" type="submit">Утвердить все</button>
        </form>
        {% endif %}
      </div>
      <div class="pending-list">
        {% for change in pending_changes %}
        <div class="pending-item">
          <div>
            <strong>#{{ change.id }} · {{ change.title }}</strong>
            <span>{{ change.created_at }} · {{ change.scope_label }} · {{ change.created_by }}</span>
            <div class="pending-desc">{{ change.description }}</div>
          </div>
          {% if is_super_admin %}
          <div class="pending-actions">
            <form method="post">
              <input type="hidden" name="scope" value="{{ scope }}" />
              <input type="hidden" name="action" value="approve_change" />
              <input type="hidden" name="change_id" value="{{ change.id }}" />
              <button class="pri" type="submit">Утвердить</button>
            </form>
            <form method="post">
              <input type="hidden" name="scope" value="{{ scope }}" />
              <input type="hidden" name="action" value="reject_change" />
              <input type="hidden" name="change_id" value="{{ change.id }}" />
              <button class="danger" type="submit">Отклонить</button>
            </form>
          </div>
          {% endif %}
        </div>
        {% endfor %}
      </div>
    </section>
    {% endif %}

    {% if is_super_admin %}
    <section class="box admin-users-box">
      <h3>Администраторы</h3>
      {% if created_admin_credentials %}
      <div class="created-admin-card">
        <strong>Новый админ создан</strong>
        <div>Логин: <code>{{ created_admin_credentials.username }}</code></div>
        <div>Пароль: <code>{{ created_admin_credentials.password }}</code></div>
        <div>Доступ: <code>{{ created_admin_credentials.access }}</code></div>
        <div class="created-admin-share">
          <textarea id="createdAdminShare" readonly>Админка: {{ request.url_root.rstrip('/') }}{{ request.path }}
Логин: {{ created_admin_credentials.username }}
Пароль: {{ created_admin_credentials.password }}</textarea>
          <button class="pri" type="button" data-copy-target="createdAdminShare">Скопировать</button>
        </div>
      </div>
      {% endif %}
      <form method="post" class="admin-create">
        <input type="hidden" name="scope" value="{{ scope }}" />
        <input type="hidden" name="action" value="create_admin" />
        <input type="text" name="username" placeholder="Логин нового пользователя" required />
        <select name="new_role" id="newRoleSelect" style="padding:8px 10px;border-radius:8px;border:1px solid #ccd8f0;font-family:Manrope,sans-serif;font-size:13px;color:#1a3060;" onchange="toggleScopeFields(this.value)">
          <option value="admin">Администратор матрицы</option>
          <option value="news_editor">Редактор новостей</option>
        </select>
        <span id="scopeFields"><label><input type="checkbox" name="allow_infra" checked /> Инфраструктура</label>
        <label><input type="checkbox" name="allow_ib" checked /> ИБ</label></span>
        <button class="pri" type="submit">Создать</button>
      </form>
      <script>
      function toggleScopeFields(role) {
        document.getElementById('scopeFields').style.display = role === 'news_editor' ? 'none' : '';
      }
      </script>
      <div class="admin-users-list">
        {% for user in admin_users %}
        <form class="admin-user-item" method="post">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <input type="hidden" name="action" value="delete_admin" />
          <input type="hidden" name="admin_id" value="{{ user.id }}" />
          <span>
            <strong>{{ user.username }}</strong>
            <small>{{ user.role }} · {{ user.access_scopes }}</small>
          </span>
          {% if user.role != 'super' %}
          <button class="danger" type="submit">Удалить</button>
          {% endif %}
        </form>
        {% endfor %}
      </div>
    </section>
    {% endif %}

    <section class="grid">
      <div class="box">
        <h3>Добавить вендора</h3>
        <form method="post" class="inline">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <input type="hidden" name="action" value="add_vendor" />
          <input type="text" name="name" placeholder="Название вендора" required />
          <button class="pri" type="submit">Добавить</button>
        </form>
      </div>
      <div class="box">
        <h3>Добавить категорию</h3>
        <form method="post" class="inline">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <input type="hidden" name="action" value="add_category" />
          <input type="text" name="name" placeholder="Название категории" required />
          <button class="pri" type="submit">Добавить</button>
        </form>
      </div>
      <div class="box">
        <h3>Добавить продукт</h3>
        <form method="post" class="inline inline-product">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <input type="hidden" name="action" value="add_product" />
          <select name="vendor_id" required style="padding:9px 10px; border:1px solid #c6d8fb; border-radius:9px;">
            {% for v in vendors %}
              <option value="{{ v.id }}" {% if v.pending %}disabled{% endif %}>{{ v.name }}{% if v.pending %} (на проверке){% endif %}</option>
            {% endfor %}
          </select>
          <input type="text" name="name" placeholder="Название продукта" required />
          <input type="text" name="url" placeholder="URL продукта (необязательно)" />
          <button class="pri" type="submit">Добавить</button>
        </form>
      </div>
    </section>

    <section class="lists">
      <div class="box">
        <h3>Вендоры</h3>
        <div class="list-box">
        {% for v in vendors %}
        <div class="vendor-item">
          <div class="list-item">
            <span>
              {{ v.name }}{% if v.pending %} <small>(на проверке)</small>{% endif %}
              {% if v.website %}<a href="{{ v.website }}" target="_blank" rel="noopener" style="margin-left:6px;font-size:11px;">сайт</a>{% endif %}
              {% if v.mont_page %}<a href="{{ v.mont_page }}" target="_blank" rel="noopener" style="margin-left:4px;font-size:11px;">MONT</a>{% endif %}
            </span>
            {% if not v.pending %}
            <div class="product-actions">
              <button class="warn" type="button" data-edit-vendor="{{ v.id }}">Изменить</button>
              <form method="post" style="margin:0">
                <input type="hidden" name="scope" value="{{ scope }}" />
                <input type="hidden" name="action" value="delete_vendor" />
                <input type="hidden" name="vendor_id" value="{{ v.id }}" />
                <button class="danger" type="submit">Удалить</button>
              </form>
            </div>
            {% endif %}
          </div>
          {% if not v.pending %}
          <form class="product-edit" method="post" data-vendor-edit="{{ v.id }}" hidden>
            <input type="hidden" name="scope" value="{{ scope }}" />
            <input type="hidden" name="action" value="update_vendor" />
            <input type="hidden" name="vendor_id" value="{{ v.id }}" />
            <input type="text" name="name" value="{{ v.name }}" placeholder="Название вендора" required />
            <input type="url" name="website" value="{{ v.website or '' }}" placeholder="Сайт вендора (https://...)" />
            <input type="url" name="mont_page" value="{{ v.mont_page or '' }}" placeholder="Страница на MONT (https://...)" />
            <button class="pri" type="submit">Сохранить</button>
          </form>
          {% endif %}
        </div>
        {% endfor %}
        </div>
      </div>
      <div class="box">
        <h3>Удалить категорию</h3>
        <div class="list-box">
        {% for c in categories %}
        <form class="list-item" method="post">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <span>{{ c.name }}{% if c.pending %} <small>(на проверке)</small>{% endif %}</span>
          <input type="hidden" name="action" value="delete_category" />
          <input type="hidden" name="category_id" value="{{ c.id }}" />
          {% if not c.pending %}<button class="danger" type="submit">Удалить</button>{% endif %}
        </form>
        {% endfor %}
        </div>
      </div>
      <div class="box">
        <h3>Продукты</h3>
        <div class="list-box">
        {% for p in products %}
        <div class="product-item">
          <div class="product-row">
            <span>
              {{ p.vendor_name }} :: {{ p.name }}
              {% if p.pending %}<small>(на проверке)</small>{% endif %}
              {% if p.url %}<a href="{{ p.url }}" target="_blank" rel="noopener noreferrer" style="margin-left:6px; font-size:11px;">ссылка</a>{% endif %}
            </span>
            {% if not p.pending %}
            <div class="product-actions">
              <button class="warn" type="button" data-edit-product="{{ p.id }}">Изменить</button>
              <form method="post">
                <input type="hidden" name="scope" value="{{ scope }}" />
                <input type="hidden" name="action" value="delete_product" />
                <input type="hidden" name="product_id" value="{{ p.id }}" />
                <button class="danger" type="submit">Удалить</button>
              </form>
            </div>
            {% endif %}
          </div>
          {% if not p.pending %}
          <form class="product-edit" method="post" data-product-edit="{{ p.id }}" hidden>
            <input type="hidden" name="scope" value="{{ scope }}" />
            <input type="hidden" name="action" value="update_product" />
            <input type="hidden" name="product_id" value="{{ p.id }}" />
            <input type="text" name="name" value="{{ p.name }}" placeholder="Название продукта" required />
            <input type="text" name="url" value="{{ p.url or '' }}" placeholder="URL продукта" />
            <button class="pri" type="submit">Сохранить</button>
          </form>
          {% endif %}
        </div>
        {% endfor %}
        </div>
      </div>
    </section>

    <section class="matrix-wrap">
      <p class="matrix-tip">Прокрутка: колесо/тачпад вниз-вверх внутри таблицы, полосой ниже - влево-вправо.</p>
      <form method="post" id="matrixForm">
        <input type="hidden" name="scope" value="{{ scope }}" />
        <input type="hidden" name="action" value="save_matrix" />
        <div id="matrixHScroll" class="matrix-h-scroll"><div id="matrixHScrollInner" class="matrix-h-scroll-inner"></div></div>
        <div id="matrixScroll" class="matrix-scroll">
          <table id="matrixTable">
            <tr>
              <th>Вендор / Продукт</th>
              {% for c in categories %}
                <th>{{ c.name }}</th>
              {% endfor %}
            </tr>
            {% for p in products %}
            <tr>
              <td><strong>{{ p.vendor_name }}</strong><br/>{{ p.name }}</td>
              {% for c in categories %}
              <td>
                <input
                  type="checkbox"
                  name="pc_{{ p.id }}_{{ c.id }}"
                  {% if (p.id, c.id) in links %}checked{% endif %}
                  {% if p.pending or c.pending %}disabled{% endif %}
                />
              </td>
              {% endfor %}
            </tr>
            {% endfor %}
          </table>
        </div>
      </form>
    </section>
  </main>
  <script src="{{ url_for('static', filename='js/admin.js') }}"></script>
</body>
</html>
