<?php
$proposal_id = $proposal_id ?? ($proposals_info->proposals_id ?? null);
$reference_no = $proposals_info->reference_no ?? '';
$status_value = strtolower((string) ($proposals_info->status ?? ''));
$status_label = !empty($status_label) ? $status_label : lang($proposals_info->status ?? 'proposal');
$currency_symbol = $currency_symbol ?? (is_object($currency ?? null) && !empty($currency->symbol) ? $currency->symbol : (is_string($currency ?? null) ? $currency : default_currency()));
$client_name = $client_name ?? '-';
$address = $address ?? '-';
$city = $city ?? '-';
$zipcode = $zipcode ?? '-';
$country = $country ?? '-';
$phone = $phone ?? '-';
$vat = $vat ?? '';
$subtotal = $subtotal ?? 0;
$discount = $discount ?? 0;
$total = $total ?? 0;
$tax_rows = $tax_rows ?? array();
$all_items = $all_items ?? array();
$custom_field_markup = $custom_field_markup ?? '';
$overdue_days = isset($overdue_days) ? $overdue_days : 0;
$overdue_message = $overdue_message ?? '';
$client_lang = $client_lang ?? 'english';
$language_info = $language_info ?? array();
$rootPath = defined('ROOTPATH') ? ROOTPATH : FCPATH;

if (is_file(config_item('invoice_logo'))) {
	$logo = $rootPath . '/' . config_item('invoice_logo');
} elseif (is_file($rootPath . '/' . config_item('invoice_logo'))) {
	$logo = $rootPath . '/' . config_item('invoice_logo');
} else {
	$logo = $rootPath . '/' . 'uploads/default_logo.png';
}

if (!file_exists($logo)) {
	$logo = $rootPath . '/' . 'uploads/default_logo.png';
}

if (!function_exists('proposal_pdf_party_text')) {
	function proposal_pdf_party_text($label, $value)
	{
		return '<div class="pdf-party-line"><span class="pdf-party-dot"></span><span>' . html_escape($label) . '</span> <strong>' . html_escape($value) . '</strong></div>';
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title><?= html_escape(lang('proposal')) ?></title>
	<style type="text/css">
		@page {
			margin: 18mm 14mm 16mm;
		}

		body {
			margin: 0;
			color: #172033;
			background: #f3f6fb;
			font-size: 12px;
			line-height: 1.55;
			font-family: "DejaVu Sans", "Source Sans Pro", Arial, sans-serif;
			<?php if (!empty($RTL)) { ?> direction: rtl; text-align: right; <?php } ?>
		}

		* {
			box-sizing: border-box;
		}

		a {
			color: #4f46e5;
			text-decoration: none;
		}

		.pdf-wrap {
			background: #f3f6fb;
		}

		.pdf-sheet {
			background: #ffffff;
			border: 1px solid #dfe6f1;
			border-radius: 12px;
			overflow: hidden;
		}

		.pdf-paper {
			padding: 22px 24px 18px;
			background: #fff;
		}

		.pdf-header-table,
		.pdf-meta-table,
		.pdf-party-table,
		.pdf-bottom-table {
			width: 100%;
			border-collapse: collapse;
		}

		.pdf-header-table td {
			vertical-align: top;
		}

		.pdf-brand {
			display: inline-block;
			font-size: 22px;
			font-weight: 800;
			letter-spacing: -0.04em;
			color: #2b3a70;
			line-height: 1;
		}

		.pdf-brand-badge {
			display: inline-block;
			width: 28px;
			height: 28px;
			border-radius: 9px;
			margin-right: 8px;
			background: linear-gradient(135deg, #5b5ef2 0%, #4f46e5 100%);
			color: #fff;
			font-size: 14px;
			line-height: 28px;
			text-align: center;
			font-weight: 800;
			vertical-align: middle;
		}

		.pdf-logo {
			display: inline-block;
			height: 34px;
			width: auto;
			margin-right: 10px;
			vertical-align: middle;
		}

		.pdf-title {
			font-size: 30px;
			font-weight: 900;
			line-height: 0.95;
			letter-spacing: -0.04em;
			color: #0f172a;
			text-align: right;
		}

		.pdf-title-ref {
			margin-top: 5px;
			font-size: 14px;
			font-weight: 800;
			color: #64748b;
			text-align: right;
		}

		.pdf-metrics {
			margin-top: 22px;
			border: 1px solid #e9eef6;
			border-radius: 10px;
			overflow: hidden;
		}

		.pdf-metrics-table {
			width: 100%;
			border-collapse: collapse;
		}

		.pdf-metrics-table td {
			width: 25%;
			padding: 12px 14px;
			border-right: 1px solid #e9eef6;
			background: #f9fbfe;
			vertical-align: top;
		}

		.pdf-metrics-table td:last-child {
			border-right: 0;
		}

		.pdf-metric-label {
			font-size: 9px;
			font-weight: 800;
			letter-spacing: 0.18em;
			text-transform: uppercase;
			color: #9aa6b8;
		}

		.pdf-metric-value {
			margin-top: 6px;
			font-size: 12px;
			font-weight: 800;
			color: #0f172a;
		}

		.pdf-metric-value--accent {
			color: #4f46e5;
			font-size: 15px;
		}

		.pdf-two-col {
			margin-top: 24px;
		}

		.pdf-two-col-table {
			width: 100%;
			border-collapse: collapse;
		}

		.pdf-two-col-table td {
			width: 50%;
			vertical-align: top;
			padding-right: 16px;
		}

		.pdf-block-title {
			font-size: 9px;
			font-weight: 800;
			letter-spacing: 0.2em;
			text-transform: uppercase;
			color: #9aa6b8;
			margin-bottom: 10px;
		}

		.pdf-party-name {
			font-size: 14px;
			font-weight: 800;
			color: #0f172a;
		}

		.pdf-party-sub {
			margin-top: 5px;
			font-size: 11px;
			color: #475569;
		}

		.pdf-address {
			margin-top: 4px;
			font-size: 11px;
			line-height: 1.55;
			color: #64748b;
		}

		.pdf-party-line {
			margin-top: 3px;
			font-size: 11px;
			color: #64748b;
		}

		.pdf-party-dot {
			display: inline-block;
			width: 5px;
			height: 5px;
			margin-right: 6px;
			border-radius: 50%;
			background: #cbd5e1;
			vertical-align: middle;
		}

		.pdf-custom {
			margin-top: 12px;
		}

		.pdf-chip {
			display: inline-block;
			margin: 0 6px 6px 0;
			padding: 5px 9px;
			border-radius: 999px;
			background: #eef2ff;
			border: 1px solid #d9dff9;
			color: #4338ca;
			font-size: 10px;
			font-weight: 700;
		}

		.pdf-items {
			margin-top: 22px;
		}

		.pdf-table {
			width: 100%;
			border-collapse: collapse;
			table-layout: fixed;
		}

		.pdf-table thead th {
			padding: 9px 0 8px;
			border-bottom: 1px solid #0f172a;
			font-size: 10px;
			font-weight: 800;
			text-align: left;
			color: #0f172a;
		}

		.pdf-table thead th:nth-child(3),
		.pdf-table thead th:nth-child(4),
		.pdf-table thead th:nth-child(5),
		.pdf-table thead th:nth-child(6) {
			text-align: right;
		}

		.pdf-table tbody td {
			padding: 11px 0 9px;
			border-bottom: 0;
			vertical-align: top;
			font-size: 11px;
			color: #0f172a;
		}

		.pdf-item-name {
			font-size: 11px;
			font-weight: 800;
			color: #0f172a;
		}

		.pdf-item-desc {
			margin-top: 3px;
			font-size: 10px;
			line-height: 1.5;
			color: #64748b;
		}

		.pdf-right {
			text-align: right;
		}

		.pdf-totals {
			margin-top: 16px;
			display: table;
			width: 100%;
		}

		.pdf-totals-right {
			display: table;
			width: 260px;
			margin-left: auto;
		}

		.pdf-total-row {
			display: table-row;
		}

		.pdf-total-cell {
			display: table-cell;
			padding: 3px 0;
			font-size: 11px;
			color: #475569;
		}

		.pdf-total-cell strong {
			color: #0f172a;
		}

		.pdf-total-cell--value {
			text-align: right;
			font-weight: 800;
			color: #0f172a;
		}

		.pdf-total-line {
			height: 1px;
			margin: 6px 0 8px;
			background: #0f172a;
		}

		.pdf-grand {
			display: table;
			width: 100%;
			font-size: 13px;
			font-weight: 900;
			color: #0f172a;
		}

		.pdf-grand .pdf-total-cell--value {
			font-size: 14px;
			color: #4f46e5;
		}

		.pdf-bottom {
			margin-top: 22px;
			padding-top: 16px;
			border-top: 1px solid #e7ecf3;
		}

		.pdf-bottom-table td {
			width: 50%;
			vertical-align: top;
			padding-right: 16px;
		}

		.pdf-note-title {
			font-size: 9px;
			font-weight: 800;
			letter-spacing: 0.14em;
			text-transform: uppercase;
			color: #0f172a;
			margin-bottom: 8px;
		}

		.pdf-note-body {
			font-size: 11px;
			line-height: 1.7;
			color: #64748b;
		}

		.pdf-footer {
			padding: 10px 14px 12px;
			border-top: 1px solid #edf2f8;
			font-size: 9px;
			color: #94a3b8;
			text-align: center;
		}

		.pdf-muted {
			color: #94a3b8;
		}
	</style>
</head>
<body>
<div class="pdf-wrap">
	<div class="pdf-sheet">
		<div class="pdf-paper">
			<table class="pdf-topbar-table">
				<tr>
					<td>
						<div class="pdf-ref"><?= html_escape($reference_no) ?></div>
						<?php if (!empty($overdue_message)) { ?>
							<div class="pdf-overdue"><?= html_escape($overdue_message) ?></div>
						<?php } ?>
					</td>
					<td class="pdf-actions"></td>
				</tr>
			</table>

			<table class="pdf-header-table">
				<tr>
					<td>
						<div class="pdf-brand">
							<?php if (!empty($logo) && file_exists($logo)) { ?>
								<img src="<?= html_escape($logo) ?>" class="pdf-logo" alt="logo">
							<?php } else { ?>
								<span class="pdf-brand-badge">Z</span>
							<?php } ?>
							<span>ZiscoERP</span>
						</div>
					</td>
					<td>
						<div class="pdf-title"><?= html_escape(lang('proposal')) ?></div>
						<div class="pdf-title-ref"># <?= html_escape($reference_no) ?></div>
					</td>
				</tr>
			</table>

			<div class="pdf-metrics">
				<table class="pdf-metrics-table">
					<tr>
						<td>
							<div class="pdf-metric-label">Date Issued</div>
							<div class="pdf-metric-value"><?= display_date($proposals_info->proposal_date) ?></div>
						</td>
						<td>
							<div class="pdf-metric-label">Valid Until</div>
							<div class="pdf-metric-value" style="color:#ef4444;"><?= display_date($proposals_info->due_date) ?></div>
						</td>
						<td>
							<div class="pdf-metric-label">Sales Agent</div>
							<div class="pdf-metric-value"><?= html_escape(!empty($proposals_info->user_id) ? fullname($proposals_info->user_id) : '-') ?></div>
						</td>
						<td>
							<div class="pdf-metric-label">Amount Due</div>
							<div class="pdf-metric-value pdf-metric-value--accent"><?= display_money($total, $currency_symbol) ?></div>
						</td>
					</tr>
				</table>
			</div>

			<div class="pdf-two-col">
				<table class="pdf-two-col-table">
					<tr>
						<td>
							<div class="pdf-block-title">From</div>
							<div class="pdf-party-name"><?= html_escape(config_item('company_legal_name_' . $client_lang) ? config_item('company_legal_name_' . $client_lang) : config_item('company_legal_name')) ?></div>
							<div class="pdf-party-sub"><?= html_escape(config_item('company_legal_name')) ?></div>
							<div class="pdf-address">
								<div><?= html_escape(config_item('company_address_' . $client_lang) ? config_item('company_address_' . $client_lang) : config_item('company_address')) ?></div>
								<div><?= html_escape((config_item('company_city_' . $client_lang) ? config_item('company_city_' . $client_lang) : config_item('company_city')) . ', ' . config_item('company_zip_code')) ?></div>
								<div><?= html_escape(config_item('company_country_' . $client_lang) ? config_item('company_country_' . $client_lang) : config_item('company_country')) ?></div>
								<?php if (!empty(config_item('company_phone'))) { ?><div><?= lang('phone') ?>: <?= html_escape(config_item('company_phone')) ?></div><?php } ?>
								<?php if (!empty(config_item('company_vat'))) { ?><div><?= lang('vat_number') ?>: <?= html_escape(config_item('company_vat')) ?></div><?php } ?>
							</div>
						</td>
						<td>
							<div class="pdf-block-title">Prepared For</div>
							<div class="pdf-party-name"><?= html_escape($client_name) ?></div>
							<div class="pdf-address">
								<div>Client Address <?= html_escape((string) $proposal_id) ?></div>
								<div><?= html_escape($address) ?></div>
								<div><?= html_escape(trim($city . ', ' . $zipcode)) ?></div>
								<div><?= html_escape($country) ?></div>
								<?php if (!empty($phone)) { ?><div><?= lang('phone') ?>: <?= html_escape($phone) ?></div><?php } ?>
								<?php if (!empty($vat)) { ?><div><?= lang('vat_number') ?>: <?= html_escape($vat) ?></div><?php } ?>
							</div>
						</td>
					</tr>
				</table>
			</div>

			<?php if (!empty($custom_field_markup)) { ?>
				<div class="pdf-custom"><?= $custom_field_markup ?></div>
			<?php } ?>

			<div class="pdf-items">
				<table class="pdf-table">
					<thead>
					<tr>
						<th style="width:18px;">#</th>
						<th>Description</th>
						<th style="width:64px;" class="pdf-right">Qty</th>
						<th style="width:84px;" class="pdf-right">Price</th>
						<th style="width:92px;" class="pdf-right">Tax</th>
						<th style="width:92px;" class="pdf-right">Total</th>
					</tr>
					</thead>
					<tbody>
					<?php if (!empty($all_items)) : ?>
						<?php foreach ($all_items as $index => $item) : ?>
							<?php
							$item_name = !empty($item->item_name) ? $item->item_name : $item->item_desc;
							$item_taxes = array();
							if (!empty($item->item_tax_name)) {
								$decoded_taxes = json_decode($item->item_tax_name);
								if (is_array($decoded_taxes)) {
									foreach ($decoded_taxes as $raw_tax) {
										$tax = explode('|', $raw_tax);
										$item_taxes[] = array('label' => $tax[0] ?? '', 'percent' => $tax[1] ?? '');
									}
								}
							}
							?>
							<tr>
								<td><?= $index + 1 ?></td>
								<td>
									<div class="pdf-item-name"><?= html_escape($item_name) ?></div>
									<div class="pdf-item-desc"><?= nl2br(html_escape($item->item_desc)) ?></div>
								</td>
								<td class="pdf-right"><?= html_escape($item->quantity . ' ' . $item->unit) ?></td>
								<td class="pdf-right"><?= display_money($item->unit_cost, $currency_symbol) ?></td>
								<td class="pdf-right">
									<?php if (!empty($item_taxes)) : ?>
										<?php foreach ($item_taxes as $tax) : ?>
											<div><?= html_escape($tax['label']) ?> (<?= html_escape($tax['percent']) ?>%)</div>
										<?php endforeach; ?>
									<?php else : ?>
										<span class="pdf-muted">-</span>
									<?php endif; ?>
								</td>
								<td class="pdf-right" style="font-weight:800;"><?= display_money($item->total_cost, $currency_symbol) ?></td>
							</tr>
						<?php endforeach; ?>
					<?php else : ?>
						<tr>
							<td colspan="6" class="pdf-right" style="text-align:center; padding:20px 0; color:#64748b;">No items found.</td>
						</tr>
					<?php endif; ?>
					</tbody>
				</table>
			</div>

			<div class="pdf-totals">
				<div class="pdf-totals-right">
					<div class="pdf-total-row">
						<div class="pdf-total-cell">Sub Total</div>
						<div class="pdf-total-cell pdf-total-cell--value"><?= display_money($subtotal, $currency_symbol) ?></div>
					</div>
					<?php if (!empty($discount) && $discount > 0) { ?>
						<div class="pdf-total-row">
							<div class="pdf-total-cell">Discount</div>
							<div class="pdf-total-cell pdf-total-cell--value">-<?= display_money($discount, $currency_symbol) ?></div>
						</div>
					<?php } ?>
					<?php if (!empty($tax_rows)) { ?>
						<?php foreach ($tax_rows as $tax_row) { ?>
							<div class="pdf-total-row">
								<div class="pdf-total-cell"><?= html_escape($tax_row['label']) ?> (<?= html_escape($tax_row['percent']) ?>%)</div>
								<div class="pdf-total-cell pdf-total-cell--value"><?= display_money($tax_row['value'], $currency_symbol) ?></div>
							</div>
						<?php } ?>
					<?php } ?>
					<?php if (!empty($proposals_info->adjustment)) { ?>
						<div class="pdf-total-row">
							<div class="pdf-total-cell">Adjustment</div>
							<div class="pdf-total-cell pdf-total-cell--value"><?= display_money($proposals_info->adjustment, $currency_symbol) ?></div>
						</div>
					<?php } ?>
					<div class="pdf-total-line"></div>
					<div class="pdf-grand">
						<div class="pdf-total-cell">TOTAL</div>
						<div class="pdf-total-cell pdf-total-cell--value"><?= display_money($total, $currency_symbol) ?></div>
					</div>
				</div>
			</div>

			<div class="pdf-bottom">
				<table class="pdf-bottom-table">
					<tr>
						<td>
							<div class="pdf-note-title">Terms and Conditions</div>
							<div class="pdf-note-body"><?= html_escape($proposals_info->notes) ?></div>
						</td>
						<td>
							<div class="pdf-note-title">Proposal Notes</div>
							<div class="pdf-note-body">Payment is due within 30 days. Late payments may be subject to a 1.5% monthly fee. Please review all terms outlined in the master service agreement.</div>
						</td>
					</tr>
				</table>
			</div>
		</div>

		<div class="pdf-footer">© <?= date('Y') ?> Uniquecoder. All rights reserved. Generated by ZiscoERP.</div>
	</div>
</div>
</body>
</html>
