import {
  login,
  logout,
  getInfo,
  loginOss
} from '@/api/user'
import {leads} from '@/api/task/patrol.js'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    role:[],
    roles: [],
    login_id:'',
    department_id:'',
    is_guiji:false,
    is_chuzhang:false,
    is_leader:false,
    is_main_leader:false,
    is_admin:false,
    is_manger:false,
    leads_list:{}
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  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_ROLE: (state, role) => {
    state.role = role
  },
  SET_LOGINID: (state, login_id) => {
    state.login_id = login_id
  },
  SET_DEPARTMENTID: (state, department_id) => {
    state.department_id = department_id
  },
  SET_ISADMIN: (state, is_admin) => {
    state.is_admin = is_admin
  },
  SET_ISGUIJI: (state, is_guiji) => {
    state.is_guiji = is_guiji
  },
  SET_ISCHUZHANG: (state, is_chuzhang) => {
    state.is_chuzhang = is_chuzhang
  },
  SET_ISLEADER: (state, is_leader) => {
    state.is_leader = is_leader
  },
  SET_ISMAINLEADER: (state, is_main_leader) => {
    state.is_main_leader = is_main_leader
  },
  SET_ISMANGER: (state, is_manger) => {
    state.is_manger = is_manger
  },

  SET_LEADSLIST: (state, leads_list) => {
    state.leads_list = leads_list
  },


}

const actions = {
  loginskip({
    commit
  }, userInfo) {
    const {
      token,
      tp,
      loginId
    } = userInfo

    return new Promise((resolve, reject) => {
      loginOss({
        token: token,
        tp: tp,
        loginId: loginId
      }).then(response => {

        //console.log(response)

        const {
          data
        } = response;
        console.log(response.access_token)
        commit('SET_TOKEN', response.access_token)
        setToken(response.access_token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password
      }).then(response => {
        console.log(response)
        const {
          data
        } = response;
        console.log(response.access_token)
        commit('SET_TOKEN', response.access_token)
        setToken(response.access_token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        console.log("response111",response)
        if (!response) {
          reject('身份验证失败请重新登录')
        }
        response["roles"] = ["admin"];
        response["role"] = response["role"];
        response["login_id"] = response['id'];
        response["department_id"] = response['department_id'];
        for(var k of response["role"]){
          if(k.name==='系统管理员'){
            response["is_admin"] = true
          }
          if(k.name==='安监科'){
            response["is_guiji"] = true
          }
          if(k.name==='处领导'){
            response["is_chuzhang"] = true
          }
          if(k.name==='分管领导'){
            response["is_leader"] = true
          }
          if(k.name==='主要领导'){
            response["is_main_leader"] = true
          }
          // 科室负责人
          if(k.name==='科室负责人'){
            response["is_manger"] = true
          }
        }
        const {
          roles,
          role,
          name,
          avatar,
          login_id,
          department_id,
          is_admin,
          is_guiji,
          is_chuzhang,
          is_leader,
          is_main_leader,
          is_manger
        } = response
        commit('SET_ROLES', roles)
        commit('SET_ROLE', role)
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_LOGINID', login_id)
        commit('SET_DEPARTMENTID', department_id)
        commit('SET_ISADMIN', is_admin)
        commit('SET_ISGUIJI', is_guiji)
        commit('SET_ISCHUZHANG', is_chuzhang)
        commit('SET_ISLEADER', is_leader)
        commit('SET_ISMAINLEADER', is_main_leader)
        commit('SET_ISMANGER', is_manger)
        console.log("state",state)

        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  getLeads({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      leads({token:state.token}).then(response => {

        if (!response) {
          reject('身份验证失败请重新登录')
        }
        for(var g of response.guiji){
          if(state.login_id===g.id){
            response["is_guiji"] = true;
          }
        }
        for(var c of response.chuzhang){
          if(state.login_id===c.id){
            response["is_chuzhang"] = true;
          }
        }
        // 是否分管领导
        if(state.login_id==response.guiji_leader.id){
          response["is_leader"] = true
        }
        // 是否处长
        if(state.login_id==response.chuzhang_leader.id){
          response["is_main_leader"] = true
        }
        response["leads_list"] = {
          departmen:response['departmen']
        }
        const {
          is_guiji,
          is_chuzhang,
          is_leader,
          is_main_leader,
          leads_list
        } = response
        commit('SET_ISGUIJI', is_guiji)
        commit('SET_ISCHUZHANG', is_chuzhang)
        commit('SET_ISLEADER', is_leader)
        commit('SET_ISMAINLEADER', is_main_leader)
        commit('SET_LEADSLIST', leads_list)

        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
