import * as api from "../api/api.js"

export default {
    namespaced: true,
    state: {
        dialog_confirmation: false,
        dialog_delete: false,
        dialog_detail_view: false,
        snackbar: {
            color: 'success',
            msg: '',
            isOpen: false
        },
        filter: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            search: ''
        },
        curr_page: 1,
        list_data_ro: {
            records: [],
            total: 1
        },
        list_supplier: [],
        list_warehouse: [],
        selected_ro: {},
        list_staff: [],
        list_lokasi: [],
        dialog_handover: false,
        selected_staff: {},
        dialog_document: false,
        show_progrees_upload: false,
        imageFiles: [],
        imagePreviews: [],
        cekFile: false,
        act_dialog_doc: "",
        dialog_barcode: false,
        barcodes: [],
        urlQrCode: '',
        indeterminatex: false,
        bar_chx_all: false,
        selected_barcode: [],
        list_itembelumserah: []
    },
    mutations: {
        update_dialog_confirmation(state, data) {
            state.dialog_confirmation = data
        },
        update_dialog_delete(state, data) {
            state.dialog_delete = data
        },
        update_dialog_detail_view(state, data) {
            state.dialog_detail_view = data
        },
        update_snackbar(state, data) {
            state.snackbar = data
        },
        update_filter(state, data) {
            state.filter = data
        },
        update_curr_page(state, data) {
            state.curr_page = data
        },
        update_list_data_ro(state, data) {
            state.list_data_ro = data
        },
        update_list_supplier(state, val) {
            state.list_supplier = val
        },
        update_list_warehouse(state, val) {
            state.list_warehouse = val
        },
        update_selected_ro(state, val) {
            state.selected_ro = val
        },
        update_list_staff(state, val) {
            state.list_staff = val
        },
        update_list_lokasi(state, val) {
            state.list_lokasi = val
        },
        update_dialog_handover(state, val) {
            state.dialog_handover = val
        },
        update_selected_staff(state, val) {
            state.selected_staff = val
        },
        update_dialog_document(state, val) {
            state.dialog_document = val
        },
        update_show_progrees_upload(state, val) {
            state.show_progrees_upload = val
        },
        update_imageFiles(state, val) {
            state.imageFiles = val
        },
        update_imagePreviews(state, val) {
            state.imagePreviews = val
        },
        update_cekFile(state, val) {
            state.cekFile = val
        },
        update_act_dialog_doc(state, val) {
            state.act_dialog_doc = val
        },
        update_dialog_barcode(state, val) {
            state.dialog_barcode = val
        },
        update_barcodes(state, val) {
            state.barcodes = val
        },
        update_urlQrCode(state, val) {
            state.urlQrCode = val
        },
        update_indeterminatex(state, val) {
            state.indeterminatex = val
        },
        update_bar_chx_all(state, val) {
            state.bar_chx_all = val
        },
        update_selected_barcode(state, val) {
            state.selected_barcode = val
        },
        update_list_itembelumserah(state, val) {
            state.list_itembelumserah = val
        }
    },
    actions: {
        async search(context) {
            try {
                let param = {
                    token: one_token(),
                    page: context.state.curr_page,
                    ...context.state.filter
                }

                console.log("search OR");
                let resp = await api.search(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_list_data_ro", resp.data);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async getSupplier(context) {
            try {
                let param = {
                    token: one_token(),
                    search: ''
                }
                const resp = await api.getSupplier(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_list_supplier", resp.data);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async getWarehouse(context) {
            try {
                let param = {
                    token: one_token()
                }
                const resp = await api.getWarehouse(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_list_warehouse", resp.data);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async getRuangan(context) {
            try {
                let param = {
                    token: one_token(),
                    search: ''
                }
                const resp = await api.getRuangan(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_list_lokasi", resp.data);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async getItemUpdate(context) {
            try {
                let param = {
                    token: one_token(),
                    ROID: context.state.selected_ro.ReceiveOrderPoID
                }

                const resp = await api.getItemUpdate(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    let temp_ship = []
                    resp.data.forEach(element => {
                        let ship = {
                            num_po: element.PurchaseOrderNumber,
                            cost: element.PurchaseOrderShippingCost,
                            isUpfront: element.PurchaseOrderShippingCostStatus
                        }

                        const isExist = temp_ship.some(
                            item => item.num_po == element.PurchaseOrderNumber
                        );
                        if (!isExist) {
                            temp_ship.push(ship);
                        }
                    });

                    let form = {
                        receive_date: context.state.selected_ro.ReceiveOrderPoIDate,
                        number_do: context.state.selected_ro.ReceiveOrderPoDONumber,
                        referensi: context.state.selected_ro.ReceiveOrderPoRefNumber,
                        supplier: {
                            SupplierID: context.state.selected_ro.SupplierID,
                            SupplierName: context.state.selected_ro.SupplierName
                        },
                        warehouse: {
                            WarehouseID: context.state.selected_ro.WarehouseID,
                            WarehouseName: context.state.selected_ro.WarehouseName,
                            WarehouseCode: context.state.selected_ro.WarehouseCode
                        },
                        lokasi: {
                            M_RuanganID: context.state.selected_ro.M_RuanganID,
                            M_RuanganName: context.state.selected_ro.M_RuanganName,
                            M_RuanganCode: context.state.selected_ro.M_RuanganCode
                        },
                        shipping_cost: context.state.selected_ro.ReceiveOrderShippingCostAmount,
                        note: context.state.selected_ro.ReceiveOrderPoNote
                    }

                    context.commit("form/update_form_receive", form, { root: true });
                    context.commit("form/update_shipping_info", temp_ship, { root: true });
                    context.commit("form/update_form_receive_item", resp.data, { root: true });
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async deleteRO(context) {
            try {
                let param = {
                    token: one_token(),
                    ROID: context.state.selected_ro.ReceiveOrderPoID
                }
                const resp = await api.deleteRO(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_selected_ro", {});
                    context.dispatch("search", {});

                    context.commit("update_snackbar", {
                        color: 'success',
                        msg: resp.data,
                        isOpen: true
                    });
                    context.commit("update_dialog_delete", false);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async getListStaff(context, isHandover) {
            try {
                let param = {
                    token: one_token(),
                    ROID: context.state.selected_ro.ReceiveOrderPoID,
                    isSerahTerima: isHandover ?? "N"
                }
                let resp = await api.getListStaff(param)
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_list_staff", resp.data)
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async getItemSerahTerima(context) {
            try {
                let param = {
                    token: one_token(),
                    ROID: context.state.selected_ro.ReceiveOrderPoID
                }
                let resp = await api.getInventarisBelumSerah(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_list_itembelumserah", resp.data);
                }
            } catch(error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async saveHandover(context, param) {
            try {
                param.token = one_token()
                const resp = await api.saveHandover(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_selected_ro", {});
                    context.commit("update_selected_staff", {});
                    context.dispatch("search", {});

                    context.commit("update_snackbar", {
                        color: 'success',
                        msg: resp.data,
                        isOpen: true
                    });
                    context.commit("update_dialog_handover", false);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async saveFiles(context, param) {
            try {
                const resp = await api.saveFiles(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_selected_ro", {});
                    context.commit("update_imageFiles", []);
                    context.commit("update_imagePreviews", []);
                    context.commit("update_cekFile", false);
                    context.dispatch("search", {});

                    context.commit("update_snackbar", {
                        color: 'success',
                        msg: resp.data,
                        isOpen: true
                    });
                    context.commit("update_dialog_document", false);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
        async getBarcodes(context) {
            try {
                let param = {
                    token: one_token(),
                    ROID: context.state.selected_ro.ReceiveOrderPoID
                }
                const resp = await api.getBarcodes(param);
                if (resp.status != 'OK') {
                    context.commit("update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    });
                } else {
                    context.commit("update_barcodes", resp.data);
                }
            } catch (error) {
                context.commit("update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                });
            }
        },
    }
}
