import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		carArr: []
	},
	mutations: {
		addCar(state, obj) {
			obj.check = true
			// console.log(obj.check)
			let index = state.carArr.findIndex(val => val.id == obj.id)
			if (index == -1) {
				obj.num = 1;
				state.carArr.push(obj)
			} else {
				obj.num += 1
			}
		},
		jia(state, id) {
			let carArr = [...state.carArr]
			// console.log(carArr)
			let index = carArr.findIndex(val => val.id == id)
			carArr[index].num += 1
			state.carArr = carArr
		},
		jian(state, id) {
			let carArr = [...state.carArr]
			let index = carArr.findIndex(val => val.id == id)
			if (carArr[index].num > 1) {
				carArr[index].num -= 1
				state.carArr = carArr
			}
		},
		sc(state, id) {
			let index = state.carArr.findIndex(val => val.id == id)
			state.carArr.splice(index, 1)
		}
	},
	getters: {
		sumPrice(state) {
			let sum = 0;
			state.carArr.map(val => {
				if (val.check == true) {
					sum += val.minPrice * val.num
				}
			})
			return sum
		}
	},
	actions: {
	},
	modules: {
	}
})
