{% extends "admin_base.html" %}
{% block extra_head %}
{{ super() }}
<link rel="stylesheet" href="{{ static_url('/static/css/services.css') }}">
<link rel="stylesheet" href="{{ static_url('/static/css/backups.css') }}">
{% endblock %}
{% block admin_content %}
<div class="admin-toolbar">
    <h2>Backups</h2>
    <div class="backup-controls">
        <form id="backup-run-form" method="POST" action="/admin/backups/run" class="admin-inline-form">
            <label>Target
                <select name="target" id="backup-run-target">
                    {% for target in targets %}
                    <option value="{{ target.key }}">{{ target.label }}</option>
                    {% endfor %}
                </select>
            </label>
            <button type="submit" class="admin-btn admin-btn-primary"><span class="icon">&#x1F4BE;</span> Create backup</button>
        </form>
        <a href="#" class="admin-btn admin-btn-sm" data-modal="backup-schedule-modal" id="backup-schedule-new"><span class="icon">&#x1F551;</span> New schedule</a>
        <span class="backup-generated" data-meta="generated">-</span>
    </div>
</div>

<div id="backups-root" class="backups">
    <p class="admin-empty" data-empty>Loading backup status...</p>
</div>

<div id="backup-schedule-modal" class="modal-overlay">
    <div class="modal">
        <div class="modal-header">
            <h3 id="backup-schedule-title">New backup schedule</h3>
            <a href="#" class="modal-close">&times;</a>
        </div>
        <form id="backup-schedule-form" method="POST" action="/admin/backups/schedules/create" class="modal-body backup-schedule-form">
            <label>Name
                <input type="text" name="name" maxlength="120" required placeholder="Nightly full backup">
            </label>
            <label>Target
                <select name="target">
                    {% for target in targets %}
                    <option value="{{ target.key }}">{{ target.label }}</option>
                    {% endfor %}
                </select>
            </label>
            <label>Schedule type
                <select name="kind" id="backup-schedule-kind">
                    <option value="interval">Every N seconds</option>
                    <option value="cron">Cron expression</option>
                </select>
            </label>
            <label data-kind="interval">Interval (seconds)
                <input type="number" name="every_seconds" min="60" value="86400">
            </label>
            <label data-kind="cron" hidden>Cron (minute hour dom month dow)
                <input type="text" name="cron" maxlength="120" placeholder="0 3 * * *">
            </label>
            <label>Keep last N backups (0 = keep all)
                <input type="number" name="keep_last" min="0" max="1000" value="7">
            </label>
            <div class="modal-actions">
                <button type="submit" class="admin-btn admin-btn-primary">Save schedule</button>
            </div>
        </form>
    </div>
</div>
{% endblock %}
{% block extra_js %}
<script type="module" src="{{ static_url('/static/js/BackupMonitor.js') }}"></script>
<script type="module">
new window.BackupMonitor().start();
</script>
{% endblock %}
