import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getReviewNumber,getApprovalNumber,getInvestigateNumber,getDecisionNumber } from '@/api/invest/pool'
import { getMyTaskInvestOppNumber } from '@/api/project/investOpp/pool';
import { getMyTaskApprovalNumber } from '@/api/project/approval/pool';
import { getMyTaskInvestigateNumber } from '@/api/project/investigate/pool';
import { getMyTaskDecisionNumber } from '@/api/project/decision/pool';
import { getMyTaskTerminationNumber } from "@/api/project/termination/pool";

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    user: {},
    dept: {},
    dhrmCompany: {},
    projectItemMessage: {},
    tProjectScoring: {},
    // 各项代办数量
    inspectNum: 0,//评估考察
    projectLXNum: 0,//项目立项
    dueNum: 0,//尽职背调
    projectTJNum: 0,//项目投决
    projectState: [
      [
        {
          dictValue: "a",
          dictLabel: "项目报备完成",
        },
        {
          dictValue: "1",
          dictLabel: "暂存",
        },
      ],
      [
        {
          dictValue: "b",
          dictLabel: "评估考察中",
        },
      ],
      [
        {
          dictValue: "c",
          dictLabel: "准备立项材料",
        },
        {
          dictValue: "d",
          dictLabel: "项目立项",
        },
        {
          dictValue: "e",
          dictLabel: "立项通过",
        },
      ],
      [
        {
          dictValue: "f",
          dictLabel: "尽职背调",
        },
        {
          dictValue: "g",
          dictLabel: "尽职背调通过",
        },
      ],
      [
        {
          dictValue: "h",
          dictLabel: "准备项目投决材料",
        },
        {
          dictValue: "i",
          dictLabel: "项目投决通过",
        },
      ],
      [
        {
          dictValue: "j",
          dictLabel: "签约协议沟通中",
        },
        {
          dictValue: "k",
          dictLabel: "协议签约完成",
        },
      ],
      [
        {
          dictValue: "l",
          dictLabel: "投后管理",
        },
        {
          dictValue: "m",
          dictLabel: "准备投后退出材料",
        },
        {
          dictValue: "n",
          dictLabel: "项目退出完成",
        },
      ],
    ],
    // 我的任务-各项代办数量
    investOppNum: 0,//项目机会
    approvalNum: 0,//项目立项
    investigateNum: 0,//项目背调
    decisionNum: 0,//项目投决
    terminationNum: 0,//项目终止

  },

  mutations: {
    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
    },
    SET_USER: (state, user) => {
      state.user = user
    },
    SET_DEPT: (state, dept) => {
      state.dept = dept
    },
    SET_DHRMCOMPANY: (state, dhrmCompany) => {
      state.dhrmCompany = dhrmCompany
    },
    SET_PROJECTITEMMESSAGE: (state, projectItemMessage) => {
      state.projectItemMessage = projectItemMessage
    },
    SET_TPROJECTSCORING: (state, tProjectScoring) => {
      state.tProjectScoring = tProjectScoring
    },
    SET_INSPECTNUM: (state, inspectNum) => {
      state.inspectNum = inspectNum
    },
    SET_PROJECTLXNUM: (state, projectLXNum) => {
      state.projectLXNum = projectLXNum
    },
    SET_DUENUM: (state, dueNum) => {
      state.dueNum = dueNum
    },
    SET_PROJECTTJNUM: (state, projectTJNum) => {
      state.projectTJNum = projectTJNum
    },
    //我的任务
    SET_INVESTOPPNUM: (state, investOppNum) => {
      state.investOppNum = investOppNum
    },
    SET_APPROVALNUM: (state, approvalNum) => {
      state.approvalNum = approvalNum
    },
    SET_INVESTIGATENUM: (state, investigateNum) => {
      state.investigateNum = investigateNum
    },
    SET_DECISIONNUM: (state, decisionNum) => {
      state.decisionNum = decisionNum
    },
    SET_TERMINATIONNUM: (state, terminationNum ) => {
      state.terminationNum  = terminationNum
    }
  },

  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)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          res.user.userId = res.user.userId.toString();
          const user = res.user;
          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'])
          }
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          commit('SET_USER', user)
          commit('SET_DEPT', user.dept)
          commit('SET_DHRMCOMPANY', res.dhrmCompany)
          resolve(res)
        }).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', [])
          commit('SET_USER', {})
          commit('SET_DEPT', {})
          commit('SET_DHRMCOMPANY', {})
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

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

    // 更新评估考察代办数量
    getReviewNumber({ commit, state }) {
      // this.$store.dispatch("getReviewNumber"); //初始化评估考察代办数量
      return new Promise(resolve => {
        getReviewNumber().then(res => {
          const inspectNum = parseInt(res.data);//评估考察
          commit('SET_INSPECTNUM', inspectNum);
        })
      })
    },
    // 更新项目立项代办数量
    getApprovalNumber({ commit, state }) {
      // this.$store.dispatch("getApprovalNumber"); //初始化项目立项代办数量
      return new Promise(resolve => {
        getApprovalNumber().then(res => {
          const projectLXNum = parseInt(res.data);//项目立项
          commit('SET_PROJECTLXNUM', projectLXNum);

        })
      })
    },
    // 更新尽职背调代办数量
    getInvestigateNumber({ commit, state }) {
      // this.$store.dispatch("getInvestigateNumber"); //初始化尽职背调代办数量
      return new Promise(resolve => {
        getInvestigateNumber().then(res => {
          const dueNum = parseInt(res.data);//尽职背调
          commit('SET_DUENUM', dueNum);
        })
      })
    },
    // 更新项目投决代办数量
    getDecisionNumber({ commit, state }) {
      // this.$store.dispatch("getDecisionNumber"); //初始化项目投决代办数量
      return new Promise(resolve => {
        getDecisionNumber().then(res => {
          const projectTJNum = parseInt(res.data);//项目投决
          commit('SET_PROJECTTJNUM', projectTJNum);
        })
      })
    },
    //我的任务-待办数量
    //更新项目机会待办
    getMyTaskInvestOppNumber({ commit, state }) {
      return new Promise(resolve => {
        getMyTaskInvestOppNumber().then(res => {
          const investOppNum = parseInt(res.total);//项目机会
          commit('SET_INVESTOPPNUM', investOppNum);
        })
      })
    },
    //更新项目立项待办
    getMyTaskApprovalNumber({ commit, state }) {
      return new Promise(resolve => {
        getMyTaskApprovalNumber().then(res => {
          const approvalNum = parseInt(res.total);//项目立项
          commit('SET_APPROVALNUM', approvalNum);
        })
      })
    },
    //更新项目投决待办
    getMyTaskInvestigateNumber({ commit, state }) {
      return new Promise(resolve => {
        getMyTaskInvestigateNumber().then(res => {
          const investigateNum = parseInt(res.total);//项目背调
          commit('SET_INVESTIGATENUM', investigateNum);
        })
      })
    },
    //更新项目投决待办
    getMyTaskDecisionNumber({ commit, state }) {
      return new Promise(resolve => {
        getMyTaskDecisionNumber().then(res => {
          const decisionNum = parseInt(res.total);//项目投决
          commit('SET_DECISIONNUM', decisionNum);
        })
      })
    },

    //更新项目终止待办
    getMyTaskTerminationNumber({ commit, state }) {
      return new Promise(resolve => {
        getMyTaskTerminationNumber().then(res => {
          const terminationNum = parseInt(res.total);//项目终止
          commit('SET_TERMINATIONNUM', terminationNum);
        })
      })
    }
  }
}

export default user
