<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<div class="rounded-2xl border border-border bg-card shadow-sm dark:border-slate-700 dark:bg-slate-900">
	<div class="flex items-center justify-between border-b border-border px-5 py-4 dark:border-slate-700">
		<h4 class="text-sm font-semibold text-foreground"><i class="mr-2" data-lucide="calendar"></i><?= lang('Overtime_details') . ' ' . $year; ?></h4>
		<div class="hidden-print">
			<?php echo btn_pdf('admin/user/overtime_report_pdf/' . $year . '/' . $profile_info->user_id); ?>
		</div>
	</div>

	<div class="p-5 space-y-4">
		<form id="attendance-form" role="form" enctype="multipart/form-data" action="<?php echo base_url(); ?>admin/user/user_details/<?= $profile_info->user_id ?>/Overtime_details" method="post" class="rounded-xl border border-border bg-muted/30 p-4 dark:border-slate-700 dark:bg-slate-800/60">
			<div class="grid grid-cols-1 gap-2 md:grid-cols-[minmax(0,220px)_auto] md:items-end">
				<?= form_datepicker('overtime_year', lang('year'), !empty($overtime_year) ? $overtime_year : date('Y'), true,  array(
					'id' => 'overtime_year',
					'data-type' => 'month',
					'data-format' => 'Y-m',
					'data-auto-close' => 'true',
				)); ?>
				<div class="flex items-end">
					<button type="submit" id="sbtn" class="inline-flex h-9 items-center justify-center gap-2 rounded-lg bg-indigo-600 px-4 text-xs font-semibold text-white shadow-sm transition hover:bg-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-400/40">
						<i data-lucide="arrow-right" class="h-4 w-4"></i>
						<?= lang('go') ?>
					</button>
				</div>
			</div>
		</form>

		<div class="overflow-x-auto rounded-xl border border-border bg-card dark:border-slate-700 dark:bg-slate-900">
			<table class="w-full border-collapse text-sm">
				<thead class="bg-muted/50 text-xs uppercase tracking-[0.14em] text-muted-foreground dark:bg-slate-800/80 dark:text-slate-400">
					<tr>
						<th class="border-b border-border px-4 py-3 text-left font-semibold dark:border-slate-700"><?= lang('overtime_date') ?></th>
						<th class="border-b border-border px-4 py-3 text-left font-semibold dark:border-slate-700"><?= lang('overtime_hour') ?></th>
						<th class="border-b border-border px-4 py-3 text-left font-semibold dark:border-slate-700"><?= lang('status') ?></th>
					</tr>
				</thead>
				<tbody>
					<?php
					$hh = 0;
					$mm = 0;
					$has_rows = false;
					?>
					<?php if (!empty($all_overtime_info)) : ?>
						<?php foreach ($all_overtime_info as $key => $v_overtime_info) : ?>
							<?php if (!empty($v_overtime_info)) : ?>
								<?php foreach ($v_overtime_info as $v_overtime) : ?>
									<?php $has_rows = true; ?>
									<?php
									if ($v_overtime->status == 'pending') {
										$status = '<span class="inline-flex items-center rounded-full bg-amber-100 px-2 py-0.5 text-xs font-semibold text-amber-700">' . lang($v_overtime->status) . '</span>';
									} elseif ($v_overtime->status == 'approved') {
										$status = '<span class="inline-flex items-center rounded-full bg-emerald-100 px-2 py-0.5 text-xs font-semibold text-emerald-700">' . lang($v_overtime->status) . '</span>';
									} else {
										$status = '<span class="inline-flex items-center rounded-full bg-rose-100 px-2 py-0.5 text-xs font-semibold text-rose-700">' . lang($v_overtime->status) . '</span>';
									}
									?>
                                <tr class="border-t border-border dark:border-slate-700">
                                    <td class="px-4 py-3 text-foreground dark:text-slate-100"><?= display_date($v_overtime->overtime_date) ?></td>
                                    <td class="px-4 py-3 font-medium text-foreground dark:text-slate-100"><?php echo $v_overtime->overtime_hours; ?></td>
                                    <td class="px-4 py-3">
                                        <div class="flex items-center gap-2">
                                            <?= $status ?>

                                            <?php
                                            if ($this->session->userdata('user_type') == 1) {
                                                if ($v_overtime->status == 'pending') { ?>
                                                    <form action="<?= base_url('admin/utilities/change_overtime_status') ?>" method="post" class="m-0">
                                                        <input type="hidden" name="status" value="approved">
                                                        <input type="hidden" name="overtime_id" value="<?= $v_overtime->overtime_id; ?>">
                                                        <button data-toggle="tooltip" data-placment="top" title="<?= lang('approved') ?>" type="submit" class="inline-flex h-8 w-8 items-center justify-center rounded-md border border-emerald-200 bg-emerald-50 text-emerald-700 hover:bg-emerald-100"><i data-lucide="check" class="h-4 w-4"></i></button>
                                                    </form>
                                                    <form action="<?= base_url('admin/utilities/change_overtime_status') ?>" method="post" class="m-0">
                                                        <input type="hidden" name="status" value="rejected">
                                                        <input type="hidden" name="overtime_id" value="<?= $v_overtime->overtime_id; ?>">
                                                        <button data-toggle="tooltip" data-placment="top" title="<?= lang('reject') ?>" type="submit" class="inline-flex h-6 w-6 items-center justify-center rounded-md border border-rose-200 bg-rose-50 text-rose-700 hover:bg-rose-100"><i data-lucide="x" class="h-4 w-4"></i></button>
                                                    </form>
                                                <?php } elseif ($v_overtime->status == 'rejected') { ?>
                                                    <form action="<?= base_url('admin/utilities/change_overtime_status') ?>" method="post" class="m-0">
                                                        <input type="hidden" name="status" value="approved">
                                                        <input type="hidden" name="overtime_id" value="<?= $v_overtime->overtime_id; ?>">
                                                        <button data-toggle="tooltip" data-placment="top" title="<?= lang('approved') ?>" type="submit" class="inline-flex h-8 w-8 items-center justify-center rounded-md border border-emerald-200 bg-emerald-50 text-emerald-700 hover:bg-emerald-100"><i data-lucide="check" class="h-4 w-4"></i></button>
                                                    </form>
                                                <?php } elseif ($v_overtime->status == 'approved') { ?>
                                                    <form action="<?= base_url('admin/utilities/change_overtime_status') ?>" method="post" class="m-0">
                                                        <input type="hidden" name="status" value="rejected">
                                                        <input type="hidden" name="overtime_id" value="<?= $v_overtime->overtime_id; ?>">
                                                        <button data-toggle="tooltip" data-placment="top" title="<?= lang('reject') ?>" type="submit" class="inline-flex h-6 w-6 items-center justify-center rounded-md border border-rose-200 bg-rose-50 text-rose-700 hover:bg-rose-100"><i data-lucide="x" class="h-4 w-4"></i></button>
                                                    </form>
                                                <?php }
                                            }
                                            ?>
                                        </div>
                                    </td>
										<?php
										$overtime_hours = (string) $v_overtime->overtime_hours;
										if (strpos($overtime_hours, ':') !== false) {
											$time_parts = explode(':', $overtime_hours);
											$hh += (int) $time_parts[0];
											$mm += isset($time_parts[1]) ? (int) $time_parts[1] : 0;
										} else {
											$hh += (float) $overtime_hours;
										}
										?>
									</tr>
								<?php endforeach; ?>
							<?php endif; ?>
						<?php endforeach; ?>
					<?php endif; ?>
					<?php if (!$has_rows) { ?>
						<tr>
							<td colspan="3" class="px-4 py-8 text-center text-sm text-muted-foreground dark:text-slate-400"><?= lang('no_data_found') ?></td>
						</tr>
					<?php } ?>
                    <tr class="border-t border-border bg-indigo-50 dark:border-slate-700 dark:bg-indigo-950/30">
                        <td colspan="2" class="px-4 py-3 text-right font-semibold text-indigo-950 dark:text-indigo-200">
                            <?= lang('total_overtime_hour') ?> :
                        </td>
                        <td class="px-4 py-3 font-semibold text-indigo-950 dark:text-indigo-200">
                            <?php
                            if ($mm > 59) {
                                $hh += intval($mm / 60);
								$mm = intval($mm % 60);
							}
							$total_hh = str_pad((string) $hh, 2, '0', STR_PAD_LEFT);
							$total_mm = str_pad((string) $mm, 2, '0', STR_PAD_LEFT);
							echo $total_hh . " : " . $total_mm . " m";
							?>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
</div>
