<?php
$edited = can_action('57', 'edited');
$deleted = can_action('57', 'deleted');

$time_active = 1;

$new_project_html = form_lookup_add_link(
	'project_id',
	base_url('admin/projects/new_project'),
	lang('create') . ' ' . lang('project'),
);
$selected_project_id = !empty($project_details->project_id) ? $project_details->project_id : $projId;
$selected_project_label = '';
if (!empty($selected_project_id)) {
	$project_row = $this->db->where('project_id', $selected_project_id)->get('tbl_project')->row();
	if (!empty($project_row)) {
		$selected_project_label = $project_row->project_name;
	}
}

?>

<div class="panel panel-custom">
    <div class="modal-body wrap-modal wrap">
        <div class="p-6">

			<?php
			if (!empty($project_timer_info)) {
				$start_datetime = date("Y-m-d H:i", $project_timer_info->start_time);
				$end_datetime   = date("Y-m-d H:i", $project_timer_info->end_time);
			} else {
				$start_datetime = $end_datetime = '';
			}
			?>

            <form method="post" action="<?= base_url("admin/projects/update_project_timer/" .
							(!empty($project_timer_info) ? $project_timer_info->tasks_timer_id : '')) ?>" class="space-y-4">

                <div class="grid gap-4 md:grid-cols-2">
                    <?= form_lookup_group('project_id', lang('projects'), $selected_project_id, $selected_project_label, true, array(), '', array('type' => 'project', 'append' => $new_project_html)); ?>

                    <?= form_datepicker("start_datetime", lang("start_date") . ' & ' . lang("start_time"), $start_datetime, true, array(
                        'id' => 'project-start-datetime',
                        'data-type' => 'datetime',
                        'data-format' => 'Y-m-d H:i',
                        'data-max' => '#project-end-datetime',
                        'data-auto-close' => 'true',
                        'data-today-btn' => 'false',
                    )) ?>
                    <?= form_datepicker("end_datetime", lang("end_date") . ' & ' . lang("end_time"), $end_datetime, true, array(
                        'id' => 'project-end-datetime',
                        'data-type' => 'datetime',
                        'data-format' => 'Y-m-d H:i',
                        'data-min' => '#project-start-datetime',
                        'data-auto-close' => 'true',
                        'data-today-btn' => 'false',
                    )) ?>
                </div>

                <?= form_textarea_group(
					"reason",
					lang("reason"),
					!empty($project_timer_info->reason) ? $project_timer_info->reason : '',
					3,
					false
				) ?>

                <div class="flex justify-end">
                    <button
                        class="rounded-lg bg-slate-900 px-4 py-2 text-sm font-semibold text-white hover:bg-slate-800 dark:bg-slate-600">
                        <?= lang("save") ?>
                    </button>
                </div>
            </form>
        </div>

    </div>
</div>


<script>
(function() {

    const buttons = document.querySelectorAll(".tab-btn");
    const general = document.getElementById("general");
    const contact = document.getElementById("contact");

    function showTab(tab) {

        // Hide both
        general.classList.add("hidden");
        contact.classList.add("hidden");

        // Show active
        if (tab === "general") general.classList.remove("hidden");
        if (tab === "contact") contact.classList.remove("hidden");

        // Button active styles
        buttons.forEach(btn => {
            btn.classList.remove("bg-slate-900", "text-white");
            btn.classList.add("text-slate-600", "hover:bg-slate-100");
        });

        // Activate current button
        document.querySelector(`[data-tab="${tab}"]`)
            .classList.add("bg-slate-900", "text-white");

        history.replaceState(null, null, "#" + tab);
    }

    // Click event
    buttons.forEach(btn => {
        btn.addEventListener("click", function(e) {
            e.preventDefault();
            showTab(this.dataset.tab);
        });
    });

})();
</script>
