const state = {
	quotation: [
		{
			gongyingshang: '第一商砼有限公司',
			cailiaomingcheng: "混凝土",
			cailiaopinlei: "120",
			channeng: "5000",
			chuchangjiage: "290",
			danwei: "m³",
			guigexinghao: "C20",
			id: 1670829126376,
			shuilv: "3",
			xianyoukucun: "2000",
			_channeng: "5000/月",
			_shuilv: "3%",
			_xianyoukucun: "2000(m³)",
			name: '二级',
			times: '2022-12-12',
			tel: '139****1234'
		},
		{
			gongyingshang: '第二商砼有限公司',
			cailiaomingcheng: "混凝土",
			cailiaopinlei: "120",
			channeng: "5000",
			chuchangjiage: "280",
			danwei: "m³",
			guigexinghao: "C20",
			id: 1670829126376,
			shuilv: "3",
			xianyoukucun: "2000",
			_channeng: "5000/月",
			_shuilv: "3%",
			_xianyoukucun: "2000(m³)",
			name: '二级',
			times: '2022-12-12',
			tel: '139****1234'
		},
		{
			gongyingshang: '第三商砼有限公司',
			cailiaomingcheng: "混凝土",
			cailiaopinlei: "120",
			channeng: "5000",
			chuchangjiage: "270",
			danwei: "m³",
			guigexinghao: "C20",
			id: 1670829126376,
			shuilv: "3",
			xianyoukucun: "2000",
			_channeng: "5000/月",
			_shuilv: "3%",
			_xianyoukucun: "2000(m³)",
			name: '二级',
			times: '2022-12-12',
			tel: '139****1234'
		},
	],

	quotation2: [
		{
			gongyingshang: '第一商砼有限公司',
			cailiaomingcheng: "混凝土",
			cailiaopinlei: "120",
			channeng: "5000",
			chuchangjiage: "270",
			danwei: "m³",
			guigexinghao: "C20",
			id: 1670829126376,
			shuilv: "3",
			xianyoukucun: "2000",
			_channeng: "5000/月",
			_shuilv: "3%",
			_xianyoukucun: "2000(m³)",
			name: '二级',
			times: '2022-12-12',
			tel: '139****1234'
		},
		{
			gongyingshang: '第一商砼有限公司',
			cailiaomingcheng: "混凝土",
			cailiaopinlei: "120",
			channeng: "5000",
			chuchangjiage: "280",
			danwei: "m³",
			guigexinghao: "C30",
			id: 1670829126376,
			shuilv: "3",
			xianyoukucun: "2000",
			_channeng: "5000/月",
			_shuilv: "3%",
			_xianyoukucun: "2000(m³)",
			name: '二级',
			times: '2022-12-12',
			tel: '139****1234'
		},
		{
			gongyingshang: '第一商砼有限公司',
			cailiaomingcheng: "混凝土",
			cailiaopinlei: "120",
			channeng: "5000",
			chuchangjiage: "290",
			danwei: "m³",
			guigexinghao: "C35",
			id: 1670829126376,
			shuilv: "3",
			xianyoukucun: "2000",
			_channeng: "5000/月",
			_shuilv: "3%",
			_xianyoukucun: "2000(m³)",
			name: '二级',
			times: '2022-12-12',
			tel: '139****1234'
		},
	]
};

const mutations = {
	SET_QUOTATION(state, mutation) {
		state.quotation = mutation.payload;
	},
};

const actions = {
	async addQuotationItem({ state, commit }, data) {
		const quotation = state.quotation;
		const values = data.values;

		quotation.push(
			Object.assign({}, values, {
				id: new Date().getTime(),
				_shuilv: `${values.shuilv}%`,
				_xianyoukucun: `${values.xianyoukucun}(${values.danwei})`,
			})
		);
		console.log(quotation);
		commit({
			type: "SET_QUOTATION",
			payload: quotation,
		});
	},

	async removeQuotationItem({ state, commit }, id) {
		const quotation = state.quotation;

		const data = quotation.filter((item) => item.id !== id);
		commit({
			type: "SET_QUOTATION",
			payload: data,
		});
	},

	async editQuotationItem({ state, commit }, data) {
		const quotation = state.quotation;
		let index = null;
		const current = data.current;
		const values = data.values;
		console.log(data);

		for (const idx in quotation) {
			if (quotation[idx].id === current) {
				console.log(idx);
				index = idx;
			}
		}
		quotation[index] = Object.assign({}, values, {
			id: new Date().getTime(),
			_shuilv: `${values.shuilv}%`,
			_xianyoukucun: `${values.xianyoukucun}(${values.danwei})`,
		});

		commit({
			type: "SET_QUOTATION",
			payload: quotation,
		});
	},
};

const getters = {
	quotation: (state) => state.quotation,
	quotation2: (state) => state.quotation2,
};

export default {
	state,
	mutations,
	actions,
	getters,
};
