import { login, login2, logout, getInfo } from "@/api/login";
import { getToken, setToken, removeToken } from "@/utils/auth";

const user = {
  state: {
    token: getToken(),
    name: "",
    avatar: "",
    roles: [],
    permissions: [],
    //存储用户id
    dept: null,
    //存储路由
    oldRouter: [],
    newRouter: [],
    old1: [],
    new1: []
  },

  mutations: {
    setOld: (state, route) => {
      state.old1 = route
    },
    setNew: (state, route) => {
      state.new1 = route
    },
    getOldRouter: (state, route) => {
      state.oldRouter = route
    },
    getNewRouter: (state, route) => {
      state.newRouter = route
    },
    get_dept: (state, id) => {
      state.dept = id;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions;
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password;
      const code = userInfo.code;
      const uuid = userInfo.uuid;
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid)
          .then((res) => {
            setToken(res.token);
            commit("SET_TOKEN", res.token);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 匿名
    login2({ commit }, userInfo) {
      const username = userInfo.username.trim();
      const password = userInfo.password;
      const code = userInfo.code;
      const uuid = userInfo.uuid;
      return new Promise((resolve, reject) => {
        login2(username, password, code, uuid)
          .then((res) => {
            setToken(res.token);
            commit("SET_TOKEN", res.token);
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo()
          .then((res) => {
            const user = res.user;
            commit("get_dept", user.dept);
            const avatar =
              user.avatar == "" || user.avatar == null
                ? require("@/assets/images/profile.jpg")
                : process.env.VUE_APP_BASE_API + user.avatar;
            if (res.roles && res.roles.length > 0) {
              // 验证返回的roles是否是一个非空数组
              commit("SET_ROLES", res.roles);
              commit("SET_PERMISSIONS", res.permissions);
            } else {
              commit("SET_ROLES", ["ROLE_DEFAULT"]);
            }
            let arr1 = [
              {
                path: "/home",
                name: "首页",
                icon1: require("../../assets/images/index1.png"),
                icon2: require("../../assets/images/index1-change.png"),
              },
              {
                path: "/planManage",
                name: "计划管理",
                icon1: require("../../assets/images/jihua.png"),
                icon2: require("../../assets/images/jihua-change.png"),
              },
              {
                path: "/enforcing",
                name: "执法结果",
                icon1: require("../../assets/images/zhifa.png"),
                icon2: require("../../assets/images/zhifa-change.png"),
              },
              {
                path: "/enterprise",
                name: "企业名录",
                icon1: require("../../assets/images/qiye.png"),
                icon2: require("../../assets/images/qiye-change.png"),
              },
              {
                path: "/focusEnter",
                name: "省重点企业",
                icon1: require("../../assets/images/qiye.png"),
                icon2: require("../../assets/images/qiye-change.png"),
              },
            ]
            let arr2 = []
            let A1 = []
            let A2 = []
            let toRouter = ''
            commit("getOldRouter", arr1)
            arr1.forEach((value) => {
              A1.push(value.path)
            })
            commit("setOld", A1)
            if (res.roles.indexOf('admin') != -1) {
              commit("getNewRouter", arr1)
              commit("setNew", A1)
              toRouter = arr1
            } else {
              //区级和镇级
              if (user.dept.parentId == 100 || user.dept.parentId == 101) {
                arr2 = arr1.filter((value) => {
                  return value.path == "/planManage";
                });
                arr2.forEach((value) => {
                  A2.push(value.path)
                })
                commit("getNewRouter", arr2)
                commit("setNew", A2)
                toRouter = arr2
              } else {
                commit("getNewRouter", arr1)
                commit("setNew", A1)
                toRouter = arr1
              }
            }

            commit("SET_NAME", user.userName);
            commit("SET_AVATAR", avatar);
            resolve(toRouter);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token)
          .then(() => {
            commit("SET_TOKEN", "");
            commit("SET_ROLES", []);
            commit("SET_PERMISSIONS", []);
            removeToken();
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", "");
        removeToken();
        resolve();
      });
    },
  },
};

export default user;
