<div class="invoice-modern">
<?= message_box('success') ?>
<?= message_box('error') ?>
<?php
$edited = can_action('13', 'edited');
$deleted = can_action('13', 'deleted');
$invoice_url = base_url() . 'frontend/view_invoice/' . url_encode($sales_info->invoices_id);

$status_label = isset($payment_status) ? $payment_status : '';
$status_value = strtolower((string) $status_label);
$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
if ($status_value === strtolower(lang('fully_paid')) || $status_value === strtolower(lang('paid'))) {
	$status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif ($status_value === strtolower(lang('draft'))) {
	$status_class = 'bg-slate-100 text-slate-700 border-slate-200';
} elseif ($status_value === strtolower(lang('partially_paid'))) {
	$status_class = 'bg-amber-50 text-amber-700 border-amber-200';
} elseif ($status_value === strtolower(lang('cancelled'))) {
	$status_class = 'bg-rose-50 text-rose-700 border-rose-200';
} elseif ($status_value === strtolower(lang('sent'))) {
	$status_class = 'bg-blue-50 text-blue-700 border-blue-200';
}

$where = array('user_id' => $this->session->userdata('user_id'), 'module_id' => $sales_info->invoices_id, 'module_name' => 'invoice');
$check_existing = $this->invoice_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/invoice/' . $sales_info->invoices_id;
	$pin_btn = 'amber';
	$pin_title = lang('add_todo_list');
}

$notified_reminder = isset($pending_reminder_count) ? (int) $pending_reminder_count : 0;
$credit_used = isset($credit_used_count) ? (int) $credit_used_count : 0;
$can_edit = isset($can_edit_invoice) ? $can_edit_invoice : $this->invoice_model->can_action('tbl_invoices', 'edit', array('invoices_id' => $sales_info->invoices_id));
$invoice_cost_total = isset($invoice_cost_total) ? (float) $invoice_cost_total : $this->invoice_model->get_invoice_cost($sales_info->invoices_id);
$payment_count = isset($payment_count) ? (int) $payment_count : (!empty($all_payment_info) ? count($all_payment_info) : 0);
?>

<div class="space-y-6">
	<?php if ($payment_status != lang('cancelled') && $payment_status != lang('fully_paid') && !empty($total_available_credit)) { ?>
		<div class="rounded-xl border border-emerald-200 bg-emerald-50 p-4 text-sm text-emerald-700">
			<?= lang('total_credit_available', display_money($total_available_credit, client_currency($sales_info->client_id))); ?>
			<?= btn_modal(
				'admin/invoice/invoices_credit/' . $sales_info->invoices_id,
				lang('apply_credits'),
				lang('apply_credits'),
				'lg',
				false,
				array('class' => 'font-semibold text-emerald-700 underline')
			) ?>
		</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('client_link') ?></span>
				<input id="invoice-url" type="text" value="<?= $invoice_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="#invoice-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,
		'paid_amount' => $paid_amount,
		'invoice_due' => $invoice_due,
		'payment_status' => $payment_status,
		'qrcode' => isset($qrcode) ? $qrcode : null,
		'footer' => isset($footer) ? $footer : null,
	));
	?>
</div>

<script>
	document.addEventListener('click', function(event) {
		var button = event.target.closest('[data-copy-target]');
		if (!button) return;
		var selector = button.getAttribute('data-copy-target');
		var input = document.querySelector(selector);
		if (!input) return;
		input.select();
		input.setSelectionRange(0, input.value.length);
		try {
			document.execCommand('copy');
		} catch (e) {
			console.warn('Copy failed', e);
		}
	});

	window.invoicePdfPrintHtml = <?= json_encode(isset($invoice_pdf_print_html) ? $invoice_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.invoicePdfPrintHtml || (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>
