<?php

/**

 * Students Manual Entry Form

 */

?>

<div class="row-counter">

    <span><i class="fas fa-users me-2"></i>Entering Students - <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 Student

    </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">Student #1</h6>

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

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

            </span>

        </div>

        <div class="row">

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

                <label class="form-label">Student ID <span class="text-danger">*</span></label>

                <input type="text" class="form-control" name="students[0][student_id]" placeholder="e.g., STU001" required>

            </div>

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

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

                <input type="text" class="form-control" name="students[0][first_name]" required>

            </div>

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

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

                <input type="text" class="form-control" name="students[0][last_name]" required>

            </div>

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

                <label class="form-label">Gender</label>

                <select class="form-select" name="students[0][sex]">

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

                    <option value="Male">Male</option>

                    <option value="Female">Female</option>

                </select>

            </div>

        </div>

        <div class="row">

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

                <label class="form-label">Date of Birth</label>

                <input type="date" class="form-control" name="students[0][date_of_birth]">

            </div>

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

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

                <select class="form-select" name="students[0][class]" 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-3 mb-3">

                <label class="form-label">Section</label>

                <input type="text" class="form-control" name="students[0][section]" placeholder="e.g., A">

            </div>

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

                <label class="form-label">Parent/Guardian Name</label>

                <input type="text" class="form-control" name="students[0][parent_name]">

            </div>

        </div>

        <div class="row">

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

                <label class="form-label">Phone Number</label>

                <input type="tel" class="form-control" name="students[0][phone]" placeholder="024XXXXXXX">

            </div>

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

                <label class="form-label">Email</label>

                <input type="email" class="form-control" name="students[0][email]">

            </div>

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

                <label class="form-label">Address</label>

                <input type="text" class="form-control" name="students[0][address]">

            </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^="students"]').forEach(input => {

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

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

        input.value = '';

    });

    

    // Update header

    template.querySelector('h6').textContent = `Student #${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 = `Student #${index + 1}`;

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

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

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

            });

        });

    }

}

</script>