$(document).ready( function(){

    $('#all_students_table').DataTable({});

    viewAllStudentsByCounselor();

    $(document).on('click', '.btn_view', function(){

        var data_student_id = $(this).data('student-id');

        // Redirect to a new page with the id as a query parameter
        window.location.href = 'view-student.php?id=' + data_student_id;
        
    });

    $(document).on('click', '.btn_edit', function(){

        var data_student_id = $(this).data('student-id');

        // Redirect to a new page with the id as a query parameter
        window.location.href = 'edit-student.php?id=' + data_student_id;
        
    });

});



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

            $('#all_students_table').DataTable().destroy();
            
            if(data.status == 1){

                var resp = data.message;

                var students_table = $('#all_students_table').DataTable({
                    data: resp,
                    columns: [
                        { 'data':'id'}, // 0
                        { 'data':'student_id'}, // 1
                        { 'data':'student_status'}, // 2
                        { 'data':'student_name'}, // 3
                        { 'data':'student_nic_or_passport_id'}, // 4
                        { 'data':'student_phone_1'}, // 5
                        { 'data':'student_email'}, // 6
                        { 'data':'campus_name'}, // 7
                        { 'data':'student_created_date_time'}, // 8
                        { 'data':'actions'} // 9
                    ],
                    columnDefs: [
                        {
                            targets: 0, // Index of the first column (ID)
                            visible: true, // Hide the column from the user
                            orderable: true, // Disable sorting for this column
                            searchable: false, // Disable searching for this column
                            className: 'text-center',
                            render: function (data, type, row, meta) {
                                // Auto-incremented ID starts from 1
                                return meta.row + 1;
                            }
                        },
                        {
                            targets: 1, // Index of the 'student_id' column
                            className: 'text-center',
                        },
                        {
                            targets: 2, // Index of the 'student_status' column
                            className: 'text-center',
                            render: function(data, type, row) {
                                
                                var btn_status = ''
                                if(data == 1){ // registered
                                    btn_status = '<span class="student-registered">Registered</span>';
                                }else if(data == 2){ // enrolled
                                    btn_status = '<span class="student-enrolled">Enrolled</span>';
                                }else if(data == 3){ // blocked
                                    btn_status = '<span class="student-blocked">Blocked</span>';
                                }
                                
                                return btn_status;
                            }
                        },
                        {
                            targets: 4, // Index of the 'student_nic_or_passport_id' column
                            className: 'text-center',
                        },
                        {
                            targets: 5, // Index of the 'student_phone_1' column
                            className: 'text-center',
                        },
                        {
                            targets: 7, // Index of the 'campus_name' column
                            className: 'text-center',
                        },
                        {
                            targets: 8, // Index of the 'student_created_date_time' column
                            className: 'text-center',
                        },
                        {
                            targets: -1, // Index of the last column
                            orderable: false, // Make the column not sortable
                            searchable: false, // Make the column not searchable
                            className: 'text-center',
                            render: function(data, type, row, meta) {
    
                                // Create buttons for view, edit
                                var btn_view = '<button class="btn btn_view" data-student-id="' + row.student_id + '">VIEW</button>';

                                return btn_view;
                            }
                        }
                    ]
                });

                // Custom search based on selected column
                $('#DataTableSearch').on('keyup', function(){
                    var columnIndex = $('input[name="searchBy"]:checked').val();
                    students_table.column(columnIndex).search(this.value).draw();
                });
                
            }else if(data.status == 0){
                $('#all_students_table').DataTable({});

            }

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

}

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;
            }
        }
    });
}