@extends('layouts.admin')

@section('title', 'Sauvegardes')
@section('page-title', 'Gestion des Sauvegardes')

@section('content')
<div class="container-fluid">
    {{-- Messages --}}
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="bi bi-check-circle me-2"></i>{{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            <i class="bi bi-exclamation-circle me-2"></i>{{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="row">
        {{-- Actions --}}
        <div class="col-md-4">
            <div class="card mb-4">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="bi bi-gear me-2"></i>Actions</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.backups.create') }}" method="POST" class="mb-3">
                        @csrf
                        <button type="submit" class="btn btn-success w-100">
                            <i class="bi bi-plus-circle me-2"></i>Créer une Sauvegarde
                        </button>
                    </form>

                    <hr>

                    <form action="{{ route('admin.backups.schedule') }}" method="POST">
                        @csrf
                        <label class="form-label">Sauvegardes Automatiques</label>
                        <select name="frequency" class="form-select mb-2">
                            <option value="none">Désactivé</option>
                            <option value="daily">Quotidien</option>
                            <option value="weekly">Hebdomadaire</option>
                            <option value="monthly">Mensuel</option>
                        </select>
                        <button type="submit" class="btn btn-outline-primary w-100">
                            <i class="bi bi-calendar-check me-2"></i>Programmer
                        </button>
                    </form>
                </div>
            </div>

            <div class="card">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0"><i class="bi bi-info-circle me-2"></i>Informations</h5>
                </div>
                <div class="card-body">
                    <p class="small">Les sauvegardes contiennent toute la base de données. Pour restaurer, assurez-vous d'avoir une sauvegarde récente.</p>
                    <p class="small text-warning"><i class="bi bi-exclamation-triangle me-1"></i>La restauration écrasera toutes les données actuelles.</p>
                </div>
            </div>
        </div>

        {{-- Liste des sauvegardes --}}
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-archive me-2"></i>Sauvegardes Disponibles</h5>
                </div>
                <div class="card-body p-0">
                    @if(count($backups) > 0)
                        <div class="table-responsive">
                            <table class="table table-hover mb-0">
                                <thead class="table-light">
                                    <tr>
                                        <th>Fichier</th>
                                        <th>Taille</th>
                                        <th>Date</th>
                                        <th class="text-end">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($backups as $backup)
                                    <tr>
                                        <td>
                                            <i class="bi bi-file-earmark-zip text-primary me-2"></i>
                                            {{ $backup->filename }}
                                        </td>
                                        <td>{{ $backup->size }}</td>
                                        <td>{{ $backup->created_at->format('d/m/Y H:i') }}</td>
                                        <td class="text-end">
                                            <a href="{{ route('admin.backups.download', $backup->id) }}" 
                                               class="btn btn-sm btn-outline-primary" title="Télécharger">
                                                <i class="bi bi-download"></i>
                                            </a>
                                            <form action="{{ route('admin.backups.restore', $backup->id) }}" 
                                                  method="POST" class="d-inline">
                                                @csrf
                                                <button type="submit" class="btn btn-sm btn-outline-warning"
                                                        onclick="return confirm('Voulez-vous vraiment restaurer cette sauvegarde ? Toutes les données actuelles seront écrasées.')"
                                                        title="Restaurer">
                                                    <i class="bi bi-arrow-clockwise"></i>
                                                </button>
                                            </form>
                                            <form action="{{ route('admin.backups.destroy', $backup->id) }}" 
                                                  method="POST" class="d-inline">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-outline-danger"
                                                        onclick="return confirm('Supprimer cette sauvegarde ?')"
                                                        title="Supprimer">
                                                    <i class="bi bi-trash"></i>
                                                </button>
                                            </form>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    @else
                        <div class="text-center py-5">
                            <i class="bi bi-archive display-4 text-muted"></i>
                            <p class="text-muted mt-2">Aucune sauvegarde disponible</p>
                            <form action="{{ route('admin.backups.create') }}" method="POST">
                                @csrf
                                <button type="submit" class="btn btn-primary">
                                    <i class="bi bi-plus-circle me-2"></i>Créer la première sauvegarde
                                </button>
                            </form>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
