import Vue from "vue";
import api from "@/api/index.js";
import {Toast} from "vant";
import store from "@/utils/store";
export default {
  namespaced: true,
  state: {
    loginInfo: store("login")
  },
  actions: {
    wxPoneLoginCallBack({ commit, state }, params) {
      
      if(state.loginInfo.phone) {
        return;
      }
      Toast.loading({
		         duration: 0,       // continuous display toast
		         forbidClick: true, // forbid click background
		         loadingType: "spinner",
		         message: "正在加载登录信息..."
		    });
      return api.request("get", api.getURL("system/wxPoneLoginCallBack"), {
        code: params.code,
        state: params.state
      }).then(res => {
        Toast.clear();
        //
        if(res && res.data.code === 10000) {
          commit("INIT_LOGIN_INFO", res.data.data); //初始化登陆信息
        }
        return res;
      });
    },
    wxPoneLoginInfo( { commit, state }, params) {
      
      if(state.loginInfo.phone) {
        return;
      }
      return api.request("get", api.getURL("system/wxPoneLoginInfo")).then(res => {
        //
        if(res && res.data.code === 10000) {
          commit("INIT_LOGIN_INFO", res.data.data); //初始化登陆信息
        }
        return res;
      });
    },
    wxLoginInfo( { commit, state }, code) {
      if(state.loginInfo.phone) {
        return;
      }
      return api.request("get", api.getURL("system/wxLoginInfo"), {
				 code: code
      }).then(res => {
        if(res && res.data.code === 10000) {
          commit("INIT_LOGIN_INFO", res.data.data); //初始化登陆信息
        }
        return res;
      });
    },
    wxLogin( { commit, state }) {
      return api.request("get", api.getURL("system/wxLogin"), {
				 toUrl: "wx/index.html"
      }).then(res => {
        
        return res;
      });
    },
    submitPassword( { commit, state }, params) {
      return api.request("post", api.getURL("customer/updatePasswordthird"), params).then(res => {
        return res;
      });
    },
    submitForgetPhoneCode( { commit, state }, info) {
      //
      return api.request("post", api.getURL("customer/updatePasswordSecond"), info).then(res => {
        return res;
      });
    },
    sendForgetCode( { commit, state }, phone) {
      return api.request("get", api.getURL("customer/updatePasswordSms"), {
				 phone,
      }).then(res => {
        return res;
      });
    },
    sendLogin({ commit, state, dispatch }, loginInfo) {
      // let lastPage = store('lastPage')
      // loginInfo.toUrl = lastPage.slice(lastPage.indexOf('wx')).replace('#/forget', '#/MyPage')
      // store('hasLogout', false)
      // //loginInfo.toUrl = "wx/index.html#/MyPage"
      // return api.request("post", api.getURL("system/wxPhoneLogin"), loginInfo).then(res => {

      // 	if(res) {
      // 		location.href = res.data.data
      // 		// dispatch('wxPoneLoginInfo').then(over => {
      // 		// })
      // 		//commit('INIT_LOGIN_INFO', res.data.data) //初始化登陆信息
      // 	}
      // 	return res
      // })
			
      return api.request("post", api.getURL("system/login"), loginInfo).then(res => {
        if(res) {
          //location.href = res.data.data
          // dispatch('wxPoneLoginInfo').then(over => {
          // })
          store("hasLogout", false);
          commit("INIT_LOGIN_INFO", res.data.data); //初始化登陆信息
        }
        return res;
      });
    },
    sendLogout({ commit, state }) {
      return api
        .request("post", api.getURL("system/logout"))
        .then(res => {
          return res;
        });
    },
    sendRegister({ commit, state }, payload) {
      payload.toUrl = "wx/index.html#/bindHouseCheck";
      return api
        .request("post", api.getURL("customer/register"), payload)
        .then(res => {
          if(res) {
            location.href = res.data.data;
          }
          return res;
        });
    },
    sendVerficationCode( { commit, state }, code) {
      return api.request("get", api.getURL("customer/smsverification"), {
				 mobileNumber: code
      }).then(res => {
        if(res) {
          Toast.success("验证码发送成功");
        }
        return res;
      });
    }
  },
  mutations: {
    INIT_DATA_STATUS(state, data) {

    },
    INIT_LOGIN_INFO(state, data) {
      if(!data.cappUserInfo) {
        data.cappUserInfo = {};
      }
      store("login", data); //设置浏览器缓存 localStorage
      Vue.set(state, "loginInfo", data);
    },
    INIT_LOGIN_NAME_CART(state, data) {
      state.loginInfo = Object.assign({}, state.loginInfo, {
        cardCode: null,
        cappUserInfo: {
				  userName: null
        }
      });
      store("login", state.loginInfo);
    },
    CHANGE_NICK_NAME(state, name) {
      state.loginInfo.nickname = name;
      store("login", state.loginInfo);
    },
    CLEAR_LOGIN_INFO(state) {
      state.loginInfo = {
        cappUserInfo: {

        }
      };
      store("login", {
        cappUserInfo: {
					
        }
      });
    },
    CLEAR_LOGIN_HOUSE_INFO(state) {
      state.loginInfo.cappUserInfo = {};
      store("login", state.loginInfo);
    },
    SET_LOGIN_HOUSE_INFO(state, data) {
      state.loginInfo.cappUserInfo = data;
      store("login", state.loginInfo);
    },
    SET_LOGIN_HEADE_IMG(state, url) {
      state.loginInfo.headImg = url;
      store("login", state.loginInfo);
    },
    updateUserInfo(state, data) {
      state.loginInfo = Object.assign({}, state.loginInfo, data);
      store("login", state.loginInfo);
    }
  },
  getters: {}
};
