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

export default {
    namespaced: true,
    state: {
        user: one_user(),
        form_transfer_date: new Date().toISOString().substr(0, 10),
        form_cabang: {},
        form_divisi: {},
        form_catatan: "",
        form_detail: [],
        
        dialog_detailform: false,
        list_cabangform: [],
        list_divisiform: [],
        list_detailform: {
            total: 0,
            records: []
        },
        list_detailform_page: 1,
        type_act: 'BARU',
        is_verif: 'Draft',

        buffer_detailitem: {},
        dialog_batchitem: false,
        list_barnote: {},
        form_barnote: [],
        edit_qty: 'Eqty',
        act_verif: ''
    },
    mutations: {
        update_form_transfer_date(state, data) {
            state.form_transfer_date = data
        },
        update_form_cabang(state, data) {
            state.form_cabang = data
        },
        update_form_divisi(state, data) {
            state.form_divisi = data
        },
        update_form_catatan(state, data) {
            state.form_catatan = data
        },
        update_form_detail(state, data) {
            state.form_detail = data
        },
        update_dialog_detailform(state, data) {
            state.dialog_detailform = data
        },
        update_list_cabangform(state, data) {
            state.list_cabangform = data
        },
        update_list_divisiform(state, data) {
            state.list_divisiform = data
        },
        update_list_detailform(state, data) {
            state.list_detailform = data
        },
        update_list_detailform_page(state, data) {
            state.list_detailform_page = data
        },
        update_type_act(state, data) {
            state.type_act = data
        },       
        update_is_verif(state, data) {
            state.is_verif = data
        },
        update_buffer_detailitem(state, data) {
            state.buffer_detailitem = data
        },
        update_list_barnote(state, data) {
            state.list_barnote = data
        },
        update_dialog_batchitem(state, data) {
            state.dialog_batchitem = data
        },
        update_form_barnote(state, data) {
            state.form_barnote = data
        },
        update_edit_qty(state, data) {
            state.edit_qty = data
        },
        update_act_verif(state, data) {
            state.act_verif = data
        }
    },
    actions: {
        async newForm(context) {
            try {
                let params = {
                    token: one_token(),
                    S_RegionalID: context.state.user.S_RegionalID
                }
                let resp = await api.getBranches(params)
                if (resp.status != "OK") {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                } else {
                    context.commit("update_type_act", "BARU")
                    context.commit("update_list_cabangform", resp.data.records)
                    context.dispatch("getDivisi", {})

                    context.commit("update_form_transfer_date", new Date().toISOString().substr(0, 10))
                    context.commit("update_form_cabang", { })
                    context.commit("update_form_divisi", { })
                    context.commit("update_form_catatan", "")
                    context.commit("update_list_detailform", {total: 0, records: []})
                    context.commit("update_is_verif", 'Draft')
                    context.commit("update_form_detail", [ ])
                    context.commit("np_data/update_selected_tf", { }, { root: true })
                }


            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        },
        async getDivisi(context) {
            try {
                let params = {token: one_token()}

                let resp = await api.getDivision(params);
                if (resp.status != 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })                    
                } else {
                    context.commit("update_list_divisiform", resp.data)
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        },
        async getListItemDetail(context) {
            try {
                let params = {
                    token: one_token(),
                    currpage: context.state.list_detailform_page,
                    regionalid: context.state.user.S_RegionalID,
                    branchcodedestin: context.state.form_cabang.M_BranchCode,
                    branchIDorigin: context.state.user.M_BranchID,
                    divisionID: context.state.form_divisi.DivisionID
                }

                let resp = await api.getListDetail(params)
                if (resp.status != 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                } else {
                    const current_detail = context.state.form_detail
                    if (current_detail.length == 0) {
                        context.commit("update_list_detailform", resp.data)
                    } else {
                        const inserted = resp.data.records.filter(item => !current_detail.some(obj => obj.PurchaseRequestFlagID == item.PurchaseRequestFlagID))
                        let data = {
                            records: inserted,
                            total: parseInt(resp.data.total)
                        }
                        context.commit("update_list_detailform", data)
                    }
                }

            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        },
        async createTFReq(context) {
            try {
                let params = {
                    token: one_token(),
                    S_RegionalID: context.state.user.S_RegionalID,
                    M_BranchCode: context.state.form_cabang.M_BranchCode,
                    DivisionID: context.state.form_divisi.DivisionID,
                    GoodsTfNotes: context.state.form_catatan,
                    GoodsTfDate: context.state.form_transfer_date,
                    GoodsTfStatus: context.state.is_verif,
                    detail: context.state.form_detail
                }

                let resp = await api.createTfRequest(params);
                if (resp.status != 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                } else {
                    // generate surat jalan atau nggak?

                    context.dispatch("newForm")
                    context.dispatch("np_data/cariTF", { }, { root: true })
                    let snac = { isOpen: true, color: "success", msg: "Success" }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        },
        async updateTFReq(context) {
            try {
                let params = {
                    token: one_token(),
                    S_RegionalID: context.state.user.S_RegionalID,
                    M_BranchCode: context.state.form_cabang.M_BranchCode,
                    DivisionID: context.state.form_divisi.DivisionID,
                    GoodsTfID: context.rootState.np_data.selected_tf.T_GoodsTransferID,
                    GoodsTfNum: context.rootState.np_data.selected_tf.T_GoodsTransferNum,
                    GoodsTfNotes: context.state.form_catatan,
                    GoodsTfDate: context.state.form_transfer_date,
                    GoodsTfStatus: context.state.is_verif,
                    editqty: context.state.edit_qty,
                    actverif: context.state.act_verif,
                    detail: context.state.form_detail
                }

                let resp = await api.updateTfRequest(params);
                if (resp.status != 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })                    
                } else {
                    // generate surat jalan atau nggak?

                    context.dispatch("newForm")
                    context.dispatch("np_data/cariTF", { }, { root: true })
                    let snac = { isOpen: true, color: "success", msg: "Success" }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        },
        async deleteTFReq(context, params) {
            try {
                params.token = one_token()
                let resp = await api.deleteTfRequest(params);
                if (resp.status != 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                } else {
                    context.dispatch("newForm")
                    context.dispatch("p_data/cariTF", { }, { root: true })

                    let snac = { isOpen: true, color: "success", msg: resp.data}
                    context.commit("p_data/update_snackbar", snac, { root: true })
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        },
        async displayDetail(context) {
            try {
                let detail = context.rootState.np_data.selected_tf
                context.commit("update_form_transfer_date", detail.T_GoodsTransferDate)
                context.commit("update_form_catatan", detail.T_GoodsTransferNotes)
                context.commit("update_is_verif", detail.T_GoodsTransferStatus)
                
                context.commit("update_form_cabang", {
                    M_BranchID: detail.M_BranchID,
                    M_BranchCode: detail.M_BranchCode,
                    M_BranchName: detail.M_BranchName
                })

                context.commit("update_form_divisi", {
                    DivisionID: detail.T_GoodsTransferDivisionID,
                    DivisionCode: detail.DivisionCode,
                    DivisionName: detail.DivisionName
                })

                let params = {
                    token: one_token(),
                    goodTfID: detail.T_GoodsTransferID
                }
                const resp = await api.getDraftDetail(params)
                if (resp.status !== 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                } else {
                    const draft = resp.data.records
                    context.commit("update_form_detail", draft);
                }
                
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        },
        async getListStock(context, params) {
            try {
                params.token = one_token()
                params.regionalid = context.state.user.S_RegionalID

                const resp = await api.getListStock(params)
                if (resp.status !== 'OK') {
                    let snac = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("p_data/update_snackbar", snac, { root: true })
                } else {
                    context.commit("update_list_barnote", resp.data)
                }
            } catch (error) {
                let snac = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("p_data/update_snackbar", snac, { root: true })
            }
        }
    }
}