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

<section class="page">
  <div class="page-header">
    <div>
      <div class="breadcrumb"><a href="/packages">Packages</a><span>/</span><span><%= packageItem.code %></span></div>
      <h1><%= packageItem.name %></h1>
      <p><%= packageItem.description %></p>
    </div>
    <div class="page-actions">
      <button class="btn btn-secondary" type="button" data-modal-open="updatePackageModal" data-package-update="<%= packageItem.id %>">
        <span class="material-symbols-outlined">upgrade</span>
        Update version
      </button>
      <form method="post" action="/packages/<%= packageItem.id %>/delete" data-confirm-submit="Xóa package <%= packageItem.code %>? Thao tác này sẽ xóa mọi version và liên kết app liên quan.">
        <button class="btn btn-danger" type="submit">
          <span class="material-symbols-outlined">delete</span>
          Xóa package
        </button>
      </form>
    </div>
  </div>

  <div class="detail-grid">
    <section class="panel">
      <div class="panel-header">
        <div>
          <h2>Thông tin package</h2>
          <p>Metadata chính dùng cho web server và API.</p>
        </div>
      </div>
      <dl class="detail-list">
        <div><dt>Code</dt><dd><%= packageItem.code %></dd></div>
        <div><dt>Type</dt><dd><span class="badge <%= helpers.packageTypeClass(packageItem.type) %>"><%= helpers.packageTypeLabel(packageItem.type) %></span></dd></div>
        <div><dt>Latest</dt><dd><strong><%= packageItem.latestVersion %></strong></dd></div>
        <div><dt>Artifact</dt><dd class="mono"><%= packageItem.artifact %></dd></div>
        <div><dt>Owner</dt><dd><%= packageItem.owner %></dd></div>
        <div><dt>Status</dt><dd><span class="badge <%= helpers.statusClass(packageItem.status) %>"><%= packageItem.status %></span></dd></div>
      </dl>
    </section>

    <section class="panel wide-panel">
      <div class="panel-header">
        <div>
          <h2>Version history</h2>
          <p>Mỗi version có ngày upload, changelog và trạng thái.</p>
        </div>
      </div>
      <div class="table-wrap compact">
        <table>
          <thead>
            <tr>
              <th>Version</th>
              <th>Release date</th>
              <th>Uploaded by</th>
              <th>Size</th>
              <th>Status</th>
              <th class="action-col">Actions</th>
            </tr>
          </thead>
          <tbody>
            <% packageItem.versions.forEach((version) => { %>
              <tr>
                <td>
                  <strong><%= version.version %></strong>
                  <span class="table-subtitle"><%= version.changeLog %></span>
                </td>
                <td><%= version.releaseDate %></td>
                <td><%= version.uploadedBy %></td>
                <td><%= version.size %></td>
                <td><span class="badge <%= helpers.statusClass(version.status) %>"><%= version.status %></span></td>
                <td class="action-col">
                  <div class="action-group">
                    <% if (packageItem.type === 'deb' && version.filePath) { %>
                      <a class="icon-button subtle" href="/api/package-versions/<%= version.id %>/download" title="Download package" aria-label="Download package version <%= version.version %>">
                        <span class="material-symbols-outlined">download</span>
                      </a>
                    <% } %>
                    <form method="post" action="/package-versions/<%= version.id %>/latest">
                      <input type="hidden" name="returnTo" value="<%= currentPath %>">
                      <button class="icon-button subtle" type="submit" title="Đặt latest" aria-label="Đặt latest <%= version.version %>" <%= version.status === 'Latest' ? 'disabled' : '' %>>
                        <span class="material-symbols-outlined">stars</span>
                      </button>
                    </form>
                    <form method="post" action="/package-versions/<%= version.id %>/delete" data-confirm-submit="Xóa version <%= version.version %> khỏi package?">
                      <input type="hidden" name="returnTo" value="<%= currentPath %>">
                      <button class="icon-button danger" type="submit" title="Xóa version" aria-label="Xóa version <%= version.version %>">
                        <span class="material-symbols-outlined">delete</span>
                      </button>
                    </form>
                  </div>
                </td>
              </tr>
            <% }) %>
          </tbody>
        </table>
      </div>
    </section>
  </div>
</section>

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