import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { isHttp, isEmpty } from "@/utils/validate"
import defAva from '@/assets/images/profile.jpg'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
	teacherClass:[],
	rolesName:[],
	dept:[],
	schoolId:0,
	nickName:'',
	userId:0,
	tenantId:0,
  userType:''

  },

  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_TEACHER_CLASS: (state, teacherClass) => {
		  state.teacherClass = teacherClass
		},
	SET_ROLES_NAME: (state, rolesName) => {
	  state.rolesName = rolesName
	},
	SET_DEPT: (state, dept) => {
	  state.dept = dept
	},
	SET_SCHOOL_ID: (state, schoolId) => {
	  state.schoolId = schoolId
	},
	SET_NIKE_NAME: (state, nickName) => {
	  state.nickName = nickName
	},
	SET_USERID: (state, userId) => {
	  state.userId = userId
	},
	SET_TENANTID: (state, tenantId) => {
	  state.tenantId = tenantId
	},
  SET_USERTYPE: (state, userType) => {
    state.userType = userType
  },





  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
	  const tenantId = userInfo.tenantId
    const grantType = userInfo.grantType
    const phonenumber = userInfo.phonenumber
    const smsCode = userInfo.smsCode
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid,tenantId,grantType,phonenumber,smsCode).then(res => {
          setToken(res.data.access_token)
          commit('SET_TOKEN', res.data.access_token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
			console.log(res.data.permissions,4)
          const user = res.data.user
          let avatar = user.avatar || ""
          if (!isHttp(avatar)) {
            avatar = (isEmpty(avatar)) ? defAva : process.env.VUE_APP_BASE_API + avatar
          }
          // const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
		  console.log(res.data.permissions,4)
            commit('SET_ROLES', res.data.roles)
            commit('SET_PERMISSIONS', res.data.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
		  commit('SET_NAME', user.userName)
          commit('SET_TEACHER_CLASS', res.data.teacherClass)
		  commit('SET_ROLES_NAME', user.roles)
		  commit('SET_DEPT', user.dept)
		 commit('SET_SCHOOL_ID', res.data.schoolId)
		 commit('SET_NIKE_NAME', user.nickName)
		 commit('SET_USERID', user.userId)
     commit('SET_USERTYPE', user.userId)
     commit('SET_AVATAR', avatar)
		 commit('SET_TENANTID', user.tenantId)

          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', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

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

export default user
