<?= message_box('success'); ?>
<?= message_box('error'); ?>
<script type="text/javascript">
	$(document).ready(function() {
		$(".different_time_input").attr('disabled', 'disabled');
		$(".different_time_hours").hide();

		function switchOfficeTime(mode) {
			if (mode === 'same_time') {
				$("#same_time").prop('checked', true);
				$("#different_time").prop('checked', false);
				$(".same_time").show();
				$(".different_time").hide();
				$(".different_time_input").attr('disabled', 'disabled');
				$(".different_time_hours").hide();
			} else if (mode === 'different_time') {
				$("#different_time").prop('checked', true);
				$("#same_time").prop('checked', false);
				$(".different_time").show();
				$(".same_time").hide();
				$(".different_time_input").removeAttr('disabled');
				syncDifferentTimeRows();
			}
		}

		function syncDifferentTimeRows() {
			$(".different_time_input").each(function() {
				var dayId = $(this).val();
				if (!dayId) return;
				if ($(this).is(":checked")) {
					$("#different_time_" + dayId).show();
					$(".different_time_hours_" + dayId).removeClass('disabled').removeAttr('disabled');
				} else {
					$("#different_time_" + dayId).hide();
					$(".different_time_hours_" + dayId).addClass('disabled').attr('disabled', 'disabled');
				}
			});
		}

		$("#same_time").on("change", function() {
			if ($(this).is(":checked")) {
				switchOfficeTime('same_time');
			} else if (!$("#different_time").is(":checked")) {
				$(".same_time, .different_time").hide();
			}
		});

		$("#different_time").on("change", function() {
			if ($(this).is(":checked")) {
				switchOfficeTime('different_time');
			} else if (!$("#same_time").is(":checked")) {
				$(".same_time, .different_time").hide();
			}
		});

		$(document).on("change", ".different_time_input", function() {
			syncDifferentTimeRows();
		});

		// Initial sync
		if ($("#same_time").is(":checked")) {
			switchOfficeTime('same_time');
		} else if ($("#different_time").is(":checked")) {
			switchOfficeTime('different_time');
			syncDifferentTimeRows();
		} else {
			$(".same_time, .different_time").hide();
		}
	});
</script>

<?php
$working_days = get_result('tbl_working_days');;
if (config_item('office_time') == 'different_time') {
	$d_working_days = get_result('tbl_working_days', array('flag' => 1));
	if (!empty($d_working_days)) {
		foreach ($d_working_days as $v_d_days) {
?>
			<script type="text/javascript">
				$(function() {
					$(".different_time_hours_" + <?= $v_d_days->day_id ?>).removeClass('disabled');
					$(".different_time_hours_" + <?= $v_d_days->day_id ?>).removeAttr('disabled');
					$("#different_time_" + <?= $v_d_days->day_id ?>).show();
					$(".different_time_input").removeAttr('disabled');
				});
			</script>
<?php
		}
	}
} ?>

<?php

if (config_item('office_time') == 'same_time') {
	$s_working_days = get_result('tbl_working_days', array('flag' => 1));;

	if (!empty($s_working_days)) {
		foreach ($s_working_days as $v_s_days) {
?>
			<script type="text/javascript">
				$(function() {
					$(".same_time").removeAttr('disabled');
				});
			</script>
<?php
		}
	}
}
$days = $this->db->get('tbl_days')->result();
?>
<div class="rounded-2xl border border-border bg-card shadow-sm">
	<div class="border-b border-border px-6 py-4">
		<h3 class="text-lg font-semibold text-foreground"><?= lang('working_days') ?></h3>
	</div>
	<div class="p-6">
		<form role="form" id="form" action="<?php echo base_url(); ?>admin/settings/save_working_days"
			method="post"
			class="space-y-6">
			<div class="grid grid-cols-1 items-start gap-3 md:grid-cols-[220px_1fr]">
				<label class="text-sm text-end font-medium text-foreground"><?= lang('office_time') ?></label>
				<div class="flex flex-wrap gap-5">
					<label class="inline-flex items-center gap-2 text-sm text-foreground">
						<input class="select_one h-4 w-4 rounded border-border" value="same_time" <?php
																									if (config_item('office_time') == 'same_time') {
																										echo "checked=\"checked\"";
																									}
																									?> id="same_time" type="checkbox" name="office_time">
						<?= lang('every_days_same_time') ?>
					</label>

					<label class="inline-flex items-center gap-2 text-sm text-foreground">
						<input class="select_one h-4 w-4 rounded border-border" <?php
																				if (config_item('office_time') == 'different_time') {
																					echo "checked=\"checked\"";
																				}
																				?> value="different_time" id="different_time" type="checkbox"
							name="office_time">
						<?= lang('set_different_time') ?>
					</label>
				</div>
			</div>
			<?php if (!empty($working_days)) { ?>
				<input type="hidden" name="already_exist" value="1" />
			<?php } ?>
			<div class="same_time" style="display: <?php
													if (config_item('office_time') == 'same_time') {
														echo 'block';
													} else {
														echo 'none';
													}
													?>">
				<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
					<?= form_timepicker('s_start_hours', lang('start_hours'), (!empty($s_working_days) ? date('h:i A', safe_strtotime($s_working_days[0]->start_hours)) : ''), false, array('class' => 'same_time'), 'horizontal') ?>
					<?= form_timepicker('s_end_hours', lang('end_hours'), (!empty($s_working_days) ? date('h:i A', safe_strtotime($s_working_days[0]->end_hours)) : ''), false, array('class' => 'same_time'), 'horizontal') ?>
				</div>
				<div class="space-y-3">
					<div class="text-sm font-medium text-foreground"><?= lang('working_days') ?></div>
					<div class="flex flex-wrap gap-4">
						<?php
						foreach (
							$days

							as $v_day
						): ?><!--Retrieve Days from Database -->
							<label class="inline-flex items-center gap-2 text-sm text-foreground">
								<input type="checkbox" class="same_time h-4 w-4 rounded border-border" name="day[]" value="<?php echo $v_day->day_id ?>" <?php
																																							if (!empty($s_working_days)) {
																																								foreach ($s_working_days as $v_s_work) { ?>
									<?php if ($v_s_work->flag == 1 && $v_s_work->day_id == $v_day->day_id) {
									?>
									checked
									<?php
																																									}
																																								}
																																							}
									?> />
								<strong><?= lang($v_day->day) ?></strong>
								<input type="hidden" name="day_id[]"
									value="<?php echo $v_day->day_id ?>" />
							</label>
						<?php endforeach; ?>
					</div>
					<div class="flex justify-end">
						<button type="submit"
							class="inline-flex items-center justify-center gap-2 rounded-lg bg-primary px-4 py-2 text-sm font-semibold text-primary-foreground shadow-sm transition hover:bg-primary/90">
							<i data-lucide="save" class="h-4 w-4"></i>
							<?= lang('save') ?>
						</button>
					</div>
				</div>

			</div>
			<?php foreach ($working_days as $v_w_days) { ?>
				<input type="hidden" name="working_days_id[]"
					value="<?php echo $v_w_days->working_days_id ?>" />
			<?php } ?>
			<div class="different_time" style="display:<?php
														if (config_item('office_time') == 'different_time') {
															echo 'block';
														} else {
															echo 'none';
														}
														?>">
				<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>asset/css/kendo.common.min.css" />
				<?php
				foreach ($days as $v_day): ?>
					<div class="grid grid-cols-1 items-start gap-3 rounded-lg border border-border p-3 md:grid-cols-[220px_1fr]">
						<label class="pt-2 text-sm font-semibold text-foreground"><?= lang($v_day->day) ?></label>
						<div class="space-y-3">
							<label class="inline-flex items-center gap-2 text-sm text-foreground">
								<input class="different_time_input h-4 w-4 rounded border-border"
									<?php
									if (!empty($d_working_days)) {
										foreach ($d_working_days as $v_d_work) {
											if ($v_d_work->flag == 1 && $v_d_work->day_id == $v_day->day_id) {
									?>
									checked
									<?php
											}
										}
									}
									?>
									type="checkbox" name="day[]"
									value="<?php echo $v_day->day_id ?>" />
								<?= lang('active') ?>
							</label>
							<div class="different_time_hours" id="different_time_<?= $v_day->day_id ?>">
								<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
									<?= form_timepicker(
										'start_hours_' . $v_day->day_id,
										lang('start'),
										(!empty($d_working_days) ? (function () use ($d_working_days, $v_day) {
											foreach ($d_working_days as $v_d_work) {
												if ($v_d_work->day_id == $v_day->day_id) {
													return date('h:i A', safe_strtotime($v_d_work->start_hours));
												}
											}
											return '';
										})() : ''),
										false,
										array('class' => 'disabled different_time_hours_' . $v_day->day_id)
									) ?>
									<?= form_timepicker(
										'end_hours_' . $v_day->day_id,
										lang('end'),
										(!empty($d_working_days) ? (function () use ($d_working_days, $v_day) {
											foreach ($d_working_days as $v_d_work) {
												if ($v_d_work->day_id == $v_day->day_id) {
													return date('h:i A', safe_strtotime($v_d_work->end_hours));
												}
											}
											return '';
										})() : ''),
										false,
										array('class' => 'disabled different_time_hours_' . $v_day->day_id)
									) ?>
								</div>
							</div>
						</div>
					</div>
				<?php endforeach; ?>
				<div class="flex justify-end mt-3">
					<button type="submit"
						class="inline-flex items-center justify-center gap-2 rounded-lg bg-primary px-4 py-2 text-sm font-semibold text-primary-foreground shadow-sm transition hover:bg-primary/90">
						<i data-lucide="save" class="h-4 w-4"></i>
						<?= lang('save') ?>
					</button>
				</div>
				<!-- List  of days -->
			</div>


		</form>
	</div>
</div>
