<?php
$edited = can_action('69', 'edited');
?>

<div class="space-y-4">
	<div class="flex items-center justify-between">
		<div class="flex items-center gap-2">

			<?php if (!empty($edited)) { ?>
				<a href="<?= base_url() ?>admin/tasks/create/goal/<?= $goal_info->goal_tracking_id ?>"
					class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
					<i data-lucide="plus" class="h-4 w-4"></i>
					<?= lang('new_task') ?>
				</a>
			<?php } ?>
		</div>
	</div>

	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="p-4">
			<div class="overflow-x-auto">
				<table id="DataTablesTasksGoal<?= $goal_info->goal_tracking_id ?>" class="min-w-full table-auto text-left text-sm DataTables" width="100%">
					<thead>
						<tr>
							<th class="w-10 text-center"><input type="checkbox" id="select_all_tasks_<?= $goal_info->goal_tracking_id ?>" class="mx-auto"></th>
							<th class="py-2 px-3 break-words"><?= lang('task_name') ?></th>
							<th class="py-2 px-3 whitespace-nowrap"><?= lang('due_date') ?></th>
							<th class="py-2 px-3 whitespace-nowrap"><?= lang('progress') ?></th>
							<th class="py-2 px-3 whitespace-nowrap"><?= lang('status') ?></th>
							<th class="py-2 px-3 text-right whitespace-nowrap"><?= lang('action') ?></th>
						</tr>
					</thead>
					<tbody>
						<?php
						$all_task_info = get_result('tbl_task', array('goal_tracking_id' => $goal_info->goal_tracking_id));
						if (!empty($all_task_info)) : foreach ($all_task_info as $key => $v_task) :
						?>
								<tr id="table-tasks-<?= $v_task->task_id ?>">
									<td>
										<div class="is_complete checkbox c-checkbox">
											<label>
												<input type="checkbox" data-id="<?= $v_task->task_id ?>" style="position: absolute;" <?php
																																		if ($v_task->task_progress >= 100) {
																																			echo 'checked';
																																		}
																																		?>>
												<span data-lucide="check"></span>
											</label>
										</div>
									</td>
									<td><a class="text-info" style="<?php
																	if ($v_task->task_progress >= 100) {
																		echo 'text-decoration: line-through;';
																	}
																	?>" href="<?= base_url() ?>admin/tasks/details/<?= $v_task->task_id ?>"><?php echo $v_task->task_name; ?></a>
									</td>

									<td><?php
										$due_date = $v_task->due_date;
										$due_time = safe_strtotime($due_date);
										$current_time = safe_strtotime(date('Y-m-d'));
										?>
										<?= display_date($due_date) ?>
										<?php if ($current_time > $due_time && $v_task->task_progress < 100) { ?>
											<span class="label label-danger"><?= lang('overdue') ?></span>
										<?php } ?>
									</td>
									<td>
										<div class="inline ">
											<div class="easypiechart text-success" style="margin: 0px;" data-percent="<?= $v_task->task_progress ?>" data-line-width="5" data-track-Color="#f0f0f0" data-bar-color="#<?php
																																																						if ($v_task->task_progress == 100) {
																																																							echo '8ec165';
																																																						} else {
																																																							echo 'fb6b5b';
																																																						}
																																																						?>" data-rotate="270" data-scale-Color="false" data-size="50" data-animate="2000">
												<span class="small text-muted"><?= $v_task->task_progress ?>
													%</span>
											</div>
										</div>

									</td>
									<td>
										<?php
										if ($v_task->task_status == 'completed') {
											$label = 'success';
										} elseif ($v_task->task_status == 'not_started') {
											$label = 'info';
										} elseif ($v_task->task_status == 'deferred') {
											$label = 'danger';
										} else {
											$label = 'warning';
										}
										?>
										<span class="label label-<?= $label ?>"><?= lang($v_task->task_status) ?>
										</span>
									</td>
									<td>
										

										
										<?php echo btn_edit('admin/tasks/create/' . $v_task->task_id) ?>
										<?php echo ajax_anchor(base_url("admin/tasks/delete_task/" . $v_task->task_id), "", array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table_" . $key)) . ' '; ?>
										<?php

										if ($v_task->timer_status == 'on') { ?>
											<a class="btn btn-xs btn-danger" href="<?= base_url() ?>admin/tasks/tasks_timer/off/<?= $v_task->task_id ?>"><?= lang('stop_timer') ?>
											</a>

										<?php } else { ?>
											<a class="btn btn-xs btn-success" href="<?= base_url() ?>admin/tasks/tasks_timer/on/<?= $v_task->task_id ?>"><?= lang('start_timer') ?>
											</a>
										<?php } ?>
									</td>
								</tr>
							<?php endforeach; ?>
						<?php endif; ?>
					</tbody>
				</table>
				<!-- <script type="text/javascript">
					list = base_url + "admin/goal_tracking/tasksList/<?= $goal_info->goal_tracking_id ?>";
				</script> -->
			</div>
		</div>
	</div>
</div>

