import Vue from 'vue'
import { getUserInfo, getAddressList, getOrderList, getOrderNum } from '@/request/yyf.js';

export const store = Vue.observable({
	userInfo: uni.getStorageSync('userInfo'),
	addrList: [],
	orderList: [],
	orderNum: {},
	sendAddrList: [],
	targetAddrList: []
})

export const mutations = {
	setUserInfo(data) {
		store.userInfo = data;
	},
	setAddrList(type, data) {
		store.addrList = data;
	},
	setOderList(data) {
		store.orderList = data;
	},
	setOrderNum(data) {
		store.orderNum = data;
	},
}

export const actions = {
	async onGetuserInfo() {
		const { data } = await getUserInfo();
		if(data.code === 1) {
			let info = data.data;
			mutations.setUserInfo(info);
		} 
	},
	async onGetAddrList() {
		// const { data } = await getAddressList({
		// 	type: 1,
		// });
		// if(data.code === 1) {
		// 	let info = data.data.data;
		// 	mutations.setAddrList(info);
		// }
	},
	async onGetOrderList() {
		const { data } = await getOrderList({
			page: 3,
			status: 1
		});
		if(data.code === 1) {
			let info = data.data.data;
			// console.log(data.data.data.length);
			mutations.setOderList(info);
		}
	},
	async onGetOrderNum() {
		const { data } = await getOrderNum({});
		if(data.code === 1) {
			let info = data.data;
			let obj = {};
			data.data.forEach(item=> {
				obj[item.status] = item.sum
			})
			mutations.setOrderNum(obj);
		}
	},
}

Vue.prototype.$storeMine = store
Vue.prototype.$mutationsMine = mutations
Vue.prototype.$actionsMine = actions