<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$is_income = (!empty($expense_info) && $expense_info->type == 'Income');
$perm_id = $is_income ? '30' : '31';
$edited = can_action($perm_id, 'edited');
$all_task_info = get_result('tbl_task', array('transactions_id' => $expense_info->transactions_id));
$task_total = !empty($all_task_info) ? count($all_task_info) : 0;
$task_completed = 0;
$task_overdue = 0;
$task_active = 0;
$today = safe_strtotime(date('Y-m-d'));

if (!empty($all_task_info)) {
	foreach ($all_task_info as $task_item) {
		if ((int) $task_item->task_progress >= 100 || $task_item->task_status == 'completed') {
			$task_completed++;
		} else {
			$task_active++;
		}
		if (!empty($task_item->due_date) && safe_strtotime($task_item->due_date) < $today && (int) $task_item->task_progress < 100) {
			$task_overdue++;
		}
	}
}
?>

<div class="space-y-6">
	<div class="flex flex-col gap-4 md:flex-row md:items-start md:justify-between">
		<div>
			<h3 class="text-lg font-semibold text-foreground"><?= lang('tasks') ?></h3>
			<p class="text-sm text-muted-foreground"><?= lang('all_task') ?> linked to this <?= $is_income ? lang('deposit') : lang('expense') ?>.</p>
		</div>
		<?php if (!empty($edited)) { ?>
			<a href="<?= base_url() ?>admin/tasks/create/expense/<?= $expense_info->transactions_id ?>" class="inline-flex items-center gap-2 rounded-lg bg-primary px-4 py-2 text-sm font-semibold text-white hover:bg-primary/90">
				<i data-lucide="plus" class="h-4 w-4"></i>
				<?= lang('new_task') ?>
			</a>
		<?php } ?>
	</div>

	<div class="grid gap-3 md:grid-cols-3">
		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<div class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('all_task') ?></div>
			<div class="mt-2 text-2xl font-semibold text-foreground"><?= $task_total ?></div>
		</div>
		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<div class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('completed') ?></div>
			<div class="mt-2 text-2xl font-semibold text-emerald-600"><?= $task_completed ?></div>
		</div>
		<div class="rounded-2xl border border-border bg-card p-4 shadow-sm">
			<div class="text-xs font-semibold uppercase tracking-[0.2em] text-muted-foreground"><?= lang('overdue') ?></div>
			<div class="mt-2 text-2xl font-semibold text-rose-600"><?= $task_overdue ?></div>
		</div>
	</div>

	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<?php if (!empty($all_task_info)) { ?>
			<div class="divide-y divide-border">
				<?php foreach ($all_task_info as $v_task) : ?>
					<?php
					$is_done = (int) $v_task->task_progress >= 100;
					$due_time = !empty($v_task->due_date) ? safe_strtotime($v_task->due_date) : null;
					$is_overdue = !empty($due_time) && $today > $due_time && !$is_done;
					if ($v_task->task_status == 'completed') {
						$status_class = 'bg-emerald-100 text-emerald-700';
					} elseif ($v_task->task_status == 'not_started') {
						$status_class = 'bg-sky-100 text-sky-700';
					} elseif ($v_task->task_status == 'deferred') {
						$status_class = 'bg-rose-100 text-rose-700';
					} else {
						$status_class = 'bg-amber-100 text-amber-700';
					}
					?>
					<div id="leads_tasks_<?= $v_task->task_id ?>" class="flex flex-col gap-4 p-5 xl:flex-row xl:items-start xl:justify-between">
						<div class="flex gap-4">
							<div class="pt-1">
								<div class="is_complete checkbox c-checkbox">
									<label class="needsclick">
										<input type="checkbox" data-id="<?= $v_task->task_id ?>" style="position: absolute;" <?= $is_done ? 'checked' : '' ?>>
										<span data-lucide="check"></span>
									</label>
								</div>
							</div>
							<div class="space-y-3">
								<div>
									<a href="<?= base_url() ?>admin/tasks/details/<?= $v_task->task_id ?>" class="text-base font-semibold text-foreground hover:text-primary <?= $is_done ? 'line-through opacity-70' : '' ?>">
										<?= html_escape($v_task->task_name) ?>
									</a>
									<div class="mt-2 flex flex-wrap items-center gap-2">
										<span class="inline-flex items-center rounded-full px-2.5 py-1 text-xs font-semibold <?= $status_class ?>">
											<?= lang($v_task->task_status) ?>
										</span>
										<?php if (!empty($v_task->due_date)) { ?>
											<span class="inline-flex items-center rounded-full border border-border bg-background px-2.5 py-1 text-xs font-medium text-muted-foreground">
												<i data-lucide="calendar" class="mr-1 h-3.5 w-3.5"></i>
												<?= display_date($v_task->due_date) ?>
											</span>
										<?php } ?>
										<?php if ($is_overdue) { ?>
											<span class="inline-flex items-center rounded-full bg-rose-100 px-2.5 py-1 text-xs font-semibold text-rose-700">
												<?= lang('overdue') ?>
											</span>
										<?php } ?>
									</div>
								</div>
								<div class="max-w-xl">
									<div class="mb-2 flex items-center justify-between text-xs font-medium text-muted-foreground">
										<span><?= lang('progress') ?></span>
										<span><?= (int) $v_task->task_progress ?>%</span>
									</div>
									<div class="h-2 overflow-hidden rounded-full bg-muted">
										<div class="h-full rounded-full <?= $is_done ? 'bg-emerald-500' : 'bg-primary' ?>" style="width: <?= max(0, min(100, (int) $v_task->task_progress)) ?>%"></div>
									</div>
								</div>
							</div>
						</div>
						<div class="flex flex-wrap items-center xl:justify-end">
							<?= ajax_anchor(base_url("admin/tasks/delete_task/" . $v_task->task_id), lang('delete'), array("class" => "inline-flex items-center rounded-lg border border-rose-200 bg-rose-50 px-3 py-2 text-xs font-semibold text-rose-700 hover:bg-rose-100", "title" => lang('delete'), "data-fade-out-on-success" => "#leads_tasks_" . $v_task->task_id)); ?>
							<?= btn_edit('admin/tasks/create/' . $v_task->task_id) ?>
							<?php if ($v_task->timer_status == 'on') { ?>
								<a class="inline-flex items-center  h-7 mb-1 ml-1 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-xs font-semibold text-amber-700 hover:bg-amber-100" href="<?= base_url() ?>admin/tasks/tasks_timer/off/<?= $v_task->task_id ?>">
									<?= lang('stop_timer') ?>
								</a>
							<?php } else { ?>
								<a class="inline-flex items-center h-7 mb-1 ml-1 rounded-lg border border-emerald-200 bg-emerald-50 px-3 py-2 text-xs font-semibold text-emerald-700 hover:bg-emerald-100" href="<?= base_url() ?>admin/tasks/tasks_timer/on/<?= $v_task->task_id ?>">
									<?= lang('start_timer') ?>
								</a>
							<?php } ?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		<?php } else { ?>
			<div class="p-10 text-center">
				<div class="mx-auto inline-flex h-14 w-14 items-center justify-center rounded-2xl border border-border bg-background/70">
					<i data-lucide="list-todo" class="h-6 w-6 text-muted-foreground"></i>
				</div>
				<h4 class="mt-4 text-sm font-semibold text-foreground"><?= lang('nothing_to_display') ?></h4>
				<p class="mt-1 text-sm text-muted-foreground"><?= lang('all_task') ?> <?= lang('not_found') ?>.</p>
			</div>
		<?php } ?>
	</div>
</div>

