<?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');
if (!empty($created) || !empty($edited)) {
	$category_options = array('' => lang('new_category'));
	$all_stock_category = $this->db->get('tbl_stock_category')->result();
	if (!empty($all_stock_category)) {
		foreach ($all_stock_category as $v_stock_category) {
			$label = !empty($v_stock_category->stock_category) ? $v_stock_category->stock_category : lang('undefined_category');
			$category_options[$v_stock_category->stock_category_id] = $label;
		}
	}
	$category_selected = !empty($category_info->stock_category_id) ? $category_info->stock_category_id : '';
?>
<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="layers" class="h-5 w-5"></i>
				<h3 class="text-base font-semibold text-foreground"><?= lang('new') . ' ' . lang('stock_category') ?></h3>
			</div>
		</div>
		<div class="p-5">
			<form data-parsley-validate="" novalidate="" action="<?php echo base_url() ?>admin/stock/save_stock_category/<?php
					if (!empty($stock_category_info->stock_category_id)) {
						echo $stock_category_info->stock_category_id;
					}
					?>" method="post" class="space-y-4">
				<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
					<?php
						echo form_select_group(
							'stock_category_id',
							lang('select') . ' ' . lang('category'),
							$category_options,
							$category_selected,
							false,
							array('class' => 'select_box', 'id' => 'new_stock_category', 'data-live-search' => 'true')
						);
					?>
					<div class="new_departments" style="display: <?= (!empty($category_info->stock_category_id) ? 'none' : 'block') ?>">
						<?=
							form_input_group(
								'stock_category',
								lang('new_category'),
								'',
								'text',
								false,
								array('class' => 'new_departments', 'disabled' => !empty($category_info->stock_category_id) ? 'disabled' : null)
							);
						?>
					</div>
					<?= form_input_group('stock_sub_category', lang('sub_category'), !empty($sub_category_info->stock_sub_category) ? $sub_category_info->stock_sub_category : '', 'text', false); ?>
				</div>
				<input type="hidden" name="stock_sub_category_id" value="<?php if (!empty($sub_category_info->stock_sub_category_id)) echo $sub_category_info->stock_sub_category_id; ?>" />

				<div class="flex items-center justify-end gap-2 pt-2">
					<?php if (!empty($sub_category_info)) { ?>
						<button type="submit" class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
							<i data-lucide="save" class="h-4 w-4"></i>
							<?= lang('updates') ?>
						</button>
						<button type="button" onclick="goBack()" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 text-xs font-semibold text-foreground hover:bg-muted">
							<i data-lucide="x" class="h-4 w-4"></i>
							<?= lang('cancel') ?>
						</button>
					<?php } else { ?>
						<button type="submit" class="inline-flex items-center gap-2 rounded-lg bg-indigo-600 px-3 py-2 text-xs font-semibold text-white shadow-sm hover:bg-indigo-500">
							<i data-lucide="save" class="h-4 w-4"></i>
							<?= lang('save') ?>
						</button>
					<?php } ?>
				</div>
			</form>
		</div>
	</div>
</div>
<?php } ?>

<script type="text/javascript">
$(document).ready(function () {
	function toggleNewCategory() {
		if ($('#new_stock_category').val() !== '') {
			$('.new_departments').hide();
			$('.new_departments input').prop('disabled', true);
		} else {
			$('.new_departments').show();
			$('.new_departments input').prop('disabled', false);
		}
	}

	$('#new_stock_category').on('change', toggleNewCategory);
	toggleNewCategory();
});
</script>
