import * as api from '../api/api.js';
export default {
    namespaced: true,
    state: {
        /* utility */ 
        snackbar: {
            state: false,
            color: 'success',
            msg: ''
        },
        loading_process: false,
        selected_rojasa: {},

        /* listing received order jasa  */ 
        filterjasa: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            status: "All",
            search: ""
        },
        liststatus: ['All', 'Confirmed', 'Unconfirmed'],
        list_supplier: [],
        list_branch: [],
        list_rojasa: {},
        currpage_rojasa: 1,

        /* add new receive order jasa */ 
        dialog_formro: false,
        form_rojasa: {
            rodate: moment(new Date()).format('YYYY-MM-DD'),
            supplierID: 0,
            orderjasano: "",
            branchcode: one_user()['M_BranchCode'],
            reference: "",
            typekontrak: "",
            startdate: "",
            enddate: "",
            usedcount: "0 / 0",
            catatan: ""
        },
        data_rojasa: [],

        dialog_pickjasa: false,
        list_pojasa: {
            records: [],
            total: 0
        },

        /* upload attachment */ 
        dialog_attachment: false,
        preview_attachment: [],

        /* form inspeksi */ 
        dialog_inspeksi: false,
        list_staff: [],
        form_inspeksi: {
            hasil_pengerjaan: "good",
            catatan_pengerjaan: "",
            kesimpulan: "accept",
            catatan_kesimpulan: "",
            pemeriksa: "",
            pekerja: ""
        },
        selected_jasainspek: {},

        /* delete ro */ 
        dialog_deletero: false,

        /* detail & conform ro */ 
        dialog_confirm_ro: false,

        /* edit form ro */ 
        dialog_editro: false
    },
    mutations: {
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_loading_process(state, val) {
            state.loading_process = val
        },
        update_selected_rojasa(state, val) {
            state.selected_rojasa = val
        },
        update_filterjasa(state, val) {
            state.filterjasa = val
        },
        update_liststatus(state, val) {
            state.liststatus = val
        },
        update_list_supplier(state, val) {
            state.list_supplier = val
        },
        update_list_branch(state, val) {
            state.list_branch = val
        },
        update_list_rojasa(state, val) {
            state.list_rojasa = val
        },
        update_currpage_rojasa(state, val) {
            state.currpage_rojasa = val
        },
        update_dialog_formro(state, val) {
            state.dialog_formro = val
        },
        update_form_rojasa(state, val) {
            state.form_rojasa = val
        },
        update_data_rojasa(state, val) {
            state.data_rojasa = val
        },
        update_dialog_pickjasa(state, val) {
            state.dialog_pickjasa = val
        },
        update_list_pojasa(state, val) {
            state.list_pojasa = val
        },
        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_preview_attachment(state, val) {
            state.preview_attachment = val
        },
        update_dialog_inspeksi(state, val) {
            state.dialog_inspeksi = val
        },
        update_list_staff(state, val) {
            state.list_staff = val
        },
        update_form_inspeksi(state, val) {
            state.form_inspeksi = val
        },
        update_selected_jasainspek(state, val) {
            state.selected_jasainspek = val
        },
        update_dialog_deletero(state, val) {
            state.dialog_deletero = val
        },
        update_dialog_confirm_ro(state, val) {
            state.dialog_confirm_ro = val
        },
        update_dialog_editro(state, val) {
            state.dialog_editro = val
        }
    },
    actions: {
        async lookupCabang(context) {
            try {
                const resp = await api.getListBranch({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.getListingVendor({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 lookupStaff(context) {
            try {
                const resp = await api.getDaftarStaff({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_staff', resp.data)
                }
            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async lookupOrderJasa(context, keyword) {
            try {
                let param = {
                    token: one_token(),
                    keyword: keyword,
                    supplierID: context.state.form_rojasa.supplierID,
                    branchcode: context.state.form_rojasa.branchcode,
                    // currpage: 1
                }
                
                const resp = await api.searchOrderJasa(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 saveReceiveOrder(context) {
            try {
                context.state.loading_process = true
                const detail  = context.state.data_rojasa
                let params = {
                    ...context.state.form_rojasa,
                    detail: detail,
                    token: one_token(),
                }

                const resp = await api.saveReceiveOrderJasa(params)
                if (resp.status != 'OK') {
                    context.state.loading_process = false
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.state.loading_process = false
                    let snac = {color: 'success', msg: resp.data, state: true}
                    context.commit('update_snackbar', snac)
                    
                    context.dispatch('lookupReceiveOrder')
                    context.state.dialog_formro = false
                }
            } catch (error) {
                context.state.loading_process = false
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async saveEditReceiveOrder(context) {
            try {
                context.state.loading_process = true
                const form = context.state.form_rojasa
                const detail = context.state.data_rojasa
                let param = {
                    ...form,
                    detail: detail,
                    token: one_token()
                }

                const resp = await api.saveEditReceiveOrderJasa(param)
                if (resp.status != 'OK') {
                    context.state.loading_process = false
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.state.loading_process = false
                    let snac = {color: 'success', msg: resp.data, state: true}
                    context.commit('update_snackbar', snac)
                    
                    context.dispatch('lookupReceiveOrder')
                    context.state.selected_rojasa = {}
                    context.state.data_rojasa = []
                    context.state.form_rojasa = {}
                    context.state.dialog_editro = false
                }

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

        async uploadAttachment(context, param) {
            try {
                context.state.loading_process = true
                const resp = await api.uploadAttachment(param)
                if (resp.status != 'OK') {
                    context.state.loading_process = false
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.state.loading_process = false
                    let snac = {color: 'success', msg: resp.data, state: true}
                    context.commit('update_snackbar', snac)

                    context.dispatch('lookupReceiveOrder')
                    
                    context.selected_rojasa = {}
                    context.state.dialog_attachment = false
                }
            } catch (error) {
                context.state.loading_process = false
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },

        async lookupAttachment(context) {
            try {
                const order = context.state.selected_rojasa
                let param = {
                    token: one_token(),
                    TJasaID: order.T_KontrakJasaID,
                    roID: order.ReceiveOrderPoID
                }

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

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

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

        async deleteReceiveOrder(context) {
            try {
                context.state.loading_process = true
                let order = context.state.selected_rojasa
                let param = {
                    token: one_token(),
                    roid: order.ReceiveOrderPoID
                }

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

                    context.dispatch('lookupReceiveOrder')

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

        async lookupDetailReceiveOrder(context) {
            try {
                const order = context.state.selected_rojasa
                let param = {
                    token: one_token(),
                    roid: order.ReceiveOrderPoID
                }

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

        async confirmReceiveOrder(context) {
            try {
                context.state.loading_process = true
                const formro = context.state.form_rojasa
                let param = {
                    token: one_token(),
                    roid: formro.ReceiveOrderPoID,
                    TJasaID: formro.TJasaID
                }

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

                    context.dispatch('lookupReceiveOrder')
                    context.state.data_rojasa = []
                    context.state.selected_rojasa = {}
                    context.state.dialog_confirm_ro = false
                }
            } catch (error) {
                context.state.loading_process = false
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },
    }
}