{% extends 'base.html' %}
{% block content %}
<div class="page-toolbar">
  <div><h1 class="h3 mb-1">Backups</h1><p class="text-muted mb-0">Gestión auditable de copias completas y copias exclusivas de recetas.</p></div>
  <div class="d-flex gap-2 flex-wrap"><button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#backupConfigModal"><i class="bi bi-gear"></i> Configurar</button><form method="post"><input type="hidden" name="action" value="create_full"><button class="btn btn-primary">Backup completo</button></form><form method="post"><input type="hidden" name="action" value="create_recipes"><button class="btn btn-outline-secondary">Backup solo recetas</button></form></div>
</div>
<div class="card table-panel">
  <div class="card-header"><div class="filter-toolbar row g-3 align-items-end"><div class="col-lg-8"><label class="form-label">Filtro rápido</label><input class="form-control" data-table-filter-target="#backupsTable" placeholder="Buscar por archivo, tipo, estado o destino"></div><div class="col-lg-4 small text-muted">Retención configurada: {{ settings.retention_years }} años.</div></div></div>
  <div class="table-responsive wide-table"><table class="table align-middle mb-0" id="backupsTable"><thead><tr><th>Fecha</th><th>Tipo</th><th>Archivo</th><th>Estado</th><th>Destino</th><th class="text-end">Acciones</th></tr></thead><tbody>{% for item in records %}<tr><td>{{ item.created_at.strftime('%d/%m/%Y %H:%M') }}</td><td>{{ item.scope }}</td><td>{{ item.filename }}<br><span class="small text-muted">{{ item.size_bytes }} bytes · {{ item.sha256[:12] if item.sha256 else '' }}</span></td><td><span class="badge text-bg-light">{{ item.status }}</span><br><span class="small text-muted">{{ item.notes or '—' }}</span></td><td class="text-wrap">{{ item.target_path or 'Local' }}</td><td class="text-end"><a class="btn btn-sm btn-outline-primary" href="{{ url_for('admin_backup_download', record_id=item.id) }}">Descargar</a></td></tr>{% else %}<tr><td colspan="6" class="text-center text-muted py-4">Todavía no hay copias.</td></tr>{% endfor %}</tbody></table></div>
</div>
<div class="modal fade" id="backupConfigModal" tabindex="-1" aria-hidden="true"><div class="modal-dialog modal-xl modal-dialog-scrollable"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">Configuración de backups</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><form method="post"><input type="hidden" name="action" value="save_config"><div class="modal-body"><div class="row g-4 modal-form-grid"><div class="col-lg-6"><div class="section-card h-100"><div class="section-title">Destino</div><div class="row g-3"><div class="col-12"><label class="form-label">Proveedor</label><select class="form-select searchable-select" name="backup_provider" data-placeholder="Seleccionar proveedor"><option value="local" {% if settings.provider=='local' %}selected{% endif %}>Local</option><option value="nas" {% if settings.provider=='nas' %}selected{% endif %}>NAS / carpeta de red</option><option value="server" {% if settings.provider=='server' %}selected{% endif %}>Otro servidor</option><option value="drive" {% if settings.provider=='drive' %}selected{% endif %}>Google Drive</option><option value="icloud" {% if settings.provider=='icloud' %}selected{% endif %}>iCloud</option></select></div><div class="col-12"><label class="form-label">Ruta destino</label><input class="form-control field-placeholder" name="backup_target_path" value="{{ settings.target_path }}" placeholder="Ej.: ruta local, NAS o carpeta de red"></div><div class="col-md-6"><label class="form-label">Host remoto</label><input class="form-control field-placeholder" name="backup_remote_host" value="{{ settings.remote_host }}" placeholder="192.168.1.10"></div><div class="col-md-6"><label class="form-label">Usuario remoto</label><input class="form-control field-placeholder" name="backup_remote_user" value="{{ settings.remote_user }}" placeholder="backup_user"></div><div class="col-12"><label class="form-label">Clave remota</label><input class="form-control field-placeholder" type="password" name="backup_remote_password" value="{{ settings.remote_password }}" placeholder="••••••••"></div></div></div></div><div class="col-lg-6"><div class="section-card h-100"><div class="section-title">Política de retención</div><div class="row g-3"><div class="col-md-6"><label class="form-label">Retención (años)</label><input class="form-control" name="backup_retention_years" value="{{ settings.retention_years }}"></div><div class="col-12"><div class="alert alert-light border mb-0">En esta versión la copia automática está operativa para almacenamiento local o rutas de red/NAS. Los demás proveedores quedan configurados para integración futura.</div></div></div></div></div></div></div><div class="modal-footer"><button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancelar</button><button class="btn btn-primary">Guardar configuración</button></div></form></div></div></div>
{% endblock %}
