<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');
$time_active = 1;
$task_timer_id = $this->uri->segment(6);
if ($task_timer_id) {
	$time_active = 2;
	$project_timer_info = get_row('tbl_tasks_timer', array('tasks_timer_id' => $task_timer_id));
}
?>

<div class="space-y-4">
	<div class="flex items-center justify-between">
		<div>
			<h3 class="text-base font-semibold text-slate-900 dark:text-slate-100"><?= lang('timesheet') ?></h3>
			<p class="text-sm text-slate-500"><?= lang('time_spend') ?></p>
		</div>
	</div>

	<div class="rounded-2xl border border-slate-200 dark:border-slate-800 bg-white dark:bg-slate-900 shadow-sm">
		<div class="border-b border-slate-200 dark:border-slate-800 px-6">
			<div class="flex flex-wrap items-center gap-3 py-4">

				<?php if (!empty($edited)) { ?>
					<a href="#contact" data-toggle="tab"
						class="inline-flex items-center rounded-lg px-3 py-2 text-sm font-medium <?= $time_active == 2 ? 'bg-slate-900 text-white' : 'text-slate-600 hover:bg-slate-100 dark:text-slate-300 dark:hover:bg-slate-800' ?>">
						<?= lang('manual_entry') ?>
					</a>
				<?php } ?>
			</div>
		</div>

		<div class="p-6">
			<div class="tab-content">
				<div class="tab-pane <?= $time_active == 1 ? 'active' : ''; ?>" id="general">
					<div class="overflow-x-auto">
						<table id="table-tasks-timelog" class="min-w-full text-sm">
							<thead class="bg-slate-50 dark:bg-slate-800 text-slate-500">
								<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('task_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
								$total_timer = get_result('tbl_tasks_timer', array('project_id' => $project_details->project_id, 'start_time !=' => 0, 'end_time !=' => 0));
								if (!empty($total_timer)) {
									foreach ($total_timer as $v_timer) {
										$aproject_info = get_row('tbl_project', array('project_id' => $v_timer->project_id));
										if (!empty($aproject_info)) {
											if ($v_timer->start_time != 0 && $v_timer->end_time != 0) {
								?>
												<tr id="table-timesheet-<?= $v_timer->tasks_timer_id ?>">
													<td class="px-4 py-3">
														<?php
														$profile_info = $this->db->where(array('user_id' => $v_timer->user_id))->get('tbl_account_details')->row();
														$user_info = $this->db->where(array('user_id' => $v_timer->user_id))->get('tbl_users')->row();
														if (!empty($user_info)) {
														?>
															<img class="h-8 w-8 rounded-full" src="<?= base_url() . $profile_info->avatar ?>" alt="">
														<?php } else {
															echo '-';
														} ?>
													</td>
													<td class="px-4 py-3">
														<span class="inline-flex rounded-full bg-emerald-50 px-2 py-0.5 text-xs text-emerald-700"><?= display_datetime($v_timer->start_time, true) ?></span>
													</td>
													<td class="px-4 py-3">
														<span class="inline-flex rounded-full bg-rose-50 px-2 py-0.5 text-xs text-rose-700"><?= display_datetime($v_timer->end_time, true); ?></span>
													</td>
													<td class="px-4 py-3">
														<a href="<?= base_url() ?>admin/projects/project_details/<?= $v_timer->project_id ?>" class="text-primary hover:underline">
															<?= html_escape($project_details->project_name) ?>
														</a>
														<?php
														if (!empty($v_timer->reason)) {
															$edit_user_info = $this->db->where(array('user_id' => $v_timer->edited_by))->get('tbl_users')->row();
															echo '<span class="ml-2 text-xs text-rose-500" data-tooltip="Reason : ' . $v_timer->reason . ' | Edited By : ' . $edit_user_info->username . '">Edited</span>';
														}
														?>
													</td>
													<td class="px-4 py-3">
														<span class="text-xs text-slate-500"><?= $this->items_model->get_time_spent_result($v_timer->end_time - $v_timer->start_time) ?></span>
													</td>
													<td class="px-4 py-3">
														<?php if (!empty($edited)) { ?>
															<?= btn_edit('admin/projects/project_details/' . $v_timer->project_id . '/timesheet/' . $v_timer->tasks_timer_id) ?>
															<?php if (!empty($deleted)) { ?>
																<?= ajax_anchor(base_url("admin/projects/update_project_timer/" . $v_timer->tasks_timer_id . '/delete_task_timmer'), "<i class='btn btn-danger btn-xs fa fa-trash-o'></i>", array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table-timesheet-" . $v_timer->tasks_timer_id)); ?>
															<?php } ?>
														<?php } ?>
													</td>
												</tr>
									<?php
											}
										}
									}
								} else {
									?>
									<tr>
										<td colspan="6" class="px-4 py-6 text-center text-sm text-slate-500"><?= lang('no_data_found') ?></td>
									</tr>
								<?php } ?>
							</tbody>
						</table>
					</div>
				</div>

				<div class="tab-pane <?= $time_active == 2 ? 'active' : ''; ?>" id="contact">
					<form enctype="multipart/form-data" id="form" action="<?= base_url(); ?>admin/projects/update_project_timer/<?= !empty($project_timer_info) ? $project_timer_info->tasks_timer_id : '' ?>" method="post" class="space-y-4">
						<?php
						if (!empty($project_timer_info)) {
							$start_date = date('Y-m-d', $project_timer_info->start_time);
							$start_time = date('H:i', $project_timer_info->start_time);
							$end_date = date('Y-m-d', $project_timer_info->end_time);
							$end_time = date('H:i', $project_timer_info->end_time);
						} else {
							$start_date = '';
							$start_time = '';
							$end_date = '';
							$end_time = '';
						}
						?>
						<div class="grid gap-4 md:grid-cols-2">
<!-- 
							<?php if ($this->session->userdata('user_type') == '1' && empty($project_timer_info->tasks_timer_id)) { ?>
								<div class="form-group margin">
									<div class="col-sm-8 center-block">
										<label class="control-label"><?= lang('select') . ' ' . lang('project') ?>
											<span class="required">*</span></label>
										<select class="form-control select_box" name="project_id" required="" style="width: 100%">
											<?php
											$all_tasks_info = $this->db->get('tbl_project')->result();
											if (!empty($all_tasks_info)) : foreach ($all_tasks_info as $v_task_info) :
											?>
													<option value="<?= $v_task_info->project_id ?>" <?= $v_task_info->project_id == $project_details->project_id ? 'selected' : null ?>>
														<?= $v_task_info->project_name ?></option>
												<?php endforeach; ?>
											<?php endif; ?>
										</select>
									</div>
								</div>
							<?php } else { ?>
								<input type="hidden" name="project_id" value="<?= $project_details->project_id ?>">
							<?php } ?> -->

							<?= form_datepicker('start_date', lang('start_date'), $start_date, true, array('data-auto-close' => true)) ?>
							<?= form_input_group('start_time', lang('start_time'), $start_time, 'time', true) ?>
							<?= form_datepicker('end_date', lang('end_date'), $end_date, true, array('data-auto-close' => true)) ?>
							<?= form_input_group('end_time', lang('end_time'), $end_time, 'time', true) ?>
						</div>
						<div>
							<?= form_textarea_group('reason', lang('reason'), !empty($project_timer_info->reason) ? $project_timer_info->reason : '', 3, false) ?>
						</div>
						<div class="flex justify-end">
							<button type="submit" class="inline-flex items-center rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white hover:bg-slate-800">
								<?= lang('save') ?>
							</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
