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

export default {
    namespaced: true,
    state: {
        dialog_form_receive_item: false,
        dialog_form_receive_item_edit: false,
        dialog_select_item: false,
        dialog_batch_item: false,
        form_receive: {
            receive_date: moment(new Date()).format('YYYY-MM-DD'),
            number_do: '',
            referensi: '',
            supplier: {},
            warehouse: {},
            lokasi: {},
            shipping_cost: 0,
            note: ''
        },
        form_receive_item: [],
        select_item_temp: [],
        form_receive_item_del: [],
        selected_po: {},
        list_po: [],
        list_item_po: [],
        shipping_info: [],
        selected_item_forbatch: {},
        item_batch_temp: [],

        dialog_form_inspeksi: false,
        selected_item_received: {},
        form_inspeksi: {
            qty_po: 0,
            qty_ro: 0,
            price_po: 0.00,
            price_ro: 0.00,
            date_po: moment(new Date()).format('YYYY-MM-DD'),
            date_ro: moment(new Date()).format('YYYY-MM-DD'),
            condt_kemasan: '',
            catatan_kemasan: '',
            condt_pengiriman: '',
            catatan_kirim: '',
            summary: '',
            catatan: '',
            pemeriksa: '',
            pengirim: ''
        }
    },
    mutations: {
        update_dialog_form_receive_item(state, data) {
            state.dialog_form_receive_item = data
        },
        update_dialog_form_receive_item_edit(state, data) {
            state.dialog_form_receive_item_edit = data
        },
        update_dialog_select_item(state, data) {
            state.dialog_select_item = data
        },
        update_dialog_batch_item(state, data) {
            state.dialog_batch_item = data
        },
        update_form_receive(state, val) {
            state.form_receive = val
        },
        update_form_receive_item(state, val) {
            state.form_receive_item = val
        },
        update_form_receive_item_del(state, val) {
            state.form_receive_item_del = val
        },
        update_select_item_temp(state, val) {
            state.select_item_temp = val
        },
        update_selected_po(state, val) {
            state.selected_po = val
        },
        update_list_po(state, val) {
            state.list_po = val
        },
        update_list_item_po(state, val) {
            state.list_item_po = val
        },
        update_shipping_info(state, val) {
            state.shipping_info = val
        },
        update_selected_item_forbatch(state, val) {
            state.selected_item_forbatch = val
        },
        update_item_batch_temp(state, val) {
            state.item_batch_temp = val
        },
        update_dialog_form_inspeksi(state, val) {
            state.dialog_form_inspeksi = val
        },
        update_form_inspeksi(state, val) {
            state.form_inspeksi = val
        },
        update_selected_item_received(state, val) {
            state.selected_item_received = val
        }
    },
    actions: {
        resetForm(context) {
            const form_receive = {
                receive_date: moment(new Date()).format('YYYY-MM-DD'),
                number_do: '',
                referensi: '',
                supplier: {},
                warehouse: {},
                lokasi: {},
                shipping_cost: 0,
                note: ''
            }

            context.state.form_receive = form_receive
            context.state.form_receive_item = []
            context.state.shipping_info = []
        },

        async getPurchaseOrder(context) {
            try {
                let param = {
                    token: one_token(),
                    SupplierID: context.state.form_receive.supplier.SupplierID,
                    search: '',
                    localPO_deleted: context.state.form_receive_item_del.map(item => item.PoItemReceiveID)
                }
                const resp = await api.getPurchaseOrder(param);
                if (resp.status != 'OK') {
                    context.commit("ro/update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    }, {root: true});
                } else {
                    context.commit("update_list_po", resp.data);
                }
            } catch (error) {
                context.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                }, {root: true});
            }
        },

        async getItem(context) {
            try {
                let param = {
                    token: one_token(),
                    POID: context.state.selected_po.PurchaseOrderID,
                    WarehouseID: context.state.form_receive.warehouse.WarehouseID 
                }
                const resp = await api.getItem(param);
                if (resp.status != 'OK') {
                    context.commit("ro/update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    }, {root: true});
                } else {
                    context.commit("update_list_item_po", resp.data);
                }
            } catch (error) {
                context.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                }, {root: true});
            }
        },

        async saveRO(context) {
            try {
                const cekShipping = context.state.shipping_info.some(item => item.isUpfront === 'N');
                let param = {
                    token: one_token(),
                    date: context.state.form_receive.receive_date,
                    supplierID: context.state.form_receive.supplier.SupplierID,
                    warehouseID: context.state.form_receive.warehouse.WarehouseID,
                    lokasiID: context.state.form_receive.lokasi.M_RuanganID,
                    shippingCost: context.state.form_receive.shipping_cost,
                    shippingIsPaid: cekShipping ? 'N' : 'Y',
                    note: context.state.form_receive.note,
                    refNumber: context.state.form_receive.referensi,
                    DONumber: context.state.form_receive.number_do,
                    detail: context.state.form_receive_item
                }
                
                const resp = await api.saveRO(param);
                if (resp.status != 'OK') {
                    context.commit("ro/update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    }, {root: true});
                } else {
                    context.dispatch("ro/search", {}, {root: true});
                    context.dispatch("resetForm");
 
                    context.commit("ro/update_snackbar", {
                        color: 'success',
                        msg: resp.data,
                        isOpen: true
                    }, {root: true});
                    context.commit("update_dialog_form_receive_item", false);
                }
            } catch (error) {
                context.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                }, {root: true});
            }
        },

        async saveDataInspeksi(context) {
            try {
                let param = {
                    token: one_token(),
                    ROdetailID: 0,
                    form_inspeksi: context.state.form_inspeksi
                }

                const resp = await api.saveInspeksi(param)
                if (resp.status != 'OK') {
                    context.commit("ro/update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    }, {root: true});
                } else {
                    context.commit("ro/update_snackbar", {
                        color: 'success',
                        msg: resp.data,
                        isOpen: true
                    }, {root: true});
                    context.commit("update_dialog_form_receive_item", false);
                }
            } catch (error) {
                context.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                }, {root: true});
            }
        },

        async getDataItemInspeksi(context) {
            try {
                let param = {
                    token: one_token(),
                    ROdetailID: 0,
                }

                const resp = await api.getItemDataInspeksi(param)
                if (resp.status != 'OK') {
                    context.commit("ro/update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    }, {root: true});
                } else {
                    
                }
            } catch (error) {
                context.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                }, {root: true});
            }
        },

        async confirmRO(context) {
            try {
                let param = {
                    token: one_token(),
                    ROID: context.rootState.ro.selected_ro.ReceiveOrderPoID
                }
                
                const resp = await api.confirmRO(param)
                if (resp.status != 'OK') {
                    context.commit("ro/update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    }, {root: true});
                } else {
                    context.dispatch("ro/search", {}, {root: true});
                    context.dispatch("resetForm");

                    context.commit("ro/update_snackbar", {
                        color: 'success',
                        msg: resp.data,
                        isOpen: true
                    }, {root: true});
                    context.commit("ro/update_dialog_confirmation", false, {root: true});
                }
            } catch (error) {
                context.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                }, {root: true});
            }
        },

        async saveEditRO(context) {
            try {
                const cekShipping = context.state.shipping_info.some(item => item.isUpfront === 'N');
                let param = {
                    token: one_token(),
                    ReceiveOrderID: context.rootState.ro.selected_ro.ReceiveOrderPoID,
                    numGR: context.rootState.ro.selected_ro.ReceiveOrderPoNumber,
                    date: context.state.form_receive.receive_date,
                    supplierID: context.state.form_receive.supplier.SupplierID,
                    warehouseID: context.state.form_receive.warehouse.WarehouseID,
                    lokasiID: context.state.form_receive.lokasi.M_RuanganID,
                    shippingCost: context.state.form_receive.shipping_cost,
                    shippingIsPaid: cekShipping ? 'N' : 'Y',
                    note: context.state.form_receive.note,
                    refNumber: context.state.form_receive.referensi,
                    DONumber: context.state.form_receive.number_do,
                    detail: context.state.form_receive_item
                }

                const resp = await api.saveEditRO(param)
                if (resp.status != 'OK') {
                    context.commit("ro/update_snackbar", {
                        color: 'error',
                        msg: resp.message,
                        isOpen: true
                    }, {root: true});
                } else {
                    context.dispatch("ro/search", {}, {root: true});
                    context.dispatch("resetForm");
 
                    context.commit("ro/update_snackbar", {
                        color: 'success',
                        msg: resp.data,
                        isOpen: true
                    }, {root: true});
                    context.commit("update_dialog_form_receive_item_edit", false);
                }
            } catch (error) {
                context.commit("ro/update_snackbar", {
                    color: 'error',
                    msg: error.message,
                    isOpen: true
                }, {root: true});
            }
        }
    }
}