const mutations= {
    UPDATE_VALIDATED(state, payload){
        state.validated = payload
    },
    UPDATE_PROPOSAL_DATE(state, payload){
        state.proposal_data.proposal_date = payload
    },
    UPDATE_OPEN_TILL_DATE(state, payload){
        state.proposal_data.open_till_date = payload
    },
    UPDATE_PROPOSAL_NUMBER(state, data){
        state.proposal_data.proposal_number = data.payload
    },

    UPDATE_SELECTED_PRODUCTS_COUNTER(state, payload){
        state.proposal_data.selected_products_counter  = payload
    },

    ENABLE_SUBMIT_BUTTON(state){
        //    Check all states values,
        //    if all the required states have values, then enable the submit buttons
        if (
            state.proposal_data.proposal_number_status === true &&
            state.proposal_data.proposal_number !== '' &&
            state.proposal_data.subject !== '' &&

            state.proposal_data.relation !== '' &&
            state.proposal_data.relation !== null &&

            state.proposal_data.lead_customer_id !== null &&
            Object.keys(state.proposal_data.lead_customer_id).length > 0 &&

            state.proposal_data.status !== null &&
            Object.keys(state.proposal_data.status).length > 0 &&

            state.proposal_data.currency_id !== null &&
            Object.keys(state.proposal_data.currency_id).length > 0 &&

            state.proposal_data.assigned_to !== null &&
            Object.keys(state.proposal_data.assigned_to).length > 0 &&

            state.proposal_data.selected_products_counter > 0
        ){
            state.validated = false;
        }else{
            state.validated = true;
        }
    },

    DISABLE_SUBMIT_BUTTON(state){
        state.validated = true;
    },

    UPDATE_PROPOSAL_STATUS(state, data){
        if (data.payload === 'occupied') {
            state.proposal_data.proposal_number_status = false;
        } else if(state.proposal_data.proposal_number !== '') {
            state.proposal_data.proposal_number_status = true;
        }
    },

    UPDATE_SUBJECT(state, data){
        state.proposal_data.subject = data.payload
    },
    UPDATE_STATUS(state, data){
        state.proposal_data.status = data.payload
    },
    UPDATE_CURRENCY_ID(state, data){
        state.proposal_data.currency_id = data.payload
    },
    UPDATE_ASSIGNED_TO(state, data){
        state.proposal_data.assigned_to = data.payload
    },
    UPDATE_MESSAGE(state, data){
        state.proposal_data.message = data
    },
    set_default_proposal_data(state, payload){
        state.proposal_data.proposal_number = payload.proposal_number,
        state.proposal_data.proposal_number_prefix = payload.proposal_number_prefix
        state.proposal_data.subject = payload.proposal_default_subject
    },
    UPDATE_RELATION(state, data){
        state.proposal_data.relation = data.payload
        state.proposal_data.lead_customer_id = ''
    },
    SET_CLIENTS(state, data){
        // first clear the existing data in the clients state,
        state.proposal_data.clients = [];
        // Copy the new data into the clients state.
        state.proposal_data.clients = data.clients.slice();
    },
    UPDATE_LEAD_CUSTOMER_ID(state, data){
        state.proposal_data.lead_customer_id = data.lead_customer_id
    },

    RESET_PROPOSAL_STATE(state, data){
        let getDefaultState = {
            proposal_data: {
                proposal_date: new Date().toISOString().substr(0, 10),
                proposal_number_prefix: state.proposal_data.proposal_number_prefix,
                subject: state.proposal_data.subject,
                relation: '',
                open_till_date: new Date().toISOString().substr(0, 10),
                currency_id: {},
                assigned_to: {},
                clients:[],
                proposal_number_status:true,
                proposal_number: parseInt(state.proposal_data.proposal_number)+1,
                lead_customer_id: {},
                status: {},
                message:'',
                selected_products_counter:0
            },
            validated:true,
        };
        Object.assign(state, getDefaultState)
    },

    EDIT_PROPOSAL_STATE(state, data){
        let getDefaultState = {
            proposal_id: data.proposal.id,
            proposal_data: {
                proposal_date: data.proposal.proposal_date,
                proposal_number_prefix: data.proposal.prefix,
                subject: data.proposal.subject,
                relation: data.proposal.relation,
                open_till_date: data.proposal.open_till_date,
                currency_id: data.proposal.currency,
                assigned_to: data.proposal.user,
                clients: [],
                proposal_number_status:true,
                proposal_number: data.proposal.proposal_number,
                lead_customer_id: data.lead_customer,
                status: JSON.parse(data.proposal.status),
                message: data.proposal.message,
                selected_products_counter:0
            },
            validated:true,
        };
        Object.assign(state, getDefaultState)
    }
};

 
export default mutations;