{% extends "base.html" %}
{% block title %}Clonezilla Backups - PXE Server Manager{% endblock %}

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

<div class="card">
  <div class="card-header d-flex align-items-center">
    Machine Backups
    <span class="badge bg-secondary ms-2">{{ backups|length }}</span>
  </div>
  <div class="card-body p-0">
    {% if backups %}
    <table class="table table-hover mb-0">
      <thead class="table-light">
        <tr>
          <th>Machine #</th>
          <th>Filename</th>
          <th>Size</th>
          <th>Last Modified</th>
          <th class="text-end">Actions</th>
        </tr>
      </thead>
      <tbody>
        {% for b in backups %}
        <tr>
          <td><strong>{{ b.machine }}</strong></td>
          <td><code>{{ b.filename }}</code></td>
          <td>{{ "%.1f"|format(b.size / 1073741824) }} GB</td>
          <td>{{ b.modified | timestamp_fmt }}</td>
          <td class="text-end text-nowrap">
            <a href="{{ url_for('clonezilla_download', filename=b.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="{{ b.filename }}" data-machine="{{ b.machine }}" title="Delete">
              Delete
            </button>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {% else %}
    <div class="text-center text-muted py-5">
      <p class="mt-2">No backups found. Upload a Clonezilla backup .zip to get started.</p>
    </div>
    {% endif %}
  </div>
</div>

<div class="card mt-3">
  <div class="card-body">
    <h6 class="card-title">Backup Naming Convention</h6>
    <p class="card-text mb-0">
      Name backup files with the machine number (e.g., <code>6501.zip</code>).
      The Samba share <code>\\pxe-server\clonezilla</code> is also available on the network for direct Clonezilla save/restore operations.
    </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('clonezilla_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 Backup</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>
        <div class="modal-body">
          <div class="mb-3">
            <label for="backupFile" class="form-label">Backup File (.zip)</label>
            <input type="file" class="form-control" id="backupFile" name="backup_file"
                   accept=".zip" required>
            <div class="form-text">
              Name the file with the machine number (e.g., <code>6501.zip</code>).
            </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 the backup for machine <strong id="deleteMachine"></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');
  var machine = btn.getAttribute('data-machine');
  document.getElementById('deleteMachine').textContent = machine;
  document.getElementById('deleteForm').action = '/backups/delete/' + encodeURIComponent(filename);
});
</script>
{% endblock %}
