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

billing_require_auth();
billing_ensure_product_catalog_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');
}

$stats = billing_dashboard_stats((int) $user['id']);
$organizationName = (string) ($organization['organization_name'] ?? 'bala');
$userLabel = (string) ($user['email'] ?? 'billing-user');
$billingTopbarOrganizationName = $organizationName;
$billingTopbarUserLabel = $userLabel;
$billingActiveNav = 'addons';

$addons = billing_get_addons((int) $user['id'], (int) $organization['id'], 'active');
$flashSuccess = billing_flash('success');
?>
<!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>Addons | Zoho Billing</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;600;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 customer-shell">
    <?php include dirname(__DIR__) . '/includes/app_sidebar.php'; ?>
    <div class="dashboard-main customer-main">
        <?php include dirname(__DIR__) . '/includes/topbar.php'; ?>
        <main class="catalog-page">
            <?php if ($flashSuccess): ?><div class="customer-flash"><?php echo billing_escape($flashSuccess); ?></div><?php endif; ?>
            <section class="customer-views-card">
                <div class="catalog-list-head">
                    <div class="catalog-view-links">
                        <a class="is-active" href="addons.php">All Addons</a>
                    </div>
                    <div class="customer-view-actions">
                        <a class="customer-btn customer-btn--green" href="addon_new.php"><i class="fa-solid fa-plus"></i> New</a>
                    </div>
                </div>
                <div class="customer-table-wrap">
                    <table class="customer-table-view">
                        <thead>
                            <tr>
                                <th class="customer-table-filter-col"><i class="fa-solid fa-sliders"></i></th>
                                <th>NAME</th>
                                <th>PRODUCT</th>
                                <th>ADDON CODE</th>
                                <th>DESCRIPTION</th>
                                <th>STATUS</th>
                                <th>ADDON TYPE</th>
                                <th>PRICING MODEL</th>
                                <th>BILLING FREQUENCY</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($addons === []): ?>
                                <tr><td colspan="9" class="customer-table-empty">There aren't any addons yet.</td></tr>
                            <?php else: ?>
                                <?php foreach ($addons as $addon): ?>
                                    <tr>
                                        <td><input type="checkbox" aria-label="Select addon"></td>
                                        <td><a class="customer-name-link" href="addon_new.php?id=<?php echo (int) $addon['id']; ?>"><?php echo billing_escape((string) $addon['addon_name']); ?></a></td>
                                        <td><?php echo billing_escape((string) $addon['product_name']); ?></td>
                                        <td><?php echo billing_escape((string) $addon['addon_code']); ?></td>
                                        <td><?php echo billing_escape((string) ($addon['addon_description'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) $addon['status_label']); ?></td>
                                        <td><?php echo billing_escape((string) $addon['addon_type']); ?></td>
                                        <td><?php echo billing_escape((string) $addon['pricing_model']); ?></td>
                                        <td><?php echo $addon['billing_frequency_value'] ? (int) $addon['billing_frequency_value'] . ' ' . billing_escape((string) $addon['billing_frequency_unit']) : '-'; ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
</body>
</html>



