<?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');
$invoice_view = config_item('invoice_view');
$show_hsn = !empty($invoice_view) && $invoice_view == '2';
$show_cost_price = !empty(admin());
$custom_headers = custom_form_table(18, null);
?>

<style type="text/css">


	.item-row-card {
		display: flex;
		flex-direction: column;
		gap: 0.7rem;
		min-width: 260px;
	}

	.item-row-title {
		display: inline-block;
		font-size: 0.9rem;
		font-weight: 700;
		line-height: 1.35;
		color: hsl(var(--foreground));
		text-decoration: none;
	}

	.item-row-title:hover {
		color: hsl(var(--primary));
	}

	.item-row-meta {
		display: flex;
		flex-wrap: wrap;
		gap: 0.45rem;
	}

	.item-row-chip {
		display: inline-flex;
		align-items: center;
		gap: 0.4rem;
		border: 1px solid hsl(var(--border));
		background: hsl(var(--muted) / 0.18);
		border-radius: 999px;
		padding: 0.32rem 0.65rem;
		font-size: 0.75rem;
		line-height: 1;
	}

	.item-row-chip-label {
		color: hsl(var(--muted-foreground));
		font-weight: 600;
	}

	.item-row-chip-value {
		color: hsl(var(--foreground));
		font-weight: 700;
	}

	.item-tax-list {
		font-size: 0.8rem;
		line-height: 1.6;
		color: hsl(var(--foreground));
	}

	.item-group-badge {
		display: inline-flex;
		align-items: center;
		border: 1px solid hsl(var(--border));
		background: hsl(var(--background));
		border-radius: 999px;
		padding: 0.32rem 0.75rem;
		font-size: 0.75rem;
		font-weight: 700;
		color: hsl(var(--foreground));
	}

	.item-status-wrap {
		display: inline-flex;
		align-items: center;
		gap: 0.75rem;
	}

	.item-status-badge {
		display: inline-flex;
		align-items: center;
		border-radius: 999px;
		padding: 0.35rem 0.7rem;
		font-size: 0.75rem;
		font-weight: 700;
	}

	.item-status-badge.is-active {
		background: rgb(236 253 245);
		color: rgb(5 150 105);
	}

	.item-status-badge.is-inactive {
		background: rgb(254 242 242);
		color: rgb(225 29 72);
	}

	.item-status-toggle {
		position: relative;
		display: inline-flex;
		width: 2.3rem;
		height: 1.35rem;
		border-radius: 999px;
		background: rgb(203 213 225);
		transition: background 0.2s ease;
	}

	.item-status-toggle::after {
		content: "";
		position: absolute;
		top: 0.16rem;
		left: 0.16rem;
		width: 1rem;
		height: 1rem;
		border-radius: 999px;
		background: #fff;
		box-shadow: 0 1px 2px rgba(15, 23, 42, 0.15);
		transition: transform 0.2s ease;
	}

	.item-status-toggle.is-active {
		background: rgb(34 197 94);
	}

	.item-status-toggle.is-active::after {
		transform: translateX(0.95rem);
	}

	.item-actions-cell {
		display: flex;
		flex-wrap: wrap;
		gap: 0.45rem;
		min-width: 150px;
	}

	.item-actions-cell .btn {
		margin: 0 !important;
	}
</style>

<div class="space-y-5">
	<div class="rounded-2xl border border-border bg-card shadow-sm">
		<div class="p-5">
					<table class="table table-striped DataTables bulk_table mb-0" id="DataTables" cellspacing="0" width="100%">
						<thead>
							<tr>
								<?php if (!empty($deleted)) { ?>
									<th data-orderable="false">
										<div class="checkbox c-checkbox">
											<label class="needsclick">
												<input id="select_all" type="checkbox">
											</label>
										</div>
									</th>
								<?php } ?>
								<th><?= lang('item'); ?></th>
								<?php if ($show_hsn) { ?>
									<th><?= lang('hsn_code'); ?></th>
								<?php } ?>
								<?php if ($show_cost_price) { ?>
									<th class="col-sm-1"><?= lang('cost_price'); ?></th>
								<?php } ?>
								<th class="col-sm-1"><?= lang('unit_price'); ?></th>
								<th class="col-sm-1"><?= lang('unit') . ' ' . lang('type'); ?></th>
								<th class="col-sm-2"><?= lang('tax'); ?></th>
								<th class="col-sm-1"><?= lang('group'); ?></th>
								<th class="col-sm-1"><?= lang('status'); ?></th>
								<?php
								if (!empty($custom_headers)) {
									foreach ($custom_headers as $c_label => $v_fields) {
										if (!empty($c_label)) {
								?>
											<th><?= $c_label; ?></th>
								<?php
										}
									}
								}
								?>
								<?php if (!empty($edited) || !empty($deleted)) { ?>
									<th class="col-options no-sort col-sm-1 text-right"><?= lang('action'); ?></th>
								<?php } ?>
							</tr>
						</thead>
						<tbody></tbody>
					</table>
		</div>
	</div>
</div>

<script>
	list = base_url + "admin/items/itemsList";
	window.erpDatatablePreload = window.erpDatatablePreload || {};
	window.erpDatatablePreload.DataTables = window.erpDatatablePreload.DataTables || {};
	<?php if (!empty($datatable_preload)) { ?>
	window.erpDatatablePreload.DataTables["list"] = <?= json_encode($datatable_preload, JSON_UNESCAPED_SLASHES) ?>;
	<?php } ?>
	bulk_url = base_url + "admin/items/bulk_delete";

	function getSelectedItemIds() {
		return $('#DataTables tbody .js-item-row-check:checked').map(function() {
			return $(this).val();
		}).get();
	}

	function getItemsTable() {
		if ($.fn.dataTable && $.fn.dataTable.isDataTable('#DataTables')) {
			return $('#DataTables').DataTable();
		}
		return null;
	}

	function updateItemSelectionState() {
		var ids = getSelectedItemIds();
		var allRows = $('#DataTables tbody .js-item-row-check');
		var allChecked = allRows.length > 0 && allRows.length === ids.length;

		$('#select_all').prop('checked', allChecked);
	}

	function reloadItems(filter, searchType) {
		var url = list;
		if (filter) {
			url += "/" + filter;
			if (searchType) {
				url += "/" + searchType;
			}
		}
		if (typeof table_url !== "undefined") {
			table_url(url);
		} else if (typeof fire_datatable === "function") {
			fire_datatable(url);
		}
	}

	$(function() {
		$(document).on('click', '[data-item-filter-action]', function(e) {
			e.preventDefault();
			var $item = $(this);
			var $dropdown = $item.closest('details');
			$dropdown.find('[data-header-filter-item]').removeClass('bg-indigo-50 text-indigo-700 dark:bg-indigo-900/30 dark:text-indigo-300');
			$dropdown.find('[data-header-filter-item]').addClass('text-slate-700 hover:bg-slate-50 dark:text-slate-200 dark:hover:bg-slate-800');
			$item.addClass('bg-indigo-50 text-indigo-700 dark:bg-indigo-900/30 dark:text-indigo-300');
			$item.removeClass('text-slate-700 hover:bg-slate-50 dark:text-slate-200 dark:hover:bg-slate-800');
			$dropdown.find('[data-header-filter-selected]').text($item.data('item-filter-label') || '<?= html_escape(lang('all')); ?>');
			$dropdown.prop('open', false);
			reloadItems($item.data('item-filter') || '', $item.data('item-search') || '');
		});

		window.itemsGroupFilter = function(id) {
			reloadItems(id, 'by_group');
		};
		window.itemsManufacturerFilter = function(id) {
			reloadItems(id, 'by_manufacturer');
		};
		window.itemsWarehouseFilter = function(id) {
			reloadItems(id, 'by_warehouse');
		};

		$(document).on('change', '#select_all', function() {
			$('#DataTables tbody .js-item-row-check').prop('checked', this.checked);
			updateItemSelectionState();
		});

		$(document).on('change', '#DataTables tbody .js-item-row-check', function() {
			updateItemSelectionState();
		});

		$(document).on('dt:bulkDelete', function() {
			var ids = getSelectedItemIds();
			if (!ids.length) {
				return;
			}
			if (!window.confirm(<?= json_encode(lang('delete_alert')) ?>)) {
				return;
			}

			$.ajax({
				url: bulk_url,
				type: 'POST',
				dataType: 'json',
				data: {
					ids: ids,
					csrf_token: typeof getCookie === 'function' ? getCookie('csrf_cookie') : ''
				}
			}).always(function() {
				var table = getItemsTable();
				if (table) {
					table.ajax.reload(null, false);
				}
				$('#select_all').prop('checked', false);
				updateItemSelectionState();
			});
		});

		$('#DataTables').on('draw.dt', function() {
			$('#select_all').prop('checked', false);
			updateItemSelectionState();
		});
	});
</script>
