<?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>
        <div class="w-full md:w-auto flex flex-col md:flex-row gap-3">
            <a href="<?php echo URLROOT; ?>/admin/emailQueue" 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="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>
                View Email Queue
            </a>
            <form action="<?php echo URLROOT; ?>/admin/runCron" method="POST" onsubmit="confirmAction(event, this, 'Run Cron Manually', 'Are you sure you want to manually trigger the background cron tasks?');" class="w-full md:w-auto m-0">
                <button type="submit" class="w-full md:w-auto bg-primary hover:opacity-90 text-white font-bold py-2.5 px-6 rounded-lg transition-all focus:ring-4 focus:ring-primary/50 text-sm flex justify-center items-center shadow-sm">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                    Run Cron Now
                </button>
            </form>
        </div>
    </div>

    <div class="bg-blue-50 dark:bg-blue-900/10 border border-blue-200 dark:border-blue-800/30 rounded-xl p-6">
        <h3 class="text-sm font-bold text-blue-900 dark:text-blue-300 mb-2">Server Setup Instructions</h3>
        <p class="text-xs text-blue-800 dark:text-blue-400/80 mb-4 leading-relaxed">To ensure background tasks and emails process automatically, add the following cron job to your cPanel or server configuration. The URL contains a cryptographic token to prevent unauthorized access. It should run every 2 minutes.</p>
        <div class="bg-gray-900 text-green-400 font-mono text-xs p-4 rounded-lg overflow-x-auto shadow-inner border border-gray-800">
            */2 * * * * wget -q -O - <?php echo URLROOT; ?>/cron/run/<?php echo htmlspecialchars($data['cron_token']); ?> >/dev/null 2>&1
        </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="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                Cron Execution History
            </h2>
        </div>
        <div class="w-full text-gray-900 dark:text-gray-100 p-5">
            <table id="cronHistoryTable" 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">Executed At</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-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Details</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($data['cron_logs'])): ?>
                        <?php foreach($data['cron_logs'] as $log): ?>
                        <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($log->created_at); ?>">
                                <?php echo $log->created_at; ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <span class="text-green-600 dark:text-green-400 font-bold">Success</span>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600 dark:text-gray-400"><?php echo htmlspecialchars($log->description); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>

<script>
    $(document).ready(function() {
        $('#cronHistoryTable').DataTable({
            responsive: true,
            order: [[ 0, "desc" ]],
            pageLength: 25,
            language: {
                search: "",
                searchPlaceholder: "Search history...",
                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="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                                </div>
                                <h3 class="text-lg font-bold text-gray-900 dark:text-white">No Execution History</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400 mt-1">The background cron task has not run yet.</p>
                            </div>`
            }
        });
    });
</script>

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