import Vue from 'vue'
import Vuex from 'vuex'
import * as util from '@/util/util.js'
import {
	get,
	union
} from 'lodash'
import * as api from '@/service/api'

Vue.use(Vuex)
export default new Vuex.Store({
	state: {
		statusHeight: 0,
		systemInfo:{},
		isLogin: false,
		userToken: '',
		themeColor: '#FF7400',
		userInfo: {},
		mineData:{},
		defealtAddress:null,
		materialInfo:null,
		homeData:null,
		showLoading:false,

	},
	mutations: {
		setSystemInfo(state, systemInfo){
			state.systemInfo = systemInfo
		},
		setStatusHeight(state, height){
			state.statusHeight = height
		},
		setThemeColor(state, color) {
			state.themeColor = color
		},
		setIsLogin(state, isLogin) {
			state.isLogin = isLogin
		},

		setUserToken(state, token) {
			state.userToken = token
		},
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo
		},
		setMineData(state, mineData) {
			state.mineData = mineData
		},
		setDefealtAddress(state, defealtAddress) {
			state.defealtAddress = defealtAddress
		},
		setMaterialInfo(state, materialInfo) {
			state.materialInfo = materialInfo
		},
		setHomeData(state, homeData) {
			state.homeData = homeData
		},
		setShowLoading(state, showLoading) {
			state.showLoading = showLoading
		}
		

	},
	actions: {
		setSystemInfo({ commit }, systemInfo){
			
			let statusHeight = get(systemInfo,'statusBarHeight')
			
			commit('setSystemInfo', systemInfo)
			commit('setStatusHeight', statusHeight)
			
		},
		setStatusHeight({ commit }, height){
			
			commit('setStatusHeight', height)
		},

		setUserToken({
			commit
		}, token) {
			
			if(token){
				util.setStorageSync('userToken', token)
				commit('setIsLogin', token ? true : false)
				commit('setUserToken', token)
				
				api.getMinePage().then(res => {
					console.log('updateUserInfo success:', res)
					let user = res.result
					util.setStorageSync('mineData', user)
					commit('setMineData', user)
				})
				api.getAddressList({
					'page':1,
					'size': 10
				}).then(res2 =>{
					console.log('getAddressList success : ',res2.result)
					if(res2.result.length>0){
						util.setStorageSync('defealtAddress', res2.result[0])
						commit('setDefealtAddress', res2.result[0])
					}
				})
			}

		},
		setUserInfo({
			commit
		}, userInfo) {
			util.setStorageSync('userInfo', userInfo)
			commit('setUserInfo', userInfo)
		},
		setMineData({
			commit
		}, mineData) {
			util.setStorageSync('mineData', mineData)
			commit('setMineData', mineData)
		},
		setDefealtAddress({
			commit
		}, defealtAddress) {
			console.log('------',defealtAddress)
			util.setStorageSync('defealtAddress', defealtAddress)
			commit('setDefealtAddress', defealtAddress)
		},
		setMaterialInfo({
			commit
		}, materialInfo) {
			util.setStorageSync('materialInfo', materialInfo)
			commit('setMaterialInfo', materialInfo)
		},
		setHomeData({
			commit
		}, homeData) {
			util.setStorageSync('homeData', homeData)
			commit('setHomeData', homeData)
		},
		updateMineDta({ commit }){
			api.getMinePage().then(res => {
				console.log('updateUserInfo success:', res)
				let user = res.result
				util.setStorageSync('mineData', user)
				commit('setMineData', user)
			})
		},
		setShowLoading({
			commit
		}, showLoading) {
			commit('setShowLoading', showLoading)
		},
		
		setNopayOrder({
			commit
		}, data) {
			util.setStorageSync('nopayOrder', data)
			commit('setNopayOrder', data)
		},
		
	},
	
	
})
