<?php
$office_hours = config_item('office_hours');
$leave_cate_info = $this->db->where('leave_category_id', $application_info->leave_category_id)->get('tbl_leave_category')->row();
$profile_info = $this->db->where('user_id', $application_info->user_id)->get('tbl_account_details')->row();
$approve_by = $this->db->where('user_id', $application_info->approve_by)->get('tbl_account_details')->row();

$status_labels = [
	'1' => ['label' => lang('pending'), 'classes' => 'bg-amber-100 text-amber-700'],
	'2' => ['label' => lang('approved'), 'classes' => 'bg-emerald-100 text-emerald-700'],
	'3' => ['label' => lang('rejected'), 'classes' => 'bg-rose-100 text-rose-700'],
];
$status_data = $status_labels[$application_info->application_status] ?? [
	'label' => lang('pending'),
	'classes' => 'bg-amber-100 text-amber-700',
];
$duration_display = $duration_display ?? '';
$attachments = [];
if (!empty($application_info->attachment)) {
	$attachments = json_decode($application_info->attachment);
}
$custom_fields = custom_form_label(17, $application_info->leave_application_id);
$my_details = MyDetails();
$designation_info = $this->application_model->check_by(['designations_id' => $my_details->designations_id], 'tbl_designations');
$dept_head = !empty($designation_info)
	? $this->application_model->check_by(['departments_id' => $designation_info->departments_id], 'tbl_departments')
	: null;
$show_status_controls = $this->session->userdata('user_type') == 1
	|| (!empty($dept_head) && $dept_head->department_head_id == $my_details->user_id);
?>

<div class="mx-auto max-w-5xl space-y-4">
	<div class="rounded-2xl bg-gradient-to-r from-sky-500 via-indigo-500 to-purple-500 p-[1px] shadow-lg">
		<div class="rounded-2xl bg-white p-4 dark:bg-slate-800">
			<div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
				<div>
					<p class="text-xs font-semibold uppercase tracking-[0.2em] text-sky-600 dark:text-sky-300">
						<?= lang('leave') . ' ' . lang('details') ?>
					</p>
					<p class="mt-1 text-lg font-semibold text-gray-900 dark:text-white">
						<?= html_escape($profile_info->fullname) ?>
					</p>
					<p class="text-sm text-gray-500 dark:text-gray-300">
						<?= display_date($application_info->leave_start_date) ?>
						<?php if ($application_info->leave_type == 'multiple_days' && !empty($application_info->leave_end_date)) : ?>
							<?= lang('leave_to') ?> <?= display_date($application_info->leave_end_date) ?>
						<?php endif; ?>
					</p>
				</div>
				<div class="flex flex-wrap items-center gap-2">
					<div class="rounded-lg bg-white/30 px-3 py-1 text-xs font-semibold text-white">
						<?= lang('start_date') ?>: <?= display_date($application_info->leave_start_date) ?>
					</div>
					<?php if ($application_info->leave_type == 'multiple_days' && !empty($application_info->leave_end_date)) : ?>
						<div class="rounded-lg bg-white/30 px-3 py-1 text-xs font-semibold text-white">
							<?= lang('end_date') ?>: <?= display_date($application_info->leave_end_date) ?>
						</div>
					<?php endif; ?>
					<span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold text-white bg-white/20">
						<?= lang($application_info->leave_type) ?>
					</span>
				</div>
			</div>
		</div>
	</div>

	<div class="grid grid-cols-1 gap-4 lg:grid-cols-12">
		<div class="space-y-4 lg:col-span-4">
			<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
				<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
					<?= lang('employee') ?>
				</p>
				<p class="mt-2 text-sm font-medium text-gray-800 dark:text-gray-100">
					<?= html_escape($profile_info->fullname) ?>
					<?php if (!empty($profile_info->employment_id)) : ?>
						(<?= html_escape($profile_info->employment_id) ?>)
					<?php endif; ?>
				</p>
			</div>

			<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
				<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
					<?= lang('leave_category') ?>
				</p>
				<p class="mt-1 text-sm font-medium text-gray-800 dark:text-gray-100">
					<?= html_escape($leave_cate_info->leave_category ?? '') ?>
				</p>
			</div>

			<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
				<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
					<?= lang('duration') ?>
				</p>
				<p class="mt-1 text-sm font-medium text-gray-800 dark:text-gray-100"><?= $duration_display ?></p>
			</div>

			<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
				<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
					<?= lang('applied_on') ?>
				</p>
				<p class="mt-1 text-sm font-medium text-gray-800 dark:text-gray-100">
					<?= display_date($application_info->application_date) ?> <?= lang('at') ?> <?= display_time($application_info->application_date) ?>
				</p>
			</div>

			<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
				<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
					<?= lang('status') ?>
				</p>
				<span class="mt-2 inline-flex items-center rounded px-2 py-1 text-xs <?= $status_data['classes'] ?>">
					<?= $status_data['label'] ?>
				</span>
			</div>

			<?php if (!empty($approve_by)) : ?>
				<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
					<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
						<?= lang('approved_by') ?>
					</p>
					<p class="mt-1 text-sm font-medium text-gray-800 dark:text-gray-100">
						<?= html_escape($approve_by->fullname) ?>
					</p>
				</div>
			<?php endif; ?>
		</div>

		<div class="space-y-4 lg:col-span-8">
			<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
				<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
					<?= lang('attachment') ?>
				</p>
				<?php if (!empty($attachments)) : ?>
					<ul class="mt-3 grid grid-cols-1 gap-3 sm:grid-cols-2">
						<?php foreach ($attachments as $fKey => $file) :
							if (empty($file)) {
								continue;
							}
						?>
							<li class="rounded-lg border border-gray-200 bg-gray-50 p-3 dark:border-gray-700 dark:bg-gray-900">
								<div class="flex items-center gap-3">
									<?php if (!empty($file->is_image)) : ?>
										<img src="<?= base_url($file->path) ?>" class="h-12 w-12 rounded-md object-cover ring-1 ring-gray-200 dark:ring-gray-600">
									<?php else : ?>
										<div class="flex h-12 w-12 items-center justify-center rounded-md bg-gray-200 text-xs font-semibold text-gray-700 dark:bg-gray-700 dark:text-gray-100">
											FILE
										</div>
									<?php endif; ?>
									<div class="min-w-0 flex-1">
										<a href="<?= base_url() ?>admin/leave_management/download_files/<?= $application_info->leave_application_id . '/' . $fKey ?>" class="block truncate text-sm font-semibold text-indigo-600 hover:underline dark:text-indigo-300">
											<?= $file->fileName ?>
										</a>
										<div class="mt-1 flex items-center justify-between text-xs text-gray-500 dark:text-gray-400">
											<span><?= $file->size ?> <?= lang('kb') ?></span>
											<a href="<?= base_url() ?>admin/leave_management/download_files/<?= $application_info->leave_application_id . '/' . $fKey ?>" class="font-semibold text-indigo-600 dark:text-indigo-300">
												<?= lang('download') ?>
											</a>
										</div>
									</div>
								</div>
							</li>
						<?php endforeach; ?>
					</ul>
				<?php else : ?>
					<p class="mt-3 text-sm text-gray-500 dark:text-gray-400">No attachments available.</p>
				<?php endif; ?>
			</div>

			<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
				<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
					<?= lang('reason') ?>
				</p>
				<div class="mt-3 rounded-lg border-l-4 border-emerald-500 bg-emerald-50 p-3 text-sm leading-6 text-gray-700 dark:border-emerald-400 dark:bg-emerald-900/20 dark:text-gray-200">
					<?= nl2br($application_info->reason) ?>
				</div>
			</div>

			<?php if (!empty($application_info->comments)) : ?>
				<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
					<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
						<?= lang('comments') ?>
					</p>
					<div class="mt-3 rounded-lg border-l-4 border-indigo-500 bg-indigo-50 p-3 text-sm leading-6 text-gray-700 dark:border-indigo-400 dark:bg-indigo-900/20 dark:text-gray-200">
						<?= nl2br($application_info->comments) ?>
					</div>
				</div>
			<?php endif; ?>

			<?php if (!empty($custom_fields)) : ?>
				<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
					<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
						<?= lang('custom_fields') ?>
					</p>
					<div class="mt-3 divide-y divide-gray-200 dark:divide-gray-700">
						<?php foreach ($custom_fields as $c_label => $v_fields) :
							if (empty($v_fields)) {
								continue;
							}
						?>
							<div class="grid grid-cols-12 gap-2 py-2">
								<div class="col-span-5 text-sm font-medium text-gray-700 dark:text-gray-300">
									<?= $c_label ?>
								</div>
								<div class="col-span-7 text-sm text-gray-700 dark:text-gray-100">
									<?= $v_fields ?>
								</div>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>

			<?php if ($show_status_controls) : ?>
				<div class="rounded-xl border border-gray-200 bg-white p-4 shadow-sm dark:border-gray-700 dark:bg-slate-800">
					<p class="text-xs font-semibold uppercase tracking-wide text-gray-500 dark:text-gray-400">
						<?= lang('change') . ' ' . lang('status') ?>
					</p>
					<div class="mt-3 flex flex-wrap gap-2">
						<?php if ($application_info->application_status == '1') : ?>
							<?= btn_modal(
								'admin/leave_management/change_status/2/' . $application_info->leave_application_id,
								lang('approved'),
								lang('approved'),
								'md',
								'check',
								[
									'class' => 'inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-emerald-500',
								],
							); ?>
							<?= btn_modal(
								'admin/leave_management/change_status/3/' . $application_info->leave_application_id,
								lang('reject'),
								lang('reject'),
								'md',
								'x',
								[
									'class' => 'inline-flex items-center gap-2 rounded-lg bg-rose-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-rose-500',
								],
							); ?>
						<?php elseif ($application_info->application_status == '2') : ?>
							<?= btn_modal(
								'admin/leave_management/change_status/1/' . $application_info->leave_application_id,
								lang('pending'),
								lang('pending'),
								'md',
								'clock',
								[
									'class' => 'inline-flex items-center gap-2 rounded-lg bg-amber-500 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-amber-400',
								],
							); ?>
							<?= btn_modal(
								'admin/leave_management/change_status/3/' . $application_info->leave_application_id,
								lang('reject'),
								lang('reject'),
								'md',
								'x',
								[
									'class' => 'inline-flex items-center gap-2 rounded-lg bg-rose-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-rose-500',
								],
							); ?>
						<?php elseif ($application_info->application_status == '3') : ?>
							<?= btn_modal(
								'admin/leave_management/change_status/2/' . $application_info->leave_application_id,
								lang('approved'),
								lang('approved'),
								'md',
								'check',
								[
									'class' => 'inline-flex items-center gap-2 rounded-lg bg-emerald-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-emerald-500',
								],
							); ?>
							<?= btn_modal(
								'admin/leave_management/change_status/1/' . $application_info->leave_application_id,
								lang('pending'),
								lang('pending'),
								'md',
								'clock',
								[
									'class' => 'inline-flex items-center gap-2 rounded-lg bg-amber-500 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-amber-400',
								],
							); ?>
						<?php endif; ?>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>

