<?php require_once __DIR__ . '/_sidebar.php'; ?>
<main class="flex-1 py-16 md:py-0 md:pl-64">
    <div class="max-w-7xl mx-auto px-4 py-8 md:py-16">
        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
            <div>
                <h1 class="text-3xl font-bold text-slate-950">Customers</h1>
                <p class="mt-2 text-slate-600">Review contact details, billing totals and payment status for customers.</p>
            </div>
            <div class="flex flex-col sm:flex-row gap-3">
                <a href="<?= htmlspecialchars($baseUrl) ?>/admin/customers/create" class="inline-flex items-center justify-center rounded-full btn-primary px-5 py-3 text-sm font-semibold text-white hover:bg-rich-crimson transition">Add Customer</a>
                <a href="<?= htmlspecialchars($baseUrl) ?>/admin" class="inline-flex items-center justify-center rounded-full border border-slate-300 bg-white px-5 py-3 text-sm text-slate-950 hover:bg-slate-100 transition">Back to Dashboard</a>
            </div>
        </div>
        <div class="mt-10 overflow-x-auto rounded-3xl border border-slate-200 bg-white shadow-lg">
            <table class="min-w-full divide-y divide-slate-200 text-sm">
                <thead class="bg-charcoal text-white">
                    <tr>
                        <th class="px-6 py-4 text-left">Customer</th>
                        <th class="px-6 py-4 text-left">Contact</th>
                        <th class="px-6 py-4 text-left">Billed</th>
                        <th class="px-6 py-4 text-left">Paid</th>
                        <th class="px-6 py-4 text-left">Outstanding</th>
                        <th class="px-6 py-4 text-left">Payment Status</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200 bg-white">
                    <?php foreach ($customers as $customer): ?>
                    <?php
                        $billed = (float)$customer['billed_amount'];
                        $paid = (float)$customer['paid_amount'];
                        $outstanding = (float)$customer['outstanding_amount'];
                        $paymentStatus = 'No Billing';
                        if ($billed > 0) {
                            if ($outstanding <= 0) {
                                $paymentStatus = 'Paid';
                            } elseif ($paid > 0) {
                                $paymentStatus = 'Partial';
                            } else {
                                $paymentStatus = 'Outstanding';
                            }
                        }
                    ?>
                    <tr>
                        <td class="px-6 py-4 text-slate-700"><strong><?= htmlspecialchars($customer['name']) ?></strong><br><?= htmlspecialchars($customer['company']) ?></td>
                        <td class="px-6 py-4 text-slate-700"><?= htmlspecialchars($customer['email']) ?><br><?= htmlspecialchars($customer['phone']) ?></td>
                        <td class="px-6 py-4 text-slate-700">GHS <?= number_format($billed, 2) ?></td>
                        <td class="px-6 py-4 text-slate-700">GHS <?= number_format($paid, 2) ?></td>
                        <td class="px-6 py-4 text-slate-700">GHS <?= number_format($outstanding, 2) ?></td>
                        <td class="px-6 py-4 text-slate-700"><span class="inline-flex rounded-full bg-amber-100 px-3 py-1 text-xs font-semibold text-amber-800"><?= htmlspecialchars($paymentStatus) ?></span></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</main>
</div>
