import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/mall/index.js';

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		isUpdate: true,
		hasLogin: false,
		userInfo: {},
		orderid: '',
		djqid: '',
		lasturl: '',
		type: '',
		groupid: '',
		zfMoney: 0,
		problem_co: '', //常见问题内容
		bindid: 0,
		tabbarList: [],
		likeList: [],
		page: 1, // 当前请求数据是第几页
		hasMoreData: true, // 上拉时是否继续请求数据，即是否还有更多数据
		pageSize: 10, // 获取的数据列表，以追加的形式添加进去,
		// 租地订单
		landleaseorder: {},
		// 租地提交订单
		updaordermess: {
			//下单的用户id
			// vip_id: null,
			//土地id
			soil_id: null,
			//地块键的id
			subdivision_id: null,
			// 土地图片
			soil_pic: null,
			// 订单金额
			order_money: null,
			// 服务id
			service_id: null,
			// 服务名称
			// service_name: null,
			// 租赁周期
			period_days: null,
			// 选购种子
			seeds: [],
			// 地址id
			address_id: null,
			// 订单信息姓名
			order_name: null,
			// 订单手机号
			order_mobile: null,
			// 备注
			remark: null,
			// 支付方式
			type_pay: null
		},
		addressmess: '',
		islogin: () => {
			if (!store.state.hasLogin) {
				let token = uni.getStorageSync('token')
				if (token) {
					store.state.hasLogin = true
				}
			}
			return store.state.hasLogin
		}
	},
	mutations: {
		//更改更新弹窗状态
		changeUpdate(state, value) {
			state.isUpdate = value
		},
		//更改租地提交订单
		setaddressmess(state, content) {
			state.addressmess = content
		},
		//更改租地提交订单
		setupdaordermess(state, content) {
			state.updaordermess = content
		},
		//保存租地订单
		setlandleaseorder(state, content) {
			state.landleaseorder = content
		},
		setTabbar(state, list) {
			state.tabbarList = list
			console.log("已经赋值" + state.tabbarList)
		},
		setProblem_co(state, content) {
			state.problem_co = content
		},
		getLike(state) {
			// 获取我的收藏
			api.goodslikelist({
				p: state.page
			}).then(res => {
				if (res.data.length > 0) {
					if (state.page == 1) {
						state.likeList = []
					}
					var contentlist = res.data;
					if (contentlist.length < state.pageSize) {

						state.likeList = state.likeList.concat(contentlist)
						state.hasMoreData = false
					} else {
						state.likeList = state.likeList.concat(contentlist)
						state.page = state.page + 1
						state.hasMoreData = true
					}
				} else {
					if (state.likeList.length == 1) {
						state.likeList = []
					} else {
						state.likeList = state.likeList.concat(res.data)
					}
				}
			})
		},
		login(state, provider) {
			state.userInfo = provider;
			state.hasLogin = true
			uni.setStorageSync( //缓存用户登陆状态
				'token',
				provider.token
			)
			uni.setStorageSync( //缓存用户登陆状态
				'userInfo',
				provider,
			)
			console.log("登陆状态：" + state.hasLogin)
			console.log("userInfo：" + JSON.stringify(provider))
		},
		/* 綁定上下级关系 */
		bind(state, pid) {
			state.bindid = pid;
			console.log('调用成功，已经储存上级 pid' + state.bindid)
		},
		logout(state) {
			state.hasLogin = false;
			console.log("登陆状态" + state.hasLogin)
			state.userInfo = {};
			uni.clearStorage()
		},
		islogin() {
			if (!state.hasLogin) {
				let token = uni.getStorageSync('token')
				if (token) {
					state.hasLogin = true
				}
			}
			return state.hasLogin
		}
	},
	actions: {

	}
})

export default store
