<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
$all_task_info = get_result('tbl_task', array('project_id' => $project_details->project_id));
$t_edited = can_action('54', 'edited');

$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']++;
		}
	}
}
$total_tasks = count($all_task_info);
$active_tasks = (int) $status_counts['in_progress'] + (int) $status_counts['testing'] + (int) $status_counts['awaiting_feedback'];
$completed_pct = $total_tasks > 0 ? round(((int) $status_counts['completed'] / $total_tasks) * 100) : 0;
?>

<div class="project-overview-shell 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">Delivery board and execution health</p>
		</div>
		<div class="flex items-center gap-2">
			<button type="button" class="project-action-btn inline-flex items-center gap-2 px-3 py-2 text-xs font-semibold text-foreground transition hover:bg-muted">
				<i data-lucide="filter" class="h-4 w-4"></i>
				<?= lang('filter') ?>
			</button>
			<?php if (!empty($edited)) { ?>
				<a href="<?= base_url() ?>admin/tasks/create/project/<?= $project_details->project_id ?>"
					class="inline-flex items-center gap-2 rounded-2xl bg-slate-900 px-3.5 py-2 text-xs font-semibold text-white shadow-sm hover:bg-slate-800 dark:hover:bg-slate-700">
					<i data-lucide="plus" class="h-4 w-4"></i>
					<?= lang('new_task') ?>
				</a>
			<?php } ?>
		</div>
	</div>

	<div class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
		<div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
			<div class="flex items-center gap-3">
				<div class="rounded-xl bg-blue-50 p-2.5 text-blue-600 dark:bg-blue-900/30 dark:text-blue-400">
					<i data-lucide="list-todo" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-foreground"><?= (int) $total_tasks ?></div>
					<div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground">Total Tasks</div>
				</div>
			</div>
		</div>

		<div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
			<div class="flex items-center gap-3">
				<div class="rounded-xl bg-amber-50 p-2.5 text-amber-600 dark:bg-amber-900/30 dark:text-amber-400">
					<i data-lucide="activity" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-foreground"><?= (int) $active_tasks ?></div>
					<div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground">Live Workload</div>
				</div>
			</div>
		</div>

		<div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
			<div class="flex items-center gap-3">
				<div class="rounded-xl bg-emerald-50 p-2.5 text-emerald-600 dark:bg-emerald-900/30 dark:text-emerald-400">
					<i data-lucide="check-check" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-foreground"><?= $completed_pct ?>%</div>
					<div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('completed') ?></div>
				</div>
			</div>
		</div>

		<div class="project-kpi-card rounded-xl border border-border bg-card p-4 shadow-sm">
			<div class="flex items-center gap-3">
				<div class="rounded-xl bg-rose-50 p-2.5 text-rose-600 dark:bg-rose-900/30 dark:text-rose-400">
					<i data-lucide="alarm-clock" class="h-5 w-5"></i>
				</div>
				<div>
					<div class="text-2xl font-bold text-rose-600 dark:text-rose-400"><?= (int) $status_counts['overdue'] ?></div>
					<div class="text-[11px] font-medium uppercase tracking-[0.16em] text-muted-foreground"><?= lang('overdue') ?></div>
				</div>
			</div>
		</div>
	</div>

	<div class="project-section-card overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
		<div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4 py-3">
			<div>
				<h4 class="text-sm font-semibold text-foreground">Task Register</h4>
				<p class="text-xs text-muted-foreground">Status, due dates, and execution flow in one place.</p>
			</div>
			<div class="flex flex-wrap gap-2 text-xs">
				<span class="project-summary-chip inline-flex items-center gap-2 px-2.5 py-1 text-slate-700 dark:text-slate-200"><span class="h-2 w-2 rounded-full bg-blue-500"></span><?= (int) $status_counts['not_started'] ?> <?= lang('not_started') ?></span>
				<span class="project-summary-chip inline-flex items-center gap-2 px-2.5 py-1 text-slate-700 dark:text-slate-200"><span class="h-2 w-2 rounded-full bg-amber-500"></span><?= (int) $status_counts['testing'] ?> <?= lang('testing') ?></span>
				<span class="project-summary-chip inline-flex items-center gap-2 px-2.5 py-1 text-slate-700 dark:text-slate-200"><span class="h-2 w-2 rounded-full bg-emerald-500"></span><?= (int) $status_counts['completed'] ?> <?= lang('completed') ?></span>
			</div>
		</div>
		<div class="overflow-x-auto overflow-y-visible">
			<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"></th>
						<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>
						<th class="px-4 py-3 text-left"><?= lang('changes/view') ?></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="text-slate-700 transition hover:bg-slate-50/70 dark:text-slate-200 dark:hover:bg-slate-800/50">
								<td class="px-4 py-3">
									<label class="inline-flex items-center">
										<input type="checkbox" data-id="<?= $v_task->task_id ?>" class="h-4 w-4 rounded border-slate-300 text-primary dark:border-slate-600"
											<?= $v_task->task_progress >= 100 ? 'checked' : '' ?>>
									</label>
								</td>
									<td class="px-4 py-3">
										<a class="font-medium text-primary hover:underline dark:text-sky-300" style="<?= $v_task->task_status == 'completed' ? 'text-decoration: line-through;' : '' ?>"
											href="<?= base_url() ?>admin/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-900 dark:text-slate-100"><?= 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-700">
											<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/40 dark:text-emerald-300';
									} elseif ($v_task->task_status == 'not_started') {
										$label = 'bg-blue-50 text-blue-700 dark:bg-blue-900/40 dark:text-blue-300';
									} elseif ($v_task->task_status == 'deferred') {
										$label = 'bg-rose-50 text-rose-700 dark:bg-rose-900/40 dark:text-rose-300';
									} else {
										$label = 'bg-amber-50 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300';
									}
									?>
									<span class="inline-flex rounded-full px-2 py-0.5 text-xs <?= $label ?>">
										<?= lang($v_task->task_status) ?>
									</span>
								</td>
								<td class="px-4 py-3">
									<div class="flex items-center gap-1.5">
										<a href="<?= base_url('admin/tasks/details/' . $v_task->task_id) ?>" class="inline-flex h-8 w-8 items-center justify-center rounded-lg bg-muted text-foreground hover:bg-slate-300 dark:bg-slate-700 dark:text-slate-300 dark:hover:bg-slate-600" title="<?= lang('view') ?>">
											<i data-lucide="list-tree" class="h-4 w-4"></i>
										</a>
										<?php if (!empty($t_edited)) { ?>
											<a href="<?= base_url('admin/tasks/create/' . $v_task->task_id) ?>" class="inline-flex h-8 w-8 items-center justify-center rounded-lg bg-slate-900 text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white" title="<?= lang('edit') ?>">
												<i data-lucide="square-pen" class="h-4 w-4"></i>
											</a>
										<?php } ?>
									</div>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php else : ?>
						<tr>
								<td colspan="6" 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>
