@extends('layouts.app')
@section('title', 'Email Center')
@section('content')
    <x-page-head title="Email Center" subtitle="Compose messages, templates & delivery logs">
        <x-slot:actions>
            <a href="{{ route('system.settings.index', ['tab' => 'email']) }}" class="btn btn-outline">Email Settings</a>
            <a href="{{ route('communication.email-templates.index') }}" class="btn btn-outline">Templates</a>
            <a href="{{ route('communication.emails.compose') }}" class="btn btn-primary">Compose</a>
        </x-slot:actions>
    </x-page-head>

    @unless ($configReady)
        <div class="alert alert-error">Email sending is not configured or disabled. <a href="{{ route('system.settings.index', ['tab' => 'email']) }}">Configure email settings</a>.</div>
    @endunless

    <div class="stat-grid" style="grid-template-columns: repeat(4, 1fr);">
        <x-stat-card label="Sent" :value="$stats['sent']" color="green" />
        <x-stat-card label="Failed" :value="$stats['failed']" color="red" />
        <x-stat-card label="Queued" :value="$stats['queued']" color="amber" />
        <x-stat-card label="Drafts" :value="$stats['drafts']" color="violet" />
    </div>

    <x-panel title="Email Log">
        <form method="GET" class="toolbar">
            <input class="search" type="search" name="q" value="{{ request('q') }}" placeholder="Search recipient or subject">
            <select class="select" name="status" onchange="this.form.submit()">
                <option value="">All statuses</option>
                @foreach (['sent', 'failed', 'queued', 'draft', 'scheduled'] as $s)
                    <option value="{{ $s }}" @selected($status === $s)>{{ ucfirst($s) }}</option>
                @endforeach
            </select>
            <button type="submit" class="btn btn-outline btn-sm">Filter</button>
        </form>

        <div class="table-wrap">
            <table>
                <thead>
                    <tr><th>Date</th><th>To</th><th>Subject</th><th>Module</th><th>Status</th><th>By</th><th></th></tr>
                </thead>
                <tbody>
                    @forelse ($messages as $message)
                        <tr>
                            <td>{{ $message->created_at->format('d M Y H:i') }}</td>
                            <td>{{ $message->to }}</td>
                            <td>{{ str($message->subject)->limit(40) }}</td>
                            <td>{{ $message->related_module ?? '—' }}</td>
                            <td><x-badge :variant="$message->status->badgeVariant()">{{ $message->status->label() }}</x-badge></td>
                            <td>{{ $message->user?->name ?? 'System' }}</td>
                            <td>
                                <a href="{{ route('communication.emails.show', $message) }}" class="btn btn-ghost btn-sm">View</a>
                                @if ($message->status->value === 'failed')
                                    <form method="POST" action="{{ route('communication.emails.retry', $message) }}" style="display:inline;">@csrf<button class="btn btn-outline btn-sm">Retry</button></form>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7"><x-empty-state title="No emails yet" message="Sent and queued emails will appear here." /></td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div style="margin-top:16px;">{{ $messages->links() }}</div>
    </x-panel>
@endsection
