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

export default {
    namespaced: true,
    state: {
        /* utility */
        snackbar: {
            color: 'success',
            msg: '',
            state: false
        },

        filterjasa: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            status: "All",
            search: ""
        },
        liststatus: ['All', 'Draft', 'Pending', 'Approved', 'Rejected', 'Completed'],
        list_pojasa: {
            records: [],
            total: 0
        },
        currpage: 1,
        loading_process: false,
        list_supplier: [],
        list_branch: [],
        userinfo: {},

        /* dialog konfirmasi */ 
        dialog_confirm: false,
        detail_view: false,
        selected_pojasa: {
            summary: {
                subtotal: 0.00,
                diskon: 0.00,
                pajak: 0.00,
                total: 0.00
            }
        },

        /* dialog attachment */ 
        dialog_attachment: false,
        prview_attachment: []
    },
    mutations: {
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_filterjasa(state, val) {
            state.filterjasa = val
        },
        update_liststatus(state, val) {
            state.liststatus = val
        },
        update_list_pojasa(state, val) {
            state.list_pojasa = val
        },
        update_currpage(state, val) {
            state.currpage = val
        },
        update_loading_process(state, val) {
            state.loading_process = val
        },
        update_list_supplier(state, val) {
            state.list_supplier = val
        },
        update_list_branch(state, val) {
            state.list_branch = val
        },
        update_userinfo(state, val) {
            state.userinfo = val
        },
        update_dialog_confirm(state, val) {
            state.dialog_confirm = val
        },
        update_detail_view(state, val) {
            state.detail_view = val
        },
        update_selected_pojasa(state, val) {
            state.selected_pojasa = val
        },
        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_prview_attachment(state, val) {
            state.prview_attachment = val
        }
    },
    actions: {
        async lookupCabang(context) {
            try {
                const resp = await api.getDaftarCabang({token: one_token()})
                if (resp.status != 'OK') {
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.commit("update_list_branch", resp.data)
                }
            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async lookupVendor(context) {
            try {
                const resp = await api.getDaftarSupplier({token: one_token()})
                if (resp.status != 'OK') {
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.commit("update_list_supplier", resp.data)
                }
            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async lookupPOJasa(context) {
            try {
                let param = {
                    token: one_token(),
                    currpage: context.state.currpage,
                    ...context.state.filterjasa
                }

                const resp = await api.getDaftarPOJasa(param)
                if (resp.status != 'OK') {
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.commit('update_list_pojasa', resp.data)
                }
            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async lookupDetailPO(context, poid) {
            try {
                let param = {
                    token: one_token(),
                    poID: poid
                }

                const resp = await api.getDataKontrakPOJasa(param)
                if (resp.status != 'OK') {
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.commit("update_selected_pojasa", resp.data)
                }
            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async lookupUserApproveLevel(context) {
            try {
                const resp = await api.getUserApproveLevel({token: one_token()})
                if (resp.status != 'OK') {
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.commit("update_userinfo", resp.data)
                }

            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async lookupAttachment(context, param) {
            try {
                param.token = one_token()
                const resp = await api.getDaftarAttachment(param)
                if (resp.status != 'OK') {
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.commit('update_prview_attachment', resp.data)
                }

            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async updateApprovePO(context) {
            try {
                context.state.loading_process = true;

                const jasa = context.state.selected_pojasa;
                const param = {
                    token: one_token(),
                    approvelevel: context.state.userinfo.M_UserM_ApproveLevelID,
                    poID: jasa.PurchaseOrderID,
                    kontrakid: jasa.T_KontrakJasaID
                };

                const resp = await api.updateApprovalPO(param);
                if (resp.status !== 'OK') {
                    throw new Error(resp.message);
                }

                if (param.approvelevel === '2') {
                    const generate = await api.generatePOItemReceive(param);
                    if (generate.status !== 'OK') {
                        throw new Error(generate.message);
                    }
                }

                let snac = {color: 'success', msg: resp.data, state: true};
                context.commit('update_snackbar', snac);
                context.dispatch('lookupPOJasa');
                context.state.dialog_confirm = false
            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true};
                context.commit('update_snackbar', snac);
            } finally {
                context.state.loading_process = false;
            }
        }
    }
}