@extends('layouts.admin')

@section('title', 'Modifier Utilisateur')
@section('page-title', 'Modifier l\'Utilisateur')

@section('content')
<div class="card">
    <div class="card-header bg-white">
        <h5 class="mb-0">Modifier: {{ $user->name }}</h5>
    </div>
    <div class="card-body">
        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show">
                <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">
                <i class="bi bi-exclamation-triangle me-2"></i> {{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        @if($errors->any())
            <div class="alert alert-danger">
                <ul class="mb-0">
                    @foreach($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
        @endif

        <form action="{{ route('admin.users.update', $user) }}" method="POST">
            @csrf
            @method('PUT')
            
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Nom Complet *</label>
                    <input type="text" name="name" class="form-control @error('name') is-invalid @enderror" 
                           value="{{ old('name', $user->name) }}" required>
                    @error('name')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Email *</label>
                    <input type="email" name="email" class="form-control @error('email') is-invalid @enderror" 
                           value="{{ old('email', $user->email) }}" required>
                    @error('email')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Téléphone *</label>
                    <input type="text" name="phone" class="form-control @error('phone') is-invalid @enderror" 
                           value="{{ old('phone', $user->phone) }}" required>
                    @error('phone')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Nouveau Mot de Passe (optionnel)</label>
                    <input type="password" name="password" class="form-control @error('password') is-invalid @enderror" 
                           placeholder="Laisser vide pour ne pas changer">
                    @error('password')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                    <small class="text-muted">Minimum 6 caractères</small>
                </div>
            </div>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Rôle *</label>
                    <select name="role" class="form-select @error('role') is-invalid @enderror" required>
                        <optgroup label="Utilisateurs">
                            <option value="user" {{ old('role', $user->role) == 'user' ? 'selected' : '' }}>Utilisateur</option>
                            <option value="member" {{ old('role', $user->role) == 'member' ? 'selected' : '' }}>Membre MLM</option>
                            <option value="affiliate" {{ old('role', $user->role) == 'affiliate' ? 'selected' : '' }}>Affilié</option>
                        </optgroup>
                        <optgroup label="Commerce">
                            <option value="seller" {{ old('role', $user->role) == 'seller' ? 'selected' : '' }}>Vendeur</option>
                        </optgroup>
                        <optgroup label="Back-Office">
                            <option value="comptabilite" {{ old('role', $user->role) == 'comptabilite' ? 'selected' : '' }}>Comptabilité</option>
                            <option value="accountant" {{ old('role', $user->role) == 'accountant' ? 'selected' : '' }}>Comptable</option>
                            <option value="marketing" {{ old('role', $user->role) == 'marketing' ? 'selected' : '' }}>Marketing</option>
                            <option value="community_manager" {{ old('role', $user->role) == 'community_manager' ? 'selected' : '' }}>Community Manager</option>
                            <option value="verifier" {{ old('role', $user->role) == 'verifier' ? 'selected' : '' }}>Vérificateur</option>
                        </optgroup>
                        <optgroup label="Administration">
                            <option value="sub_admin" {{ old('role', $user->role) == 'sub_admin' ? 'selected' : '' }}>Sous-Administrateur</option>
                            <option value="admin" {{ old('role', $user->role) == 'admin' ? 'selected' : '' }}>Administrateur</option>
                        </optgroup>
                    </select>
                    @error('role')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-md-6 mb-3">
                    <label class="form-label">Code Membre</label>
                    <input type="text" class="form-control" value="{{ $user->member_code }}" readonly>
                    <small class="text-muted">Code non modifiable</small>
                </div>
            </div>

            <!-- Note : Le pourcentage de réduction membre est géré globalement via Settings -->
            @if($user->role === 'member')
            <div class="alert alert-info">
                <i class="bi bi-info-circle me-2"></i>
                <strong>Réduction Membre:</strong> 
                {{ floatval(\App\Models\Setting::get('member_discount_percentage', 5)) }}% 
                (configurable dans <a href="{{ route('admin.settings.index') }}">Paramètres Financiers MLM</a>)
            </div>
            @endif

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

            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-check-circle me-1"></i> Enregistrer les Modifications
                </button>
                <a href="{{ route('admin.users.index') }}" class="btn btn-secondary">
                    <i class="bi bi-x-circle me-1"></i> Annuler
                </a>
            </div>
        </form>
    </div>
</div>

<!-- Le JavaScript pour gérer dynamiquement discount_percentage a été supprimé -->
<!-- car ce champ est maintenant géré globalement via Settings -->
@endsection