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_total_page: 0,
        l_cabang: [],
        l_status: ["All", "Draft", "Process", "Completed"],
        paginat: 1,

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


        s_suratjalan: {
            SuratJalanID: 0,
            SuratJalanNumber: "",
            i_tftype: "",
            i_tfrequest: {},
            i_isInternal: "",
            i_ekspedisi: {},
            i_staff: {},
            i_phone: "",
            i_notes: "",
            i_status: "",
            i_date: new Date().toISOString().substr(0, 10),
            i_receivewarehouse: {},
            i_receivenotes: "",
            i_isConfirmed: "N",
            detail: []
        },
        d_page: 1,
        dialog_receive: false,
        receive_items: [],
        buffer_item: {},

        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_total_page(state, data) {
            state.l_total_page = data
        },
        update_paginat(state, data) {
            state.paginat = data
        },
        update_l_cabang(state, data) {
            state.l_cabang = data
        },
        update_l_ekspedisi(state, data) {
            state.l_ekspedisi = data
        },
        update_l_staff(state, data) {
            state.l_staff = data
        },
        update_l_tfrequest(state, data) {
            state.l_tfrequest = data
        },
        update_l_warehouse(state, data) {
            state.l_warehouse = data
        },
        update_s_suratjalan(state, data) {
            state.s_suratjalan = data
        },
        update_dialog_receive(state, data) {
            state.dialog_receive = data
        },
        update_receive_items(state, data) {
            state.receive_items = data
        },
        update_buffer_item(state, data) {
            state.buffer_item = data
        },
        update_d_page(state, data) {
            state.d_page = data
        },
        update_snackbar(state, data) {
            state.snackbar = data
        }
    },
    actions: {
        async newForm(context) {
            let form = {
                SuratJalanID: 0,
                SuratJalanNumber: "",
                i_tftype: "",
                i_tfrequest: {},
                i_isInternal: "",
                i_ekspedisi: {},
                i_staff: {},
                i_phone: "",
                i_notes: "",
                i_status: "",
                i_date: new Date().toISOString().substr(0, 10),
                i_receivewarehouse: {},
                i_receivenotes: "",
                i_isConfirmed: "N",
                detail: []
            }
            context.commit("update_s_suratjalan", form)
        },
        async LookupBranches(context) {
            try {
                let params = {
                    token: one_token(),
                    S_RegionalID: context.state.user.S_RegionalID
                }

                const resp = await api.listingBranch(params)
                if (resp.status != 'OK') {
                    let sn = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", sn)
                } else {
                    let data = resp.data.records
                    let all = {
                        M_BranchCode: "ALL",
                        M_BranchName: "SEMUA"
                    }

                    if (context.state.user.M_BranchCode == '') {
                        data.unshift(all)
                    } 

                    if (context.state.user.M_BranchCode != '') {
                        context.commit("update_f_cabang", data[0])
                    }
                    
                    await context.commit("update_l_cabang", data)
                    await context.dispatch("LookupSuratJalan")
                }
            } catch (error) {
                let sn = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", sn)
            }
        },
        async LookupEkpedisi(context) {
            try {
                let params = {
                    token: one_token(),
                    expdName: "",
                    isInternal: context.state.s_suratjalan.i_isInternal === 'Internal' ? 'Y' : 'N'
                }
                const resp = await api.lookupEkpedisi(params)
                if (resp.status != 'OK') {
                    let sn = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", sn)
                } else {
                    context.commit("update_l_ekspedisi", resp.data.records)
                }
            } catch (error) {
                let sn = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", sn)
            }
        },
        async LookupEkspedisiStaff(context) {
            try {
                let params = {
                    token: one_token(),
                    staffName: "",
                    expID: context.state.s_suratjalan.i_ekspedisi.ExpeditionID
                }

                const resp = await api.lookupEkspedisiStaff(params)
                if (resp.status != 'OK') {
                    let sn = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", sn)
                } else {
                    context.commit("update_l_staff", resp.data.records)
                }
            } catch (error) {
                let sn = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", sn)
            }
        },
        async LookupWarehouse(context) {
            try {
                let params = {
                    token: one_token()
                }
                const resp = await api.lookupWarehouse(params)
                if (resp.status != 'OK') {
                    let sn = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", sn)
                } else {
                    context.commit("update_l_warehouse", resp.data)
                }
            } catch (error) {
                let sn = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", sn)
            }
        },
        async LookupSuratJalan(context) {
            try {
                let params = {
                    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,
                    search: context.state.f_search,
                    currpage: context.state.paginat
                }

                const resp = await api.lookupSuratJalan(params)
                if (resp.status != 'OK') {
                    let sn = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", sn)
                } else {
                    context.commit("update_l_suratjalan", resp.data.records)
                    context.commit("update_l_total_page", resp.data.total)
                }
            } catch (error) {
                let sn = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", sn)
            }
        },
        async LookupDetailSuratJalan(context, payload) {
            try {
                let params = {
                    token: one_token(),
                    SuratJalanID: payload.id
                }
                
                const resp = await api.lookupDetailSuratJalan(params)
                if (resp.status != 'OK') {
                    let sn = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", sn)
                } else {
                    const data = resp.data.records
                    let sj = context.state.s_suratjalan
                    
                    sj.SuratJalanID = data.SuratJalanID
                    sj.SuratJalanNumber = data.SuratJalanNumber
                    sj.i_tftype = data.T_GoodsTransferType == 'P' ? 'Persediaan' : 'Non Persediaan'
                    sj.i_status = data.SuratJalanStatus
                    sj.i_notes = data.SuratJalanNote
                    sj.i_phone = data.ExpeditionStaffMobile
                    sj.i_date = data.SuratJalanDate
                    sj.i_receivenotes = data.SuratJalanReceivedNote
                    sj.i_isConfirmed = data.SuratJalanReceivedConfirmed
                    sj.detail = data.detail
                    
                    sj.i_tftype = data.T_GoodsTransferType == 'P' ? 'Persediaan' : 'Non Persediaan'
                    sj.i_isInternal = data.ExpeditionIsInternal == 'Y' ? 'Internal' : 'Eksternal'

                    let ekspedisi = {
                        ExpeditionID: data.ExpeditionID,
                        ExpeditionIsInternal: data.ExpeditionIsInternal,
                        ExpeditionName: data.ExpeditionName
                    }
                    sj.i_ekspedisi = ekspedisi

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

                    let tfreq = {
                        T_GoodsTransferID: data.T_GoodsTransferID,
                        T_GoodsTransferNum: data.T_GoodsTransferNum,
                        T_GoodsTransferType: data.T_GoodsTransferType,
                        T_GoodsTransferDate: data.T_GoodsTransferDate
                    }
                    sj.i_tfrequest = tfreq

                    let wh = context.state.l_warehouse.find(obj => obj.WarehouseID == data.SuratJalanReceivedWarehouseID)
                    sj.i_receivewarehouse = wh

                    await context.commit("update_l_tfrequest", [tfreq])
                    await context.commit("update_s_suratjalan", sj)
                    await context.dispatch("LookupEkpedisi")
                    await context.dispatch("LookupEkspedisiStaff")
                }
            } catch (error) {
                let sn = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", sn)
            }
        },
        async saveReceveiSJ(context) {
            try {
                const sj = context.state.s_suratjalan
                let params = {
                    token: one_token(),
                    ...sj
                }
                const resp = await api.saveReciveSJ(params)
                if (resp.status != 'OK') {
                    let sn = { isOpen: true, color: "error", msg: "Gagal, " + resp.message }
                    context.commit("update_snackbar", sn)
                } else {
                    context.dispatch("newForm")
                    context.dispatch("LookupSuratJalan")

                    let sn = { isOpen: true, color: "success", msg: "Success, simpan receive surat jalan" }
                    context.commit("update_snackbar", sn)
                }
            } catch (error) {
                let sn = { isOpen: true, color: "error", msg: "Gagal, " + error.message }
                context.commit("update_snackbar", sn)
            }
        },

        async testinput(context) {
            const sj = context.state.s_suratjalan
            let params = {
                token: one_token(),
                ...sj
            }
            const resp = await api.testinput(params)
            console.log(resp.data)
        }
    }
}