<?php
$datatable = true;
echo message_box('success');
echo message_box('error');
$created = can_action('4', 'created');
$deleted = can_action('4', 'deleted');
$edited = can_action('58', 'edited');
?>
<div class="space-y-4">
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="px-6 py-5 flex flex-col md:flex-row md:items-center md:justify-between gap-3 border-b border-border">
			<div>
				<h3 class="text-xl font-bold text-foreground"><?= lang('tasks'); ?></h3>
			</div>
			<div class="flex flex-wrap gap-2">

				<?php if (!empty($edited)) { ?>
					<a href="<?= base_url() ?>admin/tasks/create/bugs/<?= $bug_details->bug_id ?>" class="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white shadow hover:bg-primary/90 transition"><?= lang('new_task') ?></a>
				<?php } ?>

			</div>
		</div>

		<div class="p-6 space-y-4">
			<div class="overflow-x-auto rounded-xl border border-border">
				<table class="min-w-full text-left text-sm DataTables" id="DataTables" width="100%">
					<thead class="bg-muted/40 text-xs uppercase tracking-wide text-muted-foreground">
						<tr>
							<?php if (!empty($deleted)) : ?>
								<th class="w-10 px-4 py-3">
									<input type="checkbox" id="select_all" class="h-4 w-4 rounded border-slate-300 text-primary focus:ring-primary">
								</th>
							<?php endif; ?>
							<th class="px-4 py-3"><?= lang('task_name') ?></th>
							<th class="px-4 py-3"><?= lang('due_date') ?></th>
							<th class="px-4 py-3"><?= lang('status') ?></th>
							<th class="px-4 py-3"><?= lang('progress') ?></th>
							<th class="px-4 py-3"><?= lang('changes/view') ?></th>
						</tr>
					</thead>
					<tbody class="divide-y divide-border/70">
						<?php
						$all_task_info = get_result('tbl_task', array('bug_id' => $bug_details->bug_id));

						if (!empty($all_task_info)) : foreach ($all_task_info as $key => $v_task) :
						?>
								<tr id="table-tasks-<?= $v_task->task_id ?>" class="align-middle">
									<?php if (!empty($deleted)) : ?>
										<td class="px-4 py-3">
											<input type="checkbox" data-id="<?= $v_task->task_id ?>" class="h-4 w-4 rounded border-slate-300 text-primary focus:ring-primary" <?= ($v_task->task_progress >= 100) ? 'checked' : '' ?>>
										</td>
									<?php endif; ?>
									<td class="px-4 py-3">
										<a style="<?php
													if ($v_task->task_progress >= 100) {
														echo 'text-decoration: line-through;';
													}
													?>" href="<?= base_url() ?>admin/tasks/details/<?= $v_task->task_id ?>" class="font-medium text-primary hover:underline"><?php echo $v_task->task_name; ?></a>
									</td>
									<td class="px-4 py-3 text-foreground"><?php
										$due_date = $v_task->due_date;
										$due_time = safe_strtotime($due_date);
										?>
										<?= display_date($due_date) ?>
										<?php if (safe_strtotime(date('Y-m-d')) > $due_time && $v_task->task_progress < 100) { ?>
											<span class="ml-2 inline-flex items-center rounded-full border border-rose-200 bg-rose-50 px-2 py-0.5 text-[11px] font-semibold text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/30 dark:text-rose-300"><?= lang('overdue') ?></span>
										<?php } ?>
									</td>
									<td class="px-4 py-3"><?php
										if ($v_task->task_status == 'completed') {
											$label = 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/60 dark:bg-emerald-950/30 dark:text-emerald-300';
										} elseif ($v_task->task_status == 'not_started') {
											$label = 'border-sky-200 bg-sky-50 text-sky-700 dark:border-sky-900/60 dark:bg-sky-950/30 dark:text-sky-300';
										} elseif ($v_task->task_status == 'deferred') {
											$label = 'border-rose-200 bg-rose-50 text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/30 dark:text-rose-300';
										} else {
											$label = 'border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-900/60 dark:bg-amber-950/30 dark:text-amber-300';
										}
										?>
										<span class="inline-flex items-center rounded-full border px-2 py-0.5 text-[11px] font-semibold <?= $label ?>"><?= lang($v_task->task_status) ?>
										</span>
									</td>
									<td class="px-4 py-3">
										<div class="flex items-center gap-2">
											<div class="h-2 w-24 rounded-full bg-muted">
												<div class="h-2 rounded-full <?= $v_task->task_progress >= 100 ? 'bg-emerald-500' : 'bg-rose-500' ?>" style="width: <?= (int) $v_task->task_progress ?>%"></div>
											</div>
											<span class="text-xs text-muted-foreground"><?= (int) $v_task->task_progress ?>%</span>
										</div>
									</td>
									<td class="px-4 py-3">
										<div class="flex flex-wrap items-center gap-2">
											<?php echo ajax_anchor(base_url("admin/tasks/delete_task/" . $v_task->task_id), "<i data-lucide='trash-2' class='h-4 w-4'></i>", array("class" => "inline-flex h-8 w-8 items-center justify-center rounded-lg border border-rose-200 bg-rose-50 text-rose-600 hover:bg-rose-100 dark:border-rose-900/60 dark:bg-rose-950/30 dark:text-rose-300 dark:hover:bg-rose-900/40", "title" => lang('delete'), "data-fade-out-on-success" => "#table-tasks-" . $v_task->task_id)); ?>
											<?php echo btn_edit('admin/tasks/create/' . $v_task->task_id) ?>
										<?php

										if ($v_task->timer_status == 'on') { ?>
											<a class="inline-flex items-center rounded-lg border border-rose-200 bg-rose-50 px-2.5 py-1.5 text-xs font-semibold text-rose-700 hover:bg-rose-100 dark:border-rose-900/60 dark:bg-rose-950/30 dark:text-rose-300 dark:hover:bg-rose-900/40" href="<?= base_url() ?>admin/tasks/tasks_timer/off/<?= $v_task->task_id ?>"><?= lang('stop_timer') ?>
											</a>

										<?php } else { ?>
											<a class="inline-flex items-center rounded-lg border border-emerald-200 bg-emerald-50 px-2.5 py-1.5 text-xs font-semibold text-emerald-700 hover:bg-emerald-100 dark:border-emerald-900/60 dark:bg-emerald-950/30 dark:text-emerald-300 dark:hover:bg-emerald-900/40" href="<?= base_url() ?>admin/tasks/tasks_timer/on/<?= $v_task->task_id ?>"><?= lang('start_timer') ?>
											</a>
										<?php } ?>
										</div>
									</td>
								</tr>
							<?php endforeach; ?>
						<?php else : ?>
							<tr>
								<td colspan="<?= !empty($deleted) ? 6 : 5 ?>" class="px-4 py-8 text-center text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></td>
							</tr>
						<?php endif; ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
