<?php require APPROOT . '/app/Views/layouts/header.php'; ?>

<div class="max-w-7xl mx-auto mb-10 space-y-8">
    
    <div class="flex flex-col md:flex-row md:items-center justify-between pb-4">
        <div class="mb-4 md:mb-0">
            <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white mb-1"><?php echo $data['title']; ?></h1>
            <p class="text-sm text-gray-600 dark:text-gray-400"><?php echo $data['description']; ?></p>
        </div>
        <a href="<?php echo URLROOT; ?>/admin/cron" class="w-full md:w-auto bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700/50 hover:bg-gray-50 dark:hover:bg-gray-700/80 text-gray-800 dark:text-gray-200 font-bold py-2.5 px-6 rounded-lg text-sm transition-colors flex justify-center items-center shadow-sm">
            <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
            Back to Cron Jobs
        </a>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
        <div class="p-6 bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 flex items-center justify-between overflow-hidden">
            <div>
                <h3 class="text-xs font-bold text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-1">Pending Dispatch</h3>
                <p class="text-3xl font-extrabold text-gray-900 dark:text-white"><?php echo $data['stats']['pending']; ?></p>
            </div>
            <div class="w-12 h-12 rounded-xl bg-yellow-50 dark:bg-yellow-900/30 text-yellow-600 dark:text-yellow-400 flex items-center justify-center shadow-inner">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
            </div>
        </div>
        
        <div class="p-6 bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 flex items-center justify-between overflow-hidden">
            <div>
                <h3 class="text-xs font-bold text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-1">Sent Successfully</h3>
                <p class="text-3xl font-extrabold text-green-600 dark:text-green-500"><?php echo $data['stats']['sent']; ?></p>
            </div>
            <div class="w-12 h-12 rounded-xl bg-green-50 dark:bg-green-900/30 text-green-600 dark:text-green-400 flex items-center justify-center shadow-inner">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>
            </div>
        </div>
        
        <div class="p-6 bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 flex items-center justify-between overflow-hidden">
            <div>
                <h3 class="text-xs font-bold text-gray-500 dark:text-gray-400 uppercase tracking-wider mb-1">Failed to Send</h3>
                <p class="text-3xl font-extrabold text-red-600 dark:text-red-500"><?php echo $data['stats']['failed']; ?></p>
            </div>
            <div class="w-12 h-12 rounded-xl bg-red-50 dark:bg-red-900/30 text-red-600 dark:text-red-400 flex items-center justify-center shadow-inner">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path></svg>
            </div>
        </div>
    </div>

    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden flex flex-col">
        <div class="p-5 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex justify-between items-center">
            <h2 class="text-sm font-bold text-gray-900 dark:text-white flex items-center">
                <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path></svg>
                Recent Queue Activity
            </h2>
        </div>
        
        <div class="w-full text-gray-900 dark:text-gray-100 p-5">
            <table id="queueTable" class="w-full text-left whitespace-nowrap">
                <thead class="border-b border-gray-200 dark:border-gray-700/50">
                    <tr>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Created</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Recipient</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Subject</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Status</th>
                        <th class="px-6 py-4 text-right text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($data['recent_emails'])): ?>
                        <?php foreach($data['recent_emails'] as $email): ?>
                        <tr class="border-b border-gray-100 dark:border-gray-700/50 last:border-0 hover:bg-gray-50 dark:hover:bg-gray-700/25 transition-colors">
                            <td class="px-6 py-4 whitespace-nowrap text-xs text-gray-500 dark:text-gray-400 font-mono" data-order="<?php echo strtotime($email->created_at); ?>">
                                <?php echo $email->created_at; ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900 dark:text-white"><?php echo htmlspecialchars($email->recipient_email); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600 dark:text-gray-400"><?php echo htmlspecialchars($email->subject); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <?php if($email->status === 'sent'): ?>
                                    <span class="px-2 py-0.5 inline-flex text-[10px] font-bold uppercase tracking-wider rounded border bg-green-50 text-green-700 border-green-200 dark:bg-green-900/10 dark:text-green-400 dark:border-green-800/30">Sent</span>
                                <?php elseif($email->status === 'failed'): ?>
                                    <span class="px-2 py-0.5 inline-flex text-[10px] font-bold uppercase tracking-wider rounded border bg-red-50 text-red-700 border-red-200 dark:bg-red-900/10 dark:text-red-400 dark:border-red-800/30 cursor-help" title="<?php echo htmlspecialchars($email->error_message); ?>">Failed</span>
                                <?php else: ?>
                                    <span class="px-2 py-0.5 inline-flex text-[10px] font-bold uppercase tracking-wider rounded border bg-yellow-50 text-yellow-700 border-yellow-200 dark:bg-yellow-900/10 dark:text-yellow-400 dark:border-yellow-800/30">Pending</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium flex justify-end items-center space-x-2">
                                <button type="button" onclick="previewEmail(<?php echo $email->id; ?>, '<?php echo htmlspecialchars(addslashes($email->subject)); ?>')" class="p-1.5 bg-blue-50 dark:bg-blue-900/10 hover:bg-blue-100 dark:hover:bg-blue-900/30 text-primary dark:text-blue-400 border border-blue-200 dark:border-blue-800/30 rounded-md transition-colors shadow-sm" title="Preview Email">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
                                </button>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>

<!-- Email Preview Modal -->
<div id="email-preview-modal" class="fixed inset-0 z-[60] hidden items-center justify-center bg-black bg-opacity-60 backdrop-blur-sm transition-opacity p-4">
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-2xl border border-gray-200 dark:border-gray-700 max-w-4xl w-full h-[80vh] flex flex-col overflow-hidden transform transition-all">
        <div class="p-4 border-b border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900 flex justify-between items-center">
            <h3 id="preview-subject" class="text-lg font-bold text-gray-900 dark:text-white truncate pr-4">Email Preview</h3>
            <button type="button" onclick="closePreviewModal()" class="text-gray-500 hover:text-red-500 focus:outline-none transition-colors">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
            </button>
        </div>
        <!-- Iframe to sandbox the email HTML formatting away from Tailwind -->
        <div class="flex-grow bg-gray-100 dark:bg-gray-900 p-4">
            <iframe id="preview-iframe" class="w-full h-full bg-white rounded-lg shadow-sm border border-gray-200" src=""></iframe>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#queueTable').DataTable({
            responsive: true,
            order: [[ 0, "desc" ]],
            pageLength: 25,
            language: {
                search: "",
                searchPlaceholder: "Search queue...",
                lengthMenu: "Show _MENU_ entries",
                emptyTable: `<div class="flex flex-col items-center justify-center py-10">
                                <div class="h-16 w-16 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center mb-4">
                                    <svg class="w-8 h-8 text-gray-400 dark:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path></svg>
                                </div>
                                <h3 class="text-lg font-bold text-gray-900 dark:text-white">Queue Empty</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400 mt-1">There are no pending or recent emails in the queue.</p>
                            </div>`
            },
            columnDefs: [
                { orderable: false, targets: 4 } // Disable sorting on action column
            ]
        });
    });

    function previewEmail(id, subject) {
        document.getElementById('preview-subject').innerText = 'Subject: ' + subject;
        document.getElementById('preview-iframe').src = '<?php echo URLROOT; ?>/admin/previewEmail/' + id;
        
        const modal = document.getElementById('email-preview-modal');
        modal.classList.remove('hidden');
        modal.classList.add('flex');
    }

    function closePreviewModal() {
        const modal = document.getElementById('email-preview-modal');
        modal.classList.add('hidden');
        modal.classList.remove('flex');
        
        document.getElementById('preview-iframe').src = '';
    }
</script>

<?php require APPROOT . '/app/Views/layouts/footer.php'; ?>