<div class="invoice-modern">
<?= message_box('success') ?>
<?= message_box('error') ?>
<?php
$edited = can_action('140', 'edited');
$deleted = can_action('140', 'deleted');
$proposal_url = base_url() . 'frontend/proposals/' . url_encode($sales_info->proposals_id);

$status_label = !empty($sales_info->status_label) ? $sales_info->status_label : lang($sales_info->status);
$status_value = strtolower((string)($sales_info->status ?? ''));
$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
if ($status_value === 'accepted') {
	$status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif (in_array($status_value, array('declined', 'cancelled'), true)) {
	$status_class = 'bg-rose-50 text-rose-700 border-rose-200';
} elseif ($status_value === 'expired' || $status_value === 'open') {
	$status_class = 'bg-amber-50 text-amber-700 border-amber-200';
} elseif ($status_value === 'sent' || $status_value === 'revised') {
	$status_class = 'bg-blue-50 text-blue-700 border-blue-200';
}

$can_edit = $this->proposal_model->can_action('tbl_proposals', 'edit', array('proposals_id' => $sales_info->proposals_id));
$can_delete = $this->proposal_model->can_action('tbl_proposals', 'delete', array('proposals_id' => $sales_info->proposals_id));

$notified_reminder = (int) $this->db
	->where(array('module' => 'proposal', 'module_id' => $sales_info->proposals_id, 'notified' => 'No'))
	->count_all_results('tbl_reminders');

$convert_label = '';
$convert_url = '';
if ($sales_info->convert == 'Yes') {
	if ($sales_info->convert_module == 'invoice') {
		$convert_info = $this->proposal_model->check_by(array('invoices_id' => $sales_info->convert_module_id), 'tbl_invoices');
		$convert_url = base_url() . 'admin/invoice/manage_invoice/invoice_details/' . $sales_info->convert_module_id;
		$convert_label = !empty($convert_info) ? $convert_info->reference_no : '';
	} else {
		$convert_info = $this->proposal_model->check_by(array('estimates_id' => $sales_info->convert_module_id), 'tbl_estimates');
		$convert_url = base_url() . 'admin/estimates/create/estimates_details/' . $sales_info->convert_module_id;
		$convert_label = !empty($convert_info) ? $convert_info->reference_no : '';
	}
}
?>

<div class="space-y-6">
	<div class="rounded-xl border border-border bg-card shadow-sm">
		<div class="border-b border-border px-2 py-3 sm:px-6">
			<span class="inline-block text-[10px] font-bold uppercase tracking-widest text-muted-foreground"><?= lang('unique_url') . ' & ' . lang('action') ?></span>
		</div>
		<div class="flex flex-col gap-4 px-2 py-4 sm:px-6">
			<div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:gap-3">
				<div class="flex flex-1 items-center gap-2">
					<input id="proposal-url" type="text" value="<?= $proposal_url ?>" class="flex-1 rounded-lg border border-border bg-muted/40 px-3 py-2.5 text-sm text-foreground shadow-sm focus:outline-none focus:ring-2 focus:ring-primary/20" readonly>
					<button type="button" data-copy-target="#proposal-url" class="inline-flex h-9 items-center justify-center gap-2 rounded-lg border border-border bg-white px-3 text-xs font-semibold text-slate-700 shadow-sm hover:bg-slate-50 dark:border-slate-700 dark:bg-slate-900 dark:text-slate-200 dark:hover:bg-slate-800">
						<i data-lucide="copy" class="h-3.5 w-3.5"></i>
					</button>
				</div>
			</div>
		</div>
	</div>

	<?php
	$this->load->view('admin/common/sales_details_modern', array(
		'sales_info' => $sales_info,
		'all_items' => $all_items,
		'qrcode' => isset($qrcode) ? $qrcode : null,
		'footer' => isset($footer) ? $footer : null,
		'document_label' => lang('proposal'),
		'payment_status' => $status_label,
	));
	?>
</div>

<script>
	document.addEventListener('click', function(event) {
		var button = event.target.closest('[data-copy-target]');
		if (!button) return;
		var target = button.getAttribute('data-copy-target');
		var input = document.querySelector(target);
		if (!input) return;
		input.select();
		input.setSelectionRange(0, 99999);
		try {
			document.execCommand('copy');
		} catch (err) {}
	});

	window.proposalPdfPrintHtml = <?= json_encode(isset($proposal_pdf_print_html) ? $proposal_pdf_print_html : '', JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP) ?>;
	window.print_sales_details = function(salesId) {
		var content = document.getElementById(salesId);
		var printHtml = window.proposalPdfPrintHtml || (content ? content.outerHTML : '');
		if (!printHtml) return;

		var printWindow = window.open('', 'PRINT', 'height=900,width=1200');
		if (!printWindow) {
			if (typeof window.erpNativePrint === 'function') {
				window.erpNativePrint();
			} else if (typeof window.print === 'function') {
				window.print();
			}
			return;
		}

		var styles = '';
		document.querySelectorAll('link[rel="stylesheet"], style').forEach(function(node) {
			styles += node.outerHTML;
		});
		styles += '<style>@page{size:A4;margin:14mm;}html,body{background:#fff!important;color:#0f172a!important;-webkit-print-color-adjust:exact;print-color-adjust:exact;}body{margin:0!important;padding:0!important;}table{border-collapse:collapse;}thead{display:table-header-group!important;}tr,img{break-inside:avoid!important;page-break-inside:avoid!important;}img{max-width:100%!important;}</style>';

		printWindow.document.open();
		printWindow.document.write('<!doctype html><html><head><meta charset="utf-8"><title>' + document.title + '</title>' + styles + '</head><body>' + printHtml + '</body></html>');
		printWindow.document.close();

		var printed = false;
		var doPrint = function() {
			if (printed || printWindow.closed) return;
			printed = true;
			printWindow.focus();
			if (typeof printWindow.print === 'function') {
				printWindow.print();
			}
			printWindow.close();
		};
		printWindow.onload = function() {
			printWindow.setTimeout(doPrint, 350);
		};
		printWindow.setTimeout(doPrint, 1000);
	};
</script>
</div>
