$(document).ready( function(){

    viewPendingRegistrants();

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

        var data_lead_id = $(this).data('lead-id');

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

});

function viewPendingRegistrants(){

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

            console.log(data);
            $('#pending_registrants_table').DataTable().destroy();
            
            if(data.status == 1){

                var resp = data.message;
                
                $('#pending_registrants_table').DataTable({
                    data: resp,
                    columns: [
                        { 'data':'id'}, // 0
                        { 'data':'lead_id'}, // 1
                        { 'data':'lead_status'}, // 2
                        { 'data':'registrant_name'}, // 3
                        { 'data':'lead_phone_1'}, // 4
                        { 'data':'lead_email'}, // 5
                        { 'data':'lead_source'}, // 6
                        { 'data':'latest_note_description'}, // 7
                        { 'data':'campus_name'}, // 8
                        { 'data':'lead_created_date_time'}, // 9
                        { 'data':'actions'} // 10
                    ],
                    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 'lead_id' column
                            className: 'text-center',
                        },
                        {
                            targets: 2, // Index of the 'lead_status' column
                            className: 'text-center',
                            render: function(data, type, row) {
                                
                                var btn_status = ''
                                if(data == 6){ // completed | pending list for register to system as a student
                                    btn_status = '<span class="lead-pending">Pending</span>';
                                }
                                
                                return btn_status;
                            }
                        },
                        {
                            targets: 6, // Index of the 'lead_source' column
                            className: 'text-center',
                        },
                        {
                            targets: 7, // Index of the 'latest_note_description' column
                            render: function(data, type, row, meta) {
                            // If the text length is more than 3 characters, add a tooltip
                                if (data.length > 10) {
                                    return '<span data-fulltooltip="'+ data +'" class="truncate" title="' + data + '">' + data.substring(0, 10) + '...</span>';
                                } else {
                                    return data;
                                }
                            
                            }
                        },
                        {
                            targets: 8, // Index of the 'campus_name' 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, and delete
                                var btn_view = '<button class="btn btn_view" data-lead-id="' + row.lead_id + '">VIEW</button>';
                                return btn_view;
                            }
                        }
                    ]
                });

            }else {
                $('#pending_registrants_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;
            }
        }
    });
}