<?php
/* Copyright (C) 2026  Eduard Wisch  <data@data-it-solution.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 */

/**
 * \file    bankimport/card.php
 * \ingroup bankimport
 * \brief   Card page for a single bank transaction
 */

// Load Dolibarr environment
$res = 0;
if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
	$res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
}
$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
$tmp2 = realpath(__FILE__);
$i = strlen($tmp) - 1;
$j = strlen($tmp2) - 1;
while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
	$i--;
	$j--;
}
if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
	$res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
}
if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
	$res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
}
if (!$res && file_exists("../main.inc.php")) {
	$res = @include "../main.inc.php";
}
if (!$res && file_exists("../../main.inc.php")) {
	$res = @include "../../main.inc.php";
}
if (!$res) {
	die("Include of main fails");
}

require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
dol_include_once('/bankimport/class/banktransaction.class.php');
dol_include_once('/bankimport/lib/bankimport.lib.php');

/**
 * @var Conf $conf
 * @var DoliDB $db
 * @var Translate $langs
 * @var User $user
 */

$langs->loadLangs(array("bankimport@bankimport", "banks", "bills"));

$id = GETPOSTINT('id');
$ref = GETPOST('ref', 'alpha');
$action = GETPOST('action', 'aZ09');
$confirm = GETPOST('confirm', 'alpha');

// Security check
if (!$user->hasRight('bankimport', 'read')) {
	accessforbidden();
}

/*
 * Actions
 */

$object = new BankImportTransaction($db);

if ($id > 0 || !empty($ref)) {
	$result = $object->fetch($id, $ref);
	if ($result <= 0) {
		setEventMessages($langs->trans("RecordNotFound"), null, 'errors');
	}
}

// Set status
if ($action == 'setstatus' && $object->id > 0) {
	$newstatus = GETPOSTINT('status');
	$result = $object->setStatus($newstatus, $user);
	if ($result > 0) {
		setEventMessages($langs->trans("StatusUpdated"), null, 'mesgs');
		header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
		exit;
	} else {
		setEventMessages($object->error, null, 'errors');
	}
}

// Unlink payment (reset to NEW status)
if ($action == 'unlink' && $object->id > 0) {
	if ($object->status == BankImportTransaction::STATUS_MATCHED) {
		$result = $object->unlinkPayment($user);
		if ($result > 0) {
			setEventMessages($langs->trans("PaymentUnlinked"), null, 'mesgs');
			header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
			exit;
		} else {
			setEventMessages($object->error, null, 'errors');
		}
	} else {
		setEventMessages($langs->trans("CannotUnlinkThisStatus"), null, 'warnings');
	}
}

// Find matches
if ($action == 'findmatches' && $object->id > 0) {
	$matches = $object->findMatches();
	if (count($matches) > 0) {
		$_SESSION['bankimport_matches_'.$object->id] = $matches;
		setEventMessages($langs->trans("MatchesFound", count($matches)), null, 'mesgs');
	} else {
		setEventMessages($langs->trans("NoMatchesFound"), null, 'warnings');
	}
}

// Link to object (old method - just link without payment)
if ($action == 'linkto' && $object->id > 0) {
	$linktype = GETPOST('linktype', 'alpha');
	$linkid = GETPOSTINT('linkid');

	if ($linktype && $linkid > 0) {
		$result = $object->linkTo($linktype, $linkid, $user);
		if ($result > 0) {
			setEventMessages($langs->trans("LinkCreated"), null, 'mesgs');
			unset($_SESSION['bankimport_matches_'.$object->id]);
			header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
			exit;
		} else {
			setEventMessages($object->error, null, 'errors');
		}
	}
}

// Confirm payment (creates payment in Dolibarr)
$bankAccountId = getDolGlobalInt('BANKIMPORT_BANK_ACCOUNT_ID');
if ($action == 'confirmpayment' && $object->id > 0 && !empty($bankAccountId)) {
	$matchtype = GETPOST('matchtype', 'alpha');
	$matchid = GETPOSTINT('matchid');

	if ($matchtype && $matchid > 0) {
		if ($object->status != BankImportTransaction::STATUS_NEW) {
			setEventMessages($langs->trans("TransactionAlreadyProcessed"), null, 'warnings');
		} else {
			$result = $object->confirmPayment($user, $matchtype, $matchid, $bankAccountId);
			if ($result > 0) {
				setEventMessages($langs->trans("PaymentCreatedSuccessfully", dol_escape_htmltag($object->name), price(abs($object->amount))), null, 'mesgs');
				unset($_SESSION['bankimport_matches_'.$object->id]);
			} else {
				setEventMessages($object->error, $object->errors, 'errors');
			}
		}
		header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
		exit;
	}
}

// Confirm multiple invoices payment
if ($action == 'confirmmulti' && $object->id > 0 && !empty($bankAccountId)) {
	$invoiceIds = GETPOST('invoices', 'array');

	if (!empty($invoiceIds)) {
		if ($object->status != BankImportTransaction::STATUS_NEW) {
			setEventMessages($langs->trans("TransactionAlreadyProcessed"), null, 'warnings');
		} else {
			// Build invoices array with amounts
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
			$invoices = array();
			foreach ($invoiceIds as $invId) {
				$invId = (int) $invId;
				if ($invId > 0) {
					$invoice = new FactureFournisseur($db);
					if ($invoice->fetch($invId) > 0) {
						$alreadyPaid = $invoice->getSommePaiement();
						$creditnotes = $invoice->getSumCreditNotesUsed();
						$deposits = $invoice->getSumDepositsUsed();
						$remainToPay = price2num($invoice->total_ttc - $alreadyPaid - $creditnotes - $deposits, 'MT');
						if ($remainToPay > 0) {
							$invoices[] = array(
								'type' => 'facture_fourn',
								'id' => $invId,
								'ref' => $invoice->ref,
								'amount' => $remainToPay
							);
						}
					}
				}
			}

			if (!empty($invoices)) {
				$result = $object->confirmMultiplePayment($user, $invoices, $bankAccountId);
				if ($result > 0) {
					setEventMessages($langs->trans("PaymentCreatedSuccessfully", dol_escape_htmltag($object->name), price(abs($object->amount))).' ('.count($invoices).' '.$langs->trans("Invoices").')', null, 'mesgs');
					unset($_SESSION['bankimport_matches_'.$object->id]);
				} else {
					setEventMessages($object->error, $object->errors, 'errors');
				}
			} else {
				setEventMessages($langs->trans("NoInvoicesSelected"), null, 'errors');
			}
		}
		header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
		exit;
	}
}

// Link existing payment to bank entry (for already paid invoices)
if ($action == 'linkpayment' && $object->id > 0 && !empty($bankAccountId)) {
	$invoiceType = GETPOST('invoicetype', 'alpha');
	$invoiceId = GETPOSTINT('invoiceid');

	if ($invoiceType && $invoiceId > 0) {
		if ($object->status != BankImportTransaction::STATUS_NEW) {
			setEventMessages($langs->trans("TransactionAlreadyProcessed"), null, 'warnings');
		} else {
			$result = $object->linkExistingPayment($user, $invoiceType, $invoiceId, $bankAccountId);
			if ($result > 0) {
				setEventMessages($langs->trans("PaymentLinkedSuccessfully"), null, 'mesgs');
			} else {
				setEventMessages($object->error, $object->errors, 'errors');
			}
		}
		header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
		exit;
	}
}

// Link multiple existing payments to bank entry (for already paid invoices)
if ($action == 'linkpaymentmulti' && $object->id > 0 && !empty($bankAccountId)) {
	$paidInvoiceIds = GETPOST('paid_invoice', 'array');
	$paidInvoiceType = GETPOST('paid_invoice_type', 'alpha');

	if (!empty($paidInvoiceIds) && !empty($paidInvoiceType)) {
		if ($object->status != BankImportTransaction::STATUS_NEW) {
			setEventMessages($langs->trans("TransactionAlreadyProcessed"), null, 'warnings');
		} else {
			// Build invoices array
			$invoices = array();
			foreach ($paidInvoiceIds as $invId) {
				$invoices[] = array(
					'type' => $paidInvoiceType,
					'id' => (int) $invId
				);
			}

			$result = $object->linkMultipleExistingPayments($user, $invoices, $bankAccountId);
			if ($result > 0) {
				setEventMessages($langs->trans("PaymentLinkedSuccessfully"), null, 'mesgs');
			} else {
				setEventMessages($object->error, $object->errors, 'errors');
			}
		}
		header("Location: ".$_SERVER["PHP_SELF"]."?id=".$object->id);
		exit;
	} else {
		setEventMessages($langs->trans("NoInvoicesSelected"), null, 'warnings');
	}
}

// Search for invoice manually
if ($action == 'searchinvoice' && $object->id > 0) {
	$searchInvoiceRef = GETPOST('search_invoice_ref', 'alpha');
	$searchInvoiceType = GETPOST('search_invoice_type', 'alpha'); // 'customer' or 'supplier'

	if (!empty($searchInvoiceRef)) {
		$manualMatches = array();

		if ($searchInvoiceType == 'customer' || empty($searchInvoiceType)) {
			// Search customer invoices
			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
			$sql = "SELECT f.rowid, f.ref, f.ref_client, f.total_ttc, f.date_lim_reglement, s.nom as socname, s.rowid as socid";
			$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
			$sql .= " WHERE f.entity = ".$conf->entity;
			$sql .= " AND f.fk_statut = 1"; // Unpaid
			$sql .= " AND (f.ref LIKE '%".$db->escape($searchInvoiceRef)."%' OR f.ref_client LIKE '%".$db->escape($searchInvoiceRef)."%')";

			$resql = $db->query($sql);
			if ($resql) {
				while ($obj = $db->fetch_object($resql)) {
					$manualMatches[] = array(
						'type' => 'facture',
						'id' => $obj->rowid,
						'ref' => $obj->ref,
						'ref_client' => $obj->ref_client,
						'amount' => $obj->total_ttc,
						'socname' => $obj->socname,
						'socid' => $obj->socid,
						'match_score' => 0,
						'match_reasons' => array('manual'),
						'date_due' => $obj->date_lim_reglement
					);
				}
			}
		}

		if ($searchInvoiceType == 'supplier' || empty($searchInvoiceType)) {
			// Search supplier invoices
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
			$sql = "SELECT f.rowid, f.ref, f.ref_supplier, f.total_ttc, f.date_lim_reglement, s.nom as socname, s.rowid as socid";
			$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
			$sql .= " WHERE f.entity = ".$conf->entity;
			$sql .= " AND f.fk_statut = 1"; // Unpaid
			$sql .= " AND (f.ref LIKE '%".$db->escape($searchInvoiceRef)."%' OR f.ref_supplier LIKE '%".$db->escape($searchInvoiceRef)."%')";

			$resql = $db->query($sql);
			if ($resql) {
				while ($obj = $db->fetch_object($resql)) {
					$manualMatches[] = array(
						'type' => 'facture_fourn',
						'id' => $obj->rowid,
						'ref' => $obj->ref,
						'ref_supplier' => $obj->ref_supplier,
						'amount' => $obj->total_ttc,
						'socname' => $obj->socname,
						'socid' => $obj->socid,
						'match_score' => 0,
						'match_reasons' => array('manual'),
						'date_due' => $obj->date_lim_reglement
					);
				}
			}
		}

		if (!empty($manualMatches)) {
			$_SESSION['bankimport_matches_'.$object->id] = $manualMatches;
			setEventMessages($langs->trans("MatchesFound", count($manualMatches)), null, 'mesgs');
		} else {
			setEventMessages($langs->trans("NoMatchesFound"), null, 'warnings');
		}
	}
}

/*
 * View
 */

$form = new Form($db);

$title = $langs->trans("Transaction").' - '.$object->ref;
llxHeader('', $title, '', '', 0, 0, '', '', '', 'mod-bankimport page-card');

if ($object->id > 0) {
	print '<div class="fichecenter">';

	// Title
	print load_fiche_titre($langs->trans("Transaction"), '', 'bank');

	// Card header
	print '<div class="underbanner clearboth"></div>';
	print '<table class="border centpercent tableforfield">';

	// Reference
	print '<tr>';
	print '<td class="titlefield">'.$langs->trans("Ref").'</td>';
	print '<td>'.dol_escape_htmltag($object->ref).'</td>';
	print '</tr>';

	// IBAN
	print '<tr>';
	print '<td>'.$langs->trans("AccountIBAN").'</td>';
	print '<td>'.dol_escape_htmltag($object->iban).'</td>';
	print '</tr>';

	// Date
	print '<tr>';
	print '<td>'.$langs->trans("Date").'</td>';
	print '<td>'.dol_print_date($object->date_trans, 'day').'</td>';
	print '</tr>';

	// Value date
	if ($object->date_value) {
		print '<tr>';
		print '<td>'.$langs->trans("DateValue").'</td>';
		print '<td>'.dol_print_date($object->date_value, 'day').'</td>';
		print '</tr>';
	}

	// Name
	print '<tr>';
	print '<td>'.$langs->trans("Counterparty").'</td>';
	print '<td><strong>'.dol_escape_htmltag($object->name).'</strong></td>';
	print '</tr>';

	// Counterparty IBAN
	if ($object->counterparty_iban) {
		print '<tr>';
		print '<td>'.$langs->trans("CounterpartyIBAN").'</td>';
		print '<td>'.dol_escape_htmltag($object->counterparty_iban).'</td>';
		print '</tr>';
	}

	// Amount
	print '<tr>';
	print '<td>'.$langs->trans("Amount").'</td>';
	print '<td>';
	if ($object->amount >= 0) {
		print '<span style="color: green; font-size: 1.3em; font-weight: bold;">+'.price($object->amount, 0, $langs, 1, -1, 2, $object->currency).'</span>';
	} else {
		print '<span style="color: red; font-size: 1.3em; font-weight: bold;">'.price($object->amount, 0, $langs, 1, -1, 2, $object->currency).'</span>';
	}
	print '</td>';
	print '</tr>';

	// Label
	if ($object->label) {
		print '<tr>';
		print '<td>'.$langs->trans("Label").'</td>';
		print '<td>'.dol_escape_htmltag($object->label).'</td>';
		print '</tr>';
	}

	// Description
	print '<tr>';
	print '<td>'.$langs->trans("Description").'</td>';
	print '<td>'.nl2br(dol_escape_htmltag($object->description)).'</td>';
	print '</tr>';

	// End-to-End ID
	if ($object->end_to_end_id) {
		print '<tr>';
		print '<td>'.$langs->trans("EndToEndId").'</td>';
		print '<td>'.dol_escape_htmltag($object->end_to_end_id).'</td>';
		print '</tr>';
	}

	// Mandate ID
	if ($object->mandate_id) {
		print '<tr>';
		print '<td>'.$langs->trans("MandateId").'</td>';
		print '<td>'.dol_escape_htmltag($object->mandate_id).'</td>';
		print '</tr>';
	}

	// Status
	print '<tr>';
	print '<td>'.$langs->trans("Status").'</td>';
	print '<td>'.$object->getLibStatut(4).'</td>';
	print '</tr>';

	// Linked payment and invoices (for matched transactions)
	if ($object->status == BankImportTransaction::STATUS_MATCHED || $object->status == BankImportTransaction::STATUS_RECONCILED) {
		// Customer payment
		if ($object->fk_paiement > 0) {
			require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
			$payment = new Paiement($db);
			$payment->fetch($object->fk_paiement);

			print '<tr>';
			print '<td>'.$langs->trans("Payment").'</td>';
			print '<td>'.$payment->getNomUrl(1).' <span class="opacitymedium">('.dol_print_date($payment->datepaye, 'day').')</span></td>';
			print '</tr>';

			// Find all invoices linked to this payment
			$sql = "SELECT pf.fk_facture, pf.amount, f.ref, f.total_ttc";
			$sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf";
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON pf.fk_facture = f.rowid";
			$sql .= " WHERE pf.fk_paiement = ".((int) $object->fk_paiement);
			$resql = $db->query($sql);
			if ($resql && $db->num_rows($resql) > 0) {
				print '<tr>';
				print '<td>'.$langs->trans("Invoices").'</td>';
				print '<td>';
				while ($obj = $db->fetch_object($resql)) {
					$inv = new Facture($db);
					$inv->fetch($obj->fk_facture);
					print $inv->getNomUrl(1);
					print ' <span class="opacitymedium">('.price($obj->amount, 0, $langs, 1, -1, 2, 'EUR').')</span>';
					print '<br>';
				}
				print '</td>';
				print '</tr>';
			}
		}

		// Supplier payment
		if ($object->fk_paiementfourn > 0) {
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
			$payment = new PaiementFourn($db);
			$payment->fetch($object->fk_paiementfourn);

			print '<tr>';
			print '<td>'.$langs->trans("Payment").'</td>';
			print '<td>'.$payment->getNomUrl(1).' <span class="opacitymedium">('.dol_print_date($payment->datepaye, 'day').')</span></td>';
			print '</tr>';

			// Find all invoices linked to this payment
			$sql = "SELECT pf.fk_facturefourn, pf.amount, f.ref, f.ref_supplier, f.total_ttc";
			$sql .= " FROM ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf";
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture_fourn as f ON pf.fk_facturefourn = f.rowid";
			$sql .= " WHERE pf.fk_paiementfourn = ".((int) $object->fk_paiementfourn);
			$resql = $db->query($sql);
			$invoicesFound = false;
			if ($resql && $db->num_rows($resql) > 0) {
				$invoicesFound = true;
				print '<tr>';
				print '<td>'.$langs->trans("SupplierInvoices").'</td>';
				print '<td>';
				while ($obj = $db->fetch_object($resql)) {
					$inv = new FactureFournisseur($db);
					$inv->fetch($obj->fk_facturefourn);
					print $inv->getNomUrl(1);
					if (!empty($obj->ref_supplier)) {
						print ' <span class="opacitymedium small">('.$obj->ref_supplier.')</span>';
					}
					print ' <span class="opacitymedium">('.price($obj->amount, 0, $langs, 1, -1, 2, 'EUR').')</span>';
					print '<br>';
				}
				print '</td>';
				print '</tr>';
			}

			// Also check note_private for multi-invoice links (format: "Multi-invoice link: SI2602-0146, SI2602-0147")
			if (!$invoicesFound && !empty($object->note_private) && strpos($object->note_private, 'Multi-invoice link:') !== false) {
				if (preg_match('/Multi-invoice link:\s*(.+)$/m', $object->note_private, $matches)) {
					$invoiceRefs = array_map('trim', explode(',', $matches[1]));
					print '<tr>';
					print '<td>'.$langs->trans("SupplierInvoices").'</td>';
					print '<td>';
					foreach ($invoiceRefs as $invRef) {
						// Try to find invoice by ref
						$sql2 = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture_fourn WHERE ref = '".$db->escape($invRef)."' AND entity = ".$conf->entity;
						$resql2 = $db->query($sql2);
						if ($resql2 && $db->num_rows($resql2) > 0) {
							$obj2 = $db->fetch_object($resql2);
							$inv = new FactureFournisseur($db);
							$inv->fetch($obj2->rowid);
							print $inv->getNomUrl(1);
							if (!empty($inv->ref_supplier)) {
								print ' <span class="opacitymedium small">('.$inv->ref_supplier.')</span>';
							}
						} else {
							print dol_escape_htmltag($invRef);
						}
						print '<br>';
					}
					print '</td>';
					print '</tr>';
					$invoicesFound = true;
				}
			}

			// Fallback: Show linked invoice from transaction
			if (!$invoicesFound && $object->fk_facture_fourn > 0) {
				$inv = new FactureFournisseur($db);
				$inv->fetch($object->fk_facture_fourn);
				print '<tr>';
				print '<td>'.$langs->trans("SupplierInvoice").'</td>';
				print '<td>'.$inv->getNomUrl(1);
				if (!empty($inv->ref_supplier)) {
					print ' <span class="opacitymedium small">('.$inv->ref_supplier.')</span>';
				}
				print '</td>';
				print '</tr>';
			}
		}

		// If no payment link but invoice link exists (edge case)
		if (empty($object->fk_paiement) && empty($object->fk_paiementfourn)) {
			if ($object->fk_facture_fourn > 0) {
				require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
				$inv = new FactureFournisseur($db);
				$inv->fetch($object->fk_facture_fourn);
				print '<tr>';
				print '<td>'.$langs->trans("SupplierInvoice").'</td>';
				print '<td>'.$inv->getNomUrl(1);
				if (!empty($inv->ref_supplier)) {
					print ' <span class="opacitymedium small">('.$inv->ref_supplier.')</span>';
				}
				print '</td>';
				print '</tr>';
			}
			if ($object->fk_facture > 0) {
				require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
				$inv = new Facture($db);
				$inv->fetch($object->fk_facture);
				print '<tr>';
				print '<td>'.$langs->trans("Invoice").'</td>';
				print '<td>'.$inv->getNomUrl(1).'</td>';
				print '</tr>';
			}
		}

		// Bank entry
		if ($object->fk_bank > 0) {
			require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
			$bankline = new AccountLine($db);
			$bankline->fetch($object->fk_bank);

			print '<tr>';
			print '<td>'.$langs->trans("BankEntry").'</td>';
			print '<td>';
			print '<a href="'.DOL_URL_ROOT.'/compta/bank/line.php?rowid='.$object->fk_bank.'">';
			print img_picto('', 'bank_account', 'class="pictofixedwidth"');
			print $bankline->ref ?: '#'.$object->fk_bank;
			print '</a>';
			print ' <span class="opacitymedium">('.dol_print_date($bankline->dateo, 'day').' - '.price($bankline->amount, 0, $langs, 1, -1, 2, 'EUR').')</span>';
			print '</td>';
			print '</tr>';
		}
	} else {
		// For non-matched transactions, show simple links if they exist
		// Linked invoice
		if ($object->fk_facture > 0) {
			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
			$invoice = new Facture($db);
			$invoice->fetch($object->fk_facture);
			print '<tr>';
			print '<td>'.$langs->trans("Invoice").'</td>';
			print '<td>'.$invoice->getNomUrl(1).'</td>';
			print '</tr>';
		}

		// Linked supplier invoice
		if ($object->fk_facture_fourn > 0) {
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
			$invoice = new FactureFournisseur($db);
			$invoice->fetch($object->fk_facture_fourn);
			print '<tr>';
			print '<td>'.$langs->trans("SupplierInvoice").'</td>';
			print '<td>'.$invoice->getNomUrl(1).'</td>';
			print '</tr>';
		}
	}

	// Linked PDF statement
	if (!empty($object->fk_statement)) {
		dol_include_once('/bankimport/class/bankstatement.class.php');
		$stmt = new BankImportStatement($db);
		$stmt->fetch($object->fk_statement);
		print '<tr>';
		print '<td>'.$langs->trans("PDFStatement").'</td>';
		print '<td>';
		print '<a href="'.dol_buildpath('/bankimport/pdfstatements.php', 1).'?action=view&id='.$stmt->id.'&token='.newToken().'" target="_blank">';
		print img_picto($langs->trans("ViewPDFStatement"), 'pdf').' ';
		print $langs->trans("StatementNumber").' '.$stmt->statement_number.'/'.$stmt->statement_year;
		print '</a>';
		if ($stmt->date_from && $stmt->date_to) {
			print ' <span class="opacitymedium">('.dol_print_date($stmt->date_from, 'day').' - '.dol_print_date($stmt->date_to, 'day').')</span>';
		}
		print '</td>';
		print '</tr>';
	}

	// Linked third party
	if ($object->fk_societe > 0) {
		require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
		$soc = new Societe($db);
		$soc->fetch($object->fk_societe);
		print '<tr>';
		print '<td>'.$langs->trans("ThirdParty").'</td>';
		print '<td>'.$soc->getNomUrl(1).'</td>';
		print '</tr>';
	}

	// Import date
	print '<tr>';
	print '<td>'.$langs->trans("DateCreation").'</td>';
	print '<td>'.dol_print_date($object->datec, 'dayhour').'</td>';
	print '</tr>';

	print '</table>';
	print '</div>';

	// Actions buttons
	print '<div class="tabsAction">';

	if ($object->status == BankImportTransaction::STATUS_NEW) {
		// Find matches button
		print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=findmatches&token='.newToken().'">'.$langs->trans("FindMatches").'</a>';

		// Set as ignored
		print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=setstatus&status='.BankImportTransaction::STATUS_IGNORED.'&token='.newToken().'">'.$langs->trans("SetAsIgnored").'</a>';
	}

	if ($object->status == BankImportTransaction::STATUS_MATCHED) {
		// Edit/Unlink - allows correcting wrong matches
		print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=unlink&token='.newToken().'">'.$langs->trans("UnlinkPayment").'</a>';
	}

	if ($object->status == BankImportTransaction::STATUS_IGNORED) {
		// Reopen
		print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=setstatus&status='.BankImportTransaction::STATUS_NEW.'&token='.newToken().'">'.$langs->trans("Reopen").'</a>';
	}

	print '</div>';

	// Manual invoice selection (only for new transactions)
	if ($object->status == BankImportTransaction::STATUS_NEW) {
		// Determine if this is likely a supplier payment (negative amount) or customer (positive)
		$defaultType = ($object->amount < 0) ? 'supplier' : 'customer';
		$selectedType = GETPOST('invoice_type', 'alpha') ?: $defaultType;
		$searchFilter = GETPOST('search_filter', 'alpha');
		$showPaid = GETPOSTINT('show_paid');

		print '<br>';
		print load_fiche_titre($langs->trans("SelectInvoicesManually"), '', 'object_invoice');

		// Type selector and search filter
		print '<form method="GET" action="'.$_SERVER["PHP_SELF"].'" style="margin-bottom: 15px;">';
		print '<input type="hidden" name="id" value="'.$object->id.'">';
		print '<div class="inline-block" style="margin-right: 15px;">';
		print '<label>'.$langs->trans("Type").': </label>';
		print '<select name="invoice_type" class="flat" onchange="this.form.submit()">';
		print '<option value="supplier"'.($selectedType == 'supplier' ? ' selected' : '').'>'.$langs->trans("SupplierInvoices").'</option>';
		print '<option value="customer"'.($selectedType == 'customer' ? ' selected' : '').'>'.$langs->trans("CustomerInvoices").'</option>';
		print '</select>';
		print '</div>';
		print '<div class="inline-block" style="margin-right: 15px;">';
		print '<label><input type="checkbox" name="show_paid" value="1"'.($showPaid ? ' checked' : '').' onchange="this.form.submit()"> '.$langs->trans("ShowPaidInvoices").'</label>';
		print '</div>';
		print '<div class="inline-block">';
		print '<label>'.$langs->trans("Search").': </label>';
		print '<input type="text" name="search_filter" class="flat minwidth200" value="'.dol_escape_htmltag($searchFilter).'" placeholder="'.$langs->trans("SearchInvoiceByRef").'">';
		print ' <input type="submit" class="button small" value="'.$langs->trans("Filter").'">';
		if (!empty($searchFilter)) {
			print ' <a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&invoice_type='.$selectedType.'&show_paid='.$showPaid.'" class="button small">'.$langs->trans("RemoveFilter").'</a>';
		}
		print '</div>';
		print '</form>';

		// Fetch invoices
		$invoiceList = array();
		$absAmount = abs($object->amount);

		if ($selectedType == 'supplier') {
			require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
			$sql = "SELECT f.rowid, f.ref, f.ref_supplier, f.total_ttc, f.datef, f.date_lim_reglement, f.fk_statut,";
			$sql .= " s.nom as socname, s.rowid as socid";
			$sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
			$sql .= " WHERE f.entity = ".$conf->entity;
			if ($showPaid) {
				$sql .= " AND f.fk_statut IN (1, 2)"; // 1=Unpaid, 2=Paid
			} else {
				$sql .= " AND f.fk_statut = 1"; // Unpaid only
			}

			if (!empty($searchFilter)) {
				$sql .= " AND (f.ref LIKE '%".$db->escape($searchFilter)."%'";
				$sql .= " OR f.ref_supplier LIKE '%".$db->escape($searchFilter)."%'";
				$sql .= " OR s.nom LIKE '%".$db->escape($searchFilter)."%')";
			}

			$sql .= " ORDER BY f.datef DESC";
			$sql .= " LIMIT 100";

			$resql = $db->query($sql);
			if ($resql) {
				while ($obj = $db->fetch_object($resql)) {
					$inv = new FactureFournisseur($db);
					$inv->fetch($obj->rowid);
					$alreadyPaid = $inv->getSommePaiement();
					$creditnotes = $inv->getSumCreditNotesUsed();
					$deposits = $inv->getSumDepositsUsed();
					$remainToPay = price2num($inv->total_ttc - $alreadyPaid - $creditnotes - $deposits, 'MT');

					$isPaid = ($obj->fk_statut == 2);

					// For paid invoices, check if already linked via BankImport transaction
					$isLinkedViaBankImport = false;
					if ($isPaid) {
						$sqlBi = "SELECT rowid FROM ".MAIN_DB_PREFIX."bankimport_transaction";
						$sqlBi .= " WHERE fk_facture_fourn = ".((int) $obj->rowid);
						$sqlBi .= " AND status > 0";
						$resqlBi = $db->query($sqlBi);
						if ($resqlBi && $db->num_rows($resqlBi) > 0) {
							$isLinkedViaBankImport = true;
						}
					}

					// Show if unpaid OR if paid but not yet linked via BankImport
					if ($remainToPay > 0 || ($isPaid && !$isLinkedViaBankImport)) {
						$invoiceList[] = array(
							'id' => $obj->rowid,
							'ref' => $obj->ref,
							'ref_supplier' => $obj->ref_supplier,
							'amount' => $isPaid ? $obj->total_ttc : $remainToPay,
							'total_ttc' => $obj->total_ttc,
							'socname' => $obj->socname,
							'socid' => $obj->socid,
							'datef' => $obj->datef,
							'date_due' => $obj->date_lim_reglement,
							'type' => 'facture_fourn',
							'object' => $inv,
							'is_paid' => $isPaid
						);
					}
				}
			}
		} else {
			require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
			$sql = "SELECT f.rowid, f.ref, f.ref_client, f.total_ttc, f.datef, f.date_lim_reglement, f.fk_statut,";
			$sql .= " s.nom as socname, s.rowid as socid";
			$sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
			$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON f.fk_soc = s.rowid";
			$sql .= " WHERE f.entity = ".$conf->entity;
			if ($showPaid) {
				$sql .= " AND f.fk_statut IN (1, 2)"; // 1=Unpaid, 2=Paid
			} else {
				$sql .= " AND f.fk_statut = 1"; // Unpaid only
			}

			if (!empty($searchFilter)) {
				$sql .= " AND (f.ref LIKE '%".$db->escape($searchFilter)."%'";
				$sql .= " OR f.ref_client LIKE '%".$db->escape($searchFilter)."%'";
				$sql .= " OR s.nom LIKE '%".$db->escape($searchFilter)."%')";
			}

			$sql .= " ORDER BY f.datef DESC";
			$sql .= " LIMIT 100";

			$resql = $db->query($sql);
			if ($resql) {
				while ($obj = $db->fetch_object($resql)) {
					$inv = new Facture($db);
					$inv->fetch($obj->rowid);
					$alreadyPaid = $inv->getSommePaiement();
					$creditnotes = $inv->getSumCreditNotesUsed();
					$deposits = $inv->getSumDepositsUsed();
					$remainToPay = price2num($inv->total_ttc - $alreadyPaid - $creditnotes - $deposits, 'MT');

					$isPaid = ($obj->fk_statut == 2);

					// For paid invoices, check if already linked via BankImport transaction
					$isLinkedViaBankImport = false;
					if ($isPaid) {
						$sqlBi = "SELECT rowid FROM ".MAIN_DB_PREFIX."bankimport_transaction";
						$sqlBi .= " WHERE fk_facture = ".((int) $obj->rowid);
						$sqlBi .= " AND status > 0";
						$resqlBi = $db->query($sqlBi);
						if ($resqlBi && $db->num_rows($resqlBi) > 0) {
							$isLinkedViaBankImport = true;
						}
					}

					// Show if unpaid OR if paid but not yet linked via BankImport
					if ($remainToPay > 0 || ($isPaid && !$isLinkedViaBankImport)) {
						$invoiceList[] = array(
							'id' => $obj->rowid,
							'ref' => $obj->ref,
							'ref_client' => $obj->ref_client,
							'amount' => $isPaid ? $obj->total_ttc : $remainToPay,
							'total_ttc' => $obj->total_ttc,
							'socname' => $obj->socname,
							'socid' => $obj->socid,
							'datef' => $obj->datef,
							'date_due' => $obj->date_lim_reglement,
							'type' => 'facture',
							'object' => $inv,
							'is_paid' => $isPaid
						);
					}
				}
			}
		}

		// Separate paid and unpaid invoices
		$unpaidInvoices = array_filter($invoiceList, function($inv) { return empty($inv['is_paid']); });
		$paidInvoices = array_filter($invoiceList, function($inv) { return !empty($inv['is_paid']); });

		// Display unpaid invoices table with checkboxes
		if (!empty($unpaidInvoices)) {
			print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
			print '<input type="hidden" name="token" value="'.newToken().'">';
			print '<input type="hidden" name="action" value="confirmmulti">';
			print '<input type="hidden" name="id" value="'.$object->id.'">';

			// Table without div-table-responsive for compact layout
			print '<table class="noborder centpercent" id="invoice_table" style="margin-bottom: 0;">';

			// Info header row
			print '<tr class="liste_titre">';
			print '<td colspan="7" style="background: #f8f8f8; padding: 8px;">';
			print $langs->trans("TransactionAmount").': <strong>'.price(abs($object->amount), 0, $langs, 1, -1, 2, 'EUR').'</strong>';
			print ' &nbsp;|&nbsp; <span id="selected_sum">'.$langs->trans("Selected").': 0,00 €</span>';
			print ' &nbsp;|&nbsp; <span id="difference">'.$langs->trans("Difference").': '.price(abs($object->amount), 0, $langs, 1, -1, 2, 'EUR').'</span>';
			print '</td>';
			print '</tr>';

			print '<tr class="liste_titre">';
			print '<th class="center" style="width: 30px;"><input type="checkbox" id="checkall" onclick="toggleAll(this)"></th>';
			print '<th>'.$langs->trans("Ref").'</th>';
			print '<th>'.($selectedType == 'supplier' ? $langs->trans("SupplierRef") : $langs->trans("CustomerRef")).'</th>';
			print '<th>'.$langs->trans("ThirdParty").'</th>';
			print '<th>'.$langs->trans("Date").'</th>';
			print '<th>'.$langs->trans("DateDue").'</th>';
			print '<th class="right">'.$langs->trans("AmountRemaining").'</th>';
			print '</tr>';

			foreach ($unpaidInvoices as $inv) {
				$amountMatch = (abs($inv['amount'] - $absAmount) < 1.00);
				$rowClass = $amountMatch ? 'oddeven highlight' : 'oddeven';

				print '<tr class="'.$rowClass.'" data-amount="'.$inv['amount'].'">';
				print '<td class="center">';
				print '<input type="checkbox" name="invoices[]" value="'.$inv['id'].'" class="invoice_checkbox" onchange="updateSum()">';
				print '</td>';
				print '<td>'.$inv['object']->getNomUrl(1).'</td>';
				print '<td>'.dol_escape_htmltag($inv['ref_supplier'] ?? $inv['ref_client'] ?? '').'</td>';
				print '<td>'.dol_escape_htmltag($inv['socname']).'</td>';
				print '<td>'.dol_print_date($inv['datef'], 'day').'</td>';
				print '<td>'.($inv['date_due'] ? dol_print_date($inv['date_due'], 'day') : '-').'</td>';
				print '<td class="right">'.price($inv['amount'], 0, $langs, 1, -1, 2, 'EUR').'</td>';
				print '</tr>';
			}

			// Footer row with button
			print '<tr class="liste_total">';
			print '<td colspan="6"></td>';
			print '<td class="right" style="padding: 8px;">';
			if (!empty($bankAccountId)) {
				print '<button type="submit" class="butAction" style="margin: 0; padding: 5px 12px;">'.$langs->trans("ConfirmPayment").'</button>';
			} else {
				print '<span class="butActionRefused" title="'.$langs->trans("NoBankAccountConfigured").'">'.$langs->trans("ConfirmPayment").'</span>';
			}
			print '</td>';
			print '</tr>';

			print '</table>';
			print '</form>';
		}

		// Display paid invoices table (link to existing payment)
		if (!empty($paidInvoices)) {
			print '<div style="margin-top: 15px;"></div>';
			print load_fiche_titre($langs->trans("PaidInvoices"), '', 'object_invoice');
			print '<div class="opacitymedium" style="margin-bottom: 5px;">'.$langs->trans("PaidInvoicesInfo").'</div>';

			// Determine invoice type for all paid invoices (should be same type)
			$paidInvoiceType = $paidInvoices[0]['type'] ?? 'facture';

			print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'" id="paid_invoices_form">';
			print '<input type="hidden" name="token" value="'.newToken().'">';
			print '<input type="hidden" name="action" value="linkpaymentmulti">';
			print '<input type="hidden" name="id" value="'.$object->id.'">';
			print '<input type="hidden" name="paid_invoice_type" value="'.$paidInvoiceType.'">';

			// Table without div-table-responsive wrapper for compact layout
			print '<table class="noborder centpercent" id="paid_invoices_table" style="margin-bottom: 0;">';
			print '<tr class="liste_titre">';
			print '<th class="center" style="width: 30px;"><input type="checkbox" id="paid_select_all" onclick="toggleAllPaid(this)"></th>';
			print '<th>'.$langs->trans("Ref").'</th>';
			print '<th>'.($selectedType == 'supplier' ? $langs->trans("SupplierRef") : $langs->trans("CustomerRef")).'</th>';
			print '<th>'.$langs->trans("ThirdParty").'</th>';
			print '<th>'.$langs->trans("Date").'</th>';
			print '<th class="right">'.$langs->trans("Amount").'</th>';
			print '<th>'.$langs->trans("Status").'</th>';
			print '<th class="center">'.$langs->trans("Action").'</th>';
			print '</tr>';

			foreach ($paidInvoices as $inv) {
				$amountMatch = (abs($inv['amount'] - $absAmount) < 1.00);
				$rowClass = $amountMatch ? 'oddeven highlight' : 'oddeven';

				print '<tr class="'.$rowClass.'" data-amount="'.$inv['amount'].'">';
				print '<td class="center">';
				print '<input type="checkbox" name="paid_invoice[]" value="'.$inv['id'].'" class="paid_invoice_checkbox" onchange="updatePaidSum()">';
				print '</td>';
				print '<td>'.$inv['object']->getNomUrl(1).'</td>';
				print '<td>'.dol_escape_htmltag($inv['ref_supplier'] ?? $inv['ref_client'] ?? '').'</td>';
				print '<td>'.dol_escape_htmltag($inv['socname']).'</td>';
				print '<td>'.dol_print_date($inv['datef'], 'day').'</td>';
				print '<td class="right">'.price($inv['amount'], 0, $langs, 1, -1, 2, 'EUR').'</td>';
				print '<td><span class="badge badge-status4">'.$langs->trans("Paid").'</span></td>';
				print '<td class="center nowraponall">';
				if (!empty($bankAccountId)) {
					print '<a class="butActionSmall" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=linkpayment&invoicetype='.$inv['type'].'&invoiceid='.$inv['id'].'&token='.newToken().'">'.$langs->trans("LinkExistingPayment").'</a>';
				}
				print '</td>';
				print '</tr>';
			}

			// Footer row with sum and button - always inside table
			print '<tr class="liste_total">';
			print '<td colspan="5" style="padding: 8px;">';
			print '<span id="paid_selected_sum" style="font-weight: bold;">'.$langs->trans("Selected").': 0,00 €</span>';
			print ' &nbsp;|&nbsp; ';
			print '<span id="paid_difference">'.$langs->trans("Difference").': '.price(abs($object->amount), 0, $langs, 1, -1, 2, 'EUR').'</span>';
			print '</td>';
			print '<td colspan="3" class="right" style="padding: 8px;">';
			if (!empty($bankAccountId)) {
				print '<button type="submit" class="butAction" id="linkpayment_multi_btn" disabled style="margin: 0; padding: 5px 12px;">'.$langs->trans("LinkExistingPayment").'</button>';
			} else {
				print '<span class="butActionRefused" title="'.$langs->trans("NoBankAccountConfigured").'">'.$langs->trans("LinkExistingPayment").'</span>';
			}
			print '</td>';
			print '</tr>';

			print '</table>';
			print '</form>';
		}

		if (empty($unpaidInvoices) && empty($paidInvoices)) {
			print '<div class="opacitymedium">'.$langs->trans("NoUnpaidInvoices").'</div>';
		}

		// JavaScript for sum calculation (only if we have unpaid invoices)
		if (!empty($unpaidInvoices)) {
			print '<script>
			var transactionAmount = '.abs($object->amount).';

			function toggleAll(source) {
				var checkboxes = document.getElementsByClassName("invoice_checkbox");
				for (var i = 0; i < checkboxes.length; i++) {
					checkboxes[i].checked = source.checked;
				}
				updateSum();
			}

			function updateSum() {
				var checkboxes = document.getElementsByClassName("invoice_checkbox");
				var sum = 0;
				for (var i = 0; i < checkboxes.length; i++) {
					if (checkboxes[i].checked) {
						var row = checkboxes[i].closest("tr");
						sum += parseFloat(row.getAttribute("data-amount"));
					}
				}

				var diff = transactionAmount - sum;
				document.getElementById("selected_sum").innerHTML = "'.$langs->trans("Selected").': " + sum.toFixed(2).replace(".", ",") + " €";

				var diffEl = document.getElementById("difference");
				if (Math.abs(diff) < 0.01) {
					diffEl.innerHTML = "'.$langs->trans("Difference").': <span style=\"color: green; font-weight: bold;\">0,00 € ✓</span>";
				} else if (diff > 0) {
					diffEl.innerHTML = "'.$langs->trans("Difference").': <span style=\"color: orange;\">+" + diff.toFixed(2).replace(".", ",") + " €</span>";
				} else {
					diffEl.innerHTML = "'.$langs->trans("Difference").': <span style=\"color: red;\">" + diff.toFixed(2).replace(".", ",") + " €</span>";
				}
			}
			</script>';
		}

		// JavaScript for paid invoices multi-select (only if we have paid invoices)
		if (!empty($paidInvoices)) {
			print '<script>
			var transactionAmountPaid = '.abs($object->amount).';

			function toggleAllPaid(source) {
				var checkboxes = document.getElementsByClassName("paid_invoice_checkbox");
				for (var i = 0; i < checkboxes.length; i++) {
					checkboxes[i].checked = source.checked;
				}
				updatePaidSum();
			}

			function updatePaidSum() {
				var checkboxes = document.getElementsByClassName("paid_invoice_checkbox");
				var sum = 0;
				var count = 0;
				for (var i = 0; i < checkboxes.length; i++) {
					if (checkboxes[i].checked) {
						var row = checkboxes[i].closest("tr");
						sum += parseFloat(row.getAttribute("data-amount"));
						count++;
					}
				}

				var diff = transactionAmountPaid - sum;
				document.getElementById("paid_selected_sum").innerHTML = "'.$langs->trans("Selected").': " + sum.toFixed(2).replace(".", ",") + " €";

				var diffEl = document.getElementById("paid_difference");
				if (Math.abs(diff) < 0.01) {
					diffEl.innerHTML = "'.$langs->trans("Difference").': <span style=\"color: green; font-weight: bold;\">0,00 € ✓</span>";
				} else if (diff > 0) {
					diffEl.innerHTML = "'.$langs->trans("Difference").': <span style=\"color: orange;\">+" + diff.toFixed(2).replace(".", ",") + " €</span>";
				} else {
					diffEl.innerHTML = "'.$langs->trans("Difference").': <span style=\"color: red;\">" + diff.toFixed(2).replace(".", ",") + " €</span>";
				}

				// Enable/disable submit button
				var btn = document.getElementById("linkpayment_multi_btn");
				if (btn) {
					btn.disabled = (count == 0);
				}
			}
			</script>';
		}

		// CSS for highlighting
		print '<style>
		tr.highlight { background-color: #ffffcc !important; }
		</style>';
	}

	// Show matches if found
	$matches = $_SESSION['bankimport_matches_'.$object->id] ?? array();
	if (!empty($matches) && $object->status == BankImportTransaction::STATUS_NEW) {
		print '<br>';
		print load_fiche_titre($langs->trans("MatchesFound", count($matches)), '', 'object_invoice');

		// Translate match reasons
		$reasonLabels = array(
			'ref' => $langs->trans("MatchByRef"),
			'ref_client' => $langs->trans("MatchByClientRef"),
			'ref_supplier' => $langs->trans("MatchBySupplierRef"),
			'amount' => $langs->trans("MatchByAmount"),
			'amount_close' => $langs->trans("MatchByAmountClose"),
			'name_exact' => $langs->trans("MatchByNameExact"),
			'name_similar' => $langs->trans("MatchByNameSimilar"),
			'iban' => $langs->trans("MatchByIBAN"),
			'multi_invoice' => $langs->trans("MatchByMultiInvoice"),
			'manual' => $langs->trans("ManualSearch")
		);

		// Check for multi-invoice matches
		$hasMultiMatch = false;
		foreach ($matches as $match) {
			if ($match['type'] == 'multi_facture_fourn') {
				$hasMultiMatch = true;
				break;
			}
		}

		// If we have multi-invoice matches, show them first with selection form
		if ($hasMultiMatch) {
			foreach ($matches as $match) {
				if ($match['type'] == 'multi_facture_fourn' && !empty($match['invoices'])) {
					print '<div class="info" style="margin-bottom: 15px;">';
					print '<strong>'.$langs->trans("MatchByMultiInvoice").'</strong>: ';
					print count($match['invoices']).' '.$langs->trans("Invoices").' = '.price($match['total'] ?? $match['amount'], 0, $langs, 1, -1, 2, 'EUR');
					if (isset($match['difference']) && abs($match['difference']) > 0.01) {
						print ' <span class="opacitymedium">('.$langs->trans("Difference").': '.price($match['difference'], 0, $langs, 1, -1, 2, 'EUR').')</span>';
					}
					print '</div>';

					print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
					print '<input type="hidden" name="token" value="'.newToken().'">';
					print '<input type="hidden" name="action" value="confirmmulti">';
					print '<input type="hidden" name="id" value="'.$object->id.'">';

					print '<div class="div-table-responsive">';
					print '<table class="noborder centpercent">';
					print '<tr class="liste_titre">';
					print '<th class="center" style="width: 30px;"><input type="checkbox" id="checkall_multi" checked onclick="toggleAllCheckboxes(this)"></th>';
					print '<th>'.$langs->trans("Ref").'</th>';
					print '<th>'.$langs->trans("SupplierRef").'</th>';
					print '<th>'.$langs->trans("ThirdParty").'</th>';
					print '<th class="right">'.$langs->trans("Amount").'</th>';
					print '<th>'.$langs->trans("DateDue").'</th>';
					print '</tr>';

					require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
					foreach ($match['invoices'] as $invData) {
						$inv = new FactureFournisseur($db);
						$inv->fetch($invData['id']);

						print '<tr class="oddeven">';
						print '<td class="center"><input type="checkbox" name="invoices[]" value="'.$invData['id'].'" checked></td>';
						print '<td>'.$inv->getNomUrl(1).'</td>';
						print '<td>'.dol_escape_htmltag($invData['ref_supplier']).'</td>';
						print '<td>'.dol_escape_htmltag($invData['socname']).'</td>';
						print '<td class="right">'.price($invData['amount'], 0, $langs, 1, -1, 2, 'EUR').'</td>';
						print '<td>'.($invData['date_due'] ? dol_print_date($invData['date_due'], 'day') : '-').'</td>';
						print '</tr>';
					}

					print '</table>';
					print '</div>';
					print '<div style="margin-top: 10px;">';
					print '<button type="submit" class="butAction">'.$langs->trans("ConfirmPayment").'</button>';
					print '</div>';
					print '</form>';
					print '<br>';

					// JavaScript for toggle all
					print '<script>
					function toggleAllCheckboxes(source) {
						var checkboxes = document.getElementsByName("invoices[]");
						for (var i = 0; i < checkboxes.length; i++) {
							checkboxes[i].checked = source.checked;
						}
					}
					</script>';
				}
			}
		}

		// Show single invoice matches
		$singleMatches = array_filter($matches, function($m) { return $m['type'] != 'multi_facture_fourn'; });
		if (!empty($singleMatches)) {
			if ($hasMultiMatch) {
				print '<br>';
				print load_fiche_titre($langs->trans("Alternatives"), '', '');
			}

			print '<div class="div-table-responsive">';
			print '<table class="noborder centpercent">';
			print '<tr class="liste_titre">';
			print '<th>'.$langs->trans("Type").'</th>';
			print '<th>'.$langs->trans("Ref").'</th>';
			print '<th>'.$langs->trans("ThirdParty").'</th>';
			print '<th class="right">'.$langs->trans("Amount").'</th>';
			print '<th>'.$langs->trans("DateDue").'</th>';
			print '<th>'.$langs->trans("Score").'</th>';
			print '<th>'.$langs->trans("MatchReason").'</th>';
			print '<th></th>';
			print '</tr>';

			foreach ($singleMatches as $match) {
				print '<tr class="oddeven">';
				print '<td>'.($match['type'] == 'facture' ? $langs->trans("CustomerInvoice") : $langs->trans("SupplierInvoice")).'</td>';

				// Get invoice link
				if ($match['type'] == 'facture') {
					require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
					$inv = new Facture($db);
					$inv->fetch($match['id']);
					print '<td>'.$inv->getNomUrl(1).'</td>';
				} else {
					require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
					$inv = new FactureFournisseur($db);
					$inv->fetch($match['id']);
					print '<td>'.$inv->getNomUrl(1).'</td>';
				}

				// Third party with link
				if ($match['socid'] > 0) {
					require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
					$soc = new Societe($db);
					$soc->fetch($match['socid']);
					print '<td>'.$soc->getNomUrl(1).'</td>';
				} else {
					print '<td>'.dol_escape_htmltag($match['socname']).'</td>';
				}

				print '<td class="right">'.price($match['amount'], 0, $langs, 1, -1, 2, 'EUR').'</td>';
				print '<td>'.($match['date_due'] ? dol_print_date($match['date_due'], 'day') : '-').'</td>';

				// Score with color
				$scoreColor = $match['match_score'] >= 80 ? 'green' : ($match['match_score'] >= 60 ? 'orange' : 'gray');
				print '<td><span style="color: '.$scoreColor.'; font-weight: bold;">'.$match['match_score'].'%</span></td>';

				// Match reasons as badges
				print '<td>';
				if (!empty($match['match_reasons'])) {
					foreach ($match['match_reasons'] as $reason) {
						$label = $reasonLabels[$reason] ?? $reason;
						print '<span class="badge badge-secondary">'.$label.'</span> ';
					}
				}
				print '</td>';

				// Action buttons
				print '<td class="center nowraponall">';
				if (!empty($bankAccountId)) {
					// Confirm payment button (creates payment in Dolibarr)
					print '<a class="butActionSmall" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=confirmpayment&matchtype='.urlencode($match['type']).'&matchid='.$match['id'].'&token='.newToken().'">'.$langs->trans("ConfirmPayment").'</a>';
				} else {
					// Just link (no bank account configured)
					print '<a class="butActionSmall" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&action=linkto&linktype='.$match['type'].'&linkid='.$match['id'].'&token='.newToken().'">'.$langs->trans("Link").'</a>';
				}
				print '</td>';
				print '</tr>';
			}

			print '</table>';
			print '</div>';
		}
	}

} else {
	print '<div class="error">'.$langs->trans("RecordNotFound").'</div>';
}

// Back link
print '<div class="tabsAction">';
print '<a class="butAction" href="'.dol_buildpath('/bankimport/list.php', 1).'">'.$langs->trans("BackToList").'</a>';
print '</div>';

llxFooter();
$db->close();
