<?php
$active_purchase_id = isset($active_purchase_id) ? $active_purchase_id : null;
$list_title = !empty($list_title) ? $list_title : lang('all') . ' ' . lang('purchase');
$item_url = !empty($item_url) ? $item_url : base_url('admin/purchase/payment/');
$show_new = !empty($show_new);
$new_url = !empty($new_url) ? $new_url : base_url('admin/purchase/new_purchase');

$currency = $this->purchase_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
?>

<div class="rounded-2xl border border-border bg-card shadow-sm">
	<div class="flex items-center justify-between border-b border-border px-4 py-4">
		<div class="flex items-center gap-2 text-sm font-semibold text-foreground">
			<i data-lucide="list" class="h-5 w-5"></i>
			<span><?= $list_title ?></span>
		</div>
		<?php if ($show_new) { ?>
			<a href="<?= $new_url ?>" class="inline-flex items-center gap-2 rounded-lg border border-border bg-card px-2 py-1 text-xs font-semibold text-foreground hover:bg-muted">
				<i data-lucide="plus" class="h-4 w-4"></i>
			</a>
		<?php } ?>
	</div>
	<div class="max-h-[70vh] overflow-y-auto divide-y divide-border">
		<?php if (!empty($all_purchases)) {
			$all_purchases = array_reverse($all_purchases);
			foreach ($all_purchases as $v_purchase) {
				$status = $this->purchase_model->get_payment_status($v_purchase->purchase_id);
				$badge_class = 'bg-rose-50 text-rose-700 border-rose-200';
				if ($status == ('fully_paid')) {
					$badge_class = 'bg-emerald-50 text-emerald-700 border-emerald-200';
				} elseif ($status == ('partially_paid')) {
					$badge_class = 'bg-amber-50 text-amber-700 border-amber-200';
				} elseif ($status == ('cancelled')) {
					$badge_class = 'bg-slate-100 text-slate-700 border-slate-200';
				} elseif ($v_purchase->emailed == 'Yes') {
					$badge_class = 'bg-blue-50 text-blue-700 border-blue-200';
					$status = 'sent';
				}
				$client_info = $this->purchase_model->check_by(array('supplier_id' => $v_purchase->supplier_id), 'tbl_suppliers');
				$client_name = !empty($client_info) ? $client_info->name : '-';
				$due = $this->purchase_model->calculate_to('purchase_due', $v_purchase->purchase_id);
				?>
				<a href="<?= $item_url . $v_purchase->purchase_id ?>" class="block px-4 py-3 hover:bg-muted <?php if ($v_purchase->purchase_id == $active_purchase_id) { echo 'bg-muted'; } ?>">
					<div class="flex items-center justify-between gap-2">
						<div class="font-medium text-foreground truncate"><?= html_escape($client_name) ?></div>
						<div class="text-xs text-muted-foreground whitespace-nowrap"><?= display_money($due, $currency->symbol) ?></div>
					</div>
					<div class="mt-2 flex items-center gap-2 text-xs text-muted-foreground">
						<span class="truncate"><?= html_escape($v_purchase->reference_no) ?></span>
						<span class="inline-flex items-center rounded-full border px-2 py-0.5 <?= $badge_class ?>">
							<?= lang($status) ?>
						</span>
					</div>
				</a>
			<?php }
		} else { ?>
			<div class="p-4 text-sm text-muted-foreground"><?= lang('nothing_to_display') ?></div>
		<?php } ?>
	</div>
</div>
