<?php

/**

 * Students Medical & Additional Information Form

 */

?>

<div id="medical-container">

    <!-- This will be populated by JavaScript based on the current row -->

</div>



<div class="row-counter mt-3">

    <span><i class="fas fa-notes-medical me-2"></i>Medical & Additional Information</span>

</div>



<div class="row">

    <div class="col-md-12" id="medical-fields-container">

        <!-- Fields will be loaded here -->

    </div>

</div>



<script>

function loadMedicalFields(rowIndex) {

    const container = document.getElementById('medical-fields-container');

    

    let html = `

        <div class="row">

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

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

                <input type="text" class="form-control" name="students[${rowIndex}][religion]" value="${getFieldValue(rowIndex, 'religion')}" placeholder="e.g., Christian">

            </div>

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

                <label class="form-label">Languages Spoken</label>

                <input type="text" class="form-control" name="students[${rowIndex}][languages_spoken]" value="${getFieldValue(rowIndex, 'languages_spoken')}" placeholder="e.g., English, Twi">

            </div>

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

                <label class="form-label">Has Handicap/Allergy?</label>

                <select class="form-select" name="students[${rowIndex}][has_handicap]" onchange="toggleHandicapDesc(${rowIndex})">

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

                    <option value="Yes" ${getFieldValue(rowIndex, 'has_handicap') == 'Yes' ? 'selected' : ''}>Yes</option>

                    <option value="No" ${getFieldValue(rowIndex, 'has_handicap') == 'No' ? 'selected' : ''}>No</option>

                </select>

            </div>

        </div>

        

        <div class="row" id="handicap-desc-${rowIndex}" style="display: ${getFieldValue(rowIndex, 'has_handicap') == 'Yes' ? 'block' : 'none'};">

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

                <label class="form-label">Handicap/Allergy Description</label>

                <textarea class="form-control" name="students[${rowIndex}][handicap_description]" rows="2">${getFieldValue(rowIndex, 'handicap_description')}</textarea>

            </div>

        </div>

        

        <h6 class="bg-light p-2 mb-3 mt-3">Previous School Information</h6>

        <div class="row">

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

                <label class="form-label">Last School Attended</label>

                <input type="text" class="form-control" name="students[${rowIndex}][last_school]" value="${getFieldValue(rowIndex, 'last_school')}">

            </div>

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

                <label class="form-label">Last Class</label>

                <input type="text" class="form-control" name="students[${rowIndex}][last_class]" value="${getFieldValue(rowIndex, 'last_class')}">

            </div>

        </div>

    `;

    

    container.innerHTML = html;

}



function toggleHandicapDesc(rowIndex) {

    const select = document.querySelector(`select[name="students[${rowIndex}][has_handicap]"]`);

    const descDiv = document.getElementById(`handicap-desc-${rowIndex}`);

    if (descDiv) {

        descDiv.style.display = select.value === 'Yes' ? 'block' : 'none';

    }

}



// When switching to medical tab, load fields for all rows

document.addEventListener('DOMContentLoaded', function() {

    const medicalTab = document.getElementById('student-medical-tab');

    if (medicalTab) {

        medicalTab.addEventListener('shown.bs.tab', function() {

            const rows = document.querySelectorAll('.data-row');

            rows.forEach((row, index) => {

                loadMedicalFields(index);

            });

        });

    }

});

</script>