<?php defined('BASEPATH') or exit('No direct script access allowed');

$gateway_name = $gateway['name'] ?? ucfirst((string) $gateway_key);
$currency_code = !empty($invoice_info->currency) ? $invoice_info->currency : config_item('default_currency');
$currency_symbol = client_currency($invoice_info->client_id);
$return_url = base_url('client/invoice/manage_invoice/invoice_details/' . $invoice_info->invoices_id);
$is_ready = !empty($gateway_allowed) && !empty($gateway_active) && $invoice_due > 0;
$status_message = '';
if (empty($gateway_allowed)) {
	$status_message = $gateway_name . ' is not enabled for this invoice.';
} elseif (empty($gateway_active)) {
	$status_message = $gateway_name . ' is not active in payment settings.';
} elseif ($invoice_due <= 0) {
	$status_message = 'This invoice has no remaining balance.';
}
?>

<div class="<?= !empty($is_modal) ? 'space-y-4' : 'mx-auto max-w-2xl space-y-4'; ?>">
	<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
		<div class="flex items-start justify-between gap-4">
			<div>
				<p class="text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('payment') ?></p>
				<h2 class="mt-1 text-xl font-semibold text-foreground"><?= html_escape($gateway_name) ?></h2>
				<p class="mt-1 text-sm text-muted-foreground">Invoice #<?= html_escape($invoice_info->reference_no) ?></p>
			</div>
			<div class="rounded-xl border border-border bg-muted/30 px-3 py-2 text-right">
				<p class="text-xs font-semibold text-muted-foreground"><?= lang('amount') ?></p>
				<p class="text-lg font-semibold text-foreground"><?= display_money($invoice_due, $currency_symbol) ?></p>
			</div>
		</div>
	</div>

	<?php if (!$is_ready) { ?>
		<div class="rounded-2xl border border-amber-200 bg-amber-50 p-5 text-sm text-amber-800 dark:border-amber-500/30 dark:bg-amber-500/10 dark:text-amber-200">
			<?= html_escape($status_message); ?>
		</div>
		<div class="flex justify-end">
			<a href="<?= $return_url ?>" class="inline-flex items-center rounded-lg border border-border bg-card px-4 py-2 text-sm font-semibold text-foreground hover:bg-muted">
				<?= lang('back') ?>
			</a>
		</div>
	<?php } elseif ($gateway_key === 'paypal') { ?>
		<form action="<?= html_escape($paypal_url); ?>" method="post" class="rounded-2xl border border-border bg-card p-5 shadow-sm">
			<input type="hidden" name="cmd" value="_xclick">
			<input type="hidden" name="business" value="<?= html_escape(config_item('paypal_email') ?: config_item('paypal_api_username')); ?>">
			<input type="hidden" name="item_name" value="<?= html_escape($invoice_info->reference_no); ?>">
			<input type="hidden" name="item_number" value="<?= (int) $invoice_info->invoices_id; ?>">
			<input type="hidden" name="amount" value="<?= number_format((float) $invoice_due, 2, '.', ''); ?>">
			<input type="hidden" name="currency_code" value="<?= html_escape($currency_code); ?>">
			<input type="hidden" name="return" value="<?= $return_url; ?>">
			<input type="hidden" name="cancel_return" value="<?= $return_url; ?>">
			<input type="hidden" name="notify_url" value="<?= base_url('payment/paypal/ipn/' . $invoice_info->invoices_id); ?>">
			<div class="space-y-2">
				<p class="text-sm text-muted-foreground">You will be redirected to PayPal to complete the payment securely.</p>
				<button type="submit" class="inline-flex w-full items-center justify-center rounded-lg bg-primary px-4 py-2.5 text-sm font-semibold text-white hover:bg-primary/90">
					Continue to PayPal
				</button>
			</div>
		</form>
	<?php } elseif ($gateway_key === 'checkout') { ?>
		<form action="<?= base_url('client/checkout/process'); ?>" method="post" class="rounded-2xl border border-border bg-card p-5 shadow-sm">
			<input type="hidden" name="invoice_id" value="<?= (int) $invoice_info->invoices_id; ?>">
			<input type="hidden" name="amount" value="<?= number_format((float) $invoice_due, 2, '.', ''); ?>">
			<div class="space-y-3">
				<p class="text-sm text-muted-foreground">2Checkout is active for this invoice. Enter the token returned by your 2Checkout checkout widget to process payment.</p>
				<input type="text" name="token" class="form-control" placeholder="2Checkout token" required>
				<button type="submit" class="inline-flex w-full items-center justify-center rounded-lg bg-primary px-4 py-2.5 text-sm font-semibold text-white hover:bg-primary/90">
					Process 2Checkout Payment
				</button>
			</div>
		</form>
	<?php } else { ?>
		<div class="rounded-2xl border border-border bg-card p-5 shadow-sm">
			<div class="space-y-2">
				<h3 class="text-base font-semibold text-foreground"><?= html_escape($gateway_name); ?> checkout</h3>
				<p class="text-sm text-muted-foreground">
					<?= html_escape($gateway_name); ?> is enabled for this invoice, but this installation does not include the server-side checkout handler for this gateway. Please use an available configured gateway or add the gateway SDK/controller before collecting live payments.
				</p>
			</div>
		</div>
	<?php } ?>
</div>
