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

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

        /* 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,

        /* delete order */
        dialog_deleteorder: false,

        /* attachment */
        dialog_attachment: false,
        assets_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
        },
        update_viewonlymode(state, val) {
            state.viewonlymode = 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
        },

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

        /* attachment */
        update_dialog_attachment(state, val) {
            state.dialog_attachment = val
        },
        update_assets_attachment(state, val) {
            state.assets_attachment = val
        }
    },
    actions: {
        async getListingPOAsset(context) {
            try {
                const params = {
                    token: one_token(),
                    currpage: context.state.currpage,
                    ...context.state.filterpo
                };
                const resp = await api.getDaftarPoAset(params);
                if (resp.status != 'OK') {
                    throw new Error(resp.message);
                }

                context.commit('update_list_asetpo', 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_assets_attachment', resp.data);
            } catch (e) {
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },
        async uploadAttachment(context, params) {
            try {
                context.state.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('getListingPOAsset');
                context.state.loading_process = false;
                context.state.dialog_attachment = false;
            } catch (e) {
                context.state.loading_process = false;
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        },
        async deleteOrderAsset(context) {
            try {
                context.state.loading_process = true;
                const order = context.state.selected_orderaset;
                const params = {
                    token: one_token(),
                    poID: order.PurchaseOrderID,
                    contractID: order.contractID,
                    ponumber: order.PurchaseOrderNumber,
                    SupplierDownpaymentID: order.SupplierDownpaymentID
                };
                const resp = await api.deletePoAsset(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('getListingPOAsset');
                context.state.loading_process = false;
                context.state.dialog_deleteorder = false;
            } catch (e) {
                context.state.loading_process = false;
                const snac = { color: 'error', msg: e.message, state: true };
                context.commit("update_snackbar", snac);
            }
        }
    }
}
