<?php
$office_hours = max(1, (int) config_item('office_hours'));
$current_user_id = (int) $this->session->userdata('user_id');
$is_inline_modal = !empty($inline_modal);
$return_url = !empty($return_url) ? $return_url : base_url('admin/leave_management/my_leave');

$user_options = array('' => lang('select') . ' ' . lang('users'));
$all_users = $this->db->where('role_id !=', 2)->get('tbl_users')->result();
if (!empty($all_users)) {
	foreach ($all_users as $v_users) {
		$profile = $this->db->where('user_id', $v_users->user_id)->get('tbl_account_details')->row();
		if (!empty($profile)) {
			$user_options[$profile->user_id] = $profile->fullname;
		}
	}
}

$leave_category_options = array('' => lang('select') . ' ' . lang('leave_category'));
$all_leave_category = $this->db->get('tbl_leave_category')->result();
if (!empty($all_leave_category)) {
	foreach ($all_leave_category as $v_category) {
		$leave_category_options[$v_category->leave_category_id] = $v_category->leave_category;
	}
}

$hours_options = array();
for ($i = 1; $i <= $office_hours; $i++) {
	$label = ($office_hours <= 9 ? '0' : '') . $i;
	$hours_options[$i] = $label;
}
$request_label = lang('leave_request');
if (empty($request_label) || $request_label === 'leave_request') {
	$request_label = lang('apply') . ' ' . lang('leave');
}

$leave_category_append = '';
$_created = can_action('122', 'created');
if (!empty($_created)) {
	$leave_category_append = form_lookup_add_link(
		'leave_category_id',
		base_url('admin/leave_management/leave_category'),
		lang('new') . ' ' . lang('leave_category'),
		'sm',
		array(
			'class' => 'inline-flex h-10 w-10 items-center justify-center rounded-r-lg border border-l-0 border-slate-300 bg-slate-50 text-slate-500 transition hover:bg-slate-100 hover:text-slate-700 dark:border-slate-600 dark:bg-slate-800 dark:text-slate-300 dark:hover:bg-slate-700 dark:hover:text-slate-100',
		)
	);
}

$summary_rows = array();
$total_taken = 0;
$total_quota = 0;
$leave_report = leave_report($current_user_id);
if (!empty($leave_report['leave_category'])) {
	foreach ($leave_report['leave_category'] as $index => $category_name) {
		$taken = $leave_report['leave_taken'][$index] ?? 0;
		$quota = $leave_report['leave_quota'][$index] ?? 0;
		$total_taken += $taken;
		$total_quota += $quota;
		$summary_rows[] = array(
			'label' => $category_name,
			'value' => $taken . '/' . $quota,
		);
	}
}
$summary_rows[] = array(
	'label' => lang('total'),
	'value' => $total_taken . '/' . $total_quota,
	'is_total' => true,
);
$summary_badge_value = $total_taken . '/' . $total_quota;

$render_leave_summary = static function ($title, array $rows) {
	ob_start();
	$badge_value = !empty($rows) ? end($rows)['value'] : '0/0';
	reset($rows);
?>
	<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
		<div class="border-b border-border px-4 py-3.5">
			<div class="flex items-start justify-between gap-3">
				<div class="min-w-0">
					<p class="text-xs font-semibold uppercase tracking-[0.14em] text-muted-foreground"><?= lang('leave_management') ?></p>
					<h3 class="mt-1 text-base font-semibold tracking-tight text-foreground"><?= html_escape($title) ?></h3>
				</div>
				<span class="inline-flex items-center rounded-lg bg-primary/15 px-2.5 py-1 text-xs font-semibold text-primary"><?= html_escape($badge_value) ?></span>
			</div>
		</div>
		<div class="px-4 py-1.5">
			<?php foreach ($rows as $row) { ?>
				<div class="py-3.5 <?= !empty($row['is_total']) ? 'border-t border-border' : '' ?>">
					<div class="flex items-center justify-between gap-4">
						<div class="min-w-0">
							<p class="text-sm font-semibold text-foreground"><?= html_escape($row['label']) ?></p>
							<?php if (empty($row['is_total'])) { ?>
								<p class="mt-1 text-[11px] font-medium uppercase tracking-[0.12em] text-muted-foreground"><?= lang('taken') . ' / ' . lang('quota') ?></p>
							<?php } ?>
						</div>
						<span class="<?= !empty($row['is_total']) ? 'inline-flex items-center rounded-lg border border-border bg-background px-2.5 py-1 text-sm font-semibold text-foreground' : 'text-sm font-semibold text-muted-foreground' ?>"><?= html_escape($row['value']) ?></span>
					</div>
					<?php if (empty($row['is_total'])) { ?>
						<div class="mt-2 h-[3px] rounded-full bg-background"></div>
					<?php } ?>
				</div>
			<?php } ?>
		</div>
	</div>
<?php
	return ob_get_clean();
};

?>
<form data-leave-application-form="true" data-parsley-validate="" novalidate="" action="<?= base_url() ?>admin/leave_management/save_leave_application" method="post" enctype="multipart/form-data" class="w-full max-w-none space-y-4 text-foreground">
	<div class="grid grid-cols-1 gap-5 lg:grid-cols-12 lg:items-start">
		<div class="min-w-0 space-y-4 lg:col-span-8">
			<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
				<div class="border-b border-border px-4 py-3.5">
					<div class="flex flex-wrap items-center justify-between gap-3">
						<div>
							<p class="text-xs font-semibold uppercase tracking-[0.14em] text-muted-foreground"><?= html_escape($request_label) ?></p>
							<p class="mt-1 text-sm font-medium text-foreground"><?= lang('all_required_fill') ?></p>
						</div>
						<span class="inline-flex items-center gap-2 rounded-full border border-amber-500/30 bg-amber-500/10 px-3 py-1 text-xs font-semibold text-amber-500 dark:text-amber-300">
							<i data-lucide="clock-3" class="h-3.5 w-3.5"></i>
							<span><?= lang('pending') . ' ' . lang('approval') ?></span>
						</span>
					</div>
				</div>
				<div class="space-y-5 px-4 py-4">
					<div class="grid gap-5 md:grid-cols-2">
						<div>
							<?= form_select_group(
								'user_id',
								lang('select') . ' ' . lang('users'),
								$user_options,
								$current_user_id,
								true,
								array(
									'id' => 'users_id',
									'class' => 'select_box',
									'style' => 'width: 100%',
								),
								'<i data-lucide="users" class="h-4 w-4"></i>',
								''
							); ?>
						</div>
						<div>
							<?= form_select_group(
								'leave_category_id',
								lang('leave_category'),
								$leave_category_options,
								'',
								true,
								array(
									'id' => 'leave_category',
									'class' => 'select_box',
									'style' => 'width: 100%',
								),
								'<i data-lucide="file-text" class="h-4 w-4"></i>',
								$leave_category_append
							); ?>
						</div>
					</div>

					<div class="space-y-2">
						<div class="text-sm font-medium text-foreground">
							<?= lang('duration') ?> <span class="text-red-500">*</span>
						</div>
						<div class="grid grid-cols-3 gap-1 rounded-xl border border-border bg-background p-1">
							<label for="leave_type_single_day" class="cursor-pointer">
								<input id="leave_type_single_day" type="radio" name="leave_type" value="single_day" class="peer sr-only" checked required>
								<span data-leave-type-tab="single_day" class="flex h-9 items-center justify-center rounded-lg border border-transparent px-3 text-sm font-semibold text-muted-foreground transition">
									<?= lang('single_day') ?>
								</span>
							</label>
							<label for="leave_type_multiple_days" class="cursor-pointer">
								<input id="leave_type_multiple_days" type="radio" name="leave_type" value="multiple_days" class="peer sr-only">
								<span data-leave-type-tab="multiple_days" class="flex h-9 items-center justify-center rounded-lg border border-transparent px-3 text-sm font-semibold text-muted-foreground transition">
									<?= lang('multiple_days') ?>
								</span>
							</label>
							<label for="leave_type_hours" class="cursor-pointer">
								<input id="leave_type_hours" type="radio" name="leave_type" value="hours" class="peer sr-only">
								<span data-leave-type-tab="hours" class="flex h-9 items-center justify-center rounded-lg border border-transparent px-3 text-sm font-semibold text-muted-foreground transition">
									<?= lang('hours') ?>
								</span>
							</label>
						</div>
					</div>

					<div id="single_day_section" data-leave-section="single_day">
						<?= form_datepicker(
							'single_day_start_date',
							lang('start_date'),
							'',
							true,
							array(
								'id' => 'single_day_start_date',
								'class' => 'datepicker',
								'placeholder' => 'dd / mm / yyyy',
								'data-format' => 'd/m/Y',
							),
							'',
							''
						); ?>
					</div>

					<div id="multiple_days_section" data-leave-section="multiple_days" class="hidden space-y-4" hidden aria-hidden="true">
						<div class="grid gap-5 md:grid-cols-2">
							<div>
								<?= form_datepicker(
									'multiple_days_start_date',
									lang('start_date'),
									'',
									true,
									array(
								'id' => 'multiple_days_start_date',
								'class' => 'start_date',
								'placeholder' => 'dd / mm / yyyy',
								'data-format' => 'd/m/Y',
								'disabled' => 'disabled',
							),
							'',
							''
						); ?>
							</div>
							<div>
								<?= form_datepicker(
									'multiple_days_end_date',
									lang('end_date'),
									'',
									true,
									array(
								'id' => 'multiple_days_end_date',
								'class' => 'end_date',
								'placeholder' => 'dd / mm / yyyy',
								'data-format' => 'd/m/Y',
								'disabled' => 'disabled',
							),
							'',
							''
						); ?>
							</div>
						</div>
					</div>

					<div id="hours_section" data-leave-section="hours" class="hidden" hidden aria-hidden="true">
						<div class="grid gap-5 md:grid-cols-[minmax(0,1fr)_12rem]">
							<div>
								<?= form_datepicker(
									'hours_start_date',
									lang('start_date'),
									'',
									true,
									array(
										'id' => 'hours_start_date',
										'class' => 'datepicker',
										'placeholder' => 'dd / mm / yyyy',
										'data-format' => 'd/m/Y',
										'disabled' => 'disabled',
									),
									'',
									''
								); ?>
							</div>
							<div>
								<?= form_select_group(
									'hours',
									lang('hours'),
									$hours_options,
									1,
									true,
									array(
										'id' => 'hours_value',
										'class' => 'select_box',
										'disabled' => 'disabled',
									),
									'',
									''
								); ?>
							</div>
						</div>
					</div>

					<div id="leave_validation_message" class="hidden rounded-2xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm font-medium text-rose-700"></div>

					<?= form_textarea_group(
						'reason',
						lang('reason'),
						'',
						6,
						false,
						array(
							'id' => 'reason',
							'placeholder' => 'Provide a reason for your leave request...',
							'class' => 'min-h-[160px]',
						)
					); ?>

					<?= custom_form_Fields(17, null); ?>
						<div class="space-y-3 border-t border-border pt-4">
							<?= form_attachment_group('files[]', lang('attachments'), 'dropzone', array(
								'dropzone_id' => 'attachements-files-dropzone',
								'list_id' => 'attachements-files-list',
								'comment' => true,
							)); ?>

							<div class="flex items-center justify-end gap-3">
							<?php if ($is_inline_modal) { ?>
								<button type="button" class="inline-flex items-center rounded-lg border border-border bg-background px-3.5 py-2 text-sm font-medium text-foreground transition hover:bg-card" data-dismiss="modal"><?= lang('close') ?></button>
							<?php } else { ?>
								<a href="<?= html_escape($return_url) ?>" class="inline-flex items-center rounded-lg border border-border bg-background px-3.5 py-2 text-sm font-medium text-foreground transition hover:bg-card"><?= lang('back') ?: lang('close') ?></a>
							<?php } ?>
								<button type="submit" id="file-save-button" class="inline-flex min-w-[8.5rem] items-center justify-center rounded-lg bg-primary px-4 py-2 text-sm font-semibold text-primary-foreground shadow-sm transition hover:opacity-90 focus:outline-none focus:ring-4 focus:ring-primary/20"><?= lang('submit') ?></button>
							</div>
						</div>
					</div>
				</div>
			</div>

		<div class="mt-5 lg:mt-0 lg:col-span-4 lg:self-start">
			<div id="leave_user_details" class="lg:sticky lg:top-6">
				<?= $render_leave_summary(lang('my_leave') . ' ' . lang('details'), $summary_rows) ?>
			</div>
		</div>
	</div>
</form>
