<div class="invoice-modern">
<?= message_box('success') ?>
<?= message_box('error') ?>
<?php
$edited = can_action('156', 'edited');
$deleted = can_action('156', 'deleted');
$credit_url = base_url() . 'frontend/credit_note/' . url_encode($sales_info->credit_note_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 === 'open') {
	$status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif ($status_value === 'refund') {
	$status_class = 'bg-blue-50 text-blue-700 border-blue-200';
} elseif ($status_value === 'void') {
	$status_class = 'bg-rose-50 text-rose-700 border-rose-200';
}

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

$can_edit = $this->credit_note_model->can_action('tbl_credit_note', 'edit', array('credit_note_id' => $sales_info->credit_note_id));
$can_delete = $this->credit_note_model->can_action('tbl_credit_note', 'delete', array('credit_note_id' => $sales_info->credit_note_id));

$notified_reminder = count(array(get_result('tbl_reminders', array('module' => 'credit_note', 'module_id' => $sales_info->credit_note_id, 'notified' => 'No'))));
$credit_used = count(array(get_result('tbl_credit_used', array('credit_note_id' => $sales_info->credit_note_id))));
?>

<div class="space-y-6">
	<div class="rounded-2xl border border-border bg-card p-6 shadow-sm">
		<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
			<div class="flex-1">
				<label class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('copy_unique_url') ?></label>
				<div class="mt-2 flex items-center gap-2">
					<input id="credit-note-url" type="text" value="<?= $credit_url ?>" class="w-full rounded-lg border border-border bg-input px-3 py-2 text-sm" readonly>
					<button type="button" data-copy-target="#credit-note-url" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-3 py-2 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>
	</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('credit_note'),
		'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.creditNotePdfPrintHtml = <?= json_encode(isset($credit_note_pdf_print_html) ? $credit_note_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.creditNotePdfPrintHtml || (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>
