<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
$group = $this->db->where('customer_group_id', $items_info->customer_group_id)->get('tbl_customer_group')->row();
$currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$manufacturer_info = get_row('tbl_manufacturer', array('manufacturer_id' => $items_info->manufacturer_id));
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : default_currency();
$company_name = config_item('company_name') ?: config_item('website_name');
$company_address = config_item('company_address') ?: '';
$group_name = !empty($group->customer_group) ? $group->customer_group : '-';
$manufacturer_name = !empty($manufacturer_info->manufacturer) ? $manufacturer_info->manufacturer : '-';
$invoice_view = config_item('invoice_view');

$tax_rows = array();
$tax_rates = null;
if (!empty($items_info->tax_rates_id) && !is_numeric($items_info->tax_rates_id)) {
	$tax_rates = json_decode((string) $items_info->tax_rates_id);
}
if (!empty($tax_rates) && is_array($tax_rates)) {
	foreach ($tax_rates as $key => $tax_id) {
		$taxes_info = $this->db->where('tax_rates_id', $tax_id)->get('tbl_tax_rates')->row();
		if (!empty($taxes_info)) {
			$tax_rows[] = ($key + 1) . '. ' . $taxes_info->tax_rate_name . ' (' . $taxes_info->tax_rate_percent . '%)';
		}
	}
}

$uploaded_file = array();
if (!empty($items_info->upload_file)) {
	$uploaded_file = json_decode($items_info->upload_file);
}
$image_src = '';
if (!empty($uploaded_file[0]->is_image) && (int) $uploaded_file[0]->is_image === 1 && !empty($uploaded_file[0]->path)) {
	$local_image = FCPATH . ltrim($uploaded_file[0]->path, '/');
	if (is_file($local_image)) {
		$image_src = $local_image;
	}
}

$barcode_type_map = array(
	'code25' => 'S25',
	'code39' => 'C39',
	'code128' => 'C128B',
	'ean8' => 'EAN8',
	'ean13' => 'EAN13',
	'upca' => 'UPCA',
	'upce' => 'UPCE',
);
$barcode_symbology = strtolower((string) ($items_info->barcode_symbology ?: 'code128'));
$barcode_type = isset($barcode_type_map[$barcode_symbology]) ? $barcode_type_map[$barcode_symbology] : 'C128B';
$barcode_code = !empty($items_info->code) ? (string) $items_info->code : (string) $items_info->saved_items_id;
$details_rows = array(
	array(lang('name'), $items_info->item_name),
	array(lang('code'), !empty($items_info->code) ? $items_info->code : '-'),
	array(lang('barcode_symbology'), !empty($items_info->barcode_symbology) ? $items_info->barcode_symbology : '-'),
	array(lang('group'), $group_name),
	array(lang('manufacturer'), $manufacturer_name),
	array(lang('unit') . ' ' . lang('type'), !empty($items_info->unit_type) ? $items_info->unit_type : '-'),
);
if (!empty($invoice_view) && $invoice_view == '2') {
	$details_rows[] = array(lang('hsn_code'), !empty($items_info->hsn_code) ? $items_info->hsn_code : '-');
}
?>
<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<style>
		body {
			font-family: dejavusanscondensed, Arial, sans-serif;
			color: #111827;
			font-size: 11px;
			line-height: 1.45;
		}
		.header {
			border-bottom: 2px solid #111827;
			padding-bottom: 14px;
			margin-bottom: 18px;
		}
		.brand {
			font-size: 18px;
			font-weight: 700;
			color: #111827;
		}
		.document-title {
			text-align: right;
			font-size: 22px;
			font-weight: 700;
			color: #0f766e;
		}
		.muted {
			color: #6b7280;
		}
		.hero {
			background: #f8fafc;
			border: 1px solid #dbe3ea;
			border-radius: 10px;
			padding: 16px;
			margin-bottom: 16px;
		}
		.item-name {
			font-size: 20px;
			font-weight: 700;
			margin: 0 0 6px;
		}
		.pill {
			display: inline-block;
			border: 1px solid #cbd5e1;
			border-radius: 999px;
			padding: 4px 9px;
			margin-right: 5px;
			margin-top: 5px;
			font-size: 10px;
			color: #334155;
			background: #ffffff;
		}
		.metric {
			border: 1px solid #dbe3ea;
			border-radius: 9px;
			padding: 11px;
			background: #ffffff;
		}
		.metric-label {
			font-size: 9px;
			text-transform: uppercase;
			letter-spacing: .08em;
			color: #64748b;
			font-weight: 700;
		}
		.metric-value {
			margin-top: 5px;
			font-size: 14px;
			font-weight: 700;
			color: #111827;
		}
		.section {
			margin-top: 16px;
			border: 1px solid #dbe3ea;
			border-radius: 10px;
			overflow: hidden;
		}
		.section-title {
			background: #f1f5f9;
			border-bottom: 1px solid #dbe3ea;
			padding: 9px 12px;
			font-size: 10px;
			font-weight: 700;
			text-transform: uppercase;
			letter-spacing: .08em;
			color: #334155;
		}
		.section-body {
			padding: 12px;
		}
		table {
			width: 100%;
			border-collapse: collapse;
		}
		.detail-table td {
			border-bottom: 1px solid #e5e7eb;
			padding: 8px 7px;
			vertical-align: top;
		}
		.detail-table tr:last-child td {
			border-bottom: 0;
		}
		.label {
			width: 34%;
			color: #64748b;
			font-weight: 700;
			text-transform: uppercase;
			font-size: 9px;
			letter-spacing: .06em;
		}
		.value {
			color: #111827;
			font-weight: 600;
		}
		.warehouse-table th,
		.warehouse-table td {
			border-bottom: 1px solid #e5e7eb;
			padding: 8px;
		}
		.warehouse-table th {
			background: #f8fafc;
			color: #475569;
			font-size: 9px;
			text-transform: uppercase;
			letter-spacing: .06em;
			text-align: left;
		}
		.text-right {
			text-align: right;
		}
		.product-image {
			width: 155px;
			height: 155px;
			object-fit: cover;
			border: 1px solid #dbe3ea;
			border-radius: 10px;
			padding: 5px;
			background: #fff;
		}
		.no-image {
			width: 155px;
			height: 155px;
			border: 1px dashed #cbd5e1;
			border-radius: 10px;
			text-align: center;
			color: #64748b;
			padding-top: 64px;
			background: #f8fafc;
		}
		.barcode-box {
			border: 1px solid #dbe3ea;
			border-radius: 9px;
			padding: 12px;
			background: #fff;
			text-align: center;
		}
		.footer {
			margin-top: 18px;
			padding-top: 10px;
			border-top: 1px solid #dbe3ea;
			font-size: 9px;
			color: #64748b;
			text-align: right;
		}
	</style>
</head>
<body>
	<table class="header">
		<tr>
			<td>
				<div class="brand"><?= html_escape($company_name ?: '-') ?></div>
				<?php if (!empty($company_address)) { ?>
					<div class="muted"><?= html_escape($company_address) ?></div>
				<?php } ?>
				<div class="muted"><?= html_escape(display_date(date('Y-m-d'))) ?></div>
			</td>
			<td class="document-title"><?= html_escape(lang('items_details')) ?></td>
		</tr>
	</table>

	<table class="hero">
		<tr>
			<td style="width: 70%; vertical-align: top;">
				<h1 class="item-name"><?= html_escape($items_info->item_name) ?></h1>
				<div class="muted"><?= html_escape(lang('code')) ?>: <?= html_escape(!empty($items_info->code) ? $items_info->code : '-') ?></div>
				<div>
					<span class="pill"><?= html_escape(lang('group')) ?>: <?= html_escape($group_name) ?></span>
					<span class="pill"><?= html_escape(lang('manufacturer')) ?>: <?= html_escape($manufacturer_name) ?></span>
					<span class="pill"><?= html_escape(lang('barcode_symbology')) ?>: <?= html_escape($items_info->barcode_symbology ?: '-') ?></span>
				</div>
			</td>
			<td style="width: 30%; text-align: right;">
				<?php if (!empty($image_src)) { ?>
					<img class="product-image" src="<?= html_escape($image_src) ?>" alt="<?= html_escape($items_info->item_name) ?>">
				<?php } else { ?>
					<div class="no-image"><?= html_escape(lang('no_file_uploaded')) ?></div>
				<?php } ?>
			</td>
		</tr>
	</table>

	<table style="margin-bottom: 16px;">
		<tr>
			<td style="width: 33.33%; padding-right: 8px;">
				<div class="metric">
					<div class="metric-label"><?= html_escape(lang('unit_price')) ?></div>
					<div class="metric-value"><?= display_money($items_info->unit_cost, $currency_symbol) ?></div>
				</div>
			</td>
			<td style="width: 33.33%; padding-right: 8px;">
				<div class="metric">
					<div class="metric-label"><?= html_escape(lang('cost_price')) ?></div>
					<div class="metric-value"><?= display_money($items_info->cost_price, $currency_symbol) ?></div>
				</div>
			</td>
			<td style="width: 33.33%;">
				<div class="metric">
					<div class="metric-label"><?= html_escape(lang('unit') . ' ' . lang('type')) ?></div>
					<div class="metric-value"><?= html_escape(!empty($items_info->unit_type) ? $items_info->unit_type : '-') ?></div>
				</div>
			</td>
		</tr>
	</table>

	<div class="section">
		<div class="section-title"><?= html_escape(lang('item') . ' ' . lang('details')) ?></div>
		<div class="section-body">
			<table class="detail-table">
				<?php foreach ($details_rows as $row) { ?>
					<tr>
						<td class="label"><?= html_escape($row[0]) ?></td>
						<td class="value"><?= html_escape($row[1]) ?></td>
					</tr>
				<?php } ?>
				<tr>
					<td class="label"><?= html_escape(lang('tax')) ?></td>
					<td class="value"><?= !empty($tax_rows) ? html_escape(implode(', ', $tax_rows)) : '-' ?></td>
				</tr>
			</table>
		</div>
	</div>

	<div class="section">
		<div class="section-title"><?= html_escape(lang('barcode')) ?></div>
		<div class="section-body">
			<div class="barcode-box">
				<barcode code="<?= html_escape($barcode_code) ?>" type="<?= html_escape($barcode_type) ?>" height="1.1" text="1" />
			</div>
		</div>
	</div>

	<?php if (!empty(admin()) && !empty($warehouse_info)) { ?>
		<div class="section">
			<div class="section-title"><?= html_escape(lang('warehouse')) ?></div>
			<div class="section-body">
				<table class="warehouse-table">
					<thead>
						<tr>
							<th><?= html_escape(lang('warehouse')) ?></th>
							<th class="text-right"><?= html_escape(lang('quantity')) ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ($warehouse_info as $warehouse) { ?>
							<tr>
								<td><?= html_escape($warehouse->warehouse_name) ?></td>
								<td class="text-right"><?= html_escape($warehouse->quantity) ?></td>
							</tr>
						<?php } ?>
					</tbody>
				</table>
			</div>
		</div>
	<?php } elseif (!empty($staff_warehouse_info)) { ?>
		<div class="section">
			<div class="section-title"><?= html_escape(lang('warehouse')) ?></div>
			<div class="section-body">
				<table class="warehouse-table">
					<tr>
						<td><?= html_escape($staff_warehouse_info->warehouse_name) ?></td>
						<td class="text-right"><?= html_escape($staff_warehouse_info->quantity) ?></td>
					</tr>
				</table>
			</div>
		</div>
	<?php } ?>

	<?php if (!empty($items_info->item_desc)) { ?>
		<div class="section">
			<div class="section-title"><?= html_escape(lang('description')) ?></div>
			<div class="section-body"><?= strip_html_tags($items_info->item_desc) ?></div>
		</div>
	<?php } ?>

	<div class="footer">
		<?= html_escape(lang('items_details')) ?> - <?= html_escape($company_name ?: '-') ?>
	</div>
</body>
</html>
