<!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;">
          <a class="scope-chip {% if scope == 'infra' %}active{% endif %}" href="{{ request.path }}?scope=infra">Инфраструктура</a>
          <a class="scope-chip {% if scope == 'ib' %}active{% endif %}" href="{{ request.path }}?scope=ib">ИБ</a>
        </div>
      </div>
      <div style="display:flex; gap:8px;">
        <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>

    <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 }}">{{ v.name }}</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 %}
        <form class="list-item" method="post">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <span>{{ v.name }}</span>
          <input type="hidden" name="action" value="delete_vendor" />
          <input type="hidden" name="vendor_id" value="{{ v.id }}" />
          <button class="danger" type="submit">Удалить</button>
        </form>
        {% 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 }}</span>
          <input type="hidden" name="action" value="delete_category" />
          <input type="hidden" name="category_id" value="{{ c.id }}" />
          <button class="danger" type="submit">Удалить</button>
        </form>
        {% endfor %}
        </div>
      </div>
      <div class="box">
        <h3>Удалить продукт</h3>
        <div class="list-box">
        {% for p in products %}
        <form class="list-item" method="post">
          <input type="hidden" name="scope" value="{{ scope }}" />
          <span>
            {{ p.vendor_name }} :: {{ p.name }}
            {% if p.url %}<a href="{{ p.url }}" target="_blank" rel="noopener noreferrer" style="margin-left:6px; font-size:11px;">ссылка</a>{% endif %}
          </span>
          <input type="hidden" name="action" value="delete_product" />
          <input type="hidden" name="product_id" value="{{ p.id }}" />
          <button class="danger" type="submit">Удалить</button>
        </form>
        {% 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 %}
                />
              </td>
              {% endfor %}
            </tr>
            {% endfor %}
          </table>
        </div>
      </form>
    </section>
  </main>
  <script src="{{ url_for('static', filename='js/admin.js') }}"></script>
</body>
</html>
