@extends('layouts.admin')

@section('title', 'Reminder logs')

@section('page-title', 'Reminder logs')

@section('breadcrumbs')
    <a href="{{ route('admin.reports.transactions.sales') }}" class="hover:text-primary">Reports</a>
    <span class="material-symbols-outlined text-xs">chevron_right</span>
    <span class="text-primary font-medium">Reminder logs</span>
@endsection

@section('content')
<div class="max-w-7xl mx-auto space-y-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
        <form method="GET" action="{{ route('admin.reports.reminder-logs.index') }}" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-6 gap-3 items-end">
            <div>
                <label for="status" class="block text-xs font-medium text-gray-500 mb-1">Status</label>
                <select name="status" id="status" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                    <option value="">Any</option>
                    <option value="pending" @selected(request('status') === 'pending')>Pending</option>
                    <option value="sent" @selected(request('status') === 'sent')>Sent</option>
                    <option value="failed" @selected(request('status') === 'failed')>Failed</option>
                </select>
            </div>
            <div class="lg:col-span-2">
                <label for="reminder_type" class="block text-xs font-medium text-gray-500 mb-1">Type</label>
                <select name="reminder_type" id="reminder_type" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
                    <option value="">Any</option>
                    @foreach($reminderTypes as $t)
                        <option value="{{ $t->value }}" @selected(request('reminder_type') === $t->value)>{{ str_replace('_', ' ', $t->value) }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label for="customer_id" class="block text-xs font-medium text-gray-500 mb-1">Customer</label>
                <select name="customer_id" id="customer_id" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm max-h-10">
                    <option value="">Any</option>
                    @foreach($customers as $c)
                        <option value="{{ $c->id }}" @selected((string) request('customer_id') === (string) $c->id)>{{ $c->name }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label for="date_from" class="block text-xs font-medium text-gray-500 mb-1">From</label>
                <input type="date" name="date_from" id="date_from" value="{{ request('date_from') }}" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
            </div>
            <div>
                <label for="date_to" class="block text-xs font-medium text-gray-500 mb-1">To</label>
                <input type="date" name="date_to" id="date_to" value="{{ request('date_to') }}" class="w-full rounded-lg border border-gray-300 px-3 py-2 text-sm">
            </div>
            <div class="flex gap-2 md:col-span-2 lg:col-span-6">
                <button type="submit" class="px-4 py-2 rounded-lg bg-primary text-white text-sm font-semibold">Filter</button>
                <a href="{{ route('admin.reports.reminder-logs.index') }}" class="px-4 py-2 rounded-lg border border-gray-300 text-sm font-medium text-gray-700 hover:bg-gray-50">Reset</a>
            </div>
        </form>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-sm">
                <thead class="bg-gray-50 border-b border-gray-200 text-xs uppercase text-gray-500">
                    <tr>
                        <th class="px-4 py-3 font-semibold">Recipient</th>
                        <th class="px-4 py-3 font-semibold">Type</th>
                        <th class="px-4 py-3 font-semibold">Related</th>
                        <th class="px-4 py-3 font-semibold">Status</th>
                        <th class="px-4 py-3 font-semibold">Sent</th>
                        <th class="px-4 py-3 font-semibold">Error</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($logs as $log)
                        <tr class="hover:bg-gray-50/80 align-top">
                            <td class="px-4 py-3">
                                <div class="font-medium text-gray-900 break-all">{{ $log->recipient_email }}</div>
                                <div class="text-xs text-gray-500 line-clamp-2 mt-0.5">{{ Str::limit($log->subject, 80) }}</div>
                            </td>
                            <td class="px-4 py-3 text-gray-700 whitespace-nowrap">{{ str_replace('_', ' ', $log->reminder_type) }}</td>
                            <td class="px-4 py-3 text-xs text-gray-600 space-y-1">
                                @if($log->invoice_id && $log->invoice)
                                    <div><a href="{{ route('admin.invoices.show', $log->invoice) }}" class="text-primary font-medium hover:underline">Invoice {{ $log->invoice->invoice_number }}</a></div>
                                @endif
                                @if($log->quotation_id && $log->quotation)
                                    <div><a href="{{ route('admin.quotations.show', $log->quotation) }}" class="text-primary font-medium hover:underline">QT {{ $log->quotation->quotation_number }}</a></div>
                                @endif
                                @if($log->customer_id && $log->customer)
                                    <div>{{ $log->customer->name }}</div>
                                @endif
                                @if(!$log->invoice_id && !$log->quotation_id && !$log->customer_id)
                                    <span class="text-gray-400">—</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 whitespace-nowrap">
                                @if($log->status === 'sent')
                                    <span class="inline-flex px-2 py-0.5 rounded text-xs font-medium bg-emerald-100 text-emerald-800">sent</span>
                                @elseif($log->status === 'failed')
                                    <span class="inline-flex px-2 py-0.5 rounded text-xs font-medium bg-rose-100 text-rose-800">failed</span>
                                @else
                                    <span class="inline-flex px-2 py-0.5 rounded text-xs font-medium bg-slate-100 text-slate-700">{{ $log->status }}</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-gray-600 whitespace-nowrap text-xs">{{ $log->sent_at?->format('M j, Y g:i A') ?? '—' }}</td>
                            <td class="px-4 py-3 text-xs text-rose-700 max-w-xs break-words">{{ $log->error_message ? Str::limit($log->error_message, 120) : '—' }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="px-4 py-12 text-center text-gray-500">No log entries match your filters.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($logs->hasPages())
            <div class="px-4 py-3 border-t border-gray-100">{{ $logs->withQueryString()->links() }}</div>
        @endif
    </div>
</div>
@endsection
