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

export default {
    namespaced: true,
    state: {
        snackbar: {
            color: 'success',
            msg: '',
            state: false
        },
        user: {},
        loading_process: false,
        filter: {
            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_order: {
            records: [],
            total: 0
        },
        currpage: 1,
        list_cabang: [],
        list_gudang: [],
        list_supplier: [],

        /* dialog confirm order */
        dialog_confirm_order: false,
        selected_order: {},

        view_detail_mode: false,

        dialog_attachment: false,
        list_attachment: []
    },
    mutations: {
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_user(state, val) {
            state.user = val
        },
        update_loading_process(state, val) {
            state.loading_process = val
        },
        update_filter(state, val) {
            state.filter = val
        },
        update_list_status(state, val) {
            state.list_status = val
        },
        update_list_order(state, val) {
            state.list_order = val
        },
        update_currpage(state, val) {
            state.currpage = val
        },
        update_list_cabang(state, val) {
            state.list_cabang = val
        },
        update_list_gudang(state, val) {
            state.list_gudang = val
        },
        update_list_supplier(state, val) {
            state.list_supplier = val
        },
        update_dialog_confirm_order(state, val) {
            state.dialog_confirm_order = val
        },
        update_selected_order(state, val) {
            state.selected_order = val
        },
        update_view_detail_mode(state, val) {
            state.view_detail_mode = val
        },
        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_list_attachment(state, val) {
            state.list_attachment = val
        }
    },
    actions: {
        async listingCabang(context) {
            try {
                let param = { token: one_token() }

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

                context.commit('update_list_cabang', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("common/update_snackbar", snac, { root: true });
            }
        },
        async listingGudang(context, branchID) {
            try {
                let param = {
                    token: one_token(),
                    M_BranchID: branchID
                }

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

                context.commit('update_list_gudang', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("common/update_snackbar", snac, { root: true });
            }
        },
        async listingSupplier(context) {
            try {
                const params = { token: one_token() };
                const resp = await api.getListSupplier(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_supplier', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac, { root: true });
            }
        },
        async getListingPOAsset(context) {
            try {
                const params = {
                    token: one_token(),
                    currpage: context.state.currpage,
                    ...context.state.filter
                };
                const resp = await api.getDaftarPoAset(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_order', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },
        async getUserApproveLevel(context) {
            try {
                const params = { token: one_token() };
                const resp = await api.getUserApproveLevel(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_user', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },
        async getAssetAttachment(context, param) {
            try {
                const params = {
                    token: one_token(),
                    contractID: param.contractID,
                    poID: param.poID
                };
                const resp = await api.getDaftarAttachment(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_attachment', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },
        async confirmOrderAsset(context, poID) {
            try {
                context.state.loading_process = true;
                const userlvl = context.state.user.M_UserM_ApproveLevelID

                const params = {
                    token: one_token(),
                    approvelevel: userlvl,
                    poID: poID,
                };
                const resp = await api.updateApprovalPO(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

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

                context.dispatch("getListingPOAsset");
                context.state.loading_process = false;
                context.state.view_detail_mode = false;
                context.state.dialog_confirm_order = false;
            } catch (e) {
                context.state.loading_process = false;
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },
    }
}
