$(".alert-dismissible").fadeOut(0);
var table;

$(document).ready(function () {
    table = $('#datatable-fixedheader').DataTable({
        processing: true,
        serverSide: true,
        ordering: true,
        autoWidth: false,
        pageLength: 10,
        order: [],
        ajax: {
            url: BASEURL + 'get_an_estimate/ajax_list',
            type: 'POST'
        },
        columnDefs: [
            { targets: [0, 4, 5, 6, 7, 8, 9, 10], orderable: false }
        ]
    });
});

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

function category_active(id) {
    $.ajax({
        type: "POST",
        url: BASEURL + 'get_an_estimate/category_active/' + id,
        data: "id=" + id,
        dataType: "json",
        success: function (data) {
            if (data == 'sucess') {
                reload_table();
            } else {
                alert('Status Update Failed');
            }
        }
    });
}

function category_deactive(id) {
    $.ajax({
        type: "POST",
        url: BASEURL + 'get_an_estimate/category_deactive/' + id,
        data: "id=" + id,
        dataType: "json",
        success: function (data) {
            if (data == 'sucess') {
                reload_table();
            } else {
                alert('Status Update Failed');
            }
        }
    });
}

$(document).on('click', '.delete_btn', function () {
    var id = $(this).data('id');
    
    if (confirm('Delete this record?')) {
        $.ajax({
            type: 'POST',
            url: BASEURL + 'get_an_estimate/ajax_delete/' + id,
            data: { id: id },
            dataType: 'json',
            beforeSend: function () {
                $('.dataTables_processing').css('visibility', 'visible');
            },
            success: function (returnData) {
                if (returnData.status == 'ok') {
                    alert('Record has been deleted.');
                    reload_table();
                } else {
                    alert('Error! Something went wrong.');
                    reload_table();
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('Error occurred while deleting');
            },
            complete: function () {
                $('.dataTables_processing').css('visibility', 'hidden');
            }
        });
    }
});