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

<div class="space-y-4">
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="flex items-center justify-between border-b border-border px-5 py-4">
			<h4 class="text-sm font-semibold text-foreground"><?= lang('timecard_details') ?></h4>
			<?php if (!empty($attendace_info)) { ?>
				<div><?php echo btn_pdf('admin/user/timecard_details_pdf/' . $profile_info->user_id . '/' . date('Y-n', safe_strtotime($date))); ?></div>
			<?php } ?>
		</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 ?>/timecard_details" method="post" class="rounded-xl border border-border bg-muted/30 p-4">
				<div class="grid grid-cols-1 gap-2 md:grid-cols-[minmax(0,260px)_auto] md:items-end">
					<?= form_datepicker(
						'date',
						lang('month'),
						!empty($date) ? date('Y-m', safe_strtotime($date)) : '',
						true,
						array(
							'id' => 'date',
							'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-10 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>

			<?php if (!empty($attendace_info)) : ?>
				<div class="rounded-xl border border-border bg-card">
					<div class="border-b border-border px-4 py-3">
						<h5 class="text-sm font-semibold text-foreground"><?= lang('works_hours_deatils') . ' ' . date('F-Y', safe_strtotime($date)); ?></h5>
					</div>
					<div class="p-4 space-y-4">
						<?php foreach ($attendace_info as $week => $v_attndc_info) : ?>
							<div class="overflow-hidden rounded-lg border border-border bg-card">
								<div class="border-b border-border px-4 py-2">
									<h6 class="text-sm font-semibold text-foreground"><?= lang('week') ?> : <?php echo $week; ?></h6>
								</div>
								<div class="p-4 space-y-3">
									<div class="overflow-x-auto rounded-lg border border-border">
										<table class="w-full border-collapse text-sm">
											<thead class="bg-muted/50 text-xs uppercase tracking-[0.14em] text-muted-foreground">
												<tr>
													<?php
													if (!empty($v_attndc_info)) : foreach ($v_attndc_info as $date => $attendace) :
															$total_hour = 0;
															$total_minutes = 0;
													?>
															<th class="border-b border-border px-4 py-3 text-left font-semibold"><?= display_date($date) ?></th>
														<?php endforeach; ?>
													<?php endif; ?>
												</tr>
											</thead>
											<tbody>
												<tr>
													<?php
													if (!empty($v_attndc_info)) : foreach ($v_attndc_info as $date => $v_attendace) :
															$total_hh = 0;
															$total_mm = 0;
													?>
															<?php
															if (!empty($v_attendace)) {
																foreach ($v_attendace as $v_attandc) {
																	if (!is_object($v_attandc) || !isset($v_attandc->attendance_status)) {
																		continue;
																	}
																	if ($v_attandc->attendance_status == 1) {
																		$startdatetime = safe_strtotime($v_attandc->date_in . " " . $v_attandc->clockin_time);
																		$enddatetime = safe_strtotime($v_attandc->date_out . " " . $v_attandc->clockout_time);
																		$difference = $enddatetime - $startdatetime;
																		$years = abs(floor($difference / 31536000));
																		$days = abs(floor(($difference - ($years * 31536000)) / 86400));
																		$hours = abs(floor(($difference - ($years * 31536000) - ($days * 86400)) / 3600));
																		$mins = abs(floor(($difference - ($years * 31536000) - ($days * 86400) - ($hours * 3600)) / 60));
																		$total_mm += $mins;
																		$total_hh += $hours;
																	} elseif ($v_attandc->attendance_status == 'H') {
																		$holiday = 1;
																	} elseif ($v_attandc->attendance_status == '3') {
																		$leave = 1;
																	} elseif ($v_attandc->attendance_status == '0') {
																		$absent = 1;
																	}
																}
															}
															?>
															<td class="border-t border-border px-4 py-3 text-sm text-foreground">
																<?php
																if ($total_mm > 59) {
																	$total_hh += intval($total_mm / 60);
																	$total_mm = intval($total_mm % 60);
																}
																$total_hour += $total_hh;
																$total_minutes += $total_mm;
																if ($total_hh != 0 || $total_mm != 0) {
																	echo '<span class="font-medium text-foreground">' . $total_hh . " : " . $total_mm . " m" . '</span>';
																} elseif (!empty($holiday)) {
																	echo '<span class="inline-flex items-center rounded-full bg-sky-100 px-2 py-0.5 text-xs font-semibold text-sky-700">' . lang('holiday') . '</span>';
																} elseif (!empty($leave)) {
																	echo '<span class="inline-flex items-center rounded-full bg-amber-100 px-2 py-0.5 text-xs font-semibold text-amber-700">' . lang('on_leave') . '</span>';
																} elseif (!empty($absent)) {
																	echo '<span class="inline-flex items-center rounded-full bg-rose-100 px-2 py-0.5 text-xs font-semibold text-rose-700">' . lang('absent') . '</span>';
																} else {
																	echo '<span class="text-muted-foreground">' . $total_hh . " : " . $total_mm . " m" . '</span>';
																}
																?>
															</td>
													<?php
															$holiday = NULL;
															$leave = NULL;
															$absent = NULL;
														endforeach;
													endif;
													?>
												</tr>
											</tbody>
										</table>
									</div>
									<div class="flex items-center justify-end rounded-lg border border-indigo-100 bg-indigo-50 px-4 py-3 text-sm text-indigo-950">
										<span class="mr-2 font-semibold"><?= lang('total_working_hour') ?>:</span>
										<span class="font-medium">
											<?php
											if ($total_minutes >= 60) {
												$total_hour += intval($total_minutes / 60);
												$total_minutes = intval($total_minutes % 60);
											}
											echo $total_hour . " : " . $total_minutes . " m";
											?>
										</span>
									</div>
								</div>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php else : ?>
				<div class="rounded-xl border border-dashed border-border bg-muted/20 px-5 py-10 text-center text-sm text-muted-foreground">
					<?= lang('no_data_found') ?>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>
