const actions = {
    // action called on change estimate date
    update_estimate_date(context, payload){
        context.commit({
            type: 'UPDATE_ESTIMATE_DATE',
            payload: payload
        })
        context.commit({
            type: 'ENABLE_SUBMIT_BUTTON',
        });
    },

    UPDATE_ESTIMATE_DUE_DATE(context, data){
        context.commit({
            type: data.type,
            payload: data.estimate_due_date
        })
    },

    // action called on change Estimate number
    update_estimate_number(context, data){
        context.commit({
            type: 'UPDATE_ESTIMATE_NUMBER',
            payload: data.estimate_number
        });

        axios.post('/api/v1/validateEstimateNumber',{
            'new_estimate_number':data.estimate_number
        }).then((res) => {
            //Make a commit and pass estimate_number_status from here in that commit
            context.commit({
                type: 'UPDATE_ESTIMATE_NUMBER_STATUS',
                payload: res.data.estimate_number_status
            });
            context.commit({
                type: 'ENABLE_SUBMIT_BUTTON',
            });

        });
    },
    // action called on change Estimate subject
    UPDATE_ESTIMATE_TITLE(context, data){
        context.commit({
            type: data.type,
            payload: data.payload
        });
        context.commit({
            type: 'ENABLE_SUBMIT_BUTTON',
        });
    },

    UPDATE_ESTIMATE_STATUS(context, data){
        context.commit({
            type: 'UPDATE_ESTIMATE_STATUS',
            payload: data.estimate_status
        });
        context.commit({
            type: 'ENABLE_SUBMIT_BUTTON',
        });
    },

    UPDATE_ESTIMATE_CURRENCY_ID(context, data){
        context.commit({
            type: data.type,
            payload: data.estimate_currency_id
        });
        context.commit({
            type: 'ENABLE_SUBMIT_BUTTON',
        });
    },

    update_estimate_owner_id(context, data){
        context.commit({
            type: 'UPDATE_ESTIMATE_OWNER_ID',
            payload: data.estimate_owner_id
        });
        context.commit({ type: 'ENABLE_SUBMIT_BUTTON'});
    },

    update_estimate_termsandconditions(context, payload){
        context.commit('UPDATE_ESTIMATE_TERMS', payload);
        context.commit({ type: 'ENABLE_SUBMIT_BUTTON'});
    },

    update_estimate_customer_notes(context, payload){
        context.commit('UPDATE_ESTIMATE_CUSTOMER_NOTES', payload);
        context.commit({ type: 'ENABLE_SUBMIT_BUTTON'});
    },

    // action called on change relation
    update_estimate_relation(context, action_data){
        context.commit({
            type: 'UPDATE_ESTIMATE_RELATION',
            payload: action_data.relation
        });

        if(action_data.relation === 'Lead'){
            return axios.get('/api/v1/leads')
            .then(res => {
                let estimate_clients = res.data.leads;
                context.commit('SET_ESTIMATE_CLIENTS', {estimate_clients})
                // In case, if customer is already selected in invoice create, when invoice is created inside the customer page.
                var customer = estimate_clients.find((ele) => {
                    return ele.id == action_data.lead_customer_id
                });
                if(action_data.lead_customer_id != undefined){
                    context.commit('SET_LEAD_CUSTOMER_ID', customer);
                }
            }).catch(err => {
                console.log(err);
            })

        }else if(action_data.relation === 'Customer'){
            return axios.get('/api/v1/customers')
            .then((res) => {
                let estimate_clients = res.data.customers;
                context.commit('SET_ESTIMATE_CLIENTS', {estimate_clients})
                // In case, if customer is already selected in invoice create, when invoice is created inside the customer page.
                var customer = estimate_clients.find((ele) => {
                    return ele.id == action_data.lead_customer_id
                });
                if(action_data.lead_customer_id != undefined){
                    context.commit('SET_LEAD_CUSTOMER_ID', customer);
                }
            })  
        }

        context.commit({
            type: 'ENABLE_SUBMIT_BUTTON',
        });
    },

    // action called on change relation
    update_estimate_lead_customer_id(context, data){
        context.commit({
            type: 'UPDATE_ESTIMATE_LEAD_CUSTOMER_ID',
            estimate_lead_customer_id: data
        });
        context.commit({
            type: 'ENABLE_SUBMIT_BUTTON',
        });
    },

    update_selected_products(context, data){
        context.commit('UPDATE_SELECTED_PRODUCTS_COUNTER',data.selected_products.length)
        if (data.selected_products.length > 0){
            context.commit({
                type: 'ENABLE_SUBMIT_BUTTON',
                estimate_selected_products: data.selected_products.length
            })
        }else{
            context.commit({
                type: 'DISABLE_SUBMIT_BUTTON',
            })
        }
    },

    // action called on component mount
    async getEstimateDefaultData(context){

        await axios.get('/api/v1/createEstimateData')
        .then((res) => {
            context.commit({
                type: 'SET_DEFAULT_ESTIMATE_DATA',
                estimate_number: parseInt(res.data.estimate_data.new_estimate_number),
                estimate_number_prefix: (res.data.estimate_data.estimate_number_prefix),
                estimate_title: res.data.estimate_data.estimate_title,
                estimate_termsandconditions: res.data.estimate_data.terms,
                estimate_customer_notes: res.data.estimate_data.customer_notes
            })
        });
    },

    // GET EDIT estimate DATA
    get_edit_estimate_data(context, data){
        axios.post('/api/v1/estimate/getEditEstimateData  ', {
            'id': data
        })
        .then((res) => {
            context.commit('EDIT_ESTIMATE_STATE',  res.data)
        });
    }
  }

export default actions;