<?php
$project_id = !empty($id) ? $id : (!empty($project_details->project_id) ? $project_details->project_id : null);
$all_task_info = !empty($project_id) ? get_result('tbl_task', array('project_id' => $project_id)) : array();

$status_counts = array(
    'not_started' => 0,
    'in_progress' => 0,
    'testing' => 0,
    'awaiting_feedback' => 0,
    'completed' => 0,
    'overdue' => 0,
);
if (!empty($all_task_info)) {
    foreach ($all_task_info as $v_task) {
        if (isset($status_counts[$v_task->task_status])) {
            $status_counts[$v_task->task_status]++;
        }
        $due_time = safe_strtotime($v_task->due_date);
        $current_time = safe_strtotime(date('Y-m-d'));
        if ($current_time > $due_time && (int) $v_task->task_progress < 100) {
            $status_counts['overdue']++;
        }
    }
}
?>

    <div class="space-y-5">
        <div class="flex flex-wrap items-center justify-between gap-3">
            <div>
                <h3 class="text-base font-semibold text-foreground"><?= lang('tasks') ?></h3>
                <p class="text-sm text-muted-foreground"><?= lang('tasks') ?> overview</p>
            </div>
        </div>

        <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-5">
            <div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
                <div class="text-[11px] font-semibold uppercase tracking-widest text-muted-foreground"><?= lang('not_started') ?></div>
                <div class="mt-2 text-3xl font-bold text-foreground"><?= (int) $status_counts['not_started'] ?></div>
                <div class="mt-1 text-xs text-muted-foreground"><?= lang('tasks') ?></div>
            </div>
            <div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
                <div class="text-[11px] font-semibold uppercase tracking-widest text-blue-600 dark:text-blue-400"><?= lang('in_progress') ?></div>
                <div class="mt-2 text-3xl font-bold text-foreground"><?= (int) $status_counts['in_progress'] ?></div>
                <div class="mt-1 text-xs text-muted-foreground"><?= lang('tasks') ?></div>
            </div>
            <div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
                <div class="text-[11px] font-semibold uppercase tracking-widest text-purple-600 dark:text-purple-400"><?= lang('testing') ?></div>
                <div class="mt-2 text-3xl font-bold text-foreground"><?= (int) $status_counts['testing'] ?></div>
                <div class="mt-1 text-xs text-muted-foreground"><?= lang('tasks') ?></div>
            </div>
            <div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
                <div class="text-[11px] font-semibold uppercase tracking-widest text-emerald-600 dark:text-emerald-400"><?= lang('completed') ?></div>
                <div class="mt-2 text-3xl font-bold text-foreground"><?= (int) $status_counts['completed'] ?></div>
                <div class="mt-1 text-xs text-muted-foreground"><?= lang('tasks') ?></div>
            </div>
            <div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
                <div class="text-[11px] font-semibold uppercase tracking-widest text-rose-600 dark:text-rose-400"><?= lang('overdue') ?></div>
                <div class="mt-2 text-3xl font-bold text-rose-600 dark:text-rose-400"><?= (int) $status_counts['overdue'] ?></div>
                <div class="mt-1 text-xs text-muted-foreground"><?= lang('due_date') ?></div>
            </div>
        </div>

        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-900/40">
            <div class="overflow-x-auto">
                <table id="table-tasks" class="min-w-full text-sm">
                    <thead class="bg-slate-50 text-slate-500 dark:bg-slate-800 dark:text-slate-300">
                        <tr>
                            <th class="px-4 py-3 text-left"><?= lang('task_name') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('due_date') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('progress') ?></th>
                            <th class="px-4 py-3 text-left"><?= lang('status') ?></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                        <?php if (!empty($all_task_info)) : foreach ($all_task_info as $key => $v_task) : ?>
                            <tr class="transition hover:bg-slate-50/70 dark:hover:bg-slate-800/50 text-slate-700 dark:text-slate-200">
                                <td class="px-4 py-3">
                                    <a class="text-primary hover:underline dark:text-sky-300" style="<?= $v_task->task_status == 'completed' ? 'text-decoration: line-through;' : '' ?>"
                                       href="<?= base_url() ?>client/tasks/details/<?= $v_task->task_id ?>">
                                        <?= html_escape($v_task->task_name) ?>
                                    </a>
                                </td>
                                <td class="px-4 py-3">
                                    <?php
                                    $due_date = $v_task->due_date;
                                    $due_time = safe_strtotime($due_date);
                                    $current_time = safe_strtotime(date('Y-m-d'));
                                    ?>
                                    <span class="text-slate-700 dark:text-slate-200"><?= display_date($due_date) ?></span>
                                    <?php if ($current_time > $due_time && $v_task->task_progress < 100) { ?>
                                        <span class="ml-2 inline-flex rounded-full bg-rose-50 px-2 py-0.5 text-xs text-rose-700 dark:bg-rose-900/40 dark:text-rose-300"><?= lang('overdue') ?></span>
                                    <?php } ?>
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex items-center gap-2">
                                        <div class="h-2 w-24 rounded-full bg-slate-100 dark:bg-slate-800">
                                            <div class="h-2 rounded-full <?= $v_task->task_progress == 100 ? 'bg-emerald-500' : 'bg-rose-400' ?>" style="width: <?= $v_task->task_progress ?>%"></div>
                                        </div>
                                        <span class="text-xs text-slate-500 dark:text-slate-400"><?= $v_task->task_progress ?>%</span>
                                    </div>
                                </td>
                                <td class="px-4 py-3">
                                    <?php
                                    if ($v_task->task_status == 'completed') {
                                        $label = 'bg-emerald-50 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-200';
                                    } elseif ($v_task->task_status == 'not_started') {
                                        $label = 'bg-blue-50 text-blue-700 dark:bg-blue-900/30 dark:text-blue-200';
                                    } elseif ($v_task->task_status == 'deferred') {
                                        $label = 'bg-rose-50 text-rose-700 dark:bg-rose-900/30 dark:text-rose-200';
                                    } else {
                                        $label = 'bg-amber-50 text-amber-700 dark:bg-amber-900/30 dark:text-amber-200';
                                    }
                                    ?>
                                    <span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $label ?>">
                                        <?= lang($v_task->task_status) ?>
                                    </span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                        <?php else : ?>
                            <tr>
                                <td colspan="4" class="px-4 py-6 text-center text-sm text-slate-500 dark:text-slate-400"><?= lang('no_data_found') ?></td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
