@extends('layouts.app')

@section('title', 'Dashboard - Modification de Conducteur')

@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-account-circle me-2"></i> Modification de conducteur
                </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('listeConducteur') }}">Conducteurs</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: #ffffff">Modifier : {{ $chauffeur->name }}</h5>
                </div>

                <div class="card-body p-4">
                    <form action="{{ route('chauffeurs.update', $chauffeur->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-8">
                                    <label for="name" class="form-label">Nom complet</label>
                                    <input type="text" class="form-control" id="name" name="name"
                                           value="{{ old('name', $chauffeur->name) }}" required>
                                    @error('name')
                                        <div class="text-danger small">{{ $message }}</div>
                                    @enderror
                                </div>

                                <div class="col-md-4">
                                    <label for="telephone" class="form-label">Téléphone</label>
                                    <input type="text" class="form-control" id="telephone" name="telephone"
                                           value="{{ old('telephone', $chauffeur->telephone) }}" required>
                                    @error('telephone')
                                        <div class="text-danger small">{{ $message }}</div>
                                    @enderror
                                </div>
                            </div>
                        </div>

                        <!-- Section Photo -->
                        <div class="mb-4">
                            <h6 class="border-bottom pb-2 mb-3" style="color: #020548;">
                                <i class="mdi mdi-camera me-1"></i> Photo du conducteur
                            </h6>

                            <!-- Photo existante -->
                            <div class="mb-3">
                                <label class="form-label">Photo actuelle</label>
                                <div class="d-flex align-items-center">
                                    @if ($chauffeur->image_urlc)
                                        <div class="position-relative me-3">
                                            <img src="{{ asset('storage/'.$chauffeur->image_urlc) }}"
                                                 class="rounded-circle"
                                                 style="width: 100px; height: 100px; object-fit: cover; border: 3px solid #e0e0e0;">
                                            <button type="button"
                                                    class="btn btn-sm btn-danger position-absolute top-0 end-0 rounded-circle"
                                                    style="width: 25px; height: 25px; padding: 0;"
                                                    onclick="document.getElementById('remove-image-flag').value = '1'; this.parentElement.style.display = 'none';">
                                                ×
                                            </button>
                                            <input type="hidden"
                                                   name="existing_image"
                                                   value="{{ $chauffeur->image_urlc }}">
                                            <input type="hidden"
                                                   name="remove_image"
                                                   id="remove-image-flag"
                                                   value="0">
                                        </div>
                                    @else
                                        <div class="bg-light rounded-circle d-flex align-items-center justify-content-center"
                                             style="width: 100px; height: 100px;">
                                            <i class="mdi mdi-account" style="font-size: 2rem; color: #6c757d;"></i>
                                        </div>
                                    @endif
                                </div>
                            </div>

                            <!-- Nouvelle photo -->
                            <div class="mb-3">
                                <label for="new_image" class="form-label">Changer la photo</label>
                                <input type="file" class="form-control" id="new_image"
                                       name="new_image" accept="image/jpeg, image/png">
                                <div class="form-text">Formats acceptés: JPEG, PNG (Max 2MB)</div>
                                @error('new_image')
                                    <div class="text-danger small">{{ $message }}</div>
                                @enderror
                            </div>
                        </div>

                        <!-- Section Disponibilité -->
                        {{-- <div class="mb-4">
                            <h6 class="border-bottom pb-2 mb-3" style="color: #020548;">
                                <i class="mdi mdi-calendar-check me-1"></i> Disponibilité
                            </h6>

                            <div class="form-check form-switch">
                                <input type="checkbox" class="form-check-input" id="disponible"
                                       name="disponible" {{ $chauffeur->disponible ? 'checked' : '' }}>
                                <label class="form-check-label" for="disponible">Disponible immédiatement</label>
                            </div>
                        </div> --}}

                        <!-- Boutons d'action -->
                        <div class="d-flex justify-content-between border-top pt-3">
                            <a href="{{ route('listeConducteur') }}" 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;
        --primary-hover: #01032e;
        --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;
    }

    .border-bottom {
        border-color: rgba(2, 5, 72, 0.1) !important;
    }

    /* Style spécifique pour la photo */
    .rounded-circle {
        transition: all 0.3s;
    }

    .rounded-circle:hover {
        transform: scale(1.05);
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    }
</style>
@endpush

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Formatage du numéro de téléphone
        const phoneInput = document.getElementById('telephone');
        if (phoneInput) {
            phoneInput.addEventListener('input', function() {
                let phone = this.value.replace(/\D/g, '');
                if (phone.length > 0) {
                    phone = phone.match(/(\d{0,2})(\d{0,2})(\d{0,2})(\d{0,2})(\d{0,2})/);
                    this.value = phone[1] + ' ' + phone[2] + ' ' + phone[3] + ' ' + phone[4] + ' ' + phone[5];
                }
            });
        }

        // Prévisualisation de la nouvelle image
        const newImageInput = document.getElementById('new_image');
        if (newImageInput) {
            newImageInput.addEventListener('change', function(e) {
                if (this.files && this.files[0]) {
                    const reader = new FileReader();
                    reader.onload = function(event) {
                        // Vous pouvez ajouter ici une prévisualisation si nécessaire
                    }
                    reader.readAsDataURL(this.files[0]);
                }
            });
        }
    });
</script>
@endpush
