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,
        
        /* dialog form po */
        dialog_formpo: false,
        list_supplier: [],
        list_branch: [],
        type_kontrak: [
            { "name": "sekali beli", "value": "once" },
            { "name": "kontrak berjangka", "value": "period" },
        ],
        form_pojasa: {
            podate: moment(new Date()).format('YYYY-MM-DD'),
            reference: "",
            supplierID: "",
            typepajak: "percent",
            valuepajak: 0,
            typediskon: "percent",
            valuediskon: 0,
            branchcode: 0,
            typekontrak: 'once',
            kontrakstart: moment(new Date()).format('YYYY-MM-DD'),
            kontrakend: moment(new Date()).format('YYYY-MM-DD'),
            PIOutQty: 1,
            catatan: ""
        },

        /* dialog form edit po */
        dialog_editformpo: false,

        /* dialog request jasa */
        dialog_select_itemjasa: false,
        list_requestjasa: {
            records: [],
            total: 0
        },
        selected_requestjasa: [],
        summary_pojasa: {
            subtotal: 0,
            diskon: 0,
            pajak: 0,
            total: 0
        },

        /* dialog delete jasa */ 
        dialog_deletepojasa: false,
        deleted_pojasa: {},

        /* dialog upload attachment */ 
        dialog_attachment: false,
        pojasa_attachment: {},
        prview_attachment: [],

        /* dialog view po */ 
        dialog_viewpo: false
    },
    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
        },

        /* dialog form po */
        update_dialog_formpo(state, val) {
            state.dialog_formpo = val
        },
        update_list_supplier(state, val) {
            state.list_supplier = val
        },
        update_list_branch(state, val) {
            state.list_branch = val
        },
        update_type_kontrak(state, val) {
            state.type_kontrak = val
        },
        update_form_pojasa(state, val) {
            state.form_pojasa = val
        },
        
        update_dialog_editformpo(state, val) {
            state.dialog_editformpo = val
        },

        /* dialog select request jasa */
        update_dialog_select_itemjasa(state, val) {
            state.dialog_select_itemjasa = val
        },
        update_list_requestjasa(state, val) {
            state.list_requestjasa = val
        },
        update_selected_requestjasa(state, val) {
            state.selected_requestjasa = val
        },
        update_summary_pojasa(state, val) {
            state.summary_pojasa = val
        },

        /* delete po jasa */ 
        update_dialog_deletepojasa(state, val) {
            state.dialog_deletepojasa = val
        },
        update_deleted_pojasa(state, val) {
            state.deleted_pojasa = val
        },

        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_pojasa_attachment(state, val) {
            state.pojasa_attachment = val
        },
        update_prview_attachment(state, val) {
            state.prview_attachment = val
        },

        update_dialog_viewpo(state, val) {
            state.dialog_viewpo = 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 lookupRequestJasa(context, keyword) {
            try {
                let pojasa = context.state.form_pojasa
                let param = {
                    token: one_token(),
                    branchcode: pojasa.branchcode,
                    supplierID: pojasa.supplierID,
                    search: keyword
                }

                const resp = await api.getDaftarRequestJasa(param)
                if (resp.status != 'OK') {
                    let snac = {color: 'error', msg: resp.message, state: true}
                    context.commit('update_snackbar', snac)
                } else {
                    context.commit("update_list_requestjasa", 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 lookupDataPOJasa(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 {
                    let data = resp.data
                    
                    context.commit('update_form_pojasa', data)
                    context.commit('update_selected_requestjasa', data.detail)
                    context.commit('update_summary_pojasa', data.summary)
                }
            } 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 createPOJasa(context, param) {
            try {
                context.state.loading_process = true
                param.token = one_token()

                const resp = await api.createPOjasa(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 {
                    let snac = {color: 'success', msg: resp.data, state: true}
                    context.commit('update_snackbar', snac)

                    context.state.loading_process = false
                    context.dispatch('lookupPOJasa')
                    context.commit('update_dialog_formpo', false)
                }

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

        async deletePOJasa(context) {
            try {
                context.state.loading_process = true
                let param = {
                    token: one_token(),
                    poID: context.state.deleted_pojasa.PurchaseOrderID,
                    tjasaID: context.state.deleted_pojasa.T_KontrakJasaID
                }

                const resp = await api.deletePOjasa(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 {
                    let snac = {color: 'success', msg: resp.data, state: true}
                    context.commit('update_snackbar', snac)

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

        async sendRequestPO(context, poid) {
            try {
                context.state.loading_process = true
                let param = {
                    token: one_token(),
                    approvelevel: "0",
                    poID: poid,
                    kontrakid: '0'
                }

                const resp = await api.updateApprovalPO(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 {
                    let snac = {color: 'success', msg: resp.data, state: true}
                    context.commit('update_snackbar', snac)


                    context.state.loading_process = false
                    context.dispatch('lookupPOJasa')
                }
            } 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('lookupPOJasa')
                    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 saveEditPOJasa(context, param) {
            try {
                context.state.loading_process = true
                param.token = one_token()

                const resp = await api.editPOjasa(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('lookupPOJasa')
                    context.commit('update_dialog_editformpo', false)
                }
            } catch (error) {
                context.state.loading_process = false
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        }
    }
}