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

<div class="space-y-6">
    <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
        <div>
            <h3 class="text-base font-semibold text-slate-900 dark:text-white"><?= lang('timesheet') ?></h3>
            <p class="text-sm text-slate-500 dark:text-slate-400"><?= lang('task') . ' #' . $task_details->task_id; ?></p>
        </div>
        <?php if (!empty($edited)) { ?>
            <div class="inline-flex rounded-lg border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 overflow-hidden">
                <a href="<?= base_url('admin/tasks/details/' . $task_details->task_id . '/timesheets') ?>" class="px-4 py-2 text-xs font-semibold <?= $time_active == 1 ? 'bg-indigo-600 text-white' : 'text-slate-600 dark:text-slate-300' ?>">
                    <?= lang('timesheet') ?>
                </a>
                <a href="<?= base_url('admin/tasks/details/' . $task_details->task_id . '/timesheets/' . ($tasks_timer_info->tasks_timer_id ?? '')) ?>" class="px-4 py-2 text-xs font-semibold <?= $time_active == 2 ? 'bg-indigo-600 text-white' : 'text-slate-600 dark:text-slate-300' ?>">
                    <?= lang('manual_entry') ?>
                </a>
            </div>
        <?php } ?>
    </div>

    <?php if ($time_active == 1) { ?>
        <div class="overflow-hidden rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-50 dark:bg-slate-800 text-slate-600 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('task_name') ?></th>
                        <th class="px-4 py-3 text-left"><?= lang('time_spend') ?></th>
                        <th class="px-4 py-3 text-right"><?= lang('action') ?></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200 dark:divide-slate-800">
                    <?php
                    $total_timer = get_result('tbl_tasks_timer', array('task_id' => $task_details->task_id, 'start_time !=' => 0, 'end_time !=' => 0));
                    if (!empty($total_timer)) {
                        foreach ($total_timer as $v_tasks) {
                            $task_info = get_row('tbl_task', array('task_id' => $v_tasks->task_id));
                            if (!empty($task_info)) {
                                $profile_info = $this->db->where(array('user_id' => $v_tasks->user_id))->get('tbl_account_details')->row();
                                $user_info = $this->db->where(array('user_id' => $v_tasks->user_id))->get('tbl_users')->row();
                                ?>
                                <tr id="table_tasks_timer-<?= $v_tasks->tasks_timer_id ?>">
                                    <td class="px-4 py-3">
                                        <div class="flex items-center gap-2">
                                            <?php if (!empty($profile_info->avatar)) { ?>
                                                <img src="<?= base_url() . $profile_info->avatar ?>" class="h-8 w-8 rounded-full" alt="avatar">
                                            <?php } ?>
                                            <span class="text-sm text-slate-700 dark:text-slate-200"><?= !empty($user_info) ? ucfirst($user_info->username) : '-' ?></span>
                                        </div>
                                    </td>
                                    <td class="px-4 py-3 text-slate-600 dark:text-slate-300">
                                        <?= display_date( $v_tasks->start_time) . ' ' . display_time($v_tasks->start_time, true) ?>
                                    </td>
                                    <td class="px-4 py-3 text-slate-600 dark:text-slate-300">
                                        <?= display_date( $v_tasks->end_time) . ' ' . display_time($v_tasks->end_time, true) ?>
                                    </td>
                                    <td class="px-4 py-3">
                                        <a href="<?= base_url() ?>admin/tasks/details/<?= $v_tasks->task_id ?>" class="text-indigo-600 dark:text-indigo-400 font-medium">
                                            <?= $task_info->task_name ?>
                                        </a>
                                        <?php if (!empty($v_tasks->reason)) {
                                            $edit_user_info = $this->db->where(array('user_id' => $v_tasks->edited_by))->get('tbl_users')->row();
                                            echo '<span class="ml-2 text-xs text-rose-500" data-tooltip="Reason: ' . $v_tasks->reason . ' | Edited by: ' . $edit_user_info->username . '">Edited</span>';
                                        } ?>
                                    </td>
                                    <td class="px-4 py-3 text-slate-600 dark:text-slate-300">
                                        <?= $this->tasks_model->get_time_spent_result($v_tasks->end_time - $v_tasks->start_time) ?>
                                    </td>
                                    <td class="px-4 py-3 text-right space-x-2">
                                        <?= btn_edit('admin/tasks/details/' . $v_tasks->task_id . '/timesheets/' . $v_tasks->tasks_timer_id) ?>
                                        <?php if ($v_tasks->user_id == $this->session->userdata('user_id')) { ?>
                                            <?php echo ajax_anchor(base_url("admin/tasks/update_tasks_timer/" . $v_tasks->tasks_timer_id . '/delete_task_timmer'), lang('delete'), array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table_tasks_timer-" . $v_tasks->tasks_timer_id)); ?>
                                        <?php } ?>
                                    </td>
                                </tr>
                                <?php
                            }
                        }
                    }
                    ?>
                </tbody>
            </table>
        </div>
    <?php } ?>

    <?php if ($time_active == 2 && !empty($edited)) { ?>
        <div class="rounded-2xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 p-6">
            <form enctype="multipart/form-data" action="<?php echo base_url(); ?>admin/tasks/update_tasks_timer/<?php if (!empty($tasks_timer_info)) echo $tasks_timer_info->tasks_timer_id; ?>" method="post" class="space-y-4">
                <?php
                if (!empty($tasks_timer_info)) {
                    $start_date = date('Y-m-d', $tasks_timer_info->start_time);
                    $start_time = date('H:i', $tasks_timer_info->start_time);
                    $end_date = date('Y-m-d', $tasks_timer_info->end_time);
                    $end_time = date('H:i', $tasks_timer_info->end_time);
                } else {
                    $start_date = '';
                    $start_time = '';
                    $end_date = '';
                    $end_time = '';
                }
                ?>
                <?php if ($this->session->userdata('user_type') == '1' && empty($tasks_timer_info->tasks_timer_id)) { ?>
                    <?php
                    $task_options = [];
                    $all_tasks_info = $this->db->get('tbl_task')->result();
                    if (!empty($all_tasks_info)) {
                        foreach ($all_tasks_info as $v_task_info) {
                            $task_options[$v_task_info->task_id] = $v_task_info->task_name;
                        }
                    }
                    ?>
                    <?= form_select_group('task_id', lang('select') . ' ' . lang('tasks'), $task_options, $task_details->task_id, true); ?>
                <?php } else { ?>
                    <input type="hidden" name="task_id" value="<?= $task_details->task_id ?>">
                <?php } ?>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                    <?= form_datepicker('start_date', lang('start_date'), $start_date, false, ['data-format' => config_item('date_picker_format')]); ?>
                    <?= form_timepicker('start_time', lang('start_time'), $start_time); ?>
                    <?= form_datepicker('end_date', lang('end_date'), $end_date, false, ['data-format' => config_item('date_picker_format')]); ?>
                    <?= form_timepicker('end_time', lang('end_time'), $end_time); ?>
                </div>

                <?= form_textarea('reason', lang('edit_reason'), !empty($tasks_timer_info->reason) ? $tasks_timer_info->reason : '', 4, true); ?>

                <div class="flex justify-end">
                    <button type="submit" class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500">
                        <i data-lucide="save" class="h-4 w-4"></i>
                        <?= lang('updates') ?>
                    </button>
                </div>
            </form>
        </div>
    <?php } ?>
</div>
