<?php echo message_box('success'); ?>
<?php echo message_box('error'); ?>
<?php
$employee_options = array('' => lang('select_employee'));
if (!empty($all_employee)) {
	foreach ($all_employee as $dept_name => $v_all_employee) {
		if (empty($v_all_employee) || !is_array($v_all_employee)) {
			continue;
		}
		foreach ($v_all_employee as $v_employee) {
			if (empty($v_employee->user_id)) {
				continue;
			}
			$employee_options[$v_employee->user_id] = $dept_name . ' - ' . $v_employee->fullname . ' ( ' . $v_employee->designations . ' )';
		}
	}
}
?>

<div class="space-y-4">
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="border-b border-border px-5 py-4">
			<div class="flex items-center gap-2">
				<i data-lucide="bar-chart-3" class="h-5 w-5"></i>
				<h3 class="text-base font-semibold text-foreground"><?= lang('assign_stock_report') ?></h3>
			</div>
		</div>
		<div class="flex justify-center p-5">
			<form id="form" action="<?php echo base_url() ?>admin/stock/assign_stock_report" method="post" class="flex w-full max-w-3xl flex-col gap-3 md:flex-row md:items-end">
				<div class="flex-1">
					<?= form_select_group(
						'user_id',
						lang('select_employee'),
						$employee_options,
						!empty($employee_info->user_id) ? $employee_info->user_id : '',
						true,
						array('class' => 'select_box w-full', 'data-search' => 'true')
					) ?>
				</div>
				<div>
					<button type="submit" id="sbtn" value="1" name="flag" class="inline-flex h-8 mb-1 items-center gap-2 rounded-lg bg-slate-900 px-4 text-sm font-semibold text-white transition hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white">
						<i data-lucide="search" class="h-4 w-4"></i>
						<?= lang('go') ?>
					</button>
				</div>
			</form>
		</div>
	</div>

	<?php if (!empty($flag)): ?>
		<div class="rounded-2xl border border-border bg-card shadow-sm">
			<div class="border-b border-border px-5 py-4">
				<div class="flex flex-col gap-2 md:flex-row md:items-center md:justify-between">
					<div class="space-y-1">
						<div class="text-sm font-semibold text-foreground">
							<?php
							if (!empty($employee_info)) {
								echo $employee_info->fullname . (!empty($employee_info->employment_id) ? ' (' . $employee_info->employment_id . ') ' : '');
							}
							?>
						</div>
						<div class="text-xs text-muted-foreground"><?= lang('assign_stock_report') ?></div>
					</div>
					<?php if (!empty($employee_info)) { ?>
						<div class="hidden-print">
							<?php echo btn_pdf('admin/stock/assign_stock_pdf/' . $employee_info->user_id); ?>
						</div>
					<?php } ?>
				</div>
			</div>
			<div class="p-5">
				<?php if (!empty($assign_list)): foreach ($assign_list as $sub_category => $v_assign_list) : ?>
					<?php if (!empty($v_assign_list)): ?>
						<div class="mb-6 overflow-hidden rounded-xl border border-border/80 bg-background/40">
							<div class="border-b border-border px-4 py-3">
								<div class="text-sm font-semibold text-foreground"><?php echo $sub_category ?></div>
							</div>
							<div class="overflow-x-auto">
								<table class="min-w-full text-sm">
									<thead class="bg-slate-50 text-slate-600 dark:bg-slate-800 dark:text-slate-300">
										<tr>
											<th class="w-14 px-4 py-3 text-left font-semibold"><?= lang('sl') ?></th>
											<th class="px-4 py-3 text-left font-semibold"><?= lang('item_name') ?></th>
											<th class="px-4 py-3 text-left font-semibold"><?= lang('assign_quantity') ?></th>
											<th class="px-4 py-3 text-left font-semibold"><?= lang('assign_date') ?></th>
											<th class="w-24 px-4 py-3 text-left font-semibold hidden-print"><?= lang('action') ?></th>
										</tr>
									</thead>
									<tbody class="divide-y divide-border">
										<?php foreach ($v_assign_list as $key => $v_assign_stock) : ?>
											<tr id="table_assign_stock_<?= $v_assign_stock->assign_item_id ?>" class="text-foreground/90 hover:bg-muted/20">
												<td class="px-4 py-3"><?php echo $key + 1 ?></td>
												<td class="px-4 py-3 font-medium"><?php echo $v_assign_stock->item_name ?></td>
												<td class="px-4 py-3"><?php echo $v_assign_stock->assign_inventory ?></td>
												<td class="px-4 py-3"><?= display_date($v_assign_stock->assign_date); ?></td>
												<td class="px-4 py-3 hidden-print">
													<div class="flex items-center gap-2">
														<?php echo ajax_anchor(base_url("admin/stock/delete_assign_stock/" . $v_assign_stock->assign_item_id), '', array("class" => "", "title" => lang('delete'), "data-fade-out-on-success" => "#table_assign_stock_" . $v_assign_stock->assign_item_id)); ?>
													</div>
												</td>
											</tr>
										<?php endforeach; ?>
									</tbody>
								</table>
							</div>
						</div>
					<?php endif; ?>
				<?php endforeach; ?>
				<?php else : ?>
					<p class="text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></p>
				<?php endif; ?>
			</div>
		</div>
	<?php endif; ?>
</div>
