@extends('layouts.admin')

@section('title', 'Email Logs')

@section('admin-content')
<style>
    /* Specific Email Logs Styles */
    .logs-page .detail-truncate { max-width: 300px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
</style>

<div class="logs-page">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="fw-bold mb-1">Email Logs</h4>
            <p class="text-muted mb-0 small">History of outgoing system emails</p>
        </div>
    </div>

    <!-- Stats -->
    <div class="row g-4 mb-4">
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon primary">
                    <i class="bi bi-envelope-paper"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $logs->total() }}</div>
                    <div class="stat-label">Total Sent Emails</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon success">
                    <i class="bi bi-check-all"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $logs->where('status', 'sent')->count() }}</div>
                    <div class="stat-label">Successfully Delivered</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="stat-card d-flex align-items-center gap-3">
                <div class="stat-icon danger">
                    <i class="bi bi-x-circle"></i>
                </div>
                <div>
                    <div class="stat-value">{{ $logs->where('status', 'failed')->count() }}</div>
                    <div class="stat-label">Failed Delivery</div>
                </div>
            </div>
        </div>
    </div>

    <!-- Logs Table -->
    <div class="data-table">
        <div class="table-responsive">
            <table class="table mb-0">
                <thead>
                    <tr>
                        <th class="ps-4">Recipient</th>
                        <th>Subject</th>
                        <th class="text-center">Delivery Status</th>
                        <th>Sent Timestamp</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($logs as $log)
                        <tr>
                            <td class="ps-4">
                                <div class="d-flex align-items-center">
                                    <div class="rounded-circle bg-light d-flex align-items-center justify-content-center me-3" style="width: 38px; height: 38px;">
                                        <i class="bi bi-person text-muted"></i>
                                    </div>
                                    <div>
                                        <div class="fw-bold text-dark">{{ $log->recipient }}</div>
                                    </div>
                                </div>
                            </td>
                            <td>
                                <div class="detail-truncate text-muted d-inline-block" title="{{ $log->subject }}">
                                    {{ $log->subject }}
                                </div>
                            </td>
                            <td class="text-center">
                                @if($log->status === 'sent')
                                    <span class="badge bg-success-subtle text-success px-3 py-2 rounded-pill fw-bold">
                                        <i class="bi bi-check-circle-fill me-1"></i> Sent
                                    </span>
                                @else
                                    <span class="badge bg-danger-subtle text-danger px-3 py-2 rounded-pill fw-bold" 
                                          title="{{ $log->error_message }}" data-bs-toggle="tooltip">
                                        <i class="bi bi-exclamation-circle-fill me-1"></i> Failed
                                    </span>
                                @endif
                            </td>
                            <td>
                                <div class="text-dark small fw-medium">{{ $log->sent_at->format('M d, Y') }}</div>
                                <small class="text-muted">{{ $log->sent_at->format('h:i:s A') }}</small>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="4" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-envelope-open fs-1 text-muted opacity-50 d-block mb-3"></i>
                                    <h5 class="fw-bold text-muted">No Email Logs Found</h5>
                                    <p class="text-muted small mb-0">System has not recorded any outgoing emails yet.</p>
                                </div>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        @if($logs->hasPages())
            <div class="px-3 border-top">
                {{ $logs->links() }}
            </div>
        @endif
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.map(function (el) {
            return new bootstrap.Tooltip(el);
        });
    });
</script>
@endsection
