<?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-4">
        <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-foreground sm:text-3xl"><?= (int) $delivery['project_count'] ?></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">Tasks</p><p class="mt-3 text-2xl font-semibold text-foreground sm:text-3xl"><?= (int) $delivery['task_count'] ?></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">Recent Projects</p><p class="mt-3 text-2xl font-semibold text-blue-500 sm:text-3xl"><?= count($delivery['recent_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">Recent Tasks</p><p class="mt-3 text-2xl font-semibold text-violet-500 sm:text-3xl"><?= count($delivery['recent_tasks']) ?></p></article>
    </div>

    <div class="grid grid-cols-1 gap-6 xl:grid-cols-3">
        <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">Projects</p><h3 class="text-lg font-semibold text-foreground">Status distribution</h3></div><div data-chart="bar" data-height="260" data-height-sm="220" data-series='<?= json_encode($delivery['project_status_series'], JSON_UNESCAPED_SLASHES) ?>'></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">Tasks</p><h3 class="text-lg font-semibold text-foreground">Execution mix</h3></div><div data-chart="bar" data-height="260" data-height-sm="220" data-series='<?= json_encode($delivery['task_status_series'], JSON_UNESCAPED_SLASHES) ?>'></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">Teams</p><h3 class="text-lg font-semibold text-foreground">Department allocation</h3></div><div data-chart="bar" data-height="260" data-height-sm="220" data-series='<?= json_encode($delivery['department_series'], JSON_UNESCAPED_SLASHES) ?>'></div></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">Recent Projects</p><h3 class="text-lg font-semibold text-foreground">Delivery portfolio</h3></div>
            <div class="space-y-3">
                <?php foreach ($delivery['recent_projects'] as $project): ?>
                    <?php $progress = (int) ($project->total_tasks > 0 ? round(($project->completed_tasks / $project->total_tasks) * 100) : 0); ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <div class="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
                            <div>
                                <p class="text-sm font-semibold text-foreground"><?= html_escape($project->project_name) ?></p>
                                <p class="mt-1 text-xs text-muted-foreground"><?= html_escape(ucfirst($project->project_status)) ?> • Due <?= $date_text($project->end_date) ?></p>
                            </div>
                            <span class="text-sm font-semibold text-primary"><?= $progress ?>%</span>
                        </div>
                        <div class="mt-3 h-2 rounded-full bg-muted">
                            <div class="h-2 rounded-full bg-primary" style="width: <?= min(100, max(0, $progress)) ?>%"></div>
                        </div>
                    </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">Recent Tasks</p><h3 class="text-lg font-semibold text-foreground">Execution queue</h3></div>
            <div class="space-y-3">
                <?php foreach ($delivery['recent_tasks'] as $task): ?>
                    <div class="rounded-xl border border-border bg-card p-4">
                        <div class="flex flex-col gap-3 sm:flex-row sm:items-start sm:justify-between">
                            <div>
                                <p class="text-sm font-semibold text-foreground"><?= html_escape($task->task_name) ?></p>
                                <p class="mt-1 text-xs text-muted-foreground"><?= html_escape($task->project_name ?: 'No project') ?> • Due <?= $date_text($task->due_date) ?></p>
                            </div>
                            <div class="text-right">
                                <span class="rounded-full bg-muted px-2.5 py-1 text-[11px] font-semibold text-foreground"><?= html_escape(ucwords(str_replace('_', ' ', $task->task_status))) ?></span>
                                <p class="mt-2 text-xs font-semibold text-primary"><?= (int) $task->task_progress ?>%</p>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </article>
    </div>
</div>
