<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php $date_text = function ($value) { return !empty($value) ? date('M d, Y', strtotime($value)) : '--'; }; ?>
<div class="space-y-4 sm:space-y-6">
    <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-5">
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Tasks</p><p class="mt-3 text-2xl font-semibold text-rose-500 sm:text-3xl"><?= count($overdue['tasks']) ?></p></article>
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Projects</p><p class="mt-3 text-2xl font-semibold text-amber-500 sm:text-3xl"><?= count($overdue['projects']) ?></p></article>
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Invoices</p><p class="mt-3 text-2xl font-semibold text-blue-500 sm:text-3xl"><?= count($overdue['invoices']) ?></p></article>
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Estimates</p><p class="mt-3 text-2xl font-semibold text-violet-500 sm:text-3xl"><?= count($overdue['estimates']) ?></p></article>
        <article class="rounded-2xl border border-border bg-background/70 p-4 sm:p-5 shadow-sm"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Tickets</p><p class="mt-3 text-2xl font-semibold text-slate-700 dark:text-slate-200 sm:text-3xl"><?= count($overdue['tickets']) ?></p></article>
    </div>

    <div class="grid grid-cols-1 gap-6 xl:grid-cols-2">
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5">
            <div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Delivery Overdue</p><h3 class="text-lg font-semibold text-foreground">Tasks and projects</h3></div>
            <div class="space-y-3">
                <?php foreach ($overdue['tasks'] as $row): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <p class="text-sm font-semibold text-foreground"><?= html_escape($row->task_name) ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= html_escape($row->project_name ?: 'No project') ?> • Due <?= $date_text($row->due_date) ?></p>
                    </div>
                <?php endforeach; ?>
                <?php foreach ($overdue['projects'] as $row): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <p class="text-sm font-semibold text-foreground"><?= html_escape($row->project_name) ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= html_escape(ucfirst($row->project_status)) ?> • Due <?= $date_text($row->end_date) ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
        </article>
        <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5">
            <div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Commercial Overdue</p><h3 class="text-lg font-semibold text-foreground">Invoices and estimates</h3></div>
            <div class="space-y-3">
                <?php foreach ($overdue['invoices'] as $row): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <p class="text-sm font-semibold text-foreground"><?= html_escape($row->reference_no) ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= html_escape($row->client_name ?: 'Unassigned Client') ?> • Due <?= $date_text($row->due_date) ?></p>
                    </div>
                <?php endforeach; ?>
                <?php foreach ($overdue['estimates'] as $row): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <p class="text-sm font-semibold text-foreground"><?= html_escape($row->reference_no) ?></p>
                        <p class="mt-1 text-xs text-muted-foreground"><?= html_escape($row->client_name ?: 'Unassigned Client') ?> • Due <?= $date_text($row->due_date) ?></p>
                    </div>
                <?php endforeach; ?>
            </div>
        </article>
    </div>

    <article class="rounded-2xl border border-border bg-background/60 p-4 sm:p-5">
        <div class="mb-4"><p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground">Support Queue</p><h3 class="text-lg font-semibold text-foreground">Tickets still requiring response</h3></div>
        <div class="grid grid-cols-1 gap-3 xl:grid-cols-2">
            <?php foreach ($overdue['tickets'] as $ticket): ?>
                <div class="rounded-xl border border-border bg-card p-4">
                    <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
                        <div>
                            <p class="text-sm font-semibold text-foreground"><?= html_escape($ticket->ticket_code ?: 'Ticket') ?></p>
                            <p class="mt-1 text-xs text-muted-foreground"><?= html_escape($ticket->subject) ?></p>
                        </div>
                        <span class="rounded-full bg-muted px-2.5 py-1 text-[11px] font-semibold text-foreground"><?= html_escape(ucfirst($ticket->priority)) ?></span>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </article>
</div>
