<?php
$currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : default_currency();
$voucher_total = (float) ($sales_info->total ?? $sales_info->total_amount ?? 0);
$party_label = $active == 'receipt_voucher' ? lang('clients') : lang('supplier');
$line_label = $active == 'receipt_voucher' ? 'Receipt lines' : 'Payment lines';
?>

<div class="overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
	<div class="flex flex-col gap-3 border-b border-border px-5 py-4 md:flex-row md:items-center md:justify-between">
		<div>
			<h3 class="text-base font-semibold text-foreground"><?= lang('details') ?></h3>
			<p class="text-sm text-muted-foreground"><?= $line_label ?></p>
		</div>
		<span class="inline-flex w-fit items-center gap-2 rounded-full border border-border bg-background px-3 py-1 text-xs font-semibold text-foreground">
			<i data-lucide="<?= $active == 'receipt_voucher' ? 'user-round-check' : 'truck' ?>" class="h-3.5 w-3.5 text-muted-foreground"></i>
			<?= $party_label ?>
		</span>
	</div>

	<div class="overflow-x-auto">
		<table class="min-w-full text-sm">
			<thead class="bg-muted/40 text-xs uppercase tracking-wide text-muted-foreground">
				<tr>
					<th class="w-14 px-5 py-3 text-left">#</th>
					<th class="px-4 py-3 text-left"><?= $party_label ?></th>
					<th class="px-4 py-3 text-left"><?= lang('description') ?></th>
					<th class="w-44 px-5 py-3 text-right"><?= lang('amount') ?></th>
				</tr>
			</thead>
			<tbody class="divide-y divide-border">
				<?php if (!empty($receipt_items)) { ?>
				<?php foreach ($receipt_items as $key => $item) { ?>
					<tr class="transition hover:bg-muted/30">
						<td class="px-5 py-4 align-top text-muted-foreground"><?= $key + 1 ?></td>
						<td class="px-4 py-4 align-top">
							<div class="font-semibold text-foreground"><?= html_escape($item->name) ?></div>
						</td>
						<td class="max-w-xl px-4 py-4 align-top text-muted-foreground">
							<?= !empty($item->description) ? nl2br(html_escape($item->description)) : '<span class="text-muted-foreground/70">-</span>' ?>
						</td>
						<td class="px-5 py-4 text-right align-top font-semibold text-foreground"><?= display_money($item->amount, $currency_symbol) ?></td>
					</tr>
				<?php } ?>
				<?php } else { ?>
					<tr>
						<td colspan="4" class="px-5 py-10 text-center text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></td>
					</tr>
				<?php } ?>
			</tbody>
			<tfoot class="border-t border-border bg-muted/30">
				<tr>
					<td colspan="3" class="px-5 py-4 text-right text-xs font-semibold uppercase tracking-wide text-muted-foreground"><?= lang('total') ?></td>
					<td class="px-5 py-4 text-right text-sm font-bold text-foreground"><?= display_money($voucher_total, $currency_symbol) ?></td>
				</tr>
			</tfoot>
		</table>
	</div>

	<div class="grid grid-cols-1 gap-4 border-t border-border p-5 lg:grid-cols-3">
		<div class="lg:col-span-2">
			<div class="min-h-28 rounded-xl border border-border bg-background p-4">
				<div class="flex items-center gap-2 text-sm font-semibold text-foreground">
					<i data-lucide="file-text" class="h-4 w-4 text-muted-foreground"></i>
					<?= lang('notes') ?>
				</div>
				<div class="mt-3 whitespace-pre-line text-sm leading-6 text-muted-foreground">
					<?= !empty($sales_info->notes) ? html_escape($sales_info->notes) : '<span class="text-muted-foreground/70">-</span>' ?>
				</div>
			</div>
		</div>
		<div class="rounded-xl border border-border bg-background p-4 text-sm space-y-3">
			<div class="flex items-center justify-between gap-4">
				<span class="text-muted-foreground"><?= lang('total') ?></span>
				<span class="font-semibold text-foreground"><?= display_money($voucher_total, $currency_symbol) ?></span>
			</div>
			<?php
			$due_amount = $voucher_total;
			if (config_item('amount_to_words') == 'Yes' && !empty($due_amount) && $due_amount > 0) { ?>
				<div class="border-t border-border pt-3 text-xs leading-5 text-muted-foreground">
					<strong><?= lang('num_word') ?>:</strong> <?= number_to_word($currency->code, $due_amount); ?>
				</div>
			<?php } ?>
		</div>
	</div>
</div>
