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

export default {
    namespaced: true,
    state: {
        user: one_user(),
        f_startdate: new Date().toISOString().substr(0, 10),
        f_enddate: new Date().toISOString().substr(0, 10),
        f_cabang: { M_BranchCode: "ALL", M_BranchName: "SEMUA" },
        f_status: "All",
        f_search: "",

        l_suratjalan: {},
        l_cabang: [],
        l_status: ["All", "Draft", "Process", "Completed"],
        paginat: 1,

        l_tfrequest: [],
        l_ekspedisi: [],
        l_staff: [],

        s_suratjalan: {
            SuratJalanID: 0,
            i_tftype: "",
            i_tfrequest: {},
            i_isInternal: "",
            i_ekspedisi: {},
            i_staff: {},
            i_phone: "",
            i_notes: "",
            i_status: "BARU",
            i_date: new Date().toISOString().substr(0, 10),
            detail: []
        },
        d_page: 1,

        snackbar: {
            isOpen: false,
            color: "error",
            message: ""
        }
    },
    mutations: {
        update_f_startdate(state, data) {
            state.f_startdate = data
        },
        update_f_enddate(state, data) {
            state.f_enddate = data
        },
        update_f_cabang(state, data) {
            state.f_cabang = data
        },
        update_f_status(state, data) {
            state.f_status = data
        },
        update_f_search(state, data) {
            state.f_search = data
        },
        update_l_suratjalan(state, data) {
            state.l_suratjalan = data
        },
        update_l_cabang(state, data) {
            state.l_cabang = data
        },
        update_l_tfrequest(state, data) {
            state.l_tfrequest = data
        },
        update_l_ekspedisi(state, data) {
            state.l_ekspedisi = data
        },
        update_l_staff(state, data) {
            state.l_staff = data
        },
        update_paginat(state, data) {
            state.paginat = data
        },
        update_s_suratjalan(state, data) {
            state.s_suratjalan = data
        },
        update_d_page(state, data) {
            state.d_page = data
        },
        update_snackbar(state, data) {
            state.snackbar = data
        }
    },
    actions: {
        /* QUERY */
        async getBranches(context) {
            try {
                let params = {
                    token: one_token(),
                    S_RegionalID: context.state.user.S_RegionalID
                }
                let resp = await api.ListingBranch(params)
                if (resp.status != "OK") {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    let data = resp.data.records
                    let all = {
                        M_BranchCode: "ALL",
                        M_BranchName: "SEMUA"
                    }
                    data.unshift(all)

                    context.commit("update_l_cabang", data)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async listingSuratJalan(context) {
            try {
                let param = {
                    token: one_token(),
                    startdate: context.state.f_startdate,
                    enddate: context.state.f_enddate,
                    branchcode: context.state.f_cabang.M_BranchCode,
                    status: context.state.f_status,
                    currpage: context.state.paginat
                }
                let resp = await api.ListingSuratJalan(param)
                if (resp.status != 'OK') {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    let suratjalan = {
                        records: resp.data.records,
                        total: parseInt(resp.data.total)
                    }
                    context.commit("update_l_suratjalan", suratjalan);
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async listingEkspedisi(context) {
            try {
                let param = {
                    expdName: "",
                    isInternal: context.state.s_suratjalan.i_isInternal == 'Internal' ? "Y" : "N",
                    token: one_token()
                }
                let resp = await api.lookupEkpedisi(param)
                if (resp.status != "OK") {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    context.commit("update_l_ekspedisi", resp.data.records)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async listingEkspedisiStaff(context) {
            try {
                let param = {
                    expID: context.state.s_suratjalan.i_ekspedisi.ExpeditionID,
                    staffName: "",
                    token: one_token()
                }
                let resp = await api.lookupEkpedisiStaff(param)
                if (resp.status != "OK") {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    context.commit("update_l_staff", resp.data.records)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async listingTransferRequest(context) {
            try {
                let param = {
                    type: context.state.s_suratjalan.i_tftype,
                    TFnum: "",
                    token: one_token()
                }
                const resp = await api.lookupTransferRequest(param)
                if (resp.status != 'OK') {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    context.commit("update_l_tfrequest", resp.data.records)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async listingTFdetail(context) {
            try {
                let param = {
                    TGoodID: context.state.s_suratjalan.i_tfrequest.T_GoodsTransferID,
                    token: one_token()
                }
                const resp = await api.lookupTransferRequestDetail(param)
                if (resp.status != 'OK') {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    let data = context.state.s_suratjalan
                    data.detail = resp.data.records
                    context.commit("update_s_suratjalan", data)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async mapDetailSuratJalan(context, param) {
            try {
                param.token = one_token()

                const resp = await api.GetTFDataDetail(param)
                if (resp.status != 'OK') {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    const data = resp.data.records
                    let suratjalan = context.state.s_suratjalan
                    
                    suratjalan.i_isInternal = "Internal"
                    if (data.ExpeditionIsInternal != 'Y') {
                        suratjalan.i_isInternal = "Eksternal"
                    }
                    
                    let ekspedisi = {
                        ExpeditionID: data.ExpeditionID,
                        ExpeditionIsInternal: data.ExpeditionIsInternal,
                        ExpeditionName: data.ExpeditionName
                    }
                    suratjalan.i_ekspedisi = ekspedisi

                    let staff = {
                        ExpeditionStaffID: data.ExpeditionStaffID,
                        ExpeditionStaffMobile: data.ExpeditionStaffMobile,
                        ExpeditionStaffName: data.ExpeditionStaffName
                    }
                    suratjalan.i_staff = staff

                    let transfereq = {
                        T_GoodsTransferID: data.T_GoodsTransferID,
                        T_GoodsTransferNum: data.T_GoodsTransferNum,
                        T_GoodsTransferType: data.T_GoodsTransferType,
                        T_GoodsTransferDate: data.T_GoodsTransferDate
                    }
                    suratjalan.i_tfrequest = transfereq

                    suratjalan.i_tftype = data.T_GoodsTransferType == 'P' ? 'Persediaan' : 'Non Persediaan'
                    suratjalan.i_phone = data.ExpeditionStaffMobile
                    suratjalan.i_notes = data.SuratJalanNote
                    suratjalan.i_date = data.SuratJalanDate
                    suratjalan.i_status = data.SuratJalanStatus
                    suratjalan.detail = data.detail
                    suratjalan.SuratJalanID = data.SuratJalanID
                    suratjalan.SuratJalanNumber = data.SuratJalanNumber

                    await context.commit("update_s_suratjalan", suratjalan)
                    await context.dispatch("listingEkspedisi")
                    await context.dispatch("listingEkspedisiStaff")
                    await context.dispatch("listingTransferRequest")
                    let tfreq = context.state.l_tfrequest
                    tfreq.push(transfereq)
                    context.commit("update_l_tfrequest", tfreq)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async newForm(context) {
            try {
                let param = {
                    SuratJalanID: 0,
                    i_tftype: "",
                    i_tfrequest: {},
                    i_isInternal: "",
                    i_ekspedisi: {},
                    i_staff: {},
                    i_phone: "",
                    i_notes: "",
                    i_status: "BARU",
                    i_date: new Date().toISOString().substr(0, 10),
                    detail: []
                }

                context.commit("update_s_suratjalan", param)
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, Reset Form"}
                context.commit("update_snackbar", x)
            }
        },

        /* MUTATIONS */
        async createSuratJalan(context) {
            try {
                const suratjalan = context.state.s_suratjalan
                let sjdetail = []
                suratjalan.detail.forEach(element => {
                    let sjd = {
                        PRFlagID: element.PRFlagID,
                        TGDetailID: element.TGDetailID,
                        ItemID: element.M_ItemID,
                        Qty: element.Quantity,
                        ItemUnitID: element.ItemUnitID
                    }
                    sjdetail.push(sjd)
                });

                let param = {
                    SJdate: suratjalan.i_date,
                    SJnote: suratjalan.i_notes,
                    TFtype: suratjalan.i_tftype,
                    TGoodID: suratjalan.i_tfrequest.T_GoodsTransferID,
                    TGoodNum: suratjalan.i_tfrequest.T_GoodsTransferNum,
                    EkspedisiID: suratjalan.i_ekspedisi.ExpeditionID,
                    EkspedisiStaffID: suratjalan.i_staff.ExpeditionStaffID,
                    EkspedisiStaffHP: suratjalan.i_phone,
                    SJstatus: suratjalan.i_status == 'BARU' ? 'Draft' : suratjalan.i_status,
                    detail: sjdetail,
                    token: one_token()
                }
                
                const resp = await api.createSuratJalan(param)
                if (resp.status != 'OK') {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    await context.dispatch("newForm")
                    await context.dispatch("listingSuratJalan")

                    let x = { isOpen: true, color: "success", message: "Success" }
                    context.commit("update_snackbar", x)
                }

            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async updateSuratJalan(context) {
            try {
                const suratjalan = context.state.s_suratjalan
                let sjdetail = []

                suratjalan.detail.forEach(element => {
                    let sjd = {
                        PRFlagID: element.PRFlagID,
                        TGDetailID: element.TGDetailID,
                        ItemID: element.M_ItemID,
                        Qty: element.Quantity,
                        ItemUnitID: element.ItemUnitID
                    }
                    sjdetail.push(sjd)
                });

                let param = {
                    SJID: suratjalan.SuratJalanID,
                    SJdate: suratjalan.i_date,
                    SJnote: suratjalan.i_notes,
                    TFtype: suratjalan.i_tftype,
                    TGoodID: suratjalan.i_tfrequest.T_GoodsTransferID,
                    TGoodNum: suratjalan.i_tfrequest.T_GoodsTransferNum,
                    EkspedisiID: suratjalan.i_ekspedisi.ExpeditionID,
                    EkspedisiStaffID: suratjalan.i_staff.ExpeditionStaffID,
                    EkspedisiStaffHP: suratjalan.i_phone,
                    SJstatus: suratjalan.i_status == 'BARU' ? 'Draft' : suratjalan.i_status,
                    detail: sjdetail,
                    token: one_token()
                }

                const resp = await api.updateSuratJalan(param)
                if (resp.status != "OK") {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    await context.dispatch("newForm")
                    await context.dispatch("listingSuratJalan")

                    let x = { isOpen: true, color: "success", message: "Success" }
                    context.commit("update_snackbar", x)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        async deleteSuratJalan(context, param) {
            try {
                param.token = one_token()
                let resp = await api.deleteSuratJalan(param)
                if (resp.status != "OK") {
                    let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
                    context.commit("update_snackbar", x)
                } else {
                    await context.dispatch("newForm")
                    await context.dispatch("listingSuratJalan")

                    let x = { isOpen: true, color: "success", message: "Success" }
                    context.commit("update_snackbar", x)
                }
            } catch (error) {
                let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
                context.commit("update_snackbar", x)
            }
        },
        // async generateJurnal(context, param) {
        //     try {
        //         let listsj = context.state.l_suratjalan.records
        //         let selec = listsj.filter( sj => sj.SuratJalanNumber === param.SuratJalanNumber)[0]

        //         let per = {
        //             date: param.SuratJalanDate,
        //             token: one_token()
        //         }

        //         let resp = await api.CheckPeriode(per)
        //         if (resp.status != "OK") {
        //             let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
        //             context.commit("update_snackbar", x)
        //         } else {
        //             let paar = {
        //                 M_BranchCode: selec.M_BranchCode,
        //                 SuratJalanDate: param.SuratJalanDate,
        //                 SuratJalanID: param.SuratJalanID,
        //                 token: one_token()
        //             }
        //             let generate = await api.GenerateJurnal(paar)
        //             if (generate.status != "OK") {
        //                 let x = { isOpen: true, color: "error", message: "Gagal, " + generate.message }
        //                 context.commit("update_snackbar", x)
        //             } else {
        //                 context.dispatch("konfirmasiSuratJalan", {
        //                     SJalanID: param.SuratJalanID,
        //                     TGoodID: selec.SuratJalanT_GoodsTransferID,
        //                     detail: param.detail
        //                 })
        //             }
        //         }
        //     } catch (error) {
        //         let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
        //         context.commit("update_snackbar", x)
        //     }
        // },
        // async konfirmasiSuratJalan(context, param) {
        //     try {
        //         param.token = one_token()
        //         let resp = await api.konfirmSuratJalan(param)
        //         if (resp.status != "OK") {
        //             let x = { isOpen: true, color: "error", message: "Gagal, " + resp.message }
        //             context.commit("update_snackbar", x)
        //         } else {
        //             await context.dispatch("listingSuratJalan")

        //             let x = { isOpen: true, color: "success", message: "Success" }
        //             context.commit("update_snackbar", x)
        //         }
        //     } catch (error) {
        //         let x = { isOpen: true, color: "error", message: "Gagal, " + error.message }
        //         context.commit("update_snackbar", x)
        //     }
        // },
    }
}