<?php $bp = defined('BASE_PATH') ? BASE_PATH : '/gmind'; ?>
<div class="admin-page-header">
    <h2>Set Course Fee</h2>
    <a href="<?= $bp ?>/branch/fees" class="admin-btn admin-btn-outline">&larr; Back to Course Fees</a>
</div>

<div class="admin-card">
    <div class="admin-card-header">
        <h3>Course: <?= htmlspecialchars($course['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></h3>
    </div>
    <div class="admin-card-body">
        <form method="POST" action="<?= $bp ?>/branch/fees/save-fee/<?= (int)($course['id'] ?? 0) ?>" class="admin-form" novalidate>
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
            <input type="hidden" name="course_id" value="<?= (int)($course['id'] ?? 0) ?>">

            <div class="admin-form-row">
                <div class="admin-form-group">
                    <label for="fee_amount" class="admin-form-label">Fee Amount (&#8377;) <span class="admin-required">*</span></label>
                    <input type="number" id="fee_amount" name="fee_amount" class="admin-form-input"
                           placeholder="e.g. 5000" required min="1" step="0.01"
                           value="<?= htmlspecialchars($existingFee['fee_amount'] ?? $_POST['fee_amount'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div class="admin-form-group">
                    <label for="discount_percentage" class="admin-form-label">Discount (%) </label>
                    <input type="number" id="discount_percentage" name="discount_percentage" class="admin-form-input"
                           placeholder="e.g. 10" min="0" max="100" step="0.1"
                           value="<?= htmlspecialchars($existingFee['discount_percentage'] ?? $_POST['discount_percentage'] ?? '0', ENT_QUOTES, 'UTF-8') ?>">
                </div>
            </div>

            <div class="admin-form-group">
                <div class="admin-form-checkbox-wrap">
                    <input type="checkbox" id="is_active" name="is_active" value="1"
                           <?= !isset($existingFee) || !empty($existingFee['is_active']) ? 'checked' : '' ?>>
                    <label for="is_active" class="admin-form-checkbox-label">Active (students can enroll with this fee)</label>
                </div>
            </div>

            <?php if (!empty($existingFee)): ?>
            <div class="admin-form-info">
                <p>Current fee: &#8377; <?= htmlspecialchars(number_format((float)($existingFee['fee_amount'] ?? 0), 2), ENT_QUOTES, 'UTF-8') ?>
                   | Discount: <?= htmlspecialchars(number_format((float)($existingFee['discount_percentage'] ?? 0), 1), ENT_QUOTES, 'UTF-8') ?>%
                   | Effective: &#8377; <?= htmlspecialchars(number_format((float)($existingFee['fee_amount'] ?? 0) - ((float)($existingFee['fee_amount'] ?? 0) * (float)($existingFee['discount_percentage'] ?? 0) / 100), 2), ENT_QUOTES, 'UTF-8') ?>
                </p>
            </div>
            <?php endif; ?>

            <div class="admin-form-actions">
                <button type="submit" class="admin-btn admin-btn-primary">Save Fee</button>
                <a href="<?= $bp ?>/branch/fees" class="admin-btn admin-btn-outline">Cancel</a>
            </div>
        </form>
    </div>
</div>
