<?php

/**

 * Fees Manual Entry Form

 */

?>

<div class="row-counter">

    <span><i class="fas fa-money-bill-wave me-2"></i>Entering Fee Structures - <span id="row-count">1</span> record(s)</span>

    <button type="button" class="btn btn-sm btn-primary" onclick="addRow()">

        <i class="fas fa-plus me-2"></i>Add Another Fee

    </button>

</div>



<div id="rows-container">

    <!-- Row 1 -->

    <div class="data-row mb-4 p-3 border rounded">

        <div class="d-flex justify-content-between mb-3">

            <h6 class="text-primary">Fee Structure #1</h6>

            <span class="remove-row" onclick="removeRow(this)" title="Remove this fee">

                <i class="fas fa-times-circle"></i>

            </span>

        </div>

        <div class="row">

            <div class="col-md-4 mb-3">

                <label class="form-label">Class Name <span class="text-danger">*</span></label>

                <select class="form-select" name="fees[0][class_name]" required>

                    <option value="">Select Class</option>

                    <option value="Nursery 1">Nursery 1</option>

                    <option value="Nursery 2">Nursery 2</option>

                    <option value="KG 1">KG 1</option>

                    <option value="KG 2">KG 2</option>

                    <option value="Class 1">Class 1</option>

                    <option value="Class 2">Class 2</option>

                    <option value="Class 3">Class 3</option>

                    <option value="Class 4">Class 4</option>

                    <option value="Class 5">Class 5</option>

                    <option value="Class 6">Class 6</option>

                    <option value="JHS 1">JHS 1</option>

                    <option value="JHS 2">JHS 2</option>

                    <option value="JHS 3">JHS 3</option>

                </select>

            </div>

            <div class="col-md-4 mb-3">

                <label class="form-label">Fee Type <span class="text-danger">*</span></label>

                <select class="form-select" name="fees[0][fee_type]" required>

                    <option value="">Select Fee Type</option>

                    <option value="School Fee">School Fee</option>

                    <option value="Feeding Fee">Feeding Fee</option>

                    <option value="Admission Fee">Admission Fee</option>

                    <option value="Printing Fee">Printing Fee</option>

                    <option value="School Attire">School Attire</option>

                    <option value="Others">Others</option>

                </select>

            </div>

            <div class="col-md-4 mb-3">

                <label class="form-label">Amount (₵) <span class="text-danger">*</span></label>

                <input type="number" step="0.01" class="form-control" name="fees[0][amount]" required>

            </div>

        </div>

        <div class="row">

            <div class="col-md-6 mb-3">

                <label class="form-label">Due Date <span class="text-danger">*</span></label>

                <input type="date" class="form-control" name="fees[0][due_date]" required>

            </div>

            <div class="col-md-6 mb-3">

                <label class="form-label">Academic Year <span class="text-danger">*</span></label>

                <input type="text" class="form-control" name="fees[0][academic_year]" placeholder="e.g., 2025-2026" value="<?php echo date('Y') . '-' . (date('Y')+1); ?>" required>

                <small class="text-muted">Format: YYYY-YYYY</small>

            </div>

        </div>

    </div>

</div>



<script>

let rowCount = 1;



function addRow() {

    const container = document.getElementById('rows-container');

    const template = container.children[0].cloneNode(true);

    

    // Update indices

    rowCount++;

    template.querySelectorAll('[name^="fees"]').forEach(input => {

        const name = input.getAttribute('name');

        input.setAttribute('name', name.replace(/\[\d+\]/, `[${rowCount-1}]`));

        if (input.type !== 'select-one') {

            input.value = '';

        } else {

            input.selectedIndex = 0;

        }

    });

    

    // Update header

    template.querySelector('h6').textContent = `Fee Structure #${rowCount}`;

    

    container.appendChild(template);

    document.getElementById('row-count').textContent = rowCount;

}



function removeRow(btn) {

    if (rowCount > 1) {

        btn.closest('.data-row').remove();

        rowCount--;

        document.getElementById('row-count').textContent = rowCount;

        

        // Renumber remaining rows

        document.querySelectorAll('.data-row').forEach((row, index) => {

            row.querySelector('h6').textContent = `Fee Structure #${index + 1}`;

            row.querySelectorAll('[name^="fees"]').forEach(input => {

                const name = input.getAttribute('name');

                input.setAttribute('name', name.replace(/\[\d+\]/, `[${index}]`));

            });

        });

    }

}

</script>