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

<?php
$created = can_action('81', 'created');
$edited = can_action('81', 'edited');
$deleted = can_action('81', 'deleted');
if (!empty($created) || !empty($edited)) {
	$stock_sub_options = array('' => lang('select') . ' ' . lang('stock_category'));
	if (!empty($all_category_info)) {
		foreach ($all_category_info as $cate_name => $v_category_info) {
			$cate_label = !empty($cate_name) ? $cate_name : lang('undefined_category');
			if (!empty($v_category_info)) {
				foreach ($v_category_info as $sub_category) {
					if (!empty($sub_category->stock_sub_category)) {
						$stock_sub_options[$sub_category->stock_sub_category_id] = $cate_label . ' / ' . $sub_category->stock_sub_category;
					}
				}
			}
		}
	}
	$selected_sub = !empty($stock_info->stock_sub_category_id) ? $stock_info->stock_sub_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="package" class="h-5 w-5"></i>
				<h3 class="text-base font-semibold text-foreground"><?= !empty($stock_info) ? lang('edit') . ' ' . lang('stock') : lang('new') . ' ' . lang('stock') ?></h3>
			</div>
		</div>
		<div class="p-5">
			<form role="form" data-parsley-validate="" novalidate="" enctype="multipart/form-data"
				action="<?php echo base_url() ?>admin/stock/save_stock/<?php
					if (!empty($stock_info->item_history_id)) {
						echo $stock_info->item_history_id;
					}
					?>" method="post" class="space-y-4">

				<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
					<?= form_select_group('stock_sub_category_id', lang('stock_category'), $stock_sub_options, $selected_sub, true, array('class' => 'select_box', 'data-live-search' => 'true')); ?>
					<?= form_datepicker('purchase_date', lang('buying_date'), !empty($stock_info->purchase_date) ? $stock_info->purchase_date : '', true); ?>
					<?= form_input_group('item_name', lang('item_name'), !empty($stock_info->item_name) ? $stock_info->item_name : '', 'text', true, array('id' => 'query')); ?>
					<?php if (!empty($from_history) || empty($stock_info)) { ?>
						<?= form_input_group('inventory', lang('inventory'), !empty($stock_info->inventory) ? $stock_info->inventory : '', 'text', true, array('data-parsley-type' => 'number')); ?>
					<?php } else { ?>
						<?= form_input_group('inventory', lang('total_stock'), !empty($stock_info->total_stock) ? $stock_info->total_stock : '', 'text', true, array('readonly' => 'readonly')); ?>
					<?php } ?>
				</div>

				<div class="flex items-center justify-end gap-2 pt-2">
					<?php if (!empty($stock_info)) { ?>
						<button type="submit" id="i_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" id="i_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>

				<input type="hidden" name="item_history_id" value="<?php
					if (!empty($stock_info->item_history_id)) {
						echo $stock_info->item_history_id;
					}
					?>">
			</form>
		</div>
	</div>
</div>
<?php } ?>

<link href="<?php echo base_url() ?>assets/plugins/typehead/typehead.css" rel="stylesheet" />
<script src="<?php echo base_url() ?>assets/plugins/typehead/typehead.js"></script>

<?php $all_stock = $this->db->get('tbl_stock')->result(); ?>
<script type="text/javascript">
$('#query').typeahead({
	local: [<?php if (!empty($all_stock)) {
			foreach ($all_stock as $v_stock) { ?> "<?= $v_stock->item_name ?>", <?php }
		} ?>]
});
</script>
