$(".alert-dismissible").fadeOut(0);
var table;
$(document).ready(function () {
    table = $('#datatable-fixedheader').DataTable({
        retrieve: true,
        processing: true,
        serverSide: true,
        lengthChange: true,
        ordering: true,
        autoWidth: false,
        pagingType: "full_numbers",
        iDisplayLength: 10,
        lengthMenu: [
            [10, 25, 50, -1],
            [10, 25, 50, "All"]
        ],
        order: [],
        ajax: {
            url: BASEURL + 'apply-job/ajax_list',
            type: "POST"
        },
        columnDefs: [{
            targets: [0, 6, 7],
            orderable: false
        }]
    });
});

function reload_table() 
{
    table.ajax.reload(null, false);  
}

function view_data(id) {
    $.ajax({
        url: BASEURL + 'apply-job/view/' + id,
        type: "GET",
        dataType: "JSON",
        success: function (data) {
            $('#v_apply_for').text(data.apply_for || '-');
            $('#v_name').text(data.name || '-');
            $('#v_mobile_no').text(data.mobile_no || '-');
            $('#v_email').text(data.email || '-');
            $('#v_qualification').text(data.qualification || '-');
            $('#v_current_location').text(data.current_location || '-');
            $('#v_total_experience').text(data.total_experience || '-');
            $('#v_current_organization').text(data.current_organization || '-');
            $('#v_current_designation').text(data.current_designation || '-');
            $('#v_current_ctc').text(data.current_ctc || '-');
            $('#v_message').text(data.message || '-');
            
            if(data.resume && data.resume !== '') {
                $('#v_resume').html('<a href="' + BASEURL + '../assets/uploads/resume/' + data.resume + '" target="_blank" class="btn btn-sm btn-primary" download><i class="fa fa-download"></i> Download Resume</a>');
            } else {
                $('#v_resume').html('<span class="text-muted">Not uploaded</span>');
            }
            
            $('#viewModal').modal('show');
        },
        error: function(jqXHR, textStatus, errorThrown) {
            // Using Management style alert
            $(".alert-success").hide();
            $(".alert-danger").show();
            $(".alert-danger-msg").html('Unable to get data from database.');
            setTimeout(function () {
                $(".alert-danger").fadeOut(500);
            }, 5000);
        }
    });
}

function mark_complete(id) {
    var t = Swal.mixin({
        customClass: {
            confirmButton: "btn btn-success mx-1",
            denyButton: "btn btn-secondary mx-1",
            cancelButton: "btn btn-danger mx-1"
        },
        buttonsStyling: !1
    });
    
    t.fire({
        title: "Mark as Complete?",
        text: "This application will be marked as complete.",
        icon: "question",
        showCancelButton: !0,
        confirmButtonColor: "#34c38f",
        cancelButtonColor: "#f03b3b",
        confirmButtonText: "Yes, mark it!",
        cancelButtonText: "No, cancel!"
    }).then(function (result) {
        if (result.value) {
            $.ajax({
                url: BASEURL + 'apply-job/category_active/' + id,
                type: "POST",
                data: "id=" + id,
                dataType: "JSON",
                success: function (data) {
                    reload_table();
                    // Using Management style alert
                    $(".alert-danger").hide();
                    $(".alert-success").show();
                    $(".alert-success-msg").html('Application has been marked as complete.');
                    setTimeout(function () {
                        $(".alert-success").fadeOut(500);
                    }, 10000);
                },
                error: function() {
                    // Using Management style alert
                    $(".alert-success").hide();
                    $(".alert-danger").show();
                    $(".alert-danger-msg").html('Status update failed');
                    setTimeout(function () {
                        $(".alert-danger").fadeOut(500);
                    }, 5000);
                }
            });
        } else {
            result.dismiss === Swal.DismissReason.cancel && Swal.fire({
                title: "Cancelled",
                text: "Application status remains unchanged.",
                icon: "error"
            });
        }
    });
}

function update_status(id, status) {
    const statusText = status == 2 ? 'confirm' : 'reject';
    const statusTitle = status == 2 ? 'Confirm Application?' : 'Reject Application?';
    const statusMessage = status == 2 ? 'This application will be marked as confirmed.' : 'This application will be marked as rejected.';
    
    var t = Swal.mixin({
        customClass: {
            confirmButton: "btn btn-success mx-1",
            denyButton: "btn btn-secondary mx-1",
            cancelButton: "btn btn-danger mx-1"
        },
        buttonsStyling: !1
    });
    
    t.fire({
        title: statusTitle,
        text: statusMessage,
        icon: "question",
        showCancelButton: !0,
        confirmButtonColor: status == 2 ? "#34c38f" : "#f03b3b",
        cancelButtonColor: "#6c757d",
        confirmButtonText: 'Yes, ' + statusText + ' it!',
        cancelButtonText: "Cancel"
    }).then(function (result) {
        if (result.value) {
            $.ajax({
                url: BASEURL + 'apply-job/update_status',
                type: "POST",
                data: {
                    id: id,
                    status: status
                },
                dataType: "JSON",
                success: function (data) {
                    if(data.status == 'ok') {
                        reload_table();
                        // Using Management style alert
                        $(".alert-danger").hide();
                        $(".alert-success").show();
                        $(".alert-success-msg").html('Application status updated successfully.');
                        setTimeout(function () {
                            $(".alert-success").fadeOut(500);
                        }, 10000);
                    } else {
                        // Using Management style alert
                        $(".alert-success").hide();
                        $(".alert-danger").show();
                        $(".alert-danger-msg").html('Status update failed');
                        setTimeout(function () {
                            $(".alert-danger").fadeOut(500);
                        }, 5000);
                    }
                },
                error: function() {
                    // Using Management style alert
                    $(".alert-success").hide();
                    $(".alert-danger").show();
                    $(".alert-danger-msg").html('Status update failed');
                    setTimeout(function () {
                        $(".alert-danger").fadeOut(500);
                    }, 5000);
                }
            });
        } else {
            result.dismiss === Swal.DismissReason.cancel && Swal.fire({
                title: "Cancelled",
                text: "Status remains unchanged.",
                icon: "error"
            });
        }
    });
}

$(document).on("click", ".delete_btn", function (event) {
    var id = $(this).attr('data-id');  
    var t = Swal.mixin({
            customClass: {
                confirmButton: "btn btn-success mx-1",
                denyButton: "btn btn-secondary mx-1",
                cancelButton: "btn btn-danger mx-1"
            },
            buttonsStyling: !1
        }),
        e = Swal.mixin({
            toast: !0,
            position: "top-end",
            showConfirmButton: !1,
            timer: 3e3,
            timerProgressBar: !0,
            didOpen: function(t) {
                t.addEventListener("mouseenter", Swal.stopTimer), t.addEventListener("mouseleave", Swal.resumeTimer)
            }
        });
    
    t.fire({
        title: "Are you sure?",
        text: "You won't be able to revert this!",
        icon: "warning",
        showCancelButton: !0,
        confirmButtonColor: "#34c38f",
        cancelButtonColor: "#f03b3b",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "No, cancel!"
    }).then(
        function (t) {
            if (t.value) {
                var $url = BASEURL + 'apply-job/ajax_delete/' + id;
                $.ajax({
                    type: 'POST',
                    url: $url,
                    async: false,
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    beforeSend: function () {
                        $('.dataTables_processing').css('visibility', 'visible');
                    },
                    success: function (returnData) {
                        if (returnData.status == 'ok') {
                            reload_table();
                            
                            $(".alert-danger").hide();
                            $(".alert-success").show();
                            $(".alert-success-msg").html('Record has been deleted successfully.');
                            setTimeout(function () {
                                $(".alert-success").fadeOut(500);
                            }, 10000);
                        } else if (returnData.status == 'not') {
                            var tbc_name = returnData.tbc_name;
                            reload_table();
                             
                            $(".alert-success").hide();
                            $(".alert-danger").show();
                            $(".alert-danger-msg").html("Record can't delete, Because they are used in " + tbc_name + ".");
                            setTimeout(function () {
                                $(".alert-danger").fadeOut(500);
                            }, 10000);
                        }
                    },
                    complete: function () {
                        $('.dataTables_processing').css('visibility', 'hidden');
                    },
                    error: function() {
                        $('.dataTables_processing').css('visibility', 'hidden');
                        
                        $(".alert-success").hide();
                        $(".alert-danger").show();
                        $(".alert-danger-msg").html('Error deleting record');
                        setTimeout(function () {
                            $(".alert-danger").fadeOut(500);
                        }, 5000);
                    }
                });
            } else {
                t.dismiss === Swal.DismissReason.cancel && Swal.fire({
                    title: "Cancelled",
                    text: "Congrates! Your data is safe.",
                    icon: "error"
                })
            }
        }
    )
});