$(document).ready( function(){

    // Get the id from the URL's query parameters
    var urlParams = new URLSearchParams(window.location.search);
    var lead_id = urlParams.get('id').trim();
    
    getRegistrantById(lead_id);
  
    $('#btn_view_pending_registrants').click( function(){
        window.location.href = 'pending-registrants.php'; 
    });

    $('#btn_create_account').click( function(){

        var entered_student_note = $('#text_student_note').val().trim();
        var student_pass = $('#student_password').val();

        if(lead_id != ''){

            if(entered_student_note != '' && student_pass != ''){
                

                Swal.fire({
                    title: 'ARE YOU SURE?',
                    text: "Do you want to Create this Account ?",
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, proceed it!',
                    customClass: {
                        popup: 'confirm-pop',
                        title: 'confirm-pop-title'
                    }
                }).then((result) => {
                    if (result.isConfirmed) {
                        createStudentAccount(lead_id, entered_student_note, student_pass);
                    }
                });
    
            }else{
                unsuccessPopup('Both student note and password are required!');
            }

        }else{
            unsuccessPopup('Invalid Request. Please Try Again!');
        }

    });
    
});


function createStudentAccount(lead_id, entered_student_note, student_pass){

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

            if(data.status == 1){
                successHTMLContentPopup(data.message, 'pending-registrants.php');
            }else if(data.status == 0){
                unsuccessPopup(data.message);
            }

        },
        error: function() {
            unsuccessPopup('Failed Request!');
        }
    });

}

function getRegistrantById(lead_id){

    if(lead_id != ''){

        $.ajax({
            type: 'GET',
            url: 'classes/StudentController.php', // Replace with the URL of your server-side script
            data: {'VIEW_REGISTRANT':1, 'lead_id':lead_id},
            dataType: 'json', // Assuming the response is in JSON format
            success: function(data) {
    
                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!', 'pending-registrants.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 successHTMLContentPopup(msg, customURL){

    Swal.fire({
        title: 'SUCCESS',
        html: 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;
            }
        }
    });
}
