{% extends "base.html" %}
{% block title %}Enrollment Packages - PXE Server Manager{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
  <h2 class="mb-0">Enrollment Packages</h2>
  <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadModal">
    Upload Package
  </button>
</div>

<div class="card">
  <div class="card-header d-flex align-items-center">
    GCCH Provisioning Packages
    <span class="badge bg-secondary ms-2">{{ packages|length }}</span>
  </div>
  <div class="card-body p-0">
    {% if packages %}
    <table class="table table-hover mb-0">
      <thead class="table-light">
        <tr>
          <th>Filename</th>
          <th>Size</th>
          <th>Last Modified</th>
          <th class="text-end">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for p in packages %}
        <tr>
          <td><code>{{ p.filename }}</code></td>
          <td>{{ "%.1f"|format(p.size / 1048576) }} MB</td>
          <td>{{ p.modified | timestamp_fmt }}</td>
          <td class="text-end text-nowrap">
            <a href="{{ url_for('enrollment_download', filename=p.filename) }}"
               class="btn btn-sm btn-outline-primary" title="Download">
              Download
            </a>
            <button type="button" class="btn btn-sm btn-outline-danger"
                    data-bs-toggle="modal" data-bs-target="#deleteModal"
                    data-filename="{{ p.filename }}" title="Delete">
              Delete
            </button>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {% else %}
    <div class="text-center text-muted py-5">
      <p class="mt-2">No enrollment packages found. Upload a <code>.ppkg</code> file to get started.</p>
    </div>
    {% endif %}
  </div>
</div>

<div class="card mt-3">
  <div class="card-body">
    <h6 class="card-title">About Enrollment Packages</h6>
    <p class="card-text">
      GCCH enrollment <code>.ppkg</code> provisioning packages are copied to
      <code>C:\Enrollment\</code> on the target machine after imaging.
      At OOBE, connect to a network with internet, press <strong>Windows key 5 times</strong>,
      then browse to <code>C:\Enrollment\</code> and select the package. No USB stick needed.
    </p>
    <p class="card-text mb-0">
      <strong>Naming convention:</strong> Use <code>with-office.ppkg</code> and
      <code>without-office.ppkg</code> to match the WinPE enrollment menu options.
    </p>
  </div>
</div>

<!-- Upload Modal -->
<div class="modal fade" id="uploadModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <form action="{{ url_for('enrollment_upload') }}" method="post" enctype="multipart/form-data">
        <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
        <div class="modal-header">
          <h5 class="modal-title">Upload Enrollment Package</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>
        <div class="modal-body">
          <div class="mb-3">
            <label for="ppkgFile" class="form-label">Provisioning Package (.ppkg)</label>
            <input type="file" class="form-control" id="ppkgFile" name="ppkg_file"
                   accept=".ppkg" required>
            <div class="form-text">
              Use <code>with-office.ppkg</code> or <code>without-office.ppkg</code> to match the WinPE boot menu.
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary">Upload</button>
        </div>
      </form>
    </div>
  </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <form id="deleteForm" method="post">
        <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
        <div class="modal-header">
          <h5 class="modal-title">Confirm Delete</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>
        <div class="modal-body">
          <p>Are you sure you want to delete <strong id="deleteFilename"></strong>?</p>
          <p class="text-muted mb-0">This action cannot be undone.</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-danger">Delete</button>
        </div>
      </form>
    </div>
  </div>
</div>
{% endblock %}

{% block extra_scripts %}
<script>
document.getElementById('deleteModal').addEventListener('show.bs.modal', function (event) {
  var btn = event.relatedTarget;
  var filename = btn.getAttribute('data-filename');
  document.getElementById('deleteFilename').textContent = filename;
  document.getElementById('deleteForm').action = '/enrollment/delete/' + encodeURIComponent(filename);
});
</script>
{% endblock %}
