
/* eslint-disable no-alert, no-console ,  no-debugger */
import { doFetchOrderHistory } from '@/services/product.service';
import { getField, updateField } from 'vuex-map-fields';
import humps from 'lodash-humps';

export const orderlist = {
    namespaced: true,
    state: {
        inProgress: true,
        OrderHistory: {},

        customerId: "",
        orderNumber: "",
        createdFrom: "",
        createdTo: "",
        // purchaseOrderNumber: "",

    },
    getters: {
        getField,

    },
    mutations: {
        updateField,
        inProgress(state, yesOrNo) {
            state.inProgress = yesOrNo
        },
        setOrderHistory(state, orderhistorydata) {
            state.OrderHistory = orderhistorydata
        },
    },
/* eslint-disable no-alert, no-console ,  no-debugger */

    actions: {
        fetchOrderList: async ({ state, commit }, customerId) => {
             

            try {
                 
                let newDateTo='';
                 
                if(state.createdTo)
                {
                 
                 const date = new Date(state.createdTo);
                     let newD=date.setDate(date.getDate() + 1); 
                     let tmp = new Date(newD).toLocaleDateString().split("/");
                     newDateTo = tmp[2] + "-" + tmp[1] + "-" + tmp[0]; 
                }
                commit('inProgress', true);
                let OrderHistory = await doFetchOrderHistory({
                    customerId: customerId,
                    incrementId: state.orderNumber,
                    createdAt: state.createdFrom,
                    createdTo: newDateTo
                });
                if (OrderHistory) {
                    commit('inProgress', false);
                    commit('setOrderHistory', humps(OrderHistory));
                }
            }
            catch (error) {
                commit('setOrderHistory', []);
                commit('inProgress', false);
                throw error
            }
        },
        fetchReset: async ({ state, commit }, customerId) => {
            try {
                state.orderNumber = "";
                state.createdFrom = "";
                state.createdTo = "";
                commit('inProgress', true);
                let orderHistory = await doFetchOrderHistory({ customerId: customerId, incrementId: state.orderNumber,createdAt: state.createdFrom,createdTo: state.createdTo });

                if (orderHistory) {
                    commit('inProgress', false);
                    commit('setOrderHistory', humps(orderHistory));
                }
            }
            catch (error) {
                commit('inProgress', false);
                throw error
            }
        },
    }
}




