<script>
    // crop and upload student image
    $('body').on('click', '#abba_get_student_image', function () {
    
        var abba_get_student_id = $(this).data('id');
    
        var abba_get_campus_id = $(this).data('campusid');
    
        var abba_get_student_img_class = $(this).data('studimgclass');
    
        $('#abba_store_student_id_for_image').val(abba_get_student_id);
    
        $('#abba_store_campus_id_for_image').val(abba_get_campus_id);
    
        $('#abba_get_student_image_class_input').val(abba_get_student_img_class);
    
    });
    
    $(document).ready(function () {
    
        $abba_student_image_crop = $('#abba_student_demo_image').croppie({
            enableExif: false,
            viewport: {
                width: 300,
                height: 300,
                type: 'square' //circle
            },
            boundary: {
                width: 350,
                height: 350
            }
        });
    
        $('body').on('change', '.abba_update_student_image', function () {
            var reader = new FileReader();
            reader.onload = function (event) {
                $abba_student_image_crop.croppie('bind', {
                    url: event.target.result
                }).then(function () {
                    console.log('jQuery bind complete');
                });
            };
            reader.readAsDataURL(this.files[0]);
    
            $('#abba_update_student_image_modal').modal('show');
    
    
        });
    
        $('body').on('click', '.abba_crop_student_image', function () {
    
            $abba_student_image_crop.croppie('result', {
                
                type: 'canvas',
                size: 'viewport'
                
            }).then(function (response) {
                var abba_get_student_id = "<?php echo $studentID;?>";
    
                var abba_get_campus_id = "<?php echo $camp;?>";
    
                $('.abba_crop_student_image').html('<i class="fa fa-spinner fa-pulse"></i> uploading..');
    
                $('#abba_update_student_image_modal').modal('hide');
    
                // alert(abba_get_student_id +' '+abba_get_campus_id +' '+response);
    
                $.ajax({
                    url: '../../controller/scripts/admin/abba_update_student_image.php',
                    type: 'POST',
                    data: { "image": response, "abba_get_student_id": abba_get_student_id, "abba_get_campus_id": abba_get_campus_id },
                    success: function (data) {
    
                        // alert(data);
    
                        $('.abba_crop_student_image').html('Submit');
    
                        if (data == 1) {
                            $.wnoty({
                                type: 'success',
                                message: 'Image has been updated successfully',
                                autohideDelay: 5000, // 5 seconds
                                position: 'top-right',
                                autohide: true
                            });
                            
                            location.reload(true);
                        }
                        else {
                            $.wnoty({
                                type: 'warning',
                                message: 'Unfortunately your image was not updated, please try again',
                                autohideDelay: 5000, // 5 seconds
                                position: 'top-right',
                                autohide: true
                            });
                        }
    
                    }
                });
            });
        });
    });
    
    
    
    
    // phone number country code and flag
    var studentphone = window.intlTelInput(document.querySelector("#abba_get_student_phone"), {
        separateDialCode: true,
        preferredCountries: ["ng"],
        hiddenInput: "full",
        utilsScript: "//cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/js/utils.js"
    });

    
    // create student
    $('body').on('click', '#abba_proceed_to_create_student', function () {
    
        $('#abba_proceed_to_create_student').html('<i class="fas fa-circle-notch fa-spin"></i> Updating..');
    
        var abba_campus_id = "<?php echo $camp;?>";
        
        var abba_student_id = "<?php echo $studentID;?>";
        
        var abba_get_student_first_name = $('#abba_get_student_first_name').val();
        var abba_get_student_middle_name = $('#abba_get_student_middle_name').val();
        var abba_get_student_last_name = $('#abba_get_student_last_name').val();
    
        // alert(abba_campus_id);
        var studentphonefull = studentphone.getNumber(intlTelInputUtils.numberFormat.E164);
        $("input[name='studentphone[full]'").val(studentphonefull);
    
        var abba_get_student_email = $('#abba_get_student_email').val();
        var abba_get_student_disability = $('#abba_get_student_disability').val();
        var abba_get_student_alergy = $('#abba_get_student_alergy').val();
        var abba_get_student_gender = $('#abba_get_student_gender option:selected').val();
        var abba_get_student_dob = $('#abba_get_student_dob').val();
        var abba_get_student_blood_group = $('#abba_get_student_blood_group option:selected').val();
        var abba_get_student_religion = $('#abba_get_student_religion option:selected').val();
        var abba_display_student_country = $('#abba_display_student_country option:selected').val();
        var abba_display_student_state = $('#abba_display_student_state option:selected').val();
        var abba_display_student_lga = $('#abba_display_student_lga option:selected').val();
        var abba_get_student_city = $('#abba_get_student_city').val();
        var abba_get_student_address = $('#abba_get_student_address').val();
        var abba_display_create_student_class = $('#abba_display_create_student_class option:selected').val();
        var abba_get_student_session = $('#abba_get_student_session option:selected').val();
        var abba_get_student_doa = $('#abba_get_student_doa').val();
        var abba_get_student_reg_number = $('#abba_get_student_reg_number').val();
        var abba_get_student_password = $('#abba_get_student_password').val();
        var abba_get_student_type = $('#abba_get_student_type option:selected').val();
        var abba_get_student_parent = $('.abba_get_student_parent').val();
    
        if (abba_campus_id == '0' || abba_campus_id == 'NULL') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select a campus to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_first_name == '') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to enter the student's first name to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_last_name == '') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to enter the student's last name to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_gender == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select the student's gender to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_religion == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select the student's religion to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_display_student_country == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select the student's country to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_display_student_state == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select the student's state to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_display_student_lga == 'NULL' || abba_display_student_lga == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select the student's lga to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_display_create_student_class == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select the student's class to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_session == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select a session to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_doa == '') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to enter the student's DOA(Date of Admission) to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_reg_number == '') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to enter the student's registration number to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_password == '') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to enter the student's password to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else if (abba_get_student_type == '0') {
            $.wnoty({
                type: 'error',
                message: "Hey! you have to select the student type to proceed.",
                autohideDelay: 5000
            });
    
            $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
        }
        else {
            $.ajax({
                type: "POST",
                url: "../../controller/scripts/student/abba_finally_update_student.php",
                data: {
                    abba_campus_id: abba_campus_id,
                    abba_student_id: abba_student_id,
                    abba_get_student_first_name: abba_get_student_first_name,
                    abba_get_student_middle_name: abba_get_student_middle_name,
                    abba_get_student_last_name: abba_get_student_last_name,
                    studentphonefull: studentphonefull,
                    abba_get_student_email: abba_get_student_email,
                    abba_get_student_disability: abba_get_student_disability,
                    abba_get_student_alergy: abba_get_student_alergy,
                    abba_get_student_gender: abba_get_student_gender,
                    abba_get_student_dob: abba_get_student_dob,
                    abba_get_student_blood_group: abba_get_student_blood_group,
                    abba_get_student_religion: abba_get_student_religion,
                    abba_display_student_country: abba_display_student_country,
                    abba_display_student_state: abba_display_student_state,
                    abba_display_student_lga: abba_display_student_lga,
                    abba_get_student_city: abba_get_student_city,
                    abba_get_student_address: abba_get_student_address,
                    abba_display_create_student_class: abba_display_create_student_class,
                    abba_get_student_session: abba_get_student_session,
                    abba_get_student_doa: abba_get_student_doa,
                    abba_get_student_reg_number: abba_get_student_reg_number,
                    abba_get_student_password: abba_get_student_password,
                    abba_get_student_parent: abba_get_student_parent,
                    abba_get_student_type: abba_get_student_type
                },
    
                success: function (result) {
    
                    $('#abba_proceed_to_create_student').html('<i class="fa fa-plus"> Update</i>');
    
                    // alert(result);
    
                    if (result == 1 || result == 'done') {
                        $.wnoty({
                            type: 'success',
                            message: "Hurray! the student has been updated successfully.",
                            autohideDelay: 5000
                        });
                        
                        location.reload(true);
                    }
                    else if (result == 2) {
                        $.wnoty({
                            type: 'warning',
                            message: "Oops! the registration number entered already exist, please change the number and try again.",
                            autohideDelay: 5000
                        });
                    }
                    else {
                        $.wnoty({
                            type: 'error',
                            message: "Oops! the student was not updated successfully.",
                            autohideDelay: 5000
                        });
                    }
    
                }
            });
        }
    
    });
    
    // get student state
    $('body').on("change", "#abba_display_student_country", function () {
    
        abba_get_student_state();
    });
    
    $(document).ready(function () {
    
        abba_get_student_state();
        
        var abba_instituion_id = "<?php echo $InstitutionID; ?>";

        var abba_campus_id = "<?php echo $camp;?>";
    
        var abba_class_id = "NULL";
    
        var dataString = 'abba_instituion_id=' + abba_instituion_id + '&abba_campus_id=' + abba_campus_id + '&abba_class_id=' + abba_class_id;
    
        $.ajax({
            type: "POST",
            url: "../../controller/scripts/admin/abba_get_campus_parent_list.php",
            data: dataString,
            //cache: false,
            success: function (output) {
                // alert(output);
                $('.abba_get_student_parent').html(output);
    
    
                var select_box_element = document.querySelector('#select_box');
    
                dselect(select_box_element, {
                    search: true
                });
    
                $('.abba_get_student_parent').val('<?php echo $ParentID; ?>');
            }
        });
        
        abba_get_create_student_class();
    });
    
    function abba_get_student_state() {
    
        var abba_get_student_country_id = $('#abba_display_student_country option:selected').val();
    
        var dataString = '&abba_get_student_country_id=' + abba_get_student_country_id;
    
        $('#abba_display_student_state').html('<option value="NULL">Loading...</option>');
    
        $.ajax({
            type: "POST",
            url: "../../controller/scripts/admin/abba_get_student_state_script.php",
            data: dataString,
    
            success: function (result) {
    
                $('#abba_display_student_state').html(result);
                
                
                $('#abba_display_student_state').val('<?php echo $StudentState; ?>');
                
                abba_get_student_lga();
            }
        });
    }
    
    // get student lga
    $('body').on("change", "#abba_display_student_state", function () {
    
        abba_get_student_lga();
    });
    
    $(document).ready(function () {
    
        abba_get_student_lga();
    });
    
    function abba_get_student_lga() {
    
        var abba_get_student_country_id = $('#abba_display_student_country option:selected').val();
    
        var abba_get_student_state_id = $('#abba_display_student_state option:selected').val();
    
        var dataString = '&abba_get_student_country_id=' + abba_get_student_country_id + '&abba_get_student_state_id=' + abba_get_student_state_id;
    
        $('#abba_display_student_lga').html('<option value="NULL">Loading...</option>');
    
        $.ajax({
            type: "POST",
            url: "../../controller/scripts/admin/abba_get_student_lga_script.php",
            data: dataString,
    
            success: function (result) {
    
                $('#abba_display_student_lga').html(result);
                
                $('#abba_display_student_lga').val('<?php echo $StudentLga; ?>');
            }
        });
    }
    
    
    
    // function to get create student class
    function abba_get_create_student_class() {
    
        var abba_campus_id = "<?php echo $camp;?>";
    
        var dataString = 'abba_campus_id=' + abba_campus_id;
    
        // alert(dataString);
    
        // $("#abba_display_create_student_class").animate({ scrollTop: 0 }, "slow");
    
        $.ajax({
            type: "POST",
            url: "../../controller/scripts/admin/abba-get_create_student_class_script.php",
            data: dataString,
            //cache: false,
            success: function (output) {
                // alert(output);
                $('#abba_display_create_student_class').html(output);
                
                $('#abba_display_create_student_class').val('<?php echo $class_ID; ?>');
                
                
                $('#abba_get_student_gender').val('<?php echo $StudentGender; ?>');
                $('#abba_get_student_blood_group').val('<?php echo $StudentBloodGroup; ?>');
                $('#abba_get_student_religion').val('<?php echo $StudentReligion; ?>');
                $('#abba_display_student_country').val('<?php echo $StudentCountry; ?>');
                $('#abba_get_student_type').val('<?php echo $StudentTypeBoardingOrDay; ?>');
            }
        });
    
    }
    
</script>