import { isEmpty, random } from 'lodash';

const realtimeWatch = {
  state: {
	   //汇总信息
		countInfo: [
			
		],
	   
		// 车辆选中数据
		vheicleCheckArr: [],
		// 人员选中数据
		peopleCheckArr: [],
		
		// 更新随机数
		randomNum: 0,
		// 定时任务
		treeInterVal: null,
		loading: false, //加载数据
		
  },

  mutations: {
	   SET_REAL_DATA: (state, {key,data}) => {
			state[key] = data
	   },
  },

  actions: {
     // 设置汇总信息
	 setCountInfo({ commit, state }, data) {
		 
		 commit('SET_REAL_DATA', {key: 'countInfo',data},)
		 
	 },
	 //  设置车辆选中数据
	 setVheicleCheckArr({ commit, state }, data) {
	 		 
	 	commit('SET_REAL_DATA', {key: 'vheicleCheckArr',data},)
	 		 
	 },
	 //  设置人员选中数据
	 setPeopleCheckArr({ commit, state }, data) {
	 		 
	 	commit('SET_REAL_DATA', {key: 'peopleCheckArr',data},)
	 		 
	 },
	 
	 //  Loading状态
	 setLoading({ commit, state }, data) {
	 		 
	 	commit('SET_REAL_DATA', {key: 'loading',data},)
	 		 
	 },
	 
	 // 定时器
	 setTreeInterVal({ commit, state }, {cb,time=30000}) {
		 let that = this;
		 that.dispatch('clearTreeInterVal')
		
		 state.treeInterVal = setInterval(() => {   
			 that.dispatch('updataRandomNum')       
			 cb()
		 },time)
		 
	 },
	 
	 clearTreeInterVal ({ commit, state }) {
		 if (state.treeInterVal) {
			 clearInterval(state.treeInterVal)
		 }
	 },
	 
	 updataRandomNum({ commit, state }) {
	   let num = random(1, 1000)
	   if(state.randomNum === num) {
		 num = random(1, 1000)
	   }
		
		commit('SET_REAL_DATA', {key: 'randomNum',num},)
	 },
				 

	
  }
}

export default realtimeWatch
