$(document).ready( function(){

    // var obj_log_session = JSON.parse(log_session);
    // var logged_counselor_id = obj_log_session['user_id'];

    viewAllLeadsByCounselor();

    $(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-lead.php?id=' + data_lead_id;
        
    });
});


function viewAllLeadsByCounselor(){

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

            console.log(data);

            $('#all_leads_table').DataTable().destroy();
            
            if(data.status == 1){
                
                var resp = data.message;
                
                $('#all_leads_table').DataTable({
                    data: resp,
                    columns: [
                        { 'data':'id'}, // 0
                        { 'data':'lead_id'}, // 1
                        { 'data':'lead_status'}, // 2
                        { 'data':'lead_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':'latest_note_description'}, // 10 - hidden column
                        { 'data':'actions'}, // 11

                    ],
                    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 == 0) { // pending approval from center function
                                    btn_status = '<span class="lead-pending">Pending</span>';
                                }else if(data == 1){ // assigned to counselor
                                    btn_status = '<span class="lead-assigned">Assigned</span>';
                                }else if(data == 2){ // rejected by center function
                                    btn_status = '<span class="lead-rejected">Rejected</span>';
                                }else if(data == 3){ // not prefer to join.
                                    btn_status = '<span class="lead-prospective">Not Prospective</span>';
                                }else if(data == 4){ // convertible
                                    btn_status = '<span class="lead-convertible">Convertible</span>';
                                }else if(data == 5){ // future intakes
                                    btn_status = '<span class="lead-future-intake">Future Intake</span>';
                                }else if(data == 6){ // completed | pending list for register to system as a student
                                    btn_status = '<span class="lead-completed">Completed</span>';
                                }else if(data == 7){ // registered | register with the system as a student
                                    btn_status = '<span class="lead-registered">Registered</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 != null){
                                
                                if (data.length > 10) {
                                    return '<span data-fulltooltip="'+ data +'" class="truncate" title="' + data + '">' + data.substring(0, 10) + '...</span>';
                                } else {
                                    return data;
                                }

                            }else{
                                return data;
                            }
                            }   
                        },
                        {
                            targets: 8, // Index of the 'campus_name' column
                            className: 'text-center',
                        },
                        {
                            targets: 10, // Index of the 'latest_note_description' column
                            visible: false,
                            render: function(data, type, row, meta) {
                                return data;
                            
                            }
                        },
                        {
                            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 {
                $('#all_leads_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;
            }
        }
    });
}
