<?php echo message_box('success'); ?>
<?php echo message_box('error'); ?>
<?php
$created = can_action('82', 'created');
$edited = can_action('82', 'edited');
$deleted = can_action('82', 'deleted');
if (!empty($created) || !empty($edited)) {

	$selected_sub_category_id = '';
	if (!empty($stock_info) && is_object($stock_info) && !empty($stock_info->stock_sub_category_id)) {
		$selected_sub_category_id = $stock_info->stock_sub_category_id;
	} elseif (!empty($assign_item) && !empty($assign_item->stock_sub_category_id)) {
		$selected_sub_category_id = $assign_item->stock_sub_category_id;
	}

	$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;
			}
			$cate_label = !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] = $cate_label . ' - ' . $sub_category->stock_sub_category;
			}
		}
	}

	$item_name_options = array('' => lang('select') . ' ' . lang('item_name'));
	$this->stock_model->_table_name = "tbl_stock"; //table name
	$this->stock_model->_order_by = "stock_id";
	$stock_infos = $this->stock_model->get();
	if (!empty($stock_infos) && is_array($stock_infos)) {
		foreach ($stock_infos as $v_stock_info) {
			if (empty($v_stock_info->stock_id) || empty($v_stock_info->item_name)) {
				continue;
			}
			$item_name_options[$v_stock_info->stock_id] = $v_stock_info->item_name;
		}
	}

	$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="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
			<div>
				<h2 class="text-2xl font-semibold text-foreground"><?= lang('assign_stock') ?></h2>
				<p class="text-sm text-muted-foreground"><?= lang('manage') . ' ' . lang('assign_stock') ?></p>
			</div>
			<div class="flex items-center gap-2">
				<a href="<?= base_url('admin/stock/assign_stock') ?>" 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="arrow-left" class="h-4 w-4"></i>
					<?= lang('back') ?>
				</a>
			</div>
		</div>

		<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="clipboard-list" class="h-5 w-5"></i>
					<h3 class="text-base font-semibold text-foreground"><?= !empty($assign_item) ? lang('edit') . ' ' . lang('assign_stock') : lang('assign_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/set_assign_stock/<?php
																					if (!empty($assign_item->assign_item_id)) {
																						echo $assign_item->assign_item_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_category_options,
							$selected_sub_category_id,
							true,
							array(
								'class' => 'select_box w-full',
								'data-search' => 'true',
								'onchange' => 'get_item_name_by_id(this.value)'
							)
						) ?>

						<?= form_select_group(
							'stock_id',
							lang('item_name'),
							$item_name_options,
							!empty($assign_item->stock_id) ? $assign_item->stock_id : '',
							true,
							array(
								'id' => 'item_name',
								'class' => 'w-full'
							)
						) ?>

						<?= form_select_group(
							'user_id',
							lang('employee') . ' ' . lang('name'),
							$employee_options,
							!empty($assign_item->user_id) ? $assign_item->user_id : '',
							true,
							array(
								'class' => 'select_box w-full',
								'data-search' => 'true'
							)
						) ?>

						<?= form_input_group('assign_inventory', lang('assign_quantity'), !empty($assign_item->assign_inventory) ? $assign_item->assign_inventory : '', 'number', true, array('data-parsley-type' => 'number')); ?>
						<?= form_datepicker('assign_date', lang('assign_date'), !empty($assign_item->assign_date) ? $assign_item->assign_date : '', true); ?>
					</div>

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