@extends('layouts.admin')

@section('title', 'Import / Export')
@section('page-title', 'Import / Export de Données')

@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">
        {{-- Import --}}
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="bi bi-upload me-2"></i>Importer des Données</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.import-export.import') }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">Type de données</label>
                            <select name="type" class="form-select" required>
                                <option value="">Sélectionner...</option>
                                <option value="users">Utilisateurs</option>
                                <option value="products">Produits</option>
                                <option value="categories">Catégories</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Fichier (CSV ou JSON)</label>
                            <input type="file" name="file" class="form-control" accept=".csv,.json,.xlsx" required>
                            <small class="text-muted">Max: 10 MB</small>
                        </div>
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="bi bi-upload me-2"></i>Importer
                        </button>
                    </form>
                </div>
            </div>
        </div>

        {{-- Export --}}
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0"><i class="bi bi-download me-2"></i>Exporter des Données</h5>
                </div>
                <div class="card-body">
                    <form action="{{ route('admin.import-export.export') }}" method="POST">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">Type de données</label>
                            <select name="type" class="form-select" required>
                                <option value="">Sélectionner...</option>
                                <option value="users">Utilisateurs</option>
                                <option value="products">Produits</option>
                                <option value="categories">Catégories</option>
                                <option value="orders">Commandes</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Format</label>
                            <select name="format" class="form-select" required>
                                <option value="csv">CSV</option>
                                <option value="json">JSON</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-success w-100">
                            <i class="bi bi-download me-2"></i>Exporter
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    {{-- Historique --}}
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0"><i class="bi bi-clock-history me-2"></i>Historique des Fichiers</h5>
        </div>
        <div class="card-body p-0">
            @if(count($jobs) > 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($jobs as $job)
                            <tr>
                                <td>
                                    <i class="bi bi-file-earmark text-primary me-2"></i>
                                    {{ $job->filename }}
                                </td>
                                <td>{{ $job->size }}</td>
                                <td>{{ $job->created_at->format('d/m/Y H:i') }}</td>
                                <td class="text-end">
                                    <a href="{{ route('admin.import-export.download', $job->filename) }}" 
                                       class="btn btn-sm btn-outline-primary" title="Télécharger">
                                        <i class="bi bi-download"></i>
                                    </a>
                                    <form action="{{ route('admin.import-export.destroy', $job->id) }}" 
                                          method="POST" class="d-inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-danger"
                                                onclick="return confirm('Supprimer ce fichier ?')"
                                                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-folder display-4 text-muted"></i>
                    <p class="text-muted mt-2">Aucun fichier disponible</p>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
