<%- include('partials/page-start') %>

<section class="page">
  <div class="page-header">
    <div>
      <h1>Packages</h1>
      <p>Quản lý package `.deb`, Docker image, version và trạng thái latest.</p>
    </div>
    <div class="page-actions">
      <a class="btn btn-secondary" href="/packages/export.csv">
        <span class="material-symbols-outlined">download</span>
        Export
      </a>
      <button class="btn btn-primary" type="button" data-modal-open="uploadPackageModal">
        <span class="material-symbols-outlined">upload_file</span>
        Upload package
      </button>
    </div>
  </div>

  <div class="page-filters">
    <label class="filter-field">
      <span>Type</span>
      <select data-filter-select data-filter-column="type" data-filter-table="packagesTable">
        <option value="">Tất cả</option>
        <option value="deb">.deb</option>
        <option value="apt">APT</option>
        <option value="docker">Docker</option>
      </select>
    </label>
    <label class="filter-field">
      <span>Status</span>
      <select data-filter-select data-filter-column="status" data-filter-table="packagesTable">
        <option value="">Tất cả</option>
        <option value="Active">Active</option>
        <option value="Archived">Archived</option>
      </select>
    </label>
    <label class="filter-field wide">
      <span>Search</span>
      <input type="search" placeholder="Tìm theo tên, code, owner..." data-table-search="packagesTable">
    </label>
  </div>

  <section class="table-panel">
    <div class="table-wrap">
      <table id="packagesTable" class="data-table">
        <thead>
          <tr>
            <th>Package</th>
            <th>Type</th>
            <th>Latest version</th>
            <th>Release date</th>
            <th>Owner</th>
            <th>Status</th>
            <th class="action-col">Actions</th>
          </tr>
        </thead>
        <tbody>
          <% if (packages.length === 0) { %>
            <tr>
              <td colspan="7" class="table-empty">Chưa có package trong database. Bấm Upload package để tạo package đầu tiên.</td>
            </tr>
          <% } %>
          <% packages.forEach((item) => { %>
            <tr data-search="<%= `${item.name} ${item.code} ${item.owner} ${item.latestVersion}`.toLowerCase() %>" data-type="<%= item.type %>" data-status="<%= item.status %>">
              <td>
                <a class="table-title" href="/packages/<%= item.id %>"><%= item.name %></a>
                <span class="table-subtitle"><%= item.code %></span>
              </td>
              <td><span class="badge <%= helpers.packageTypeClass(item.type) %>"><%= helpers.packageTypeLabel(item.type) %></span></td>
              <td><strong><%= item.latestVersion %></strong></td>
              <td><%= item.latestReleaseDate %></td>
              <td><%= item.owner %></td>
              <td><span class="badge <%= helpers.statusClass(item.status) %>"><%= item.status %></span></td>
              <td class="action-col">
                <div class="action-group">
                  <a class="icon-button subtle" href="/packages/<%= item.id %>" title="Xem chi tiết" aria-label="Xem chi tiết <%= item.name %>">
                    <span class="material-symbols-outlined">visibility</span>
                  </a>
                  <% if (item.type === 'deb' && item.latestVersionId && item.artifact) { %>
                    <a class="icon-button subtle" href="/api/package-versions/<%= item.latestVersionId %>/download" title="Download latest package" aria-label="Download latest package <%= item.name %>">
                      <span class="material-symbols-outlined">download</span>
                    </a>
                  <% } %>
                  <button class="icon-button subtle" type="button" title="Update version" data-modal-open="updatePackageModal" data-package-update="<%= item.id %>">
                    <span class="material-symbols-outlined">upgrade</span>
                  </button>
                  <form method="post" action="/packages/<%= item.id %>/delete" data-confirm-submit="Xóa package <%= item.code %>? Thao tác này sẽ xóa cả version và liên kết app liên quan.">
                    <button class="icon-button danger" type="submit" title="Xóa package" aria-label="Xóa package <%= item.name %>">
                      <span class="material-symbols-outlined">delete</span>
                    </button>
                  </form>
                </div>
              </td>
            </tr>
          <% }) %>
        </tbody>
      </table>
    </div>
    <div class="page-pager">
      <span>Showing 1-<%= packages.length %> of <%= packages.length %></span>
      <div>
        <button type="button" disabled>Prev</button>
        <span>Page 1 / 1</span>
        <button type="button" disabled>Next</button>
      </div>
    </div>
  </section>
</section>

<%- include('partials/package-modal') %>
<%- include('partials/update-package-modal') %>
<%- include('partials/page-end') %>
