<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<?= message_box('success'); ?>
<?= message_box('error'); ?>

<?php
$created = can_action('39', 'created');
$edited = can_action('39', 'edited');
$deleted = can_action('39', 'deleted');

if (!empty($created) || !empty($edited)) {
	$items_id = !empty($items_info) ? $items_info->saved_items_id : null;
	$item_name = !empty($items_info->item_name) ? $items_info->item_name : '';
	$code = !empty($items_info->code) ? $items_info->code : '';
	$barcode_symbology = !empty($items_info->barcode_symbology) ? $items_info->barcode_symbology : 'code128';
	$hsn_code = !empty($items_info->hsn_code) ? $items_info->hsn_code : '';
$manufacturer_id = !empty($items_info->manufacturer_id) ? $items_info->manufacturer_id : '';
$customer_group_id = !empty($items_info->customer_group_id) ? $items_info->customer_group_id : '';
$selected_manufacturer_label = $selected_manufacturer_label ?? (!empty($all_manufacturer[$manufacturer_id]) ? $all_manufacturer[$manufacturer_id] : '');
$selected_group_label = $selected_group_label ?? (!empty($all_customer_group[$customer_group_id]) ? $all_customer_group[$customer_group_id] : '');
	$cost_price = !empty($items_info->cost_price) ? $items_info->cost_price : '';
	$unit_cost = !empty($items_info->unit_cost) ? $items_info->unit_cost : '';
	$unit_type = !empty($items_info->unit_type) ? $items_info->unit_type : '';
	$item_quantity = !empty($warehouse_product_info->quantity) ? $warehouse_product_info->quantity : (!empty($items_info->quantity) ? $items_info->quantity : '');
	$item_desc = !empty($items_info->item_desc) ? $items_info->item_desc : '';

	$invoice_view = config_item('invoice_view');
	$barcode_options = array(
		'code25' => 'Code25',
		'code39' => 'Code39',
		'code128' => 'Code128',
		'ean8' => 'EAN8',
		'ean13' => 'EAN13',
		'upca' => 'UPC-A',
		'upce' => 'UPC-E',
	);

	$warehouse = true;
	$check_assign_warehouse = null;
	if (!empty($items_info)) {
		$check_assign_warehouse = get_any_field('tbl_warehouses_products', array('product_id' => $items_info->saved_items_id), 'warehouse_id');
		if (empty($check_assign_warehouse)) {
			$warehouse = true;
		}
	}

	$warehouse_selected = !empty($check_assign_warehouse) ? $check_assign_warehouse : '';

$new_manufacturer_link = form_lookup_add_link('manufacturer_id', base_url('admin/items/manufacturer'), lang('new') . ' ' . lang('manufacturer'), 'md');
$new_group_link = form_lookup_add_link('customer_group_id', base_url('admin/items/items_group'), lang('new') . ' ' . lang('item') . ' ' . lang('group'), 'md');
$new_warehouse_link = form_select_add_link('#select_warehouse', base_url('admin/warehouse/create/0/inline'), lang('new') . ' ' . lang('warehouse'), 'xl');

	$page_title = !empty($items_info) ? (lang('edit') . ' ' . lang('item')) : lang('new_items');
	$page_subtitle = !empty($items_info) ? $item_name : (lang('items') . ' ' . lang('details'));

?>

<?php echo form_open(
	base_url('admin/items/saved_items/' . $items_id),
	array('class' => 'space-y-6', 'enctype' => 'multipart/form-data', 'data-parsley-validate' => 'true', 'id' => 'items-form')
); ?>

<div class="space-y-6">
	<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
		<div>
			<h2 class="text-xl font-semibold text-foreground"><?= $page_title; ?></h2>
			<p class="text-sm text-muted-foreground"><?= html_escape($page_subtitle); ?></p>
		</div>
		<div class="flex flex-wrap gap-2">
			<a href="<?= base_url('admin/items/items_list'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
				<?= lang('all_items'); ?>
			</a>
			<a href="<?= base_url('admin/items/newitems_group'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
				<?= lang('group') . ' ' . lang('list'); ?>
			</a>
			<a href="<?= base_url('admin/items/items_manufacturerlist'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
				<?= lang('manufacturer') . ' ' . lang('list'); ?>
			</a>
			<a href="<?= base_url('admin/items/import'); ?>" class="rounded-lg border border-border px-4 py-2 text-sm font-semibold text-foreground hover:border-primary">
				<?= lang('import') . ' ' . lang('items'); ?>
			</a>
		</div>
	</div>

	<div class="grid gap-6 xl:grid-cols-2">
		<div class="space-y-6">
			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('general'); ?></h3>
				</div>
				<div class="p-5 grid gap-4 md:grid-cols-2">
					<?= form_input_group('item_name', lang('item_name'), $item_name, 'text', true); ?>
					<?= form_input_group('code', lang('code'), $code, 'text', true, array('placeholder' => lang('item_code_help'))); ?>

					<?= form_select_group('barcode_symbology', lang('barcode_symbology'), $barcode_options, $barcode_symbology, true, array('class' => 'select_box')); ?>

					<?php if (!empty($invoice_view) && $invoice_view == '2') { ?>
						<?= form_input_group('hsn_code', lang('hsn_code'), $hsn_code, 'text', false, array('data-parsley-type' => 'number')); ?>
					<?php } ?>

					<?= form_lookup_group('manufacturer_id', lang('manufacturer'), $manufacturer_id, $selected_manufacturer_label, false, array(), '', array('type' => 'manufacturer', 'append' => $new_manufacturer_link)); ?>
					<?= form_lookup_group('customer_group_id', lang('item') . ' ' . lang('group'), $customer_group_id, $selected_group_label, false, array(), '', array('type' => 'item_group', 'append' => $new_group_link)); ?>
				</div>
			</div>

			<?php if (!empty($warehouse)) { ?>
				<div class="rounded-2xl border border-border bg-card shadow-sm">
					<div class="flex items-center justify-between border-b border-border px-5 py-4">
						<h3 class="text-sm font-semibold text-foreground"><?= lang('inventory'); ?></h3>
					</div>
					<div class="p-5 grid gap-4 md:grid-cols-2">
						<?= form_select_group('warehouse_id', lang('warehouse'), !empty($warehouseList) ? $warehouseList : array(), $warehouse_selected, true, array('id' => 'select_warehouse', 'class' => 'select_box', 'data-live-search' => 'true'), '', $new_warehouse_link); ?>
						<?= form_input_group('quantity', lang('quantity'), $item_quantity, 'number', true, array('step' => '0.01', 'data-parsley-type' => 'number', 'id' => 'quantity')); ?>
					</div>
				</div>
			<?php } ?>

			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('price') . ' & ' . lang('tax'); ?></h3>
				</div>
				<div class="p-5 grid gap-4 md:grid-cols-2">
					<?= form_input_group('cost_price', lang('cost_price'), $cost_price, 'number', true, array('step' => '0.01', 'data-parsley-type' => 'number')); ?>
					<?= form_input_group('unit_cost', lang('unit_price'), $unit_cost, 'number', true, array('step' => '0.01', 'data-parsley-type' => 'number')); ?>
					<?= form_input_group('unit_type', lang('unit') . ' ' . lang('type'), $unit_type, 'text', false, array('placeholder' => lang('unit_type_example'))); ?>
					<?php
					$taxes = $this->db->order_by('tax_rate_percent', 'ASC')->get('tbl_tax_rates')->result();
					$tax_rates_id = null;
					if (!empty($items_info->tax_rates_id) && !is_numeric($items_info->tax_rates_id)) {
						$tax_rates_id = json_decode($items_info->tax_rates_id);
					}
					$selected_tax_items = array();
					if (!empty($taxes) && !empty($tax_rates_id) && is_array($tax_rates_id)) {
						foreach ($taxes as $tax) {
							if (in_array($tax->tax_rates_id, $tax_rates_id)) {
								$selected_tax_items[] = array(
									'id' => $tax->tax_rates_id,
									'text' => $tax->tax_rate_name . ' (' . $tax->tax_rate_percent . '%)',
								);
							}
						}
					}
					?>
					<?= form_multi_lookup_group('tax_rates_id', lang('tax'), $selected_tax_items, false, array(), '', array('type' => 'tax_rate', 'min_length' => 0, 'placeholder' => lang('search') . ' ' . lang('tax'))); ?>
				</div>
			</div>
		</div>

		<div class="space-y-6">
				<div class="rounded-2xl border border-border bg-card shadow-sm">
					<div class="flex items-center justify-between border-b border-border px-5 py-4">
						<div>
							<h3 class="text-sm font-semibold text-foreground"><?= lang('image'); ?></h3>
							<p class="mt-1 text-xs text-muted-foreground"><?= lang('upload'); ?> <?= lang('image'); ?> <?= lang('for'); ?> <?= lang('items'); ?></p>
						</div>
					</div>
					<div class="p-5 space-y-4">
						<?= form_attachment_group('files[]', null, 'dropzone', array(
							'dropzone_id' => 'item-images-dropzone',
							'list_id' => 'item-images-list',
							'click_label' => '<span class="font-semibold text-slate-900 dark:text-slate-100">' . html_escape(lang('click_to_upload')) . '</span><br><span class="text-xs text-slate-500 dark:text-slate-400">' . html_escape(lang('drag_and_drop_file_here')) . '</span>',
							'hint' => lang('file_upload_instruction_js'),
						)); ?>
						<?php
						if (!empty($items_info->upload_file)) {
							$uploaded_file = json_decode($items_info->upload_file);
						}
						if (!empty($uploaded_file)) { ?>
							<div class="space-y-2">
								<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('uploaded_file'); ?></p>
								<div class="flex flex-wrap gap-3">
									<?php foreach ($uploaded_file as $v_files_image) { ?>
										<div class="relative w-[112px] rounded-xl border border-border bg-background p-2 shadow-sm">
											<button type="button" class="existing_image absolute right-1 top-1 inline-flex h-7 w-7 items-center justify-center rounded-md text-slate-400 transition-colors hover:bg-rose-50 hover:text-rose-600 dark:hover:bg-rose-950/40 dark:hover:text-rose-300" aria-label="<?= lang('delete'); ?>">
												<i data-lucide="x" class="h-4 w-4"></i>
											</button>
											<?php if ($v_files_image->is_image == 1) { ?>
												<img data-dz-thumbnail src="<?php echo base_url() . $v_files_image->path ?>" class="upload-thumbnail-sm h-24 w-full rounded-lg object-cover" />
											<?php } else { ?>
												<div class="mailbox-attachment-icon flex h-24 w-full items-center justify-center rounded-lg border border-border bg-slate-50 text-slate-500 dark:bg-slate-900 dark:text-slate-300">
													<i data-lucide="file-text" class="h-5 w-5"></i>
												</div>
											<?php } ?>
											<input type="hidden" name="path[]" value="<?php echo $v_files_image->path ?>">
											<input type="hidden" name="fileName[]" value="<?php echo $v_files_image->fileName ?>">
											<input type="hidden" name="fullPath[]" value="<?php echo $v_files_image->fullPath ?>">
											<input type="hidden" name="size[]" value="<?php echo $v_files_image->size ?>">
											<input type="hidden" name="is_image[]" value="<?php echo $v_files_image->is_image ?>">
										</div>
									<?php } ?>
								</div>
							</div>
						<?php } ?>
					</div>
				</div>

			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('description'); ?></h3>
				</div>
				<div class="p-5">
					<?= form_textarea_group('item_desc', lang('description'), $item_desc, 4, false, array('class' => 'textarea_')); ?>
				</div>
			</div>

			<div class="rounded-2xl border border-border bg-card shadow-sm">
				<div class="flex items-center justify-between border-b border-border px-5 py-4">
					<h3 class="text-sm font-semibold text-foreground"><?= lang('additional_fields'); ?></h3>
				</div>
				<div class="p-5 space-y-4">
					<?php
					$saved_items_id = !empty($items_info) ? $items_info->saved_items_id : null;
					echo custom_form_Fields(18, $saved_items_id);
					?>
				</div>
			</div>
		</div>
	</div>

	<div class="flex justify-end gap-2">
		<?php if (!empty($items_info)) { ?>
			<?= btn_update('updates', 'sm'); ?>
			<button type="button" onclick="goBack()" class="btn h-9 mb-1 btn-danger btn-sm"><?= lang('cancel'); ?></button>
		<?php } else { ?>
			<?= btn_update('save', 'sm'); ?>
		<?php } ?>
	</div>
</div>

<?php echo form_close(); ?>

<script type="text/javascript">
	$(document).ready(function() {
		$('#select_warehouse').on('change', function() {
			var warehouse_id = $(this).val();
			var item_id = '<?= !empty($items_info->saved_items_id) ? $items_info->saved_items_id : ''; ?>';
			$.ajax({
				url: '<?= base_url() ?>admin/items/get_warehouse_product',
				type: 'POST',
				data: {
					warehouse_id: warehouse_id,
					item_id: item_id
				},
				dataType: 'json',
				success: function(data) {
					$('input[name="quantity"]').val(data.quantity);
				}
			});
		});

		$(document).on('click', '.existing_image', function() {
			$(this).parent().remove();
		});
	});
</script>

<style>
	.items-image-dropzone {
		min-height: 160px;
		border: 0;
		background: transparent;
		padding: 0;
	}

	.items-image-dropzone .dz-message {
		margin: 0;
	}

	#comments_file-previews .preview {
		width: 112px !important;
	}

	#comments_file-previews .upload-thumbnail-sm {
		border-radius: 10px;
		object-fit: cover;
	}

	.dark #comments_file-previews .mailbox-attachment-icon {
		background: #0f172a;
		color: #cbd5e1;
	}
</style>

<?php } ?>

