<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
$total_timer = get_result('tbl_tasks_timer', array(
	'project_id' => $project_details->project_id,
	'start_time !=' => 0,
	'end_time !=' => 0
));
$timer_entries = array();
$user_ids = array();
$task_ids = array();
$total_spent_seconds = 0;

if (!empty($total_timer)) {
	foreach ($total_timer as $v_timer) {
		if ((int) $v_timer->start_time === 0 || (int) $v_timer->end_time === 0) {
			continue;
		}
		$timer_entries[] = $v_timer;
		$user_ids[] = (int) $v_timer->user_id;
		if (!empty($v_timer->task_id)) {
			$task_ids[] = (int) $v_timer->task_id;
		}
		$total_spent_seconds += max(0, ((int) $v_timer->end_time - (int) $v_timer->start_time));
	}
}

$user_ids = array_values(array_unique(array_filter($user_ids)));
$task_ids = array_values(array_unique(array_filter($task_ids)));
$profiles_map = array();
$tasks_map = array();

if (!empty($user_ids)) {
	$profiles = $this->db->where_in('user_id', $user_ids)->get('tbl_account_details')->result();
	foreach ($profiles as $profile) {
		$profiles_map[(int) $profile->user_id] = $profile;
	}
}

if (!empty($task_ids)) {
	$project_tasks = $this->db->select('task_id, task_name')->where_in('task_id', $task_ids)->get('tbl_task')->result();
	foreach ($project_tasks as $project_task) {
		$tasks_map[(int) $project_task->task_id] = $project_task;
	}
}

$this->load->model('items_model');
$timer_count = count($timer_entries);
$active_members = count($user_ids);
$hours_spent = $total_spent_seconds > 0 ? round($total_spent_seconds / 3600, 1) : 0;
$format_duration = static function ($seconds) {
	$total_seconds = max(0, (int) round($seconds));
	$hours = intdiv($total_seconds, 3600);
	$minutes = intdiv($total_seconds % 3600, 60);
	$remaining_seconds = $total_seconds % 60;
	$parts = array();
	if ($hours > 0) {
		$parts[] = $hours . 'h';
	}
	if ($minutes > 0) {
		$parts[] = $minutes . 'm';
	}
	if ($remaining_seconds > 0 || empty($parts)) {
		$parts[] = $remaining_seconds . 's';
	}
	return implode(' ', $parts);
};


?>

<div class="project-overview-shell space-y-4" data-project-timesheet-panel="<?= (int) $project_details->project_id ?>">
	<div class="flex flex-wrap items-center justify-between gap-3">
		<div>
			<h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('timesheet'); ?></h3>
			<p class="text-sm text-muted-foreground">Logged work sessions, execution history, and team time usage.</p>
		</div>
		<div class="flex flex-wrap gap-2">
			<?= btn_modal(
				'admin/projects/new_timesheet/' . $project_details->project_id,
				lang('new') . ' ' . lang('timesheet'),
				lang('new') . ' ' . lang('timesheet'),
				'lg',
				'plus',
				array('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')
			) ?>
		</div>
	</div>

	<div class="grid gap-3 md:grid-cols-2 xl:grid-cols-3">
		<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-cyan-50 p-2.5 text-cyan-600 dark:bg-cyan-900/30 dark:text-cyan-400">
					<i data-lucide="timer" class="h-5 w-5"></i>
				</div>
				<div>
					<p class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= (int) $timer_count ?></p>
					<p class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400">Logged Entries</p>
				</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="clock-3" class="h-5 w-5"></i>
				</div>
				<div>
					<p class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= $hours_spent ?></p>
					<p class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400">Hours Tracked</p>
				</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-violet-50 p-2.5 text-violet-600 dark:bg-violet-900/30 dark:text-violet-400">
					<i data-lucide="users" class="h-5 w-5"></i>
				</div>
				<div>
					<p class="text-2xl font-bold text-slate-900 dark:text-slate-100"><?= (int) $active_members ?></p>
					<p class="text-[11px] font-medium uppercase tracking-[0.16em] text-slate-500 dark:text-slate-400"><?= lang('project_team') ?></p>
				</div>
			</div>
		</div>
	</div>

	<div class="project-section-card overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
		
		<div class="">
			<div class="overflow-hidden ">
				<div class="overflow-x-auto">
				<table class="min-w-full text-sm">

					<thead class="bg-slate-50 dark:bg-slate-800 text-slate-500 dark:text-slate-300">
						<tr>
							<th class="px-4 py-3 text-left"><?= lang('user') ?></th>
							<th class="px-4 py-3 text-left"><?= lang('start_time') ?></th>
							<th class="px-4 py-3 text-left"><?= lang('stop_time') ?></th>
							<th class="px-4 py-3 text-left"><?= lang('project_name') ?></th>
							<th class="px-4 py-3 text-left"><?= lang('time_spend') ?></th>
							<th class="px-4 py-3 text-left"><?= lang('action') ?></th>
						</tr>
					</thead>

					<tbody class="divide-y divide-slate-100 dark:divide-slate-800">

						<?php
						if (!empty($timer_entries)) {
							foreach ($timer_entries as $v_timer) {
								$profile_info = isset($profiles_map[(int) $v_timer->user_id]) ? $profiles_map[(int) $v_timer->user_id] : null;
								$task_info = !empty($v_timer->task_id) && isset($tasks_map[(int) $v_timer->task_id]) ? $tasks_map[(int) $v_timer->task_id] : null;
						?>

									<tr id="table-timesheet-<?= $v_timer->tasks_timer_id ?>" class="transition hover:bg-slate-50/70 dark:hover:bg-slate-800/50">

										<!-- User -->
										<td class="px-4 py-3">
											<?php if (!empty($profile_info)) { ?>
												<img class="h-9 w-9 rounded-full border border-slate-200 object-cover dark:border-slate-700"
													src="<?= base_url($profile_info->avatar) ?>"
													alt="">
											<?php } else {
												echo "-";
											} ?>
										</td>

										<!-- Start -->
										<td class="px-4 py-3">
											<span class="inline-flex rounded-full bg-emerald-50 px-2 py-1 text-xs text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-300">
												<?= display_datetime($v_timer->start_time, true) ?>
											</span>
										</td>

										<!-- Stop -->
										<td class="px-4 py-3">
											<span class="inline-flex rounded-full bg-rose-50 px-2 py-1 text-xs text-rose-700 dark:bg-rose-900/40 dark:text-rose-300">
												<?= display_datetime($v_timer->end_time, true) ?>
											</span>
										</td>

										<!-- Task -->
										<td class="px-4 py-3">
											<a href="<?= !empty($task_info) ? base_url("admin/tasks/details/" . $v_timer->task_id) : base_url("admin/projects/project_details/" . $v_timer->project_id) ?>"
												class="font-medium text-primary hover:underline dark:text-sky-300">
												<?= !empty($task_info) ? html_escape($task_info->task_name) : html_escape($project_details->project_name) ?>
											</a>
										</td>

										<!-- Time Spend -->
											<td class="px-4 py-3">
												<span class="whitespace-nowrap text-sm font-semibold text-slate-600 dark:text-slate-300">
													<?= $format_duration($v_timer->end_time - $v_timer->start_time); ?>
												</span>
											</td>

										<!-- Action -->
										<td class="px-4 py-3">
											<?php if (!empty($edited)) { ?>
												<div class="flex items-center gap-0">
													<?= btn_modal(
														'admin/projects/new_timesheet/' . $project_details->project_id . '/' . $v_timer->tasks_timer_id,
														'',
														lang('edit'),
														'lg',
														'square-pen',
														array('class' => 'inline-flex h-7 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 mb-1')
													) ?>
													<?php if (!empty($deleted)) { ?>

													
														<?= ajax_anchor(
															base_url("admin/projects/update_project_timer/" .
																$v_timer->tasks_timer_id . "/delete_task_timmer"),
															"",
															array(
																"class" => "",
																"title" => lang('delete'),
																"data-fade-out-on-success" =>
																"#table-timesheet-" . $v_timer->tasks_timer_id
															)
														); ?>
													<?php } ?>
												</div>
											<?php } ?>
										</td>

									</tr>

							<?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 } ?>

					</tbody>
				</table>
			</div>
			</div>
		</div>

	</div>
</div>
