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

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

        /* main layout */
        filterpo: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            status: 'All',
            search: ''
        },
        list_status: ['All', 'Draft', 'Pending', 'Approved', 'Rejected', 'Completed'],
        list_asetpo: {
            records: [],
            total: 0
        },
        currpage: 1,

        /* form order aset */
        dialog_form_orderaset: false,
        list_supplier: [],
        list_branch: [],
        form_orderaset: {
            podate: moment(new Date()).format('YYYY-MM-DD'),
            supplierID: "",
            reference: "",
            branchcode: "",
            typepajak: "percent",
            valuepajak: 0,
            typediskon: "percent",
            valuediskon: 0
        },
        item_orderaset: [],
        summary_orderaset: {
            subtotal: 0,
            diskon: 0,
            pajak: 0,
            total: 0
        },

        dialog_add_itemaset: false,
        list_requestaset: {
            records: [],
            total: 0
        },

        /* delete order */
        dialog_deleteorder: false,

        /* attachment */
        dialog_attachment: false,
        prview_attachment: [],

    },
    mutations: {
        /* common */
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_loading_process(state, val) {
            state.loading_process = val
        },
        update_selected_orderaset(state, val) {
            state.selected_orderaset = val
        },

        /* main layout */
        update_filterpo(state, val) {
            state.filterpo = val
        },
        update_list_status(state, val) {
            state.list_status = val
        },
        update_list_asetpo(state, val) {
            state.list_asetpo = val
        },
        update_currpage(state, val) {
            state.currpage = val
        },

        /* form order aset */
        update_dialog_form_orderaset(state, val) {
            state.dialog_form_orderaset = val
        },
        update_list_supplier(state, val) {
            state.list_supplier = val
        },
        update_list_branch(state, val) {
            state.list_branch = val
        },
        update_form_orderaset(state, val) {
            state.form_orderaset = val
        },
        update_item_orderaset(state, val) {
            state.item_orderaset = val
        },
        update_summary_orderaset(state, val) {
            state.summary_orderaset = val
        },
        update_dialog_add_itemaset(state, val) {
            state.dialog_add_itemaset = val
        },
        update_list_requestaset(state, val) {
            state.list_requestaset = val
        },

        /* delete order */
        update_dialog_deleteorder(state, val) {
            state.dialog_deleteorder = val
        },

        /* attachment */
        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_prview_attachment(state, val) {
            state.prview_attachment = val
        }
    },
    actions: {
        async lookupVendor(context) {
            try {
                const resp = await api.getListSupplier({token: one_token()});
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                } 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 lookupCabang(context) {
            try {
                const resp = await api.getListCabang({token: one_token()});
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_branch', resp.data)
            } catch (error) {
                let snac = {color: 'error', msg: error.message, state: true}
                context.commit('update_snackbar', snac)
            }
        },
        async lookupOrderRequest(context, keyword) {
            try {
                const form = context.state.form_orderaset
                let param = {
                    token: one_token(),
                    branchcode: form.branchcode,
                    search: keyword,
                    currpage: 1,
                    supplierID: form.supplierID
                }
                const resp = await api.searchRequestAset(param)
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

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