<?php $__env->startSection('title', 'Email Logs'); ?>
<?php $__env->startSection('page-title', 'Email Logs'); ?>
<?php $__env->startSection('page-subtitle', 'Track platform emails sent from Super Admin to tenants and other platform recipients'); ?>

<?php $__env->startSection('content'); ?>
<?php
    $statusClasses = [
        'sent' => 'text-bg-success',
        'failed' => 'text-bg-danger',
        'skipped' => 'text-bg-secondary',
    ];
    $sourceLabel = fn (?string $source) => $source ? str($source)->replace('_', ' ')->title() : 'All sources';
    $statCards = [
        ['Sent today', number_format($stats['today_sent'] ?? 0), 'Successful emails today', 'bi-send-check', 'green'],
        ['Failed today', number_format($stats['today_failed'] ?? 0), 'Needs attention today', 'bi-exclamation-triangle', 'rose'],
        ['All failures', number_format($stats['failed_total'] ?? 0), 'Total failed attempts', 'bi-bug', 'amber'],
        ['Total logs', number_format($stats['total'] ?? 0), 'All recorded email attempts', 'bi-envelope-paper', 'blue'],
    ];
?>

<style>
    .email-log-card{border:0;border-radius:18px;box-shadow:0 16px 45px rgba(15,23,42,.08)}
    .email-stat-card{border:0;border-radius:18px;color:#fff;overflow:hidden;min-height:126px}
    .email-stat-card.green{background:linear-gradient(135deg,#047857,#22c55e)}
    .email-stat-card.rose{background:linear-gradient(135deg,#be123c,#fb7185)}
    .email-stat-card.amber{background:linear-gradient(135deg,#b45309,#f59e0b)}
    .email-stat-card.blue{background:linear-gradient(135deg,#1d4ed8,#38bdf8)}
    .email-stat-card .card-body{display:flex;justify-content:space-between;gap:1rem}
    .email-stat-card i{font-size:2rem;opacity:.85}
    .email-log-subject{max-width:320px}
    .email-log-error{max-width:260px}
    .email-filter-grid{display:grid;grid-template-columns:minmax(220px,1.5fr) repeat(5,minmax(150px,1fr)) auto;gap:.75rem;align-items:end}
    @media (max-width: 1199.98px){.email-filter-grid{grid-template-columns:repeat(2,minmax(0,1fr))}}
    @media (max-width: 575.98px){.email-filter-grid{grid-template-columns:1fr}}
</style>

<div class="row g-3 mb-4">
    <?php $__currentLoopData = $statCards; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label, $value, $hint, $icon, $tone]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div class="col-md-6 col-xl-3">
            <div class="card email-stat-card <?php echo e($tone); ?>">
                <div class="card-body">
                    <div>
                        <div class="small text-white-50"><?php echo e($label); ?></div>
                        <div class="display-6 fw-bold mb-1"><?php echo e($value); ?></div>
                        <div class="small"><?php echo e($hint); ?></div>
                    </div>
                    <i class="bi <?php echo e($icon); ?>"></i>
                </div>
            </div>
        </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
</div>

<div class="card email-log-card mb-4">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-start gap-3 mb-3">
            <div>
                <h2 class="h5 mb-1">Filter Email Activity</h2>
                <div class="text-muted small">Search by recipient, tenant, subject, message, or SMTP error.</div>
            </div>
            <a href="<?php echo e(route('super.email-logs.index')); ?>" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-counterclockwise me-1"></i>Reset</a>
        </div>
        <form method="GET" class="email-filter-grid">
            <div>
                <label class="form-label">Search</label>
                <input name="q" value="<?php echo e($filters['q'] ?? ''); ?>" class="form-control" placeholder="Tenant, email, subject, error...">
            </div>
            <div>
                <label class="form-label">Status</label>
                <select name="status" class="form-select">
                    <option value="">All statuses</option>
                    <?php $__currentLoopData = ['sent' => 'Sent', 'failed' => 'Failed', 'skipped' => 'Skipped']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($value); ?>" <?php if(($filters['status'] ?? '') === $value): echo 'selected'; endif; ?>><?php echo e($label); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div>
                <label class="form-label">Source</label>
                <select name="source" class="form-select">
                    <option value="">All sources</option>
                    <?php $__currentLoopData = $sources; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $source): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($source); ?>" <?php if(($filters['source'] ?? '') === $source): echo 'selected'; endif; ?>><?php echo e($sourceLabel($source)); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div>
                <label class="form-label">Tenant</label>
                <select name="tenant_id" class="form-select">
                    <option value="">All tenants</option>
                    <?php $__currentLoopData = $tenants; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $tenant): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($tenant->id); ?>" <?php if((string) ($filters['tenant_id'] ?? '') === (string) $tenant->id): echo 'selected'; endif; ?>><?php echo e($tenant->name); ?></option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div>
                <label class="form-label">From</label>
                <input name="from" type="date" value="<?php echo e($filters['from'] ?? ''); ?>" class="form-control">
            </div>
            <div>
                <label class="form-label">To</label>
                <input name="to" type="date" value="<?php echo e($filters['to'] ?? ''); ?>" class="form-control">
            </div>
            <div>
                <button class="btn btn-primary w-100"><i class="bi bi-search me-1"></i>Filter</button>
            </div>
        </form>
    </div>
</div>

<div class="card email-log-card">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-center gap-3 mb-3">
            <div>
                <h2 class="h5 mb-1">Delivery Timeline</h2>
                <div class="text-muted small">Showing platform email attempts sent from Super Admin workflows.</div>
            </div>
            <span class="badge text-bg-light border"><?php echo e(number_format($logs->total())); ?> records</span>
        </div>

        <div class="table-responsive">
            <table class="table align-middle">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Recipient</th>
                        <th>Tenant</th>
                        <th>Source</th>
                        <th>Subject</th>
                        <th>Status</th>
                        <th>Error</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $logs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $log): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td>
                                <div class="fw-semibold"><?php echo e(app_datetime($log->created_at)); ?></div>
                                <div class="small text-muted"><?php echo e($log->mailer ?: '-'); ?> <?php echo e($log->smtp_host ? '· '.$log->smtp_host : ''); ?></div>
                            </td>
                            <td>
                                <div class="fw-semibold"><?php echo e($log->recipient_name ?: '-'); ?></div>
                                <div class="small text-muted"><?php echo e($log->recipient_email ?: '-'); ?></div>
                            </td>
                            <td><?php echo e($log->tenant?->name ?? '-'); ?></td>
                            <td><span class="badge text-bg-light border"><?php echo e($log->sourceLabel()); ?></span></td>
                            <td>
                                <div class="fw-semibold email-log-subject text-truncate"><?php echo e($log->subject); ?></div>
                                <div class="small text-muted email-log-subject text-truncate"><?php echo e(str($log->body)->limit(90)); ?></div>
                            </td>
                            <td><span class="badge <?php echo e($statusClasses[$log->status] ?? 'text-bg-secondary'); ?>"><?php echo e($log->statusLabel()); ?></span></td>
                            <td>
                                <div class="small text-danger email-log-error text-truncate"><?php echo e($log->error_message ?: '-'); ?></div>
                            </td>
                            <td class="text-end">
                                <button class="btn btn-sm btn-outline-primary" type="button" data-bs-toggle="modal" data-bs-target="#emailLog<?php echo e($log->id); ?>">
                                    View
                                </button>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="8" class="text-center text-muted py-5">
                                <i class="bi bi-envelope-open d-block display-6 mb-2"></i>
                                No email delivery logs match this filter.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <div class="mt-3"><?php echo e($logs->links()); ?></div>
    </div>
</div>

<?php $__currentLoopData = $logs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $log): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <div class="modal fade" id="emailLog<?php echo e($log->id); ?>" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <div>
                        <h5 class="modal-title"><?php echo e($log->subject); ?></h5>
                        <div class="small text-muted"><?php echo e(app_datetime($log->created_at)); ?> · <?php echo e($log->sourceLabel()); ?></div>
                    </div>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="row g-3 mb-3">
                        <div class="col-md-6"><div class="border rounded p-3 h-100"><div class="small text-muted">Recipient</div><strong><?php echo e($log->recipient_name ?: '-'); ?></strong><div><?php echo e($log->recipient_email ?: '-'); ?></div></div></div>
                        <div class="col-md-3"><div class="border rounded p-3 h-100"><div class="small text-muted">Status</div><span class="badge <?php echo e($statusClasses[$log->status] ?? 'text-bg-secondary'); ?>"><?php echo e($log->statusLabel()); ?></span></div></div>
                        <div class="col-md-3"><div class="border rounded p-3 h-100"><div class="small text-muted">Tenant</div><strong><?php echo e($log->tenant?->name ?? '-'); ?></strong></div></div>
                    </div>
                    <div class="mb-3">
                        <div class="small text-muted mb-1">Message</div>
                        <pre class="bg-light border rounded p-3 mb-0" style="white-space:pre-wrap;font-family:inherit"><?php echo e($log->body ?: '-'); ?></pre>
                    </div>
                    <?php if($log->error_message): ?>
                        <div class="alert alert-danger mb-0">
                            <div class="fw-semibold mb-1">Delivery error</div>
                            <div><?php echo e($log->error_message); ?></div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\Users\Polite hp\Documents\Codex\2026-05-08\build-a-complete-production-ready-saas\microfinance-saas\resources\views\super\email-logs\index.blade.php ENDPATH**/ ?>