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

<?php
$created = can_action('76', 'created');
$edited = can_action('76', 'edited');
$deleted = can_action('76', 'deleted');
$edit_btn_class = 'inline-flex h-8 w-8 items-center justify-center rounded-lg 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';
$delete_btn_class = 'inline-flex h-8 w-8 items-center justify-center rounded-lg border border-rose-200 bg-rose-50 text-rose-600 transition hover:bg-rose-100 dark:border-rose-900/50 dark:bg-rose-950/40 dark:text-rose-300 dark:hover:bg-rose-900/50';
?>

<div class="space-y-4">
	<?php if (!empty($all_stock_category_info)) : ?>
		<div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
			<?php foreach ($all_stock_category_info as $akey => $v_stock_category_info) : ?>
				<?php if (!empty($v_stock_category_info)) :
					$category = !empty($stock_category_info[$akey]->stock_category) ? $stock_category_info[$akey]->stock_category : lang('undefined_category');
					$category_id = $stock_category_info[$akey]->stock_category_id ?? null;
					$sub_count = 0;
					foreach ($v_stock_category_info as $v_stock_category) {
						if (!empty($v_stock_category->stock_sub_category)) {
							$sub_count++;
						}
					}
				?>
					<div class="rounded-2xl border border-border bg-card shadow-sm" id="table_stock_category_<?= $category_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"><?= $category ?></h3>
									<p class="text-xs text-muted-foreground"><?= $sub_count ?> <?= lang('stock_sub_category') ?></p>
								</div>
								<?php if (!empty($edited) || !empty($deleted)) { ?>
									<div class="flex items-center gap-2">
										<?php if (!empty($edited)) { ?>
											<?= btn_modal(
												'admin/stock/edit_stock_category/' . $category_id,
												'',
												lang('edit'),
												'md',
												'pencil',
												array('class' => $edit_btn_class)
											) ?>
										<?php } ?>
										<?php if (!empty($deleted)) { ?>
											<a href="<?= base_url('admin/stock/delete_stock_category/' . $category_id) ?>" class="<?= $delete_btn_class ?>" title="<?= lang('delete') ?>" onclick="return confirm('<?= lang('alert_delete_category') ?>');">
												<i data-lucide="trash-2" class="h-4 w-4"></i>
											</a>
										<?php } ?>
									</div>
								<?php } ?>
							</div>
						</div>
						<div class="p-5">
							<?php if (!empty($v_stock_category_info)) : ?>
								<ul class="divide-y divide-border">
									<?php foreach ($v_stock_category_info as $key => $v_stock_category) : ?>
										<?php if (!empty($v_stock_category->stock_sub_category)) : ?>
											<li class="flex flex-col gap-3 py-3 md:flex-row md:items-center md:justify-between" id="table_sub_category_<?= $v_stock_category->stock_sub_category_id ?>">
												<div class="text-sm font-semibold text-foreground">
													<?= $v_stock_category->stock_sub_category ?>
												</div>
												<?php if (!empty($edited) || !empty($deleted)) { ?>
													<div class="flex items-center gap-2">
														<?php if (!empty($edited)) { ?>
															<a href="<?= base_url('admin/stock/create_stockcategory/' . $category_id . '/' . $v_stock_category->stock_sub_category_id) ?>" class="<?= $edit_btn_class ?>" title="<?= lang('edit') ?>">
																<i data-lucide="pencil" class="h-4 w-4"></i>
															</a>
														<?php } ?>
														<?php if (!empty($deleted)) { ?>
															<a href="<?= base_url('admin/stock/delete_stock_sub_category/' . $v_stock_category->stock_sub_category_id) ?>" class="<?= $delete_btn_class ?>" title="<?= lang('delete') ?>">
																<i data-lucide="trash-2" class="h-4 w-4"></i>
															</a>
														<?php } ?>
													</div>
												<?php } ?>
											</li>
										<?php endif; ?>
									<?php endforeach; ?>
								</ul>
							<?php else : ?>
								<p class="text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></p>
							<?php endif; ?>
						</div>
					</div>
				<?php endif; ?>
			<?php endforeach; ?>
		</div>
	<?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; ?>
</div>
