{% extends "base.html" %}
{% block content %}
<div class="container mt-5">
  <div class="card shadow-sm">
    <div class="card-header">
      <h2 class="mb-0">Zaawansowane ustawienia harmonogramu</h2>
    </div>
    <div class="card-body">
      <form action="{{ url_for('advanced_schedule') }}" method="POST">
        <div class="mb-3">
          <div class="mb-3">
            <label for="backup_retention_days" class="form-label">Próg retencji backupów (dni)</label>
            <small>Usuwanie danych starszych niż ustawione w progu</small>
            <input type="number" class="form-control" id="backup_retention_days" name="backup_retention_days" value="{{ settings.backup_retention_days }}">
          </div>
          <div class="mb-3">
            <label for="log_retention_days" class="form-label">Próg retencji logów (dni)</label>
            <input type="number" class="form-control" id="log_retention_days" name="log_retention_days" value="{{ settings.log_retention_days }}">
          </div>
          <label for="retention_cron" class="form-label">Harmonogram retencji</label> <code>cron</code>
          <div class="input-group">
            <input type="text" class="form-control" id="retention_cron" name="retention_cron" value="{{ settings.retention_cron }}">
            <button type="button" class="btn btn-outline-secondary" onclick="openCronModal('retention_cron')">Generuj cron</button>
          </div>
          <div class="form-text">Np. <code>0 */12 * * *</code> – co 12 godzin</div>
        </div>
        <div class="mb-3">
          <label for="binary_cron" class="form-label">Harmonogram kopii zapasowych binarnych</label> <code>cron</code>
          <div class="input-group">
            <input type="text" class="form-control" id="binary_cron" name="binary_cron" value="{{ settings.binary_cron|default('') }}">
            <button type="button" class="btn btn-outline-secondary" onclick="openCronModal('binary_cron')">Generuj cron</button>
          </div>
          <div class="form-text">Np. <code>15 2 * * *</code> – codziennie o 2:15</div>
        </div>
        <div class="mb-3">
          <label for="export_cron" class="form-label">Harmonogram exportów (poleceń /export) <code>cron</code></label>
          <div class="input-group">
            <input type="text" class="form-control" id="export_cron" name="export_cron" value="{{ settings.export_cron }}">
            <button type="button" class="btn btn-outline-secondary" onclick="openCronModal('export_cron')">Generuj cron</button>
          </div>
          <div class="form-text">Np. <code>0 */12 * * *</code> – co 12 godzin</div>
        </div>
        <div class="mb-3 form-check">
          <input type="checkbox" class="form-check-input" id="enable_auto_export" name="enable_auto_export" {% if settings.enable_auto_export %}checked{% endif %}>
          <label class="form-check-label" for="enable_auto_export">Włącz automatyczny export</label>
        </div>
        <button type="submit" class="btn btn-primary">Zapisz ustawienia</button>
      </form>
    </div>
  </div>
</div>

<!-- Modal do generowania wyrażenia CRON -->
<div class="modal fade" id="cronModal" tabindex="-1" aria-labelledby="cronModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="cronModalLabel">Generuj wyrażenie CRON</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Zamknij"></button>
      </div>
      <div class="modal-body">
        <div class="mb-3">
          <label for="cron_minute" class="form-label">Minuta</label>
          <input type="text" class="form-control" id="cron_minute" placeholder="0-59">
        </div>
        <div class="mb-3">
          <label for="cron_hour" class="form-label">Godzina</label>
          <input type="text" class="form-control" id="cron_hour" placeholder="0-23">
        </div>
        <div class="mb-3">
          <label for="cron_day" class="form-label">Dzień miesiąca</label>
          <input type="text" class="form-control" id="cron_day" placeholder="1-31 lub *">
        </div>
        <div class="mb-3">
          <label for="cron_month" class="form-label">Miesiąc</label>
          <input type="text" class="form-control" id="cron_month" placeholder="1-12 lub *">
        </div>
        <div class="mb-3">
          <label for="cron_dow" class="form-label">Dzień tygodnia</label>
          <input type="text" class="form-control" id="cron_dow" placeholder="0-6 (0 = niedziela) lub *">
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Anuluj</button>
        <button type="button" class="btn btn-primary" onclick="generateCronExpression()">Generuj</button>
      </div>
    </div>
  </div>
</div>

<script>
  var targetCronField = '';

  function openCronModal(fieldId) {
    targetCronField = fieldId;
    document.getElementById('cron_minute').value = '*';
    document.getElementById('cron_hour').value = '*';
    document.getElementById('cron_day').value = '*';
    document.getElementById('cron_month').value = '*';
    document.getElementById('cron_dow').value = '*';
    var cronModal = new bootstrap.Modal(document.getElementById('cronModal'));
    cronModal.show();
  }

  function generateCronExpression() {
    var minute = document.getElementById('cron_minute').value || '*';
    var hour = document.getElementById('cron_hour').value || '*';
    var day = document.getElementById('cron_day').value || '*';
    var month = document.getElementById('cron_month').value || '*';
    var dow = document.getElementById('cron_dow').value || '*';
    var cronExpr = minute + ' ' + hour + ' ' + day + ' ' + month + ' ' + dow;
    document.getElementById(targetCronField).value = cronExpr;
    var modalEl = document.getElementById('cronModal');
    var modalInstance = bootstrap.Modal.getInstance(modalEl);
    modalInstance.hide();
  }
</script>
{% endblock %}