@extends('admin.layout')

@section('title', $method ? 'Modifier '.$method->name : 'Nouvelle méthode de livraison')
@section('header', $method ? 'Modifier '.$method->name : 'Nouvelle méthode de livraison')

@section('content')
<nav class="mb-6">
    <a href="{{ route('admin.ecommerce.shipping-methods') }}" class="text-slate-500 hover:text-slate-900 text-sm">← Livraison</a>
</nav>

<form method="POST" action="{{ $method ? route('admin.ecommerce.shipping-methods.update', $method) : route('admin.ecommerce.shipping-methods.store') }}" class="max-w-2xl space-y-6">
    @csrf
    @if($method) @method('PATCH') @endif

    <div class="bg-white rounded-2xl border border-slate-200 p-6">
        <h3 class="font-semibold text-slate-800 mb-4">Informations générales</h3>
        <div class="grid md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Nom affiché *</label>
                <input type="text" name="name" value="{{ old('name', $method?->name) }}" required placeholder="Colissimo Domicile" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Transporteur *</label>
                <select name="carrier" required class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500" {{ $method ? 'disabled' : '' }}>
                    <option value="colissimo" {{ old('carrier', $method?->carrier) === 'colissimo' ? 'selected' : '' }}>Colissimo</option>
                    <option value="chronopost" {{ old('carrier', $method?->carrier) === 'chronopost' ? 'selected' : '' }}>Chronopost</option>
                    <option value="mondial_relay" {{ old('carrier', $method?->carrier) === 'mondial_relay' ? 'selected' : '' }}>Mondial Relay</option>
                    <option value="laposte" {{ old('carrier', $method?->carrier) === 'laposte' ? 'selected' : '' }}>La Poste</option>
                    <option value="custom" {{ old('carrier', $method?->carrier) === 'custom' ? 'selected' : '' }}>Personnalisé</option>
                </select>
                @if($method)<input type="hidden" name="carrier" value="{{ $method->carrier }}">@endif
            </div>
        </div>
    </div>

    <div class="bg-white rounded-2xl border border-slate-200 p-6">
        <h3 class="font-semibold text-slate-800 mb-4">Tarification</h3>
        <div class="grid md:grid-cols-2 lg:grid-cols-4 gap-6">
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Prix de base (€) *</label>
                <input type="number" name="price" step="0.01" min="0" value="{{ old('price', $method?->price ?? 5.49) }}" required class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Gratuit à partir de (€)</label>
                <input type="number" name="free_from" step="0.01" min="0" value="{{ old('free_from', $method?->free_from) }}" placeholder="Ex: 50" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Prix au kg (€)</label>
                <input type="number" name="price_per_kg" step="0.01" min="0" value="{{ old('price_per_kg', $method?->price_per_kg) }}" placeholder="Ex: 0.50" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Ordre d'affichage</label>
                <input type="number" name="order" min="0" value="{{ old('order', $method?->order ?? 0) }}" class="w-full px-4 py-2.5 border border-slate-200 rounded-xl focus:ring-2 focus:ring-primary-500 focus:border-primary-500">
            </div>
        </div>
    </div>

    <div class="bg-white rounded-2xl border border-slate-200 p-6">
        <h3 class="font-semibold text-slate-800 mb-4">Statut</h3>
        <label class="flex items-center gap-2 cursor-pointer">
            <input type="hidden" name="is_active" value="0">
            <input type="checkbox" name="is_active" value="1" {{ old('is_active', $method?->is_active ?? true) ? 'checked' : '' }} class="rounded border-slate-300 text-primary-500 focus:ring-primary-500">
            <span class="text-sm font-medium text-slate-700">Méthode active (visible au checkout)</span>
        </label>
    </div>

    <div class="flex gap-3">
        <button type="submit" class="px-6 py-2.5 bg-primary-500 text-white rounded-xl font-medium hover:bg-primary-600 transition">{{ $method ? 'Enregistrer' : 'Créer' }}</button>
        <a href="{{ route('admin.ecommerce.shipping-methods') }}" class="px-6 py-2.5 border border-slate-200 rounded-xl font-medium hover:bg-slate-50 transition text-slate-700">Annuler</a>
        @if($method)
        <form method="POST" action="{{ route('admin.ecommerce.shipping-methods.destroy', $method) }}" class="ml-auto" onsubmit="return confirm('Supprimer cette méthode ?')">
            @csrf
            @method('DELETE')
            <button type="submit" class="px-6 py-2.5 border border-red-200 text-red-600 rounded-xl font-medium hover:bg-red-50 transition">Supprimer</button>
        </form>
        @endif
    </div>
</form>

@if($method)
<div class="mt-8 p-4 bg-slate-50 rounded-xl border border-slate-200">
    <p class="text-sm text-slate-600">Les identifiants API (Colissimo, Chronopost, Mondial Relay, La Poste) se configurent dans <a href="{{ route('admin.ecommerce.settings') }}" class="text-primary-500 hover:underline font-medium">Paramètres E-commerce</a>.</p>
</div>
@endif
@endsection
