<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$created = can_action('70', 'created');
$edited = can_action('70', 'edited');
$deleted = can_action('70', 'deleted');

$dept_count = !empty($all_dept_info) ? count($all_dept_info) : 0;
$designation_count = 0;
if (!empty($all_department_info)) {
	foreach ($all_department_info as $v_department_info) {
		if (!empty($v_department_info)) {
			foreach ($v_department_info as $v_department) {
				if (!empty($v_department->designations)) {
					$designation_count++;
				}
			}
		}
	}
}
?>

<div class="space-y-4">
	<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
		<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
			<div class="flex items-center justify-between">
				<div>
					<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('departments') ?></p>
					<p class="text-2xl font-semibold text-foreground"><?= $dept_count ?></p>
				</div>
				<div class="inline-flex h-10 w-10 items-center justify-center rounded-xl bg-indigo-50 text-indigo-600">
					<i data-lucide="building-2" class="h-5 w-5"></i>
				</div>
			</div>
		</div>
		<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
			<div class="flex items-center justify-between">
				<div>
					<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('designation') ?></p>
					<p class="text-2xl font-semibold text-foreground"><?= $designation_count ?></p>
				</div>
				<div class="inline-flex h-10 w-10 items-center justify-center rounded-xl bg-emerald-50 text-emerald-600">
					<i data-lucide="briefcase" class="h-5 w-5"></i>
				</div>
			</div>
		</div>
	</div>

	<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
		<?php if (!empty($all_department_info)) : foreach ($all_department_info as $akey => $v_department_info) : ?>
			<?php if (!empty($v_department_info)) :
				$deptname = !empty($all_dept_info[$akey]->deptname) ? $all_dept_info[$akey]->deptname : lang('undefined_department');
				$dept_id = $all_dept_info[$akey]->departments_id ?? null;
			?>
			<div class="rounded-2xl border border-border bg-card shadow-sm" id="table_department_<?= $dept_id ?>">
				<div class="border-b border-border px-5 py-4">
					<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
						<div>
							<h3 class="text-lg font-semibold text-foreground"><?= $deptname ?></h3>
							<?php if (!empty($all_dept_info[$akey]->department_head_id)) { ?>
								<p class="text-xs text-muted-foreground">
									<?= lang('department_head') ?>:
									<a class="text-indigo-600 hover:underline" href="<?= base_url('admin/user/user_details/' . $all_dept_info[$akey]->department_head_id) ?>">
										<?= fullname($all_dept_info[$akey]->department_head_id) ?>
									</a>
								</p>
							<?php } ?>
						</div>
						<div class="flex flex-wrap items-center gap-2">
							<span class="inline-flex items-center rounded-full border border-border bg-muted px-2.5 py-1 text-xs font-semibold text-muted-foreground">
								<?= count($v_department_info) ?> <?= lang('designation') ?>
							</span>
							<?php if (!empty($edited) || !empty($deleted)) { ?>
								<div class="flex items-center">
									<?php if (!empty($edited)) { ?>
										<?= btn_modal(
											'admin/departments/edit_departments/' . $dept_id,
											'',
											lang('edit'),
											'sm',
											'pencil',
											array('class' => 'inline-flex items-center justify-center rounded-lg border border-indigo-200 bg-indigo-50 p-2 mb-1 text-indigo-600 hover:bg-indigo-100 h-7')
										) ?>
									<?php } ?>
									<?php if (!empty($deleted)) { ?>
										<?= ajax_anchor(base_url('admin/departments/delete_department/' . $dept_id), '', array("class" => "inline-flex items-center justify-center rounded-lg border border-red-200 bg-red-50 p-2 text-red-600 hover:bg-red-100", "title" => lang('delete'), "data-fade-out-on-success" => "#table_department_" . $dept_id)); ?>
									<?php } ?>
								</div>
							<?php } ?>
						</div>
					</div>
				</div>
				<div class="p-5">
					<?php if (!empty($v_department_info)) : ?>
						<ul class="divide-y divide-border">
							<?php foreach ($v_department_info as $key => $v_department) :
								if (!empty($v_department->designations)) {
									$total_employee = count(array($this->db->where('designations_id', $v_department->designations_id)->get('tbl_account_details')->result()));
							?>
								<li class="flex flex-col gap-3 py-4 md:flex-row md:items-center md:justify-between" id="table_designation_<?= $v_department->designations_id ?>">
									<div>
										<a class="text-sm font-semibold text-foreground hover:text-indigo-600" title="<?= lang('set_full_permission') ?>" href="<?= base_url('admin/departments/details/' . $v_department->designations_id) ?>">
											<?= $v_department->designations ?>
										</a>
										<p class="text-xs text-muted-foreground">
											<?= btn_modal(
												'admin/departments/user_by_designation/' . $v_department->designations_id,
												lang('total') . ' ' . lang('users') . ': ' . $total_employee,
												lang('users'),
												'md',
												'',
												array('class' => 'hover:underline p-0 border-0 bg-transparent text-muted-foreground')
											) ?>
										</p>
									</div>
									<?php if (!empty($edited) || !empty($deleted)) { ?>
										<div class="flex items-center">
											<?php if (!empty($edited)) { ?>
												<a href="<?= base_url('admin/departments/details/' . $v_department->designations_id) ?>" class="inline-flex items-center justify-center rounded-lg border border-indigo-200 bg-indigo-50 p-2 text-indigo-600 hover:bg-indigo-100 h-7 mb-1" title="<?= lang('edit') ?>">
													<i data-lucide="pencil" class="h-4 w-4"></i>
												</a>
											<?php } ?>
											<?php if (!empty($deleted)) { ?>
												<?= ajax_anchor(base_url('admin/departments/delete_designations/' . $v_department->designations_id), '', array("class" => "inline-flex items-center justify-center rounded-lg border border-red-200 bg-red-50 p-2 text-red-600 hover:bg-red-100", "title" => lang('delete'), "data-fade-out-on-success" => "#table_designation_" . $v_department->designations_id)); ?>
											<?php } ?>
										</div>
									<?php } ?>
								</li>
							<?php } endforeach; ?>
						</ul>
					<?php else : ?>
						<p class="text-sm text-muted-foreground"><?= lang('no_designation_create_yet') ?></p>
					<?php endif; ?>
				</div>
			</div>
			<?php endif; ?>
		<?php endforeach; ?>
		<?php endif; ?>
	</div>
</div>
