<?php
$currency = $this->db->where('code', config_item('default_currency'))->get('tbl_currencies')->row();
$currency_symbol = !empty($currency->symbol) ? $currency->symbol : default_currency();
$total_debit = (float) ($sales_info->total_debit ?? 0);
$total_credit = (float) ($sales_info->total_credit ?? 0);
$balance_difference = $total_debit - $total_credit;
$is_balanced = abs($balance_difference) < 0.01;
?>

<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">Journal entry lines</p>
		</div>
		<span class="inline-flex w-fit items-center gap-2 rounded-full border <?= $is_balanced ? 'border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-900/60 dark:bg-emerald-950/40 dark:text-emerald-300' : 'border-rose-200 bg-rose-50 text-rose-700 dark:border-rose-900/60 dark:bg-rose-950/40 dark:text-rose-300' ?> px-3 py-1 text-xs font-semibold">
			<i data-lucide="<?= $is_balanced ? 'check-circle-2' : 'triangle-alert' ?>" class="h-3.5 w-3.5"></i>
			<?= $is_balanced ? 'Balanced' : 'Out of balance' ?>
		</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"><?= lang('chart_of_accounts') ?></th>
					<th class="px-4 py-3 text-left"><?= lang('description') ?></th>
					<th class="w-40 px-4 py-3 text-right"><?= lang('debit') ?></th>
					<th class="w-40 px-5 py-3 text-right"><?= lang('credit') ?></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-3">
							<div class="font-semibold text-foreground"><?= html_escape($item->name) ?></div>
							<?php if (!empty($item->code)) { ?>
								<div class="mt-1 text-xs font-medium text-muted-foreground"><?= html_escape($item->code) ?></div>
							<?php } ?>
						</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-4 py-4 text-right align-top font-semibold text-foreground"><?= display_money($item->debit, $currency_symbol) ?></td>
						<td class="px-5 py-4 text-right align-top font-semibold text-foreground"><?= display_money($item->credit, $currency_symbol) ?></td>
					</tr>
				<?php } ?>
				<?php } else { ?>
					<tr>
						<td colspan="5" 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-4 py-4 text-right text-sm font-bold text-foreground"><?= display_money($total_debit, $currency_symbol) ?></td>
					<td class="px-5 py-4 text-right text-sm font-bold text-foreground"><?= display_money($total_credit, $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_debit') ?></span>
				<span class="font-semibold text-foreground"><?= display_money($total_debit, $currency_symbol) ?></span>
			</div>
			<div class="flex items-center justify-between gap-4">
				<span class="text-muted-foreground"><?= lang('total_credit') ?></span>
				<span class="font-semibold text-foreground"><?= display_money($total_credit, $currency_symbol) ?></span>
			</div>
			<div class="border-t border-border pt-3">
				<div class="flex items-center justify-between gap-4">
					<span class="text-muted-foreground">Difference</span>
					<span class="font-semibold <?= $is_balanced ? 'text-emerald-600 dark:text-emerald-300' : 'text-rose-600 dark:text-rose-300' ?>">
						<?= display_money(abs($balance_difference), $currency_symbol) ?>
					</span>
				</div>
			</div>
		</div>
	</div>
</div>
