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

billing_require_auth();
billing_ensure_expense_support();

$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');
}

$expenses = billing_get_expenses((int) $user['id'], (int) $organization['id']);
$billingActiveNav = 'expenses';
$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>Expenses</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;">Expenses</h1>
                        <p class="panel-subtitle">Working expense route restored after folder save.</p>
                    </div>
                    <a class="customer-btn customer-btn--green" href="expense_new.php">New Expense</a>
                </div>
                <div class="expenses-table-wrap">
                    <table class="expenses-table">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Account</th>
                                <th>Customer</th>
                                <th>Status</th>
                                <th>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($expenses === []): ?>
                                <tr><td class="expenses-empty" colspan="5">No expenses recorded yet.</td></tr>
                            <?php else: ?>
                                <?php foreach ($expenses as $expense): ?>
                                    <tr>
                                        <td><?php echo billing_escape((string) ($expense['expense_date'] ?? '')); ?></td>
                                        <td><a class="name-link" href="expense_new.php?id=<?php echo (int) ($expense['id'] ?? 0); ?>"><?php echo billing_escape((string) (($expense['expense_account'] ?? '') !== '' ? $expense['expense_account'] : ($expense['category_name'] ?? ''))); ?></a></td>
                                        <td><?php echo billing_escape((string) ($expense['customer_name'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) ($expense['status_label'] ?? '')); ?></td>
                                        <td>&#8377;<?php echo billing_escape(number_format((float) ($expense['total_amount'] ?? 0), 2)); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
</body>
</html>

