<div class="invoice-modern">
<?= message_box('success') ?>
<?= message_box('error') ?>
<?php
$edited = can_action('14', 'edited');
$deleted = can_action('14', 'deleted');
$estimate_url = base_url() . 'frontend/estimates/' . url_encode($sales_info->estimates_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_class = 'bg-amber-50 text-amber-700 border-amber-200';
} elseif ($status_value === 'sent') {
	$status_class = 'bg-blue-50 text-blue-700 border-blue-200';
} elseif ($status_value === 'pending') {
	$status_class = 'bg-amber-50 text-amber-700 border-amber-200';
}

$where = array('user_id' => $this->session->userdata('user_id'), 'module_id' => $sales_info->estimates_id, 'module_name' => 'estimates');
$check_existing = $this->estimates_model->check_by($where, 'tbl_pinaction');
if (!empty($check_existing)) {
	$pin_url = 'remove_todo/' . $check_existing->pinaction_id;
	$pin_btn = 'rose';
	$pin_title = lang('remove_todo');
} else {
	$pin_url = 'add_todo_list/estimates/' . $sales_info->estimates_id;
	$pin_btn = 'amber';
	$pin_title = lang('add_todo_list');
}

$notified_reminder = (int) $this->db
	->where(array('module' => 'estimate', 'module_id' => $sales_info->estimates_id, 'notified' => 'No'))
	->count_all_results('tbl_reminders');
$can_edit = $this->estimates_model->can_action('tbl_estimates', 'edit', array('estimates_id' => $sales_info->estimates_id));
$can_delete = $this->estimates_model->can_action('tbl_estimates', 'delete', array('estimates_id' => $sales_info->estimates_id));
?>

<div class="space-y-6">
	<?php if ($sales_info->invoiced == 'Yes') {
		$invoice_info = $this->db->where('invoices_id', $sales_info->invoices_id)->get('tbl_invoices')->row();
		if (!empty($invoice_info)) { ?>
			<div class="rounded-xl border border-emerald-200 bg-emerald-50 p-4 text-sm text-emerald-700">
				<?= lang('invoiced') ?>: <a href="<?= base_url() ?>admin/invoice/manage_invoice/invoice_details/<?= $invoice_info->invoices_id ?>" class="font-semibold underline"><?= html_escape($invoice_info->reference_no) ?></a>
			</div>
		<?php }
	} ?>

	<div class="rounded-2xl border border-border bg-card px-6 py-4 shadow-sm">
		<div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
			<div class="flex flex-1 items-center gap-3">
				<span class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('copy_unique_url') ?></span>
				<input id="estimate-url" type="text" value="<?= $estimate_url ?>" class="flex-1 rounded-lg border border-border bg-card 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="#estimate-url" class="inline-flex h-9 items-center justify-center gap-2 rounded-lg border border-border bg-card px-3 text-xs font-semibold text-foreground hover:bg-muted">
						<i data-lucide="copy" class="h-4 w-4"></i>
						<?= lang('copy') ?>
				</button>
			</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('estimate'),
		'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.estimatePdfPrintHtml = <?= json_encode(isset($estimate_pdf_print_html) ? $estimate_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.estimatePdfPrintHtml || (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>
