<?php
$client_tab_data = isset($client_tab_data) && is_array($client_tab_data) ? $client_tab_data : array();
$payments = isset($client_tab_data['payments']) ? $client_tab_data['payments'] : array();
$total_payment = isset($client_tab_data['payments_total']) ? (float) $client_tab_data['payments_total'] : 0;
$created = can_action('13', 'created');
$edited = can_action('13', 'edited');
?>

<div class="rounded-2xl border border-slate-200 bg-white shadow-sm dark:border-slate-700 dark:bg-slate-800">
	<div class="flex flex-wrap items-center justify-between gap-3 border-b border-slate-200 px-5 py-4 dark:border-slate-700">
		<div>
			<h3 class="text-sm font-semibold text-slate-900 dark:text-slate-100"><?= lang('payments') ?></h3>
			<p class="text-xs text-slate-500 dark:text-slate-400"><?= lang('client') ?> &bull; <?= html_escape($client_details->name ?? '') ?></p>
		</div>
		<div class="flex flex-wrap items-center gap-2">
			<span class="inline-flex items-center rounded-full border border-slate-200 bg-slate-50 px-2.5 py-1 text-xs font-semibold text-slate-600 dark:border-slate-600 dark:bg-slate-700/70 dark:text-slate-200">
				<?= count($payments) ?> <?= lang('records') ?>
			</span>
			<?php if (!empty($created) || !empty($edited)) { ?>
				<?= btn_modal(
					'admin/invoice/make_payment?client_id=' . $client_details->client_id . '&lock_client=1&reset=1',
					lang('batch') . ' ' . lang('payments'),
					'Add Payments',
					'xl',
					'credit-card',
					array('class' => 'inline-flex items-center rounded-lg bg-slate-900 px-3 py-1.5 text-xs font-semibold text-white hover:bg-slate-800 dark:bg-slate-100 dark:text-slate-900 dark:hover:bg-white')
				) ?>
			<?php } ?>
		</div>
	</div>

	<div class="p-5">
		<div class="overflow-hidden rounded-xl border border-slate-200 dark:border-slate-700">
			<div class="overflow-x-auto">
				<table class="min-w-[760px] w-full text-sm">
					<thead class="bg-slate-50 text-xs uppercase tracking-wide text-slate-500 dark:bg-slate-900/60 dark:text-slate-300">
						<tr>
							<th class="px-4 py-3 text-left"><?= lang('transaction_id') ?></th>
							<th class="px-4 py-3 text-left"><?= lang('payment_date') ?></th>
							<th class="px-4 py-3 text-left"><?= lang('payment_method') ?></th>
							<th class="px-4 py-3 text-right"><?= lang('amount') ?></th>
						</tr>
					</thead>
					<tbody class="divide-y divide-slate-100 text-slate-700 dark:divide-slate-800 dark:text-slate-200">
						<?php if (!empty($payments)) { ?>
							<?php foreach ($payments as $payment) { ?>
								<tr class="transition hover:bg-slate-50/80 dark:hover:bg-slate-900/40">
									<td class="px-4 py-3 font-medium text-slate-800 dark:text-slate-100">
										<?= !empty($payment->trans_id) ? html_escape($payment->trans_id) : '-' ?>
									</td>
									<td class="px-4 py-3"><?= !empty($payment->payment_date) ? display_date($payment->payment_date) : '-' ?></td>
									<td class="px-4 py-3"><?= !empty($payment->payment_method) ? lang($payment->payment_method) : '-' ?></td>
									<td class="px-4 py-3 text-right font-semibold">
										<?= display_money($payment->amount, $payment->currency) ?>
									</td>
								</tr>
							<?php } ?>
						<?php } else { ?>
							<tr>
								<td colspan="4" class="px-4 py-8 text-center text-sm text-slate-500 dark:text-slate-400">
									<?= lang('no_record_found') ?>
								</td>
							</tr>
						<?php } ?>
					</tbody>
				</table>
			</div>
		</div>
	</div>

	<div class="flex items-center justify-between border-t border-slate-200 bg-slate-50/60 px-5 py-3 text-sm dark:border-slate-700 dark:bg-slate-900/40">
		<span class="font-semibold text-slate-600 dark:text-slate-300"><?= lang('total') ?></span>
		<span class="inline-flex items-center rounded-full bg-emerald-100 px-3 py-1 text-xs font-semibold text-emerald-700 dark:bg-emerald-900/40 dark:text-emerald-200">
			<?= display_money($total_payment, client_currency($client_details->client_id)); ?>
		</span>
	</div>
</div>
