@extends('layouts.admin')

@section('title', 'Modifier - ' . $promotion->name)

@section('content')
<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="d-flex align-items-center mb-4">
                <a href="{{ route('admin.promotions.index') }}" class="btn btn-outline-secondary me-3">
                    <i class="fas fa-arrow-left"></i>
                </a>
                <div>
                    <h1 class="h3 mb-0">Modifier la Promotion</h1>
                    <p class="text-muted mb-0">{{ $promotion->name }}</p>
                </div>
            </div>

            <form action="{{ route('admin.promotions.update', $promotion) }}" method="POST">
                @csrf
                @method('PUT')
                
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">Informations générales</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="form-label">Nom de la promotion <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" value="{{ old('name', $promotion->name) }}" required>
                            @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Code promo</label>
                                <input type="text" name="code" class="form-control text-uppercase @error('code') is-invalid @enderror" value="{{ old('code', $promotion->code) }}">
                                @error('code')<div class="invalid-feedback">{{ $message }}</div>@enderror
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Type <span class="text-danger">*</span></label>
                                <select name="type" class="form-select @error('type') is-invalid @enderror" required>
                                    <option value="percentage" {{ old('type', $promotion->type) === 'percentage' ? 'selected' : '' }}>Pourcentage de réduction</option>
                                    <option value="fixed" {{ old('type', $promotion->type) === 'fixed' ? 'selected' : '' }}>Montant fixe</option>
                                    <option value="flash_sale" {{ old('type', $promotion->type) === 'flash_sale' ? 'selected' : '' }}>Vente flash</option>
                                    <option value="buy_x_get_y" {{ old('type', $promotion->type) === 'buy_x_get_y' ? 'selected' : '' }}>Achetez X, obtenez Y</option>
                                    <option value="free_shipping" {{ old('type', $promotion->type) === 'free_shipping' ? 'selected' : '' }}>Livraison gratuite</option>
                                </select>
                                @error('type')<div class="invalid-feedback">{{ $message }}</div>@enderror
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Valeur <span class="text-danger">*</span></label>
                                <input type="number" name="value" class="form-control @error('value') is-invalid @enderror" value="{{ old('value', $promotion->value) }}" min="0" step="0.01" required>
                                @error('value')<div class="invalid-feedback">{{ $message }}</div>@enderror
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Réduction maximum (FCFA)</label>
                                <input type="number" name="max_discount" class="form-control @error('max_discount') is-invalid @enderror" value="{{ old('max_discount', $promotion->max_discount) }}" min="0">
                                @error('max_discount')<div class="invalid-feedback">{{ $message }}</div>@enderror
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Description</label>
                            <textarea name="description" class="form-control @error('description') is-invalid @enderror" rows="3">{{ old('description', $promotion->description) }}</textarea>
                            @error('description')<div class="invalid-feedback">{{ $message }}</div>@enderror
                        </div>
                    </div>
                </div>

                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">Conditions d'utilisation</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Minimum d'achat (FCFA)</label>
                                <input type="number" name="min_purchase" class="form-control" value="{{ old('min_purchase', $promotion->min_purchase) }}" min="0">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Limite d'utilisations totale</label>
                                <input type="number" name="usage_limit" class="form-control" value="{{ old('usage_limit', $promotion->usage_limit) }}" min="1">
                                <small class="text-muted">Utilisé: {{ $promotion->usage_count }} fois</small>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Limite par utilisateur</label>
                                <input type="number" name="user_limit" class="form-control" value="{{ old('user_limit', $promotion->user_limit) }}" min="1">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Priorité</label>
                                <input type="number" name="priority" class="form-control" value="{{ old('priority', $promotion->priority) }}" min="0">
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Applicable à</label>
                            <select name="applicable_to" class="form-select">
                                <option value="all" {{ old('applicable_to', $promotion->applicable_to) === 'all' ? 'selected' : '' }}>Tous les produits</option>
                                <option value="products" {{ old('applicable_to', $promotion->applicable_to) === 'products' ? 'selected' : '' }}>Produits spécifiques</option>
                                <option value="categories" {{ old('applicable_to', $promotion->applicable_to) === 'categories' ? 'selected' : '' }}>Catégories spécifiques</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">Période de validité</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Date de début</label>
                                <input type="datetime-local" name="starts_at" class="form-control" value="{{ old('starts_at', $promotion->starts_at?->format('Y-m-d\TH:i')) }}">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label">Date de fin</label>
                                <input type="datetime-local" name="ends_at" class="form-control" value="{{ old('ends_at', $promotion->ends_at?->format('Y-m-d\TH:i')) }}">
                            </div>
                        </div>

                        <div class="form-check form-switch">
                            <input type="checkbox" class="form-check-input" name="is_active" id="is_active" value="1" {{ old('is_active', $promotion->is_active) ? 'checked' : '' }}>
                            <label class="form-check-label" for="is_active">Promotion active</label>
                        </div>
                    </div>
                </div>

                <div class="d-flex justify-content-end gap-2">
                    <a href="{{ route('admin.promotions.index') }}" class="btn btn-secondary">Annuler</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-1"></i> Enregistrer les modifications
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
