import { LOGIN,GET_TOKEN,GetInfo,InfoWatch,InfoWatchDay,
  InfoWatchMoth,TeacherHomeworkList } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
import axios from '@/axios'
const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    monitorTeacherList:[],
    dayTeacherList:[],
    mothTeacherList:[],
    TeacherHomeworkList:[]
  },

  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_TEACHER: (state, monitorTeacherList) => {
      state.monitorTeacherList = monitorTeacherList
    },
    SET_TEACHER_DAY: (state, monitorTeacherList) => {
      state.dayTeacherList = dayTeacherList
    },
    SET_TEACHER_MOTH: (state, monitorTeacherList) => {
      state.mothTeacherList = mothTeacherList
    },
    SET_TEACHER_HOMELISt: (state, TeacherHomeworkList) => {
      state.TeacherHomeworkList = TeacherHomeworkList
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
       //获取TOKEN
       GET_TOKEN({ state, commit }, data) {
        console.log(data);
        axios.post(GET_TOKEN, data).then(res => {
          // debugger
          if (res && res.code == '999') {
            commit('SET_TOKEN', data.data);
            sessionStorage.setItem("accessToken",data.data)
          this.$router.push("/home");
          } else {
            this.$router.push("/");
          }
        })
      },
    // 获取用户信息
    GetInfo({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        axios.post(GetInfo, data).then(response => {
          const data = response.data
          commit('SET_ROLES', data.role)
          commit('SET_NAME', data.name)
          commit('SET_AVATAR', data.imgUrl)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 校区管理员数据监控
    InfoWatch({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        axios.post(InfoWatch, data).then(response => {
          const data = response.data
          commit('SET_TEACHER', data);
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
      // 校区管理员当月数据
      InfoWatchMoth({ commit, state }, data) {
        return new Promise((resolve, reject) => {
          axios.post(InfoWatchMoth, data).then(response => {
            const data = response.data
            commit('SET_TEACHER_MOTH', data);
            resolve(response)
          }).catch(error => {
            reject(error)
          })
        })
      },
     // 校区管理员当天数据
     InfoWatchDay({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        axios.post(InfoWatchDay, data).then(response => {
          const data = response.data
          commit('SET_TEACHER_DAY', data);
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
     // 查看老师批改作业列表
     TeacherHomeworkList({ commit, state }, data) {
      return new Promise((resolve, reject) => {
        axios.post(TeacherHomeworkList, data).then(response => {
          const data = response.data
          commit('SET_TEACHER_HOMELISt', data);
          debugger
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    // LogOut({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     logout(state.token).then(() => {
    //       commit('SET_TOKEN', '')
    //       commit('SET_ROLES', [])
    //       removeToken()
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

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

export default user
