import request, { getUserId, setCustomer, setDefaultCurrency, getCustomer } from "@/utils/request";
import RSAUtils from '@/utils/RSAUtils.js';
import Cookies from 'js-cookie';

export default {
  namespace: "login",

  state: {
    loginResult: {},
    updateResult: {}
  },

  effects: {
    *login({ payload }, { call, put }) {
      const random = Math.random();
      const res = yield call(() => {
        return request('/wbalone/open/getEncryptParam', {
          method: "GET"
        });
      });
      const { username, password, autoLogin, USE_LOCALPASS_FLAG } = payload;
      var DEdata = {};
      DEdata.text = password || '';
      DEdata.exponent = res.data.exponent || '';
      DEdata.modulus = res.data.modulus || '';
      let pwd = localStorage.getItem("lastLoginPassword");
      if (!(pwd && password === USE_LOCALPASS_FLAG)) {
        pwd = RSAUtils.encryptedString(DEdata);
      }
      const response = yield call(() => {
        return request("/wbalone/account/login", {
          method: "POST",
          body: {
            username,
            password: pwd
          }
        });
      });
      if (response && response.status === "1") {
        // 是否自动登录
        localStorage.setItem("lastLoginIsRemember", autoLogin);
        localStorage.setItem("lastLoginName", username);
        if (autoLogin) {
          localStorage.setItem("lastLoginPassword", pwd);
        } else {
          localStorage.removeItem("lastLoginPassword");
        }
        // 登录成功，获取客户身份、默认币种
        const customer = yield call(() => {
          return request('/occ-base/base/channel/customers/find-by-userId', {
            method: "GET",
            body: {
              userId: getUserId()
            }
          });
        });
        setCustomer(JSON.stringify(customer));
        // 获取系统默认币种
        const currency = yield call(() => {
          return request('/buyer/order-common-info/default-currency', {
            method: "GET"
          });
        });
        setDefaultCurrency(JSON.stringify(currency));
      }
      yield put({
        type: "loginResult",
        loginResult: response
      });
    },
    // 修改密码
    *updatePassword({ payload }, { call, put }) {
      const { password } = payload;
      const res = yield call(() => {
        return request('/wbalone/open/getEncryptParam', {
          method: "GET"
        });
      });
      var DEdata = {};
      DEdata.text = password || '';
      DEdata.exponent = res.data.exponent || '';
      DEdata.modulus = res.data.modulus || '';
      const newPassword =  RSAUtils.encryptedString(DEdata);
      const response = yield call(() => {
        return request("/wbalone/open/modifyPassword", {
          method: "POST",
          body: {
            newPassword: newPassword,
            identification: getUserId()
          }
        });
      });
      console.log(response);
      if (response && response.status === 1) {
        // 修改密码成功，如无客户信息，重新获取客户
        if (!getCustomer().id) {
          // 登录成功，获取客户身份、默认币种
          const customer = yield call(() => {
            return request('/occ-base/base/channel/customers/find-by-userId', {
              method: "GET",
              body: {
                userId: getUserId()
              }
            });
          });
          setCustomer(JSON.stringify(customer));
          // 获取系统默认币种
          const currency = yield call(() => {
            return request('/buyer/order-common-info/default-currency', {
              method: "GET"
            });
          });
          setDefaultCurrency(JSON.stringify(currency));
        }
      }
      yield put({
        type: "updateResult",
        updateResult: response
      });
    },
  },
  reducers: {
    loginResult(state, { loginResult }) {
      return {
        ...state,
        loginResult
      };
    },
    updateResult(state, payload) {
      return {
        ...state,
        updateResult: payload.updateResult
      };
    }
  }
};
