<?php
$pageTitle = 'Billing & Subscription';
$breadcrumb = [['label' => 'Settings', 'url' => APP_URL . '/settings'], ['label' => 'Billing', 'url' => null]];
ob_start();
?>
<div class="page-header">
    <div>
        <h1 class="page-title">Billing &amp; Subscription</h1>
        <p class="page-subtitle">Manage your subscription plan and platform fee charges</p>
    </div>
</div>

<div class="tabs">
    <a href="<?= APP_URL ?>/settings" class="tab-item">General</a>
    <a href="<?= APP_URL ?>/settings/security" class="tab-item">Security</a>
    <a href="<?= APP_URL ?>/settings/billing" class="tab-item active">Billing</a>
    <?php if (\Core\Auth::isSuperAdmin()): ?>
    <a href="<?= APP_URL ?>/settings/email" class="tab-item">Email/SMTP</a>
    <a href="<?= APP_URL ?>/admin/system" class="tab-item">System</a>
    <a href="<?= APP_URL ?>/settings/audit" class="tab-item">Audit Log</a>
    <?php endif; ?>
</div>

<div class="card" style="margin-bottom:24px;">
    <div class="card-header">
        <h3 class="card-title"><?= $org ? 'Current Plan' : 'Platform-wide Totals' ?></h3>
    </div>
    <div class="card-body" style="display:flex;align-items:center;justify-content:space-between;">
        <div>
            <?php if ($org): ?>
            <div style="font-size:1.2rem;font-weight:700;"><?= htmlspecialchars($org['plan_name'] ?? 'Trial') ?></div>
            <div style="color:var(--text-muted);font-size:0.85rem;">Status: <?= ucfirst($org['subscription_status'] ?? 'trial') ?></div>
            <?php else: ?>
            <div style="color:var(--text-muted);font-size:0.85rem;">Platform fee charges across all organizations</div>
            <?php endif; ?>
        </div>
        <div style="display:flex;gap:24px;text-align:center;">
            <div>
                <div style="font-size:1.5rem;font-weight:700;"><?= (int)($stats['total_count'] ?? 0) ?></div>
                <div style="font-size:0.75rem;color:var(--text-muted);">Total Charges</div>
            </div>
            <div>
                <div style="font-size:1.5rem;font-weight:700;color:var(--danger);"><?= formatCurrency((float)($stats['total_pending'] ?? 0)) ?></div>
                <div style="font-size:0.75rem;color:var(--text-muted);">Pending</div>
            </div>
            <div>
                <div style="font-size:1.5rem;font-weight:700;color:var(--success);"><?= formatCurrency((float)($stats['total_paid'] ?? 0)) ?></div>
                <div style="font-size:0.75rem;color:var(--text-muted);">Paid</div>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h3 class="card-title">Platform Fee Charges</h3>
    </div>
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>Reference</th>
                    <?php if (!$org): ?><th>Organization</th><?php endif; ?>
                    <th>Project</th>
                    <th>Contractor</th>
                    <th>Amount</th>
                    <th>Status</th>
                    <th>Due Date</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($charges)): ?>
                <tr><td colspan="7" class="text-center" style="padding:32px;color:var(--text-muted);">No charges yet.</td></tr>
                <?php else: ?>
                    <?php foreach ($charges as $charge): ?>
                    <tr>
                        <td><?= htmlspecialchars($charge['reference_number']) ?></td>
                        <?php if (!$org): ?><td><?= htmlspecialchars($charge['org_name'] ?? '-') ?></td><?php endif; ?>
                        <td><?= htmlspecialchars($charge['project_name'] ?? '-') ?></td>
                        <td><?= htmlspecialchars($charge['contractor_name'] ?? '-') ?></td>
                        <td><?= formatCurrency((float)$charge['charge_amount'], $charge['currency'] ?? DEFAULT_CURRENCY) ?></td>
                        <td><span class="badge badge-<?= $charge['status'] === 'paid' ? 'active' : 'warning' ?>"><?= ucfirst($charge['status']) ?></span></td>
                        <td><?= htmlspecialchars($charge['due_date'] ?? '-') ?></td>
                        <td>
                            <?php if ($charge['status'] === 'pending'): ?>
                            <form method="POST" action="<?= APP_URL ?>/finance/billing/<?= $charge['id'] ?>/pay" onsubmit="return confirm('Record this payment?');">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                                <input type="hidden" name="amount" value="<?= htmlspecialchars($charge['charge_amount']) ?>">
                                <button type="submit" class="btn btn-sm btn-primary">Record Payment</button>
                            </form>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
