<?php echo message_box('success'); ?>
<?php echo message_box('error'); ?>

<?php
$stock_category_options = array('' => lang('select') . ' ' . lang('stock_category'));
if (!empty($all_category_info)) {
	foreach ($all_category_info as $cate_name => $v_category_info) {
		if (empty($v_category_info) || !is_array($v_category_info)) {
			continue;
		}

		$group_name = !empty($cate_name) ? $cate_name : lang('undefined_category');
		foreach ($v_category_info as $sub_category) {
			if (empty($sub_category->stock_sub_category_id) || empty($sub_category->stock_sub_category)) {
				continue;
			}
			$stock_category_options[$sub_category->stock_sub_category_id] = $group_name . ' - ' . $sub_category->stock_sub_category;
		}
	}
}
?>

<div class="space-y-4">
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="flex items-center gap-2 border-b border-border px-5 py-4">
			<i data-lucide="history" class="h-5 w-5 text-muted-foreground"></i>
			<h3 class="text-base font-semibold text-foreground"><?= lang('select') . ' ' . lang('stock_category') ?></h3>
		</div>
		<div class="flex justify-center p-5">
			<form id="form" action="<?php echo base_url() ?>admin/stock/stock_history" method="post" class="flex w-full max-w-xl flex-col gap-3 md:flex-row md:items-end">
				<div class="flex-1">
					<?= form_select_group(
						'stock_sub_category_id',
						lang('select') . ' ' . lang('stock_category'),
						$stock_category_options,
						!empty($sub_category_id) ? $sub_category_id : '',
						true,
						array(
							'id' => 'stock_sub_category_id',
							'class' => 'select_box w-full',
							'data-search' => 'true'
						)
					) ?>
				</div>
				<div>
					<button type="submit" id="sbtn" value="1" name="flag" class="inline-flex h-8 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 mb-1">
						<i data-lucide="search" class="h-4 w-4"></i>
						<?= lang('go') ?>
					</button>
				</div>
			</form>
		</div>
	</div>

	<?php if (!empty($flag)): ?>
		<?php if (!empty($item_history_info)): foreach ($item_history_info as $sub_category => $v_item_history) : ?>
			<?php if (!empty($v_item_history)): ?>
				<div class="rounded-2xl border border-border bg-card shadow-sm">
					<div class="border-b border-border px-5 py-4">
						<div class="text-sm font-semibold text-foreground"><?php echo $sub_category; ?></div>
					</div>
					<div class="space-y-5 p-5">
						<?php foreach ($v_item_history as $item_name => $item_history) : ?>
							<div class="rounded-xl border border-border/80 bg-background/40">
								<div class="border-b border-border px-4 py-3 text-sm font-semibold text-foreground"><?php echo $item_name ?></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('inventory') ?></th>
												<th class="px-4 py-3 text-left font-semibold"><?= lang('buying_date') ?></th>
												<th class="w-28 px-4 py-3 text-left font-semibold"><?= lang('action') ?></th>
											</tr>
										</thead>
										<tbody class="divide-y divide-border">
											<?php foreach ($item_history as $key => $v_item) : ?>
												<tr id="table_history_<?= $v_item->item_history_id ?>" class="text-foreground/90">
													<td class="px-4 py-3"><?php echo $key + 1 ?></td>
													<td class="px-4 py-3 font-medium"><?php echo $v_item->item_name ?></td>
													<td class="px-4 py-3"><?php echo $v_item->inventory ?></td>
													<td class="px-4 py-3"><?= display_date($v_item->purchase_date); ?></td>
													<td class="px-4 py-3">
														<div class="flex items-center">
															<a href="<?= base_url('admin/stock/stock_list/' . $v_item->item_history_id . '/h') ?>" class="inline-flex h-7 w-8 mb-1 items-center justify-center rounded-md border border-sky-200 bg-sky-50 text-sky-700 transition hover:bg-sky-100 dark:border-sky-900/50 dark:bg-sky-950/40 dark:text-sky-300 dark:hover:bg-sky-900/50" title="<?= lang('edit') ?>">
																<i data-lucide="pencil" class="h-4 w-4"></i>
															</a>
															<?= ajax_anchor(
																base_url("admin/stock/delete_stock_history/" . $v_item->stock_id . '/' . $v_item->item_history_id . '/' . $sub_category_id),
																'',
																array(
																	"class" => "",
																	"title" => lang('delete'),
																	"data-fade-out-on-success" => "#table_history_" . $v_item->item_history_id
																)
															); ?>
														</div>
													</td>
												</tr>
											<?php endforeach; ?>
										</tbody>
									</table>
								</div>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif; ?>
		<?php endforeach; ?>
		<?php else : ?>
			<div class="rounded-2xl border border-border bg-card p-6 text-sm text-muted-foreground">
				<?= lang('nothing_to_display') ?>
			</div>
		<?php endif; ?>
	<?php endif; ?>
</div>
