@extends('layouts.app')

@section('title', 'Dashboard - Formulaire de modification de Véhicule')

@section('content')
<div class="container-fluid">
    <!-- Header -->
    <div class="py-3 py-lg-4">
        <div class="row align-items-center">
            <div class="col-lg-6">
                <h4 class="page-title mb-0" style="color: #020548;">
                    <i class="mdi mdi-car me-2"></i> Modification de véhicule
                </h4>
            </div>
            <div class="col-lg-6">
                <div class="d-none d-lg-block">
                    <ol class="breadcrumb m-0 float-end">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('listeVehicule') }}">Véhicules</a></li>
                        <li class="breadcrumb-item active">Modification</li>
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <!-- Formulaire -->
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header text-white" style="background-color: #020548;">
                    <h5 class="mb-0" style="color: #fffffff">Modifier : {{ $vehicule->name }}</h5>
                </div>

                <div class="card-body p-4">
                    <form action="{{ route('vehicules.update', $vehicule->id) }}" method="POST" enctype="multipart/form-data">
                        @csrf
                        @method('PUT')

                        <!-- Section Informations de base -->
                        <div class="mb-4">
                            <h6 class="border-bottom pb-2 mb-3" style="color: #020548;">
                                <i class="mdi mdi-information-outline me-1"></i> Informations générales
                            </h6>

                            <div class="row g-3">
                                <div class="col-md-6">
                                    <label for="name" class="form-label">Nom du véhicule</label>
                                    <input type="text" class="form-control" id="name" name="name"
                                           value="{{ old('name', $vehicule->name) }}" required>
                                </div>

                                <div class="col-md-3">
                                    <label for="luggage" class="form-label">Nombre de bagages</label>
                                    <input type="number" class="form-control" id="luggage" name="luggage"
                                           value="{{ old('luggage', $vehicule->luggage) }}" min="1" required>
                                </div>

                                <div class="col-md-3">
                                    <label for="number_of_seats" class="form-label">Nombre de sièges</label>
                                    <input type="number" class="form-control" id="number_of_seats" name="number_of_seats"
                                           value="{{ old('number_of_seats', $vehicule->number_of_seats) }}" min="1" required>
                                </div>
                            </div>
                        </div>

                        <!-- Section Images -->
                        <div class="mb-4">
                            <h6 class="border-bottom pb-2 mb-3" style="color: #020548;">
                                <i class="mdi mdi-image-multiple me-1"></i> Gestion des images
                            </h6>

                            <!-- Images existantes -->
                            @if($vehicule->images->count() > 0)
                            <div class="mb-3">
                                <label class="form-label">Images actuelles</label>
                                <div class="row g-2">
                                    @foreach($vehicule->images as $image)
                                    <div class="col-md-3 col-6">
                                        <div class="border p-2 rounded">
                                            <img src="{{ asset('storage/'.$image->images_path) }}"
                                                 class="img-fluid rounded"
                                                 style="height:120px;width:100%;object-fit:cover;"
                                                 onerror="this.src='{{ asset('images/placeholder.jpg') }}'">
                                            <div class="form-check mt-2">
                                                <input type="checkbox"
                                                       name="delete_images[]"
                                                       value="{{ $image->id }}"
                                                       class="form-check-input"
                                                       id="delete_image_{{ $image->id }}">
                                                <label class="form-check-label small" for="delete_image_{{ $image->id }}">
                                                    Supprimer
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                    @endforeach
                                </div>
                            </div>
                            @endif

                            <!-- Nouvelle images -->
                            <div class="mb-3">
                                <label for="new_images" class="form-label">Ajouter des images</label>
                                <input type="file" class="form-control" id="new_images"
                                       name="new_images[]" multiple accept="image/*">
                                <div class="form-text">Formats acceptés: JPEG, PNG, JPG (Max 2MB par image)</div>
                            </div>
                        </div>

                        <!-- Section Tarifs -->
                        <div class="mb-4">
                            <h6 class="border-bottom pb-2 mb-3" style="color: #020548;">
                                <i class="mdi mdi-cash-multiple me-1"></i> Tarification
                            </h6>

                            <div class="row g-3">
                                <div class="col-md-6">
                                    <label for="price_economy" class="form-label">Prix Économique (€)</label>
                                    <div class="input-group">
                                        <span class="input-group-text">€</span>
                                        <input type="number" class="form-control" id="price_economy" name="price_economy"
                                               value="{{ old('price_economy', $vehicule->price_economy) }}" min="0" required>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <label for="price_business" class="form-label">Prix Affaires (€)</label>
                                    <div class="input-group">
                                        <span class="input-group-text">€</span>
                                        <input type="number" class="form-control" id="price_business" name="price_business"
                                               value="{{ old('price_business', $vehicule->price_business) }}" min="0" required>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Options -->
                        <div class="mb-4">
                            <h6 class="border-bottom pb-2 mb-3" style="color: #020548;">
                                <i class="mdi mdi-cog-outline me-1"></i> Options
                            </h6>

                            <div class="form-check form-switch">
                                <input type="checkbox" class="form-check-input" id="air_conditioning"
                                       name="air_conditioning" {{ $vehicule->air_conditioning ? 'checked' : '' }}>
                                <label class="form-check-label" for="air_conditioning">Climatisation</label>
                            </div>
                        </div>

                        <!-- Boutons d'action -->
                        <div class="d-flex justify-content-between border-top pt-3">
                            <a href="{{ route('listeVehicule') }}" class="btn btn-outline-secondary">
                                <i class="mdi mdi-arrow-left me-1"></i> Annuler
                            </a>
                            <button type="submit" class="btn text-white" style="background-color: #020548;">
                                <i class="mdi mdi-content-save me-1"></i> Enregistrer
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
    :root {
        --primary-color: #020548; /* Votre couleur dominante */
        --primary-hover: #01032e; /* Version plus foncée pour les hover */
        --secondary-color: #f8f9fa;
    }

    body {
        background-color: #f5f7fb;
    }

    .card {
        border: none;
        border-radius: 10px;
        box-shadow: 0 4px 20px rgba(2, 5, 72, 0.08);
        overflow: hidden;
    }

    .card-header {
        font-weight: 600;
        letter-spacing: 0.5px;
        background-color: var(--primary-color);
    }

    .form-control, .form-select {
        border-radius: 6px;
        padding: 10px 15px;
        border: 1px solid #e0e0e0;
        transition: all 0.3s;
    }

    .form-control:focus, .form-select:focus {
        border-color: var(--primary-color);
        box-shadow: 0 0 0 0.25rem rgba(2, 5, 72, 0.15);
    }

    .btn-primary {
        background-color: var(--primary-color);
        border-color: var(--primary-color);
        padding: 10px 25px;
        font-weight: 500;
        letter-spacing: 0.5px;
    }

    .btn-primary:hover {
        background-color: var(--primary-hover);
        border-color: var(--primary-hover);
    }

    .form-check-input:checked {
        background-color: var(--primary-color);
        border-color: var(--primary-color);
    }

    input[type="file"] {
        padding: 8px;
    }

    input[type="file"]::file-selector-button {
        background-color: var(--primary-color);
        color: white;
        border: none;
        padding: 8px 16px;
        border-radius: 4px;
        margin-right: 10px;
        cursor: pointer;
        transition: all 0.3s;
    }

    input[type="file"]::file-selector-button:hover {
        background-color: var(--primary-hover);
    }

    h4, h5, h6 {
        font-weight: 600;
    }

    /* Amélioration des bordures de section */
    .border-bottom {
        border-color: rgba(2, 5, 72, 0.1) !important;
    }
</style>
@endpush

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Validation basique côté client
        document.querySelector('form').addEventListener('submit', function(e) {
            const priceEco = document.getElementById('price_economy').value;
            const priceBus = document.getElementById('price_business').value;

            if (parseInt(priceBus) <= parseInt(priceEco)) {
                alert('Le prix affaires doit être supérieur au prix économique');
                e.preventDefault();
            }
        });
    });
</script>
@endpush
