<!DOCTYPE html>
<html>
<head>
	<title><?= html_escape($title) ?></title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<?php
	$direction = $this->session->userdata('direction');
	$RTL = (!empty($direction) && $direction == 'rtl') ? 'on' : config_item('RTL');
	$text_align = !empty($RTL) ? 'right' : 'left';
	$opposite_align = !empty($RTL) ? 'left' : 'right';
	$logo = FCPATH . config_item('company_logo');
	if (!file_exists($logo)) {
		$logo = FCPATH . 'uploads/default_logo.png';
	}
	$currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
	$currency_symbol = !empty($currency->symbol) ? $currency->symbol : default_currency();
	$status_text = strip_tags((string) ($sales_info->status ?? ''));
	$status_parts = explode(':', $status_text, 2);
	$status = trim(count($status_parts) > 1 ? $status_parts[1] : $status_parts[0]);
	?>
	<style type="text/css">
		body {
			color: #1f2937;
			font-family: DejaVu Sans, Arial, Helvetica, sans-serif;
			font-size: 12px;
			line-height: 1.45;
			margin: 0;
		}
		.page {
			padding: 20px 22px 24px;
		}
		table {
			border-collapse: collapse;
			width: 100%;
		}
		.report-header {
			border-bottom: 2px solid #0f172a;
			margin-bottom: 16px;
			padding-bottom: 14px;
		}
		.logo-cell {
			vertical-align: top;
			width: 62px;
		}
		.logo {
			height: 52px;
			width: 52px;
		}
		.company-name {
			color: #0f172a;
			font-size: 22px;
			font-weight: bold;
			margin: 0 0 4px;
			text-align: <?= $text_align ?>;
		}
		.report-kicker {
			color: #64748b;
			font-size: 10px;
			letter-spacing: 0.8px;
			margin: 0 0 4px;
			text-align: <?= $text_align ?>;
			text-transform: uppercase;
		}
		.report-title {
			color: #111827;
			font-size: 16px;
			font-weight: bold;
			margin: 0;
			text-align: <?= $text_align ?>;
		}
		.summary {
			background: #f8fafc;
			border: 1px solid #dbe4ee;
			margin-bottom: 16px;
			padding: 12px 14px;
		}
		.summary td {
			padding: 5px 0;
			text-align: <?= $text_align ?>;
		}
		.summary-label {
			color: #64748b;
			font-size: 10px;
			text-transform: uppercase;
			width: 22%;
		}
		.summary-value {
			color: #111827;
			font-weight: bold;
			width: 28%;
		}
		.section-title {
			background: #0f172a;
			border: 1px solid #0f172a;
			color: #ffffff;
			font-size: 12px;
			font-weight: bold;
			padding: 8px 10px;
			text-align: <?= $text_align ?>;
		}
		.items th {
			background: #e2e8f0;
			border: 1px solid #cbd5e1;
			color: #334155;
			font-size: 10px;
			padding: 7px 8px;
			text-align: <?= $text_align ?>;
			text-transform: uppercase;
		}
		.items td {
			border: 1px solid #dbe4ee;
			padding: 8px;
			text-align: <?= $text_align ?>;
			vertical-align: top;
		}
		.items tr:nth-child(even) td {
			background: #f8fafc;
		}
		.number {
			text-align: <?= $opposite_align ?> !important;
			white-space: nowrap;
		}
		.index-col {
			width: 44px;
		}
		.amount-col {
			width: 120px;
		}
		.total-row td {
			background: #f1f5f9 !important;
			font-weight: bold;
		}
		.notes {
			border: 1px solid #dbe4ee;
			color: #334155;
			margin-top: 14px;
			min-height: 42px;
			padding: 10px;
			text-align: <?= $text_align ?>;
		}
		.footer-note {
			color: #94a3b8;
			font-size: 10px;
			margin-top: 14px;
			text-align: <?= $opposite_align ?>;
		}
	</style>
</head>
<body>
<div class="page">
	<div class="report-header">
		<table>
			<tr>
				<td class="logo-cell">
					<img class="logo" src="<?= $logo ?>" alt="<?= html_escape(config_item('company_name')) ?>">
				</td>
				<td>
					<p class="report-kicker"><?= lang('accounting') ?></p>
					<p class="company-name"><?= html_escape(config_item('company_name')) ?></p>
					<p class="report-title"><?= lang('journal_entry') ?></p>
				</td>
			</tr>
		</table>
	</div>

	<div class="summary">
		<table>
			<tr>
				<td class="summary-label"><?= lang('reference_no') ?></td>
				<td class="summary-value"><?= html_escape($sales_info->reference_no) ?></td>
				<td class="summary-label"><?= lang('date') ?></td>
				<td class="summary-value"><?= display_date($sales_info->date) ?></td>
			</tr>
			<tr>
				<td class="summary-label"><?= lang('created_by') ?></td>
				<td class="summary-value"><?= html_escape($sales_info->fullname) ?></td>
				<td class="summary-label"><?= lang('status') ?></td>
				<td class="summary-value"><?= html_escape($status) ?></td>
			</tr>
			<tr>
				<td class="summary-label"><?= lang('total_debit') ?></td>
				<td class="summary-value"><?= display_money($sales_info->total_debit, $currency_symbol) ?></td>
				<td class="summary-label"><?= lang('total_credit') ?></td>
				<td class="summary-value"><?= display_money($sales_info->total_credit, $currency_symbol) ?></td>
			</tr>
		</table>
	</div>

	<div class="section-title"><?= lang('details') ?></div>
	<table class="items">
		<thead>
		<tr>
			<th class="index-col"><?= lang('sl') ?></th>
			<th><?= lang('chart_of_accounts') ?></th>
			<th><?= lang('description') ?></th>
			<th class="amount-col number"><?= lang('debit') ?></th>
			<th class="amount-col number"><?= lang('credit') ?></th>
		</tr>
		</thead>
		<tbody>
		<?php if (!empty($receipt_items)): foreach ($receipt_items as $key => $item): ?>
			<tr>
				<td><?= $key + 1 ?></td>
				<td><?= html_escape($item->name) ?><?= !empty($item->code) ? ' (' . html_escape($item->code) . ')' : '' ?></td>
				<td><?= !empty($item->description) ? nl2br(html_escape($item->description)) : '-' ?></td>
				<td class="number"><?= display_money($item->debit, $currency_symbol) ?></td>
				<td class="number"><?= display_money($item->credit, $currency_symbol) ?></td>
			</tr>
		<?php endforeach; else: ?>
			<tr>
				<td colspan="5"><?= lang('nothing_to_display') ?></td>
			</tr>
		<?php endif; ?>
		</tbody>
		<tfoot>
		<tr class="total-row">
			<td colspan="3" class="number"><?= lang('total') ?></td>
			<td class="number"><?= display_money($sales_info->total_debit, $currency_symbol) ?></td>
			<td class="number"><?= display_money($sales_info->total_credit, $currency_symbol) ?></td>
		</tr>
		</tfoot>
	</table>

	<?php if (!empty($sales_info->notes)): ?>
		<div class="notes">
			<strong><?= lang('notes') ?>:</strong><br>
			<?= nl2br(html_escape($sales_info->notes)) ?>
		</div>
	<?php endif; ?>

	<div class="footer-note">
		<?= html_escape(config_item('company_name')) ?> &middot; <?= lang('journal_entry') ?>
	</div>
</div>
</body>
</html>
