<?php
/**
 * Branch Admin — Fees View
 *
 * Table listing branch course fees: Course Name, Fee (&#8377;), Actions.
 * Also includes a form to set fees for courses that don't have one yet.
 * All amounts in &#8377; (Indian Rupee).
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>

<!-- Current Fee Schedule -->
<div class="admin-card">
    <div class="admin-card-header">
        <h2 class="admin-card-title">Course Fee Schedule</h2>
    </div>
    <div class="admin-card-body">
        <?php if (empty($fees)): ?>
        <p class="admin-empty-text">No course fees set yet. Use the form below to set fees for your courses.</p>
        <?php else: ?>
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Course Name</th>
                        <th>Duration</th>
                        <th>Fee (&#8377;)</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($fees as $fee): ?>
                    <tr>
                        <td>
                            <strong><?= htmlspecialchars($fee['course_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                        </td>
                        <td><?= htmlspecialchars($fee['course_duration'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></td>
                        <td style="font-weight:600;color:#27ae60;">&#8377;<?= number_format((float) ($fee['fee'] ?? 0), 2) ?></td>
                        <td>
                            <form method="POST" action="<?= $bp ?>/branch/fees/<?= (int) ($fee['id'] ?? 0) ?>/delete" style="display:inline;" class="delete-form">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" onclick="return confirm('Are you sure you want to remove the fee for this course?')">Remove</button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>

<!-- Set Fee Form -->
<?php if (!empty($coursesWithoutFee) && is_array($coursesWithoutFee)): ?>
<div class="admin-card" style="margin-top:20px;">
    <div class="admin-card-header">
        <h2 class="admin-card-title">Set Course Fee</h2>
    </div>
    <div class="admin-card-body">
        <form method="POST" action="<?= $bp ?>/branch/fees/set" class="admin-form" id="feeForm">
            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

            <div class="admin-form-row" style="display:grid;grid-template-columns:2fr 1fr auto;gap:16px;align-items:end;">
                <div class="admin-form-group">
                    <label class="admin-form-label" for="course_id">Course</label>
                    <select name="course_id" id="fee_course_id" class="admin-form-input" required>
                        <option value="">-- Select Course --</option>
                        <?php foreach ($coursesWithoutFee as $course): ?>
                        <option value="<?= (int) ($course['id'] ?? 0) ?>"
                                data-default-fee="<?= (float) ($course['fee'] ?? 0) ?>">
                            <?= htmlspecialchars($course['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                            <?php if (!empty($course['fee'])): ?>
                            (Default: &#8377;<?= number_format((float) $course['fee'], 2) ?>)
                            <?php endif; ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="admin-form-group">
                    <label class="admin-form-label" for="fee">Fee Amount &#8377;</label>
                    <div style="position:relative;">
                        <span style="position:absolute;left:12px;top:50%;transform:translateY(-50%);color:#888;">&#8377;</span>
                        <input type="number" name="fee" id="fee_amount" class="admin-form-input" style="padding-left:28px;"
                               required min="0" step="0.01"
                               placeholder="0.00">
                    </div>
                </div>
                <div class="admin-form-group">
                    <button type="submit" class="admin-btn admin-btn-primary">Set Fee</button>
                </div>
            </div>
        </form>
    </div>
</div>
<?php elseif (empty($coursesWithoutFee) && !empty($fees)): ?>
<div class="admin-card" style="margin-top:20px;">
    <div class="admin-card-body">
        <p style="color:#27ae60;text-align:center;margin:0;">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="20" height="20" style="vertical-align:middle;margin-right:6px;"><path d="M22 11.08V12a10 10 0 11-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
            Fees have been set for all available courses.
        </p>
    </div>
</div>
<?php endif; ?>

<script>
// Auto-fill fee amount with course default when a course is selected
(function() {
    const courseSelect = document.getElementById('fee_course_id');
    const feeInput = document.getElementById('fee_amount');

    if (courseSelect && feeInput) {
        courseSelect.addEventListener('change', function() {
            const selectedOption = this.options[this.selectedIndex];
            const defaultFee = selectedOption.getAttribute('data-default-fee');
            if (defaultFee && parseFloat(defaultFee) > 0) {
                feeInput.value = parseFloat(defaultFee).toFixed(2);
            } else {
                feeInput.value = '';
            }
        });
    }
})();
</script>
