<?= message_box('success') ?>
<?= message_box('error') ?>

<?php
$edited = can_action('153', 'edited');
$deleted = can_action('153', 'deleted');
$paid_amount = $this->return_stock_model->calculate_to('paid_amount', $sales_info->return_stock_id);
$payment_status = $this->return_stock_model->get_payment_status($sales_info->return_stock_id);
$currency = $this->return_stock_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
$can_edit = $this->return_stock_model->can_action('tbl_return_stock', 'edit', array('return_stock_id' => $sales_info->return_stock_id));
$can_delete = $this->return_stock_model->can_action('tbl_return_stock', 'delete', array('return_stock_id' => $sales_info->return_stock_id));

$main_status = !empty($sales_info->main_status) ? strtolower($sales_info->main_status) : 'pending';
$main_status_class = 'bg-amber-50 text-amber-700 border-amber-200';
if ($main_status === 'accepted') {
	$main_status_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
} elseif ($main_status === 'cancelled' || $main_status === 'declined') {
	$main_status_class = 'bg-rose-50 text-rose-700 border-rose-200';
}

if (!empty($sales_info->module)) {
	$sales_info->company_heading = lang($sales_info->module);
}
?>

<div class="space-y-6">
	<?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,
		'footer' => isset($footer) ? $footer : null,
		'document_label' => lang('return_stock'),
	));
	?>

	<?php $all_payment_info = $this->db->where('return_stock_id', $sales_info->return_stock_id)->get('tbl_return_stock_payments')->result();
	if (!empty($all_payment_info)) { ?>
		<div class="rounded-2xl border border-border bg-card shadow-sm">
			<div class="border-b border-border px-4 py-4">
				<div class="flex items-center gap-2 text-sm font-semibold text-foreground">
					<i data-lucide="wallet" class="h-5 w-5"></i>
					<h2 class="text-base font-semibold text-foreground"><?= lang('payment') . ' ' . lang('details') ?></h2>
				</div>
			</div>
			<div class="table-responsive p-4">
				<table class="table table-striped">
					<thead>
						<tr>
							<th><?= lang('transaction_id') ?></th>
							<th><?= lang('payment_date') ?></th>
							<th><?= lang('amount') ?></th>
							<th><?= lang('payment_mode') ?></th>
							<?php if (!empty($edited) || !empty($deleted)) { ?>
								<th><?= lang('action') ?></th>
							<?php } ?>
						</tr>
					</thead>
					<tbody>
						<?php foreach ($all_payment_info as $v_payments_info) {
							if (is_numeric($v_payments_info->payment_method)) {
								$v_payments_info->method_name = get_any_field('tbl_payment_methods', array('payment_methods_id' => $v_payments_info->payment_method), 'method_name');
							} else {
								$v_payments_info->method_name = $v_payments_info->payment_method;
							}
							?>
							<tr>
								<td><a href="<?= base_url('admin/return_stock/payments_details/' . $v_payments_info->payments_id) ?>"><?= html_escape($v_payments_info->trans_id) ?></a></td>
								<td><?= display_date($v_payments_info->payment_date); ?></td>
								<td><?= display_money($v_payments_info->amount, $currency->symbol) ?></td>
								<td><?= !empty($v_payments_info->method_name) ? $v_payments_info->method_name : '-'; ?></td>
								<?php if (!empty($edited) || !empty($deleted)) { ?>
									<td class="space-x-1">
										<?php if (!empty($can_edit) && !empty($edited)) { ?>
											<a href="<?= base_url('admin/return_stock/all_payments/' . $v_payments_info->payments_id) ?>" class="btn btn-xs btn-primary" title="<?= lang('edit') ?>"><i data-lucide="pencil" class="h-4 w-4"></i></a>
										<?php } ?>
										<?php if (!empty($can_delete) && !empty($deleted)) { ?>
											<?= ajax_anchor(base_url('admin/return_stock/delete_payment/' . $v_payments_info->payments_id), lang('delete'), array("class" => "btn btn-xs btn-danger", "title" => lang('delete'))) ?>
										<?php } ?>
										<a href="<?= base_url('admin/return_stock/send_payment/' . $v_payments_info->payments_id . '/' . $v_payments_info->amount) ?>" class="btn btn-xs btn-success" title="<?= lang('send_email') ?>"><i data-lucide="mail" class="h-4 w-4"></i></a>
										<a href="<?= base_url('admin/return_stock/payments_pdf/' . $v_payments_info->payments_id) ?>" class="btn btn-xs btn-warning" title="<?= lang('pdf') ?>"><i data-lucide="file-text" class="h-4 w-4"></i></a>
									</td>
								<?php } ?>
							</tr>
						<?php } ?>
					</tbody>
				</table>
			</div>
		</div>
	<?php } ?>
</div>

<script>
	window.returnStockPdfPrintHtml = <?= json_encode(isset($return_stock_pdf_print_html) ? $return_stock_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.returnStockPdfPrintHtml || (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>
