$(document).ready( function(){

    // Get the id from the URL's query parameters
    var urlParams = new URLSearchParams(window.location.search);
    const student_id = urlParams.get('id').trim();

    // getStudentDetailsById(student_id);

    $('#edu_education_info_table').DataTable({});
    $('#pe_work_info_table').DataTable({});
    
});


function getStudentDetailsById(student_id){

    if(student_id != ''){

        $.ajax({
            type: 'GET',
            url: 'classes/StudentController.php', // Replace with the URL of your server-side script
            data: {'VIEW_STUDENT':1, 'student_id':student_id},
            dataType: 'json', // Assuming the response is in JSON format
            success: function(data) {

                var student_array = data; 
                // var student_info = data.message.student_info.status
                console.log(student_array);

                if(student_array.status == 1){

                    if(student_array.message.student_info.status == 1){
                    // student(main) / campus / branch => campus and branch one record must be available because we have used INNER JOIN
                    // document / ol / al / personal_info => these tables rocords can be null because student_id not available in these tables and have used LEFT JOIN to student table
                    
                        var student_info = student_array.message.student_info.message[0];
                        console.log(student_info);

                        var student_status = '';
                        if(student_info.student_status == 1){
                            student_status = '<span class="student-registered">Registered</span>';
                        }else if(student_info.student_status == 2){
                            student_status = '<span class="student-enrolled">Enrolled</span>';
                        }else if(student_info.student_status == 3){
                            student_status = '<span class="student-blocked">Blocked</span>';
                        }

                        
                        $('#student_id').html(student_info.student_id);
                        $('#student_status').html(student_status);
                        $('#first_name').html(student_info.student_first_name);
                        $('#last_name').html(student_info.student_last_name);
                        $('#student_gender').html(student_info.student_gender);
                        $('#nic_passport_id').html(student_info.student_nic_or_passport_id);
                        $('#pref_lang').html(student_info.student_pref_lang);
                        $('#campus_name').html(student_info.campus_name);
                        $('#branch_name').html(student_info.branch_name);
                        $('#phone_1').html(student_info.student_phone_1);
                        $('#phone_2').html(student_info.student_phone_2);
                        $('#email_address').html(student_info.student_email);
                        $('#textarea_address').html(student_info.student_address);
                        $('#textarea_student_note').html(student_info.student_note);
                    

                        var document_campus_path = '';
                        if(student_info.document_campus_path == ''){
                            document_campus_path = '<span class="document-not-upload">Not Uploaded</span>'
                        }else if(student_info.document_campus_path != '' && student_info.document_campus_verify == 0){
                            document_campus_path = '<span class="document-pending-verification">Pending Verification</span>'
                        }else  if(student_info.document_campus_path != '' && student_info.document_campus_verify == 1){
                            document_campus_path = '<a href="../student/' + student_info.document_campus_path + '" download><i class="fa fa-file document-file-icon"></i></a>'
                        }

                        $('#high_education_document').html(document_campus_path);

                        
                        // Advance level - start
                        console.log(student_info.a_level_method);
                        if(student_info.a_level_method == null){
                            console.log('nullll');
                        }

                        if(student_info.a_level_method == ""){
                            console.log('empty');
                        }
                        
                        // Advance level - end



                    }else{
                        unsuccessPopup(student_array.message.student_info.message, 'all-students.php');
                    }


                }else if(student_array.status == 0){
                    unsuccessPopup(data.message, 'all-students.php');
                }

                return false;
    
                


                if(data.status == '1'){
    
                    var resp = data.message[0];
                    var lead_notes_arr = JSON.parse(data.message[0].all_notes);
                    var note_arr_count = lead_notes_arr.length;
                    var lead_status = '';
    
                    if (resp.lead_status == 0) { // pending approval from center function
                        lead_status = '<span class="lead-pending">Pending</span>';
                    }else if(resp.lead_status == 1){ // assigned to counselor
                        lead_status = '<span class="lead-assigned">Assigned</span>';
                    }else if(resp.lead_status == 2){ // rejected by center function
                        lead_status = '<span class="lead-rejected">Rejected</span>';
                    }else if(resp.lead_status == 3){ // not prefer to join.
                        lead_status = '<span class="lead-prospective">Not Prospective</span>';
                    }else if(resp.lead_status == 4){ // convertible
                        lead_status = '<span class="lead-convertible">Convertible</span>';
                    }else if(resp.lead_status == 5){ // future intakes
                        lead_status = '<span class="lead-future-intake">Future Intake</span>';
                    }else if(resp.lead_status == 6){ // completed | pending list for register to system as a student
                        lead_status = '<span class="lead-completed">Completed</span>';
                    }else if(resp.lead_status == 7){ // registered | register with the system as a student
                        lead_status = '<span class="lead-registered">Registered</span>';
                    }
    
                    $('#lead_id').html(resp.lead_id);
                    $('#lead_status').html(lead_status);
                    $('#lead_first_name').html(resp.lead_first_name);
                    $('#lead_last_name').html(resp.lead_last_name);
                    $('#lead_pref_lang').html(resp.lead_pref_lang);
                    $('#lead_source').html(resp.lead_source);
                    $('#laad_phone_1').html(resp.lead_phone_1);
                    $('#laad_phone_2').html(resp.lead_phone_2);
                    $('#lead_email').html(resp.lead_email);
                    $('#laad_campus').html(resp.campus_name);
                    $('#laad_branch').html(resp.branch_name);
                    $('#lead_address').html(resp.lead_address);
                    $('#laad_created_date_time').html(resp.lead_created_date_time);
                    $('#laad_updated_date_time').html(resp.lead_updated_date_time);
    
                    if(note_arr_count > 0){
                        //show lead notes in table
                        lead_notes_arr.forEach((element, index) => {
                            
                            var note_row = 
                            '<tr>' +
                                '<td>' + ++index + '</td>' +
                                '<td><textarea class="lead_note_textarea" rows="3" readonly>'+ element.note_description +'</textarea></td>' +
                                '<td>'+ element.note_created_date_time +'</td>' +
                            '</tr>';
    
                            $('#lead_notes_body').append(note_row);
                        });
                        
                    }else {
                        var note_row = 
                            '<tr><td colspan="3" class="text-center">Notes not available!</td></tr>';
    
                        $('#lead_notes_body').append(note_row);
                    }
    
                }else {
                    unsuccessPopup(data.message, 'pending-registrants.php');
                }
    
            },
            error: function() {
                unsuccessPopup('Failed Request!');
            }
        });

    }else{
        unsuccessPopup('Invalid Request!', 'all-students.php');
    }

}

function successPopup(msg, customURL){
    Swal.fire({
        title: 'SUCCESS',
        text: msg,
        allowOutsideClick:false,
        allowEscapeKey:false,
        customClass: {
            popup: 'suc-pop',
            title: 'suc-pop-title',
            content: 'suc-pop-content',
            confirmButton: 'suc-pop-btn-ok'
        }
    }).then(okay => {
        if (okay) {
            if(customURL){
                window.location.href = customURL;
            }else{
                location.reload();
            }
        }
    });
}

function unsuccessPopup(msg, customURL){
    Swal.fire({
        title: 'UNSUCCESS',
        text: msg,
        allowOutsideClick:false,
        allowEscapeKey:false,
        customClass: {
            popup: 'unsuc-pop',
            title: 'unsuc-pop-title',
            confirmButton: 'unsuc-pop-btn-ok'
        }
    }).then(okay => {
        if (okay) {
            if(customURL){
                window.location.href = customURL;
            }
        }
    });
}