$(function() {
    let l = abp.localization.getResource("AbpProject")

    let editModal = new abp.ModalManager(abp.appPath + "Products/EditProductModal");
    
    let dataTable = $("#ProductsTable").DataTable(
        abp.libs.datatables.normalizeConfiguration({
            serverSide: true,
            paging: true,
            order: [[0, "asc"]],
            searching: false,
            scrollX: true,
            ajax: abp.libs.datatables.createAjax(
                skcks.study.abpProject.products.product.getList
            ),
            columnDefs: [
                {
                    title: l('Name'),
                    data: "name",
                },
                {
                    title: l('CategoryName'),
                    data: "categoryName",
                },
                {
                    title: l('Price'),
                    data: "price",
                },
                {
                    title: l('StockState'),
                    data: "stockState",
                    render: (data)=>{
                        return l("Enum:StockState:" + data);
                    }
                },
                {
                    title: l('CreationTime'),
                    data: "creationTime",
                    dataFormat: "date",
                },
                {
                    title: l('Actions'),
                    rowAction: {
                        items: [
                            {
                                text: l('Edit'),
                                action: (data)=>{
                                    editModal.open({
                                        id: data.record.id
                                    })
                                }
                            },
                            {
                                text: l('Delete'),
                                confirmMessage: (data)=>{
                                    return l("ProductDeletionConfirmationMessage")
                                },
                                action: (data)=>{
                                    skcks.study.abpProject.products.product.delete(data.record.id).then(()=>{
                                        abp.notify.info(l("SuccessfullyDeleted"));
                                        dataTable.ajax.reload()
                                    })
                                }
                            }
                        ]
                    }
                }
            ]
        })
    )

    editModal.onResult(()=>{
        dataTable.ajax.reload();
    })
    
    let createModal = new abp.ModalManager(abp.appPath + "Products/CreateProductModal");
    createModal.onResult(()=>{
        dataTable.ajax.reload();
    })
    
    $("#NewProductButton").click((e)=>{
        e.preventDefault();
        createModal.open();
    })
})
