<?php
session_start();
require_once dirname(__DIR__) . '/includes/functions.php';

billing_require_auth();

$user = billing_current_user();
$organization = billing_current_organization();

if (!$user) {
    billing_redirect('signin.php');
}

if (!$organization || empty($organization['onboarding_completed_at'])) {
    billing_redirect('onboarding.php?step=1');
}

$customers = billing_get_customers((int) $user['id']);
$billingActiveNav = 'customers';
$billingTopbarOrganizationName = (string) ($organization['organization_name'] ?? 'Billing');
$billingTopbarUserLabel = (string) ($user['email'] ?? '');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="<?php echo billing_escape(billing_base_href()); ?>">
<title>Customers</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;700;800&display=swap">
<link rel="stylesheet" href="Payslip_Project/assets/fontawesome/css/all.min.css">
<link rel="stylesheet" href="assets/billing.css">
</head>
<body>
<div class="dashboard-shell">
    <?php include dirname(__DIR__) . '/includes/app_sidebar.php'; ?>
    <div class="dashboard-main">
        <?php include dirname(__DIR__) . '/includes/topbar.php'; ?>
        <main class="dashboard-content catalog-page">
            <section class="panel">
                <div class="catalog-list-head">
                    <div>
                        <h1 style="margin:0;">Customers</h1>
                        <p class="panel-subtitle">This route is restored. Your customer URLs and redirects now stay on working pages.</p>
                    </div>
                    <div class="catalog-detail-actions">
                        <a class="customer-btn" href="customer_import.php">Import</a>
                        <a class="customer-btn customer-btn--green" href="customer_new.php">New Customer</a>
                    </div>
                </div>
                <div class="expenses-table-wrap">
                    <table class="expenses-table">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Status</th>
                                <th>Receivable</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($customers === []): ?>
                                <tr><td class="expenses-empty" colspan="4">No customers available.</td></tr>
                            <?php else: ?>
                                <?php foreach ($customers as $customer): ?>
                                    <tr>
                                        <td><a class="name-link" href="customer_new.php?id=<?php echo (int) ($customer['id'] ?? 0); ?>"><?php echo billing_escape((string) ($customer['customer_name'] ?? '')); ?></a></td>
                                        <td><?php echo billing_escape((string) ($customer['email'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) ($customer['status_label'] ?? '')); ?></td>
                                        <td>&#8377;<?php echo billing_escape(number_format((float) ($customer['outstanding_amount'] ?? 0), 2)); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
</body>
</html>

