import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let now = Date.now()
let past = parseInt(localStorage.getItem('past') || 0)
let userid = '',
  phone = ''
if (now - past < 24 * 60 * 60 * 1000) {
  userid = localStorage.getItem('userid')
  phone = localStorage.getItem('phone')
}
let fromSource  = ""
let str = localStorage.getItem('beCome')
if(str){
  fromSource = str
}
export default new Vuex.Store({
  state: {
    user: {
      userid,
      phone,
    },
    pageData: {},
    modelName: '',
    tempName: "loading",
    testId: null,
    testGroupId: null,
    tid: null,
    anonymousId:"",
    fromSource:fromSource,
    global: {},
    addrData: {},
    fictitious:''
  },
  mutations: {
    setUser(state, payload) {
      if (payload.userid) {
        if (payload.userid === 'undefined') return false
        localStorage.setItem('userid', payload.userid)
        localStorage.setItem('past', Date.now())
      }
      if (payload.phone) {
        localStorage.setItem('phone', payload.phone)
        localStorage.setItem('past', Date.now())
      }
      state.user = Object.assign({}, state.user, payload)
      this.commit('addRChange')
    },
    setPageData(state, payload) {
      state.pageData = Object.assign({}, state.pageData, payload)
      this.commit('addRChange')
    },
    setGlobal(state, global) {
      state.global = global
    },
    setFictitious(state, fictitious) {
      state.fictitious = fictitious
      // 虚商号码管理
    },
    setTempName(state, val) {
      state.tempName = val;
    },
    setTestId(state, val) {
      state.testId = val;
      this.commit('addRChange')
    },
    setTestGroupId(state, val) {
      state.testGroupId = val;
      this.commit('addRChange')
    },
    setTid(state, val) {
      state.tid = val;
      this.commit('addRChange')
    },
    setAnonymousId(state, val) {
      state.anonymousId = val;
      this.commit('addRChange')
    },
    // proChange(state, payload) {
    //   let name = ''
    //   if (payload.spid === "101") {
    //     name = 'sum210705-youku'
    //   } else if (payload.spid === "1018") {
    //     name = 'sum210705-iqy'
    //   }
    //   state.modelName = name
    // },
    addRChange(state) {
      state.addrData.channelKey = state.pageData.channl.channelKey;
      state.addrData.userid = state.user.userid;
      state.addrData.phone = state.user.phone;
      state.addrData.product = state.pageData.mainProduct;
      state.addrData.fromSource = state.fromSource
      state.addrData.testId = state.testId;
      state.addrData.testGroupId = state.testGroupId;
      state.addrData.tid = state.tid;
      state.addrData.anonymousId = state.anonymousId;
    }
  },
  actions: {},
  modules: {
  },
})
