<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$allCourses = $allCourses ?? []; $branchFees = $branchFees ?? [];
?>
<div class="admin-section">
    <div style="background:#eaf0fb;border-left:4px solid #16a085;padding:12px 16px;border-radius:6px;margin-bottom:16px;font-size:0.84rem;color:#0d3b3b;">
        <strong>ℹ️ Note:</strong> Courses are created by Super Admin. You can only set the fee amount and discount for your center.
    </div>

    <div style="background:#fff;border-radius:10px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
        <div style="overflow-x:auto;">
            <table style="width:100%;border-collapse:collapse;font-size:0.84rem;">
                <thead><tr style="background:#f8f9fa;border-bottom:2px solid #dee2e6;">
                    <th style="padding:12px;text-align:left;">Course</th>
                    <th style="padding:12px;text-align:left;">Duration</th>
                    <th style="padding:12px;text-align:right;">Default Fee</th>
                    <th style="padding:12px;text-align:right;">Your Fee</th>
                    <th style="padding:12px;text-align:right;">Discount</th>
                    <th style="padding:12px;text-align:center;">Active</th>
                    <th style="padding:12px;text-align:right;">Action</th>
                </tr></thead>
                <tbody>
                <?php foreach ($allCourses as $c):
                    $bf = $branchFees[$c['id']] ?? null;
                    $feeAmount = $bf['fee_amount'] ?? '';
                    $discount = $bf['discount_percentage'] ?? '';
                    $isActive = $bf['is_active'] ?? 0;
                    $bfId = $bf['id'] ?? 0;
                ?>
                    <tr style="border-bottom:1px solid #f0f0f0;">
                        <td style="padding:12px;font-weight:600;color:#0d3b3b;"><?= htmlspecialchars($c['name'], ENT_QUOTES, 'UTF-8') ?></td>
                        <td style="padding:12px;color:#666;font-size:0.8rem;"><?= htmlspecialchars($c['duration'] ?? '—', ENT_QUOTES, 'UTF-8') ?></td>
                        <td style="padding:12px;text-align:right;color:#999;">₹<?= number_format((float)($c['default_fee'] ?? 0), 0) ?></td>
                        <form method="POST" action="<?= $bfId > 0 ? $bp.'/branch/course-fees/'.$bfId : $bp.'/branch/course-fees' ?>" style="display:contents;">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                            <?php if (!$bfId): ?><input type="hidden" name="course_id" value="<?= (int)$c['id'] ?>"><?php endif; ?>
                            <td style="padding:12px;text-align:right;">
                                <input type="number" name="fee_amount" value="<?= htmlspecialchars($feeAmount, ENT_QUOTES, 'UTF-8') ?>" placeholder="0" min="0" step="any" style="width:90px;padding:5px 8px;border:1px solid #ddd;border-radius:4px;font-size:0.8rem;text-align:right;">
                            </td>
                            <td style="padding:12px;text-align:right;">
                                <input type="number" name="discount_percentage" value="<?= htmlspecialchars($discount, ENT_QUOTES, 'UTF-8') ?>" placeholder="0" min="0" step="any" style="width:60px;padding:5px 8px;border:1px solid #ddd;border-radius:4px;font-size:0.8rem;text-align:right;">₹
                            </td>
                            <td style="padding:12px;text-align:center;">
                                <input type="checkbox" name="is_active" value="1" <?= $isActive ? 'checked' : '' ?> style="width:16px;height:16px;accent-color:#16a085;">
                            </td>
                            <td style="padding:12px;text-align:right;">
                                <button type="submit" style="background:#16a085;color:#fff;border:none;padding:5px 12px;border-radius:4px;font-size:0.76rem;cursor:pointer;font-weight:600;"><?= $bfId ? 'Update' : 'Set Fee' ?></button>
                            </td>
                        </form>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
