<?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 = 'plans';

$view = (string) ($_GET['view'] ?? 'active');
if (!in_array($view, ['active', 'all', 'inactive'], true)) {
    $view = 'active';
}
$plans = billing_get_plans((int) $user['id'], (int) $organization['id'], $view);
$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>Plans | 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="<?php echo $view === 'active' ? 'is-active' : ''; ?>" href="plans.php?view=active">All Plans</a>
                        <a class="<?php echo $view === 'all' ? 'is-active' : ''; ?>" href="plans.php?view=all">Products</a>
                    </div>
                    <div class="customer-view-actions">
                        <a class="customer-btn customer-btn--green" href="plan_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>PLAN</th>
                                <th>PRODUCT</th>
                                <th>PLAN CODE</th>
                                <th>DESCRIPTION</th>
                                <th>STATUS</th>
                                <th>PRICING MODEL</th>
                                <th>BILLING FREQUENCY</th>
                                <th>PRICE</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if ($plans === []): ?>
                                <tr><td colspan="9" class="customer-table-empty">There aren't any plans yet.</td></tr>
                            <?php else: ?>
                                <?php foreach ($plans as $plan): ?>
                                    <tr>
                                        <td><input type="checkbox" aria-label="Select plan"></td>
                                        <td><a class="customer-name-link" href="plan_new.php?id=<?php echo (int) $plan['id']; ?>"><?php echo billing_escape((string) $plan['plan_name']); ?></a></td>
                                        <td><?php echo billing_escape((string) $plan['product_name']); ?></td>
                                        <td><?php echo billing_escape((string) $plan['plan_code']); ?></td>
                                        <td><?php echo billing_escape((string) ($plan['plan_description'] ?? '')); ?></td>
                                        <td><?php echo billing_escape((string) $plan['status_label']); ?></td>
                                        <td><?php echo billing_escape((string) $plan['pricing_model']); ?></td>
                                        <td><?php echo (int) $plan['billing_frequency_value']; ?> <?php echo billing_escape((string) $plan['billing_frequency_unit']); ?></td>
                                        <td>â‚¹<?php echo billing_escape(number_format((float) $plan['price'], 2)); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </section>
        </main>
    </div>
</div>
<script src="assets/billing.js"></script>
</body>
</html>



