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

        /* main layout */
        filterasset: {
            startdate: moment(new Date()).format('YYYY-MM-DD'),
            enddate: moment(new Date()).format('YYYY-MM-DD'),
            status: 'All',
            search: ''
        },
        list_status: ['All', 'Confirmed', 'Unconfirmed'],
        list_roasset: {
            records: [],
            total: 0
        },
        currpage: 1,
        selected_roasset: {},

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

        /* delete */
        dialog_delete: false,

        /* confirm */
        dialog_confirm: false
    },
    mutations: {
        /* common */
        update_snackbar(state, val) {
            state.snackbar = val
        },
        update_loading_process(state, val) {
            state.loading_process = val
        },

        /* main layout */
        update_filterasset(state, val) {
            state.filterasset = val
        },
        update_list_status(state, val) {
            state.list_status = val
        },
        update_list_roasset(state, val) {
            state.list_roasset = val
        },
        update_currpage(state, val) {
            state.currpage = val
        },
        update_selected_roasset(state, val) {
            state.selected_roasset = val
        },

        /* attachment */
        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_preview_attachment(state, val) {
            state.preview_attachment = val
        },

        /* delete */
        update_dialog_delete(state, val) {
            state.dialog_delete = val
        },

        update_dialog_confirm(state, val) {
            state.dialog_confirm = val
        }
    },
    actions: {
        async lookupOrderAssetReceived(context) {
            try {
                const param = {
                    token: one_token(),
                    currpage: context.state.currpage,
                    ...context.state.filterasset
                }

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

                context.commit("update_list_roasset", resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: false };
                context.commit("update_snackbar", snac);
            }
        },
        async lookupAttachment(context, roID) {
            try {
                const param = {
                    token: one_token(),
                    roID: roID
                }

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

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

                context.commit('orderAsset/update_form_receive', resp.data.header, { root: true })
                context.dispatch('orderAsset/listingGudang', resp.data.header.branchID, { root: true });

                context.commit('orderAsset/update_order_received', resp.data.detail, { root: true })

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

                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("update_snackbar", snac);

                context.dispatch('lookupOrderAssetReceived');
                context.commit('update_loading_process', false);
                context.state.dialog_attachment = false;
            } catch (e) {
                context.commit('update_loading_process', false);
                const snac = { color: 'error', msg: e.message, state: false };
                context.commit("update_snackbar", snac);
            }
        },
        async deleteReceiveAsset(context, roID) {
            try {
                context.commit('update_loading_process', true);
                const param = {
                    token: one_token(),
                    roid: roID
                }

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

                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("update_snackbar", snac);

                context.dispatch('lookupOrderAssetReceived');
                context.commit('update_loading_process', false);
                context.state.dialog_delete = false;
            } catch (e) {
                context.commit('update_loading_process', false);
                const snac = { color: 'error', msg: e.message, state: false };
                context.commit("update_snackbar", snac);
            }
        },
        async confirmReceiveOrder(context, roID) {
            try {
                context.commit('update_loading_process', true);
                const param = {
                    token: one_token(),
                    roid: roID
                }

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

                const snac = { color: 'success', msg: resp.data, state: true };
                context.commit("update_snackbar", snac);

                context.dispatch('lookupOrderAssetReceived');
                context.commit('update_loading_process', false);
                context.state.dialog_confirm = false;
            } catch (e) {
                context.commit('update_loading_process', false);
                const snac = { color: 'error', msg: e.message, state: false };
                context.commit("update_snackbar", snac);
            }
        }
    }
}
