@extends('layout')

@section('title', __('store.email_history_title'))

@push('styles')
    @vite('resources/css/store/tables.css')
@endpush

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-md-12">
            <x-clientarea.page-header
                icon="fas fa-envelope-open-text"
                :title="__('store.email_history_title')"
                :subtitle="$emails->total() . ' — ' . __('store.email_history_description')"
            />

            
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body">
                    <form method="GET" action="{{ route('store.auth.emails') }}">
                        <div class="row g-3 align-items-end">
                            <div class="col-md-4">
                                <label class="form-label small">{{ __('store.email_history_search') }}</label>
                                <input type="text" name="search" class="form-control" placeholder="{{ __('store.email_history_search_placeholder') }}" value="{{ request('search') }}">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label small">{{ __('store.email_history_date_from') }}</label>
                                <input type="date" name="from" class="form-control" value="{{ request('from') }}">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label small">{{ __('store.email_history_date_to') }}</label>
                                <input type="date" name="to" class="form-control" value="{{ request('to') }}">
                            </div>
                            <div class="col-md-2">
                                <button type="submit" class="btn btn-primary w-100">
                                    <i class="fas fa-search me-1"></i>{{ __('store.email_history_filter') }}
                                </button>
                            </div>
                        </div>
                        @if(request('search') || request('from') || request('to'))
                            <div class="mt-2">
                                <a href="{{ route('store.auth.emails') }}" class="btn btn-sm btn-outline-secondary">
                                    <i class="fas fa-times me-1"></i>{{ __('store.email_history_clear_filters') }}
                                </a>
                            </div>
                        @endif
                    </form>
                </div>
            </div>

            @if($emails->count() > 0)
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>{{ __('store.email_history_subject') }}</th>
                                    <th class="d-none d-md-table-cell">{{ __('store.email_history_date') }}</th>
                                    <th>{{ __('store.email_history_status') }}</th>
                                    <th class="text-end">{{ __('store.common_actions') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($emails as $email)
                                    <tr>
                                        <td>
                                            <div>
                                                <strong>{{ $email->subject }}</strong>
                                                @if($email->attachments ?? false)
                                                    <i class="fas fa-paperclip text-muted ms-1" title="{{ __('store.email_history_has_attachments') }}"></i>
                                                @endif
                                                <br><small class="text-muted">{{ __('store.email_history_to') }}: {{ $email->to_address }}</small>
                                            </div>
                                        </td>
                                        <td class="d-none d-md-table-cell">
                                            <span>{{ $email->created_at->format($dateFormat) }}</span>
                                            <br><small class="text-muted">{{ $email->created_at->format('H:i') }}</small>
                                        </td>
                                        <td>
                                            <span class="badge bg-success">
                                                {{ __('store.email_history_status_sent') }}
                                            </span>
                                        </td>
                                        <td class="text-end">
                                            <a href="{{ route('store.auth.emails.detail', $email->id) }}" class="btn btn-sm btn-outline-primary">
                                                <i class="fas fa-eye me-1"></i>{{ __('store.email_history_view') }}
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="d-flex justify-content-center mt-4">
                    {{ $emails->links() }}
                </div>
            @else
                <div class="card border-0 shadow-sm">
                    <div class="card-body text-center py-5">
                        <i class="fas fa-envelope fa-3x text-muted mb-3"></i>
                        <h5>{{ __('store.email_history_no_emails') }}</h5>
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
