@extends('admin.layout')

@section('title', 'Paramètres Email')
@section('header', 'Paramètres Email')

@section('content')
<nav class="mb-6">
    <a href="{{ route('admin.dashboard') }}" class="text-slate-500 hover:text-slate-900 text-sm">← Tableau de bord</a>
</nav>

@if(session('success'))
    <div class="mb-6 p-4 bg-green-50 border border-green-200 text-green-800 rounded-xl">{{ session('success') }}</div>
@endif
@if(session('error'))
    <div class="mb-6 p-4 bg-red-50 border border-red-200 text-red-800 rounded-xl">{{ session('error') }}</div>
@endif

@php
    $mailOk = !empty($settings['mail_host'] ?? '');
@endphp

<div class="mb-8 p-4 rounded-2xl border {{ $mailOk ? 'bg-emerald-50 border-emerald-200' : 'bg-amber-50 border-amber-200' }}">
    <p class="font-medium {{ $mailOk ? 'text-emerald-800' : 'text-amber-800' }}">
        {{ $mailOk ? '✓ SMTP configuré – les emails seront envoyés.' : 'SMTP non configuré – les emails sont enregistrés mais non envoyés.' }}
    </p>
    <p class="text-sm {{ $mailOk ? 'text-emerald-600' : 'text-amber-600' }} mt-1">
        Utilisé pour : commandes (client + admin), messages de contact, réservations (client + admin), newsletters planifiées.
    </p>
</div>

<form method="POST" action="{{ route('admin.settings.email.store') }}" class="max-w-2xl space-y-8">
    @csrf

    <div class="bg-white rounded-2xl border border-slate-200 overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-100 bg-slate-50/50">
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 rounded-xl bg-white border border-slate-200 flex items-center justify-center text-slate-600">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"/></svg>
                </div>
                <div>
                    <h3 class="font-semibold text-slate-800">Serveur SMTP</h3>
                    <p class="text-sm text-slate-500">Configuration de l'envoi des emails</p>
                </div>
            </div>
        </div>
        <div class="p-6">
        <div class="grid md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Hôte *</label>
                <input type="text" name="mail_host" value="{{ old('mail_host', $settings['mail_host'] ?? '') }}" placeholder="smtp.example.com" 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">Port</label>
                <input type="number" name="mail_port" value="{{ old('mail_port', $settings['mail_port'] ?? '587') }}" placeholder="587" 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">Chiffrement</label>
                <p class="text-xs text-slate-500 mb-1">Pour Mailpit / MailHog en local : <strong>Aucun</strong>, utilisateur et mot de passe vides.</p>
                <select name="mail_encryption" 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">
                    <option value="tls" {{ old('mail_encryption', $settings['mail_encryption'] ?? 'tls') === 'tls' ? 'selected' : '' }}>TLS</option>
                    <option value="ssl" {{ old('mail_encryption', $settings['mail_encryption'] ?? '') === 'ssl' ? 'selected' : '' }}>SSL</option>
                    <option value="" {{ old('mail_encryption', $settings['mail_encryption'] ?? '') === '' ? 'selected' : '' }}>Aucun</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Utilisateur</label>
                <input type="text" name="mail_username" value="{{ old('mail_username', $settings['mail_username'] ?? '') }}" placeholder="user@example.com" 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 class="md:col-span-2">
                <label class="block text-sm font-medium text-slate-700 mb-1">Mot de passe</label>
                <input type="password" name="mail_password" value="" placeholder="{{ ($settings['mail_password'] ?? '') ? '•••••••• (laisser vide pour conserver)' : '••••••••' }}" 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" autocomplete="new-password">
            </div>
        </div>
        </div>
    </div>

    <div class="bg-white rounded-2xl border border-slate-200 overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-100 bg-slate-50/50">
            <h3 class="font-semibold text-slate-800">Expéditeur (From)</h3>
            <p class="text-sm text-slate-500">Adresse et nom affichés aux destinataires</p>
        </div>
        <div class="p-6">
        <div class="grid md:grid-cols-2 gap-6">
            <div>
                <label class="block text-sm font-medium text-slate-700 mb-1">Email expéditeur *</label>
                <input type="email" name="mail_from_address" value="{{ old('mail_from_address', $settings['mail_from_address'] ?? '') }}" placeholder="contact@monsite.fr" 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">Nom expéditeur</label>
                <input type="text" name="mail_from_name" value="{{ old('mail_from_name', $settings['mail_from_name'] ?? '') }}" placeholder="{{ $settings['site_name'] ?? 'Mon Site' }}" 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>

    <div class="bg-white rounded-2xl border border-slate-200 overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-100 bg-slate-50/50">
            <h3 class="font-semibold text-slate-800">Notifications admin</h3>
            <p class="text-sm text-slate-500">Adresses qui reçoivent les alertes (commandes, contact, réservations)</p>
        </div>
        <div class="p-6">
        <div class="space-y-4">
        <p class="text-sm text-slate-500">Séparez par des virgules pour plusieurs adresses. Si vide, les alertes (contact, commandes, réservations) sont envoyées à l’<strong>e-mail expéditeur</strong> ci-dessus.</p>
        <input type="text" name="mail_admin_email" value="{{ old('mail_admin_email', $settings['mail_admin_email'] ?? '') }}" placeholder="admin@monsite.fr" 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="flex flex-wrap 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">Enregistrer</button>
        <a href="{{ route('admin.dashboard') }}" class="px-6 py-2.5 border border-slate-200 rounded-xl font-medium hover:bg-slate-50 transition text-slate-700">Annuler</a>
    </div>
</form>

<div class="max-w-2xl mt-10 bg-white rounded-2xl border border-slate-200 overflow-hidden">
    <div class="px-6 py-4 border-b border-slate-100 bg-slate-50/50">
        <h3 class="font-semibold text-slate-800">Tester l’envoi</h3>
        <p class="text-sm text-slate-500 mt-1">Utilise la configuration SMTP <strong>déjà enregistrée</strong> (enregistrez le formulaire ci-dessus si vous venez de modifier les champs).</p>
    </div>
    <form method="POST" action="{{ route('admin.settings.email.test') }}" class="p-6 flex flex-col sm:flex-row sm:items-end gap-4">
        @csrf
        <div class="flex-1 min-w-0">
            <label for="test_email" class="block text-sm font-medium text-slate-700 mb-1">Envoyer un e-mail de test à</label>
            <input type="email" name="test_email" id="test_email" value="{{ old('test_email', $defaultTestEmail ?? '') }}" required placeholder="vous@exemple.fr" 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>
        <button type="submit" class="shrink-0 px-6 py-2.5 bg-slate-800 text-white rounded-xl font-medium hover:bg-slate-900 transition">
            Envoyer le test
        </button>
    </form>
</div>
@endsection
