import { getaddr, userinfo } from '@/api';
import { setLanvuage } from '@/api/base';
import { getValue, saveValue } from '@/utils/common';
import { geti18n } from '@/utils/i18n';
import { isProxy } from 'vue';
import { createStore } from 'vuex'

export default createStore({
  state: {
    userinfo:{
      accesstoken: "",
      apiToken: "",
      birthday: "",
      country: "",
      countryValue: "",
      cover: "",
      code:"",
      createdAt: "",
      currency: "",
      currencyValue: "",
      deletedAt: null,
      desc: "",
      email: "",
      img: "",
      interest: "",
      language: "中文",
      languageValue: "zh",
      live: "",
      livetime: "",
      loginType: 0,
      memberid: 0,
      mobile: "",
      money: "",
      moneyValue: 0,
      msg: "",
      mtongue: "",
      name: "",
      password: "",
      position: 0,
      publish: 0,
      score: "",
      status: false,
      tlanguage: "中文",
      tlanguageValue: 1,
      type: 1,
      uid: "",
      updatedAt: "",
      video: "",
      videoid: 0,
      willsay: [{name: "请选择", level: 0}],
      willsayValue: [{name: "0", level: 0}],
      zoneStr: "中途岛GMT-11:00",
      zoneid: 1,
      symbol: "$",
      zoneValue: "Atlantic/South_Georgia",
      currencytag: 0
    },
    islogin: false,
    seting:{
      lowerLimit: "",
      minmoney: 0,
      minwithdraw: "",
      numberCeiling: "",
      sxf: "",
      symbol: "",
      timeCeiling: "",
      timeLowerLimit: "",
      views:0
    },
    qh: {
      code: "",
      ename: "",
      name: "",
    }
  },
  mutations: {
    setseting(state, info){
      console.log(info, 112)
      state.seting = info
    },
    setUserInfo(state, userinfo){
      userinfo.money = userinfo.money.toString()
      state.userinfo = userinfo
      state.islogin = true;
    },
    login(state, islogin: boolean){
      state.islogin = islogin;
    },
    setLanguage(state, data){
      const split = new Date().toString().split(" ");
      const timeZoneFormatted = split[split.length - 2] + " " + split[split.length - 1];
      const lan = getValue("Lanvuage");
      state.qh = data.qh.data;
      if(lan != null && lan){
        // state.userinfo.language = data[1] // English 中文
        // state.userinfo.languageValue = data[0] // 'en' 'zh'
        state.userinfo.zoneStr = timeZoneFormatted;
        state.userinfo.currency = data.hb;
      }else{
        console.log(data, 111)
        state.userinfo.language = data.yyx // English 中文
        state.userinfo.languageValue = data.yy // 'en' 'zh'
        state.userinfo.zoneStr = timeZoneFormatted;
        state.userinfo.currency = data.hb;
        saveValue("Lanvuage", data.yy);
        if( data.yy != geti18n().$s()){
          location.reload()
        }
        // setLanvuage(data[0]);
      }
    
    },
    setquhao(state, data){
      state.qh = data.qh.data;
    },
    setWlan(state){
      const lan = getValue("Lanvuage");
      if(lan != null && lan){
        state.userinfo.languageValue = !getValue("Lanvuage") ? 'zh' : getValue("Lanvuage");
        state.userinfo.language = (!getValue("Lanvuage") ? 'zh' : getValue("Lanvuage")) == 'zh' ? '中文' : 'English';
      }
    }
  },
  actions: {
    async setUserInfo({ commit }){
      const user = await userinfo();
      if(user != '未登录'){
        saveValue("Lanvuage", user.languageValue);
        commit('setUserInfo', user);
      } else {
        const info = await getaddr();
        console.log(info,11256445)
        commit("setLanguage", info)
      }
    },
    async getip({ commit }){
      const info = await getaddr();
      console.log(info,11256445);
      commit("setLanguage", info)
    },
    async getcode({ commit }){
      const info = await getaddr();
      console.log(info,11256445);
      commit("setquhao", info)
    }
  },
  modules: {
  }
})
