﻿var table;
$(document).ready(function () {
    table = $('#table').DataTable({
        responsive: true,
        processing: true,
        serverSide: true,
        searching: true,
        ordering: true,
        paging: true,
        pageLength: 10,
        lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]],
        pagingType: 'full_numbers',
        info: true,
        initComplete: function (settings, json) {
            //Do something after finish
        },
        language: languageTable,
        order: [[2, "asc"]],
        ajax: {
            url: document.URL,
            type: 'post',
            data: function (d) {
                //d.ModuleCode = ""
            }
        },
        columns: [
            {
                orderable: false,
                width: '40px',
                className: 'ctn-center text-bold vertical-middle',
                render: function (obj, type, data, meta) {
                    var o = meta.row + meta.settings._iDisplayStart + 1;
                    return o;
                }
            },
            {
                data: 'Code',
                orderable: true,
                searchable: true,
                className: 'vertical-middle',
                width: '80px'
            },
            {
                data: 'Name',
                orderable: true,
                searchable: true,
                className: 'vertical-middle',
                width: '200px'
            },
            {
                data: 'GenderString',
                orderable: true,
                searchable: true,
                className: 'vertical-middle',
                width: '50px',

            },
            {
                data: 'Phone',
                orderable: true,
                searchable: true,
                className: 'vertical-middle',
                width: '80px'
            },
            {
                data: 'Address',
                orderable: true,
                className: 'vertical-middle',
                searchable: true
            },
            {
                data: 'GroupName',
                orderable: true,
                className: 'vertical-middle',
                searchable: true
            },
            {
                data: 'LocationName',
                orderable: true,
                className: 'vertical-middle',
                searchable: true
            },
            {
                orderable: false,
                width: '40px',
                className: 'ctn-center vertical-middle',
                render: function (obj, type, data, meta) {
                    return '<a href="javascript:;" title="Chi tiết" onclick="infoItem(\'' + data.ID + '\');"><i class="glyphicon glyphicon-info-sign" aria-hidden="true"></i></a> \
                                    <a href="javascript:;" title="Cập nhật" onclick="updateItem(\'' + data.ID + '\');"><i class="glyphicon glyphicon-edit" aria-hidden="true"></i></a> \
                                    <a href="javascript:;" title="Xóa" onclick="deleteItem(\'' + data.ID + '\');"><i class="glyphicon glyphicon-remove" aria-hidden="true"></i></a>';
                }
            }
        ]
    });

});
function infoItem(id) {
    $('#loadingModel').modal('show');
    $.ajax({
        url: '/sales/customer/info',
        type: 'get',
        dataType: 'html',
        async: false,
        data: { id: id },
        success: function (result) {
            console.log(result);
            $('#divContainer').html(result);
            $('#loadingModel').modal('hide');

        },
        error: function (xhr, ajaxOptions, thrownError) {
            if (xhr.status == 401 || xhr.status == 530) {
                window.location.href = 'home/login';
            }
        }

    });
}

function updateItem(id) {
    $('#loadingModel').modal('show');
    $.ajax({
        url: '/sales/customer/edit',
        type: 'get',
        dataType: 'html',
        data: { id: id },
        success: function (result) {
            $('#divContainer').html(result);
            $('#loadingModel').modal('hide');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            if (xhr.status == 401 || xhr.status == 530) {
                window.location.href = '/home/login';
            }
        }
    });
}

$(document).on('click', '#btnAdd', function (e) {
    $('#loadingModel').modal('show');
    $.ajax({
        url: '/sales/customer/add',
        type: 'get',
        dataType: 'html',
        success: function (result) {
            $('#divContainer').html(result);
            $('#loadingModel').modal('hide');
        },
        error: function (xhr, ajaxOptions, thrownError) {
            if (xhr.status == 401 || xhr.status == 530) {
                window.location.href = '/home/login';
                console.log(xhr);
            }
        }
    });
});

function deleteItem(id) {
    $.ajax({
        url: '/sales/customer/checkdelete',
        type: 'get',
        contentType: 'application/json',
        dataType: 'json',
        data: { id: id },
        success: function (result) {
            if (result === true) {
                Lobibox.confirm({
                    iconClass: false,
                    title: 'Xác nhận',
                    msg: 'Bạn có muốn xóa thông tin khách hàng đang chọn?',
                    buttons: {
                        yes: {
                            'class': 'btn btn-success',
                            closeOnClick: true
                        },
                        no: {
                            'class': 'btn btn-warning',
                            closeOnClick: true
                        }
                    },
                    callback: function (lobibox, type) {
                        if (type === 'yes') {
                            $.ajax({
                                url: '/sales/customer/delete',
                                type: 'get',
                                contentType: 'application/json',
                                dataType: 'json',
                                data: { id: id },
                                success: function (result) {
                                    if (result.status === 200) {
                                        notification(result.message, 'success');
                                        table.ajax.reload();
                                    } else {
                                        notification(result.message, 'error');
                                    }
                                },
                                error: function (xhr, ajaxOptions, thrownError) {
                                    if (xhr.status == 401 || xhr.status == 530) {
                                        window.location.href = '/home/login';
                                    }
                                    notification('Lỗi khi thực thi xóa thông tin khách hàng', 'error');
                                }
                            });
                        }
                    }
                });
            } else {
                Lobibox.alert("warning",
                {
                    title: 'Thông báo',
                    msg: "Thông tin khách hàng này đang được sử dụng, không thể xóa!"
                });
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            if (xhr.status == 401 || xhr.status == 530) {
                window.location.href = '/home/login';
            }
            notification('Lỗi khi thực hiện kiểm tra xóa thông tin khách hàng', 'error');
        }
    });
};