@extends('layouts.app')

@section('title', 'Campaign Emails')

@section('content')
<div class="page-header">
    <div class="page-header-left d-flex align-items-center">
        <div class="page-header-title"><h5 class="m-b-10">Campaign Emails</h5></div>
        <ul class="breadcrumb"><li class="breadcrumb-item"><a href="{{ route('campaigns') }}">Campaigns</a></li><li class="breadcrumb-item">Email History</li></ul>
    </div>
    <div class="page-header-right ms-auto">
        <a href="{{ route('schedule-activity-create', ['type' => 9]) }}" class="btn btn-primary"><i class="feather-plus me-2"></i>Compose Email</a>
    </div>
</div>

<div class="main-content">
    <div class="card stretch stretch-full">
        <div class="card-header"><h5 class="card-title">Email History</h5><span class="badge bg-soft-primary text-primary">{{ $emails->total() }} records</span></div>
        <div class="card-body p-0">
            <x-server-list-toolbar form-id="campaignEmailFilters" apply-label="Apply Filters" reset-label="Reset Filters"
                actions-class="w-100 justify-content-center" placeholder="Search recipient, sender, or subject" :filters="[
                    'email_status'=>['label'=>'Delivery status','options'=>['draft'=>'Draft','scheduled'=>'Scheduled','sent'=>'Sent','opened'=>'Opened','replied'=>'Replied','failed'=>'Failed']],
                    'campaign_id'=>['label'=>'Campaign','options'=>$campaigns->pluck('campaign_name','id')->all()],
                    'date_from'=>['label'=>'Created from','type'=>'date'], 'date_to'=>['label'=>'Created to','type'=>'date'],
                ]" />
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0" id="campaignEmailList">
                    <thead><tr>
                        <th>Recipient</th><th>Subject</th><th>Campaign</th><th>Status</th>
                        <th>Scheduled</th><th>Sent / Updated</th><th>Owner</th><th class="text-end">Actions</th>
                    </tr></thead>
                    <tbody>
                    @forelse($emails as $email)
                        <tr>
                            <td><div class="fw-semibold">{{ $email->email_to ?: collect($email->to_recipients)->first() ?: '—' }}</div><small class="text-muted">From: {{ $email->from_email ?: 'Company email' }}</small></td>
                            <td><div class="fw-semibold">{{ $email->subject ?: $email->title ?: 'Untitled email' }}</div><small class="text-muted text-truncate d-block" style="max-width:280px">{{ strip_tags($email->email_body ?: $email->description ?: '') }}</small></td>
                            <td>{{ $campaignNames[$email->related_campaign_id] ?? 'General' }}</td>
                            <td><span class="badge bg-soft-{{ match($email->email_status){'sent'=>'primary','opened'=>'info','replied'=>'success','failed'=>'danger','scheduled'=>'warning',default=>'secondary'} }} text-{{ match($email->email_status){'sent'=>'primary','opened'=>'info','replied'=>'success','failed'=>'danger','scheduled'=>'warning',default=>'secondary'} }}">{{ ucfirst($email->email_status ?: 'draft') }}</span></td>
                            <td>{{ optional($email->scheduled_at)->format('d M Y, h:i A') ?: '—' }}</td>
                            <td>{{ optional($email->sent_at ?: $email->updated_at)->format('d M Y, h:i A') }}</td>
                            <td>{{ $email->owner?->name ?: 'System' }}</td>
                            <td class="text-end"><div class="d-inline-flex gap-2">
                                <a href="{{ route('activities.edit', ['id'=>$email->id]) }}" class="avatar-text avatar-md bg-soft-primary text-primary" title="View Email" aria-label="View Email"><i class="feather-eye"></i></a>
                                @can('update_activities')<a href="{{ route('activities.edit', ['id'=>$email->id]) }}" class="avatar-text avatar-md bg-soft-warning text-warning" title="Edit Email" aria-label="Edit Email"><i class="feather-edit-3"></i></a>@endcan
                                @can('delete_activities')<form method="POST" action="{{ route('activities.destroy', $email->id) }}" onsubmit="return confirm('Delete this email record?')">@csrf @method('DELETE')<button class="avatar-text avatar-md border-0 bg-soft-danger text-danger" title="Delete Email" aria-label="Delete Email"><i class="feather-trash-2"></i></button></form>@endcan
                            </div></td>
                        </tr>
                    @empty
                        <tr><td colspan="8" class="text-center text-muted py-5">No email records match the selected filters.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        <div class="card-footer d-flex flex-wrap align-items-center justify-content-between gap-2">
            <small class="text-muted">Showing {{ $emails->firstItem() ?? 0 }}–{{ $emails->lastItem() ?? 0 }} of {{ $emails->total() }} emails</small>
            {{ $emails->links() }}
        </div>
    </div>
</div>
@endsection
