import http from '@/utils/http'
import { AppDispatch } from '..'

/**
 * 获取用户列表信息
 */
export const getUserListAPI = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    // 获取列表数据
    const res: any = await http.post('sys/user/list', data)
    const list = res.data.records
    const obj = {
      list,
      total: res.data.total
    }
    dispatch({ type: 'system/getTableList', payload: obj })
  }
}

/**
 * 获取系统日志列表
 */
export const getLogListAPI = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    // 获取列表数据
    const res: any = await http.post('sys/log/list', data)
    const list = res.data.records
    const obj = {
      list,
      total: res.data.total
    }
    dispatch({ type: 'system/getTableList', payload: obj })
  }
}

/**
 * 获取角色管理列表
 */
export const getRoleListAPI = (data: any): any => {
  return async (dispatch: AppDispatch) => {
    // 获取列表数据
    const res: any = await http.post('sys/role/listCountPage', data)
    const list = res.data.records
    const obj = {
      list,
      total: res.data.total
    }
    dispatch({ type: 'system/getTableList', payload: obj })
  }
}

/**
 * 根据id获取角色详情
 */
export const getRoleInfoAPI = (id: any) => {
  return http.get(`sys/role/detail/${id}`)
}

/**
 * 新增角色或者修改角色
 */
export const addRoleAPI = (data: any) => {
  return http.post('sys/role/save', data)
}

/**
 * 删除角色
 */
export const delRoleAPI = (id: any) => {
  return http.get(`sys/role/remove/${id}`)
}

/**
 * 角色的启用和停用
 */
export const roleEditStatusAPI = (id: any, isDisable: any) => {
  return http.get(`sys/role/editStatus/${id}/${isDisable}`)
}

/**
 * 所有角色的下拉列表
 */
export const userGetRoleAPI = (): any => {
  return async (dispatch: AppDispatch) => {
    // 获取列表数据
    const res: any = await http.get('sys/user/getRole')
    dispatch({ type: 'system/getRoleSelect', payload: res.data })
  }
}

/**
 * 新增用户或者修改用户
 */
export const addUserAPI = (data: any) => {
  return http.post('sys/user/save', data)
}

/**
 * 通过id获取用户详情
 */
export const getUserInfoAPI = (id: number) => {
  return http.get(`sys/user/detail/${id}`)
}

/**
 * 通过id删除用户
 */
export const delUserAPI = (ids: number) => {
  return http.get(`sys/user/removes/${ids}`)
}

/**
 * 通过id重置用户密码
 */
export const userResetPassAPI = (id: number) => {
  return http.get(`sys/user/resetPass/${id}`)
}

/**
 * 用户的启用和停用
 */
export const userEditStatusAPI = (id: any, isDisable: any) => {
  return http.get(`sys/user/editStatus/${id}/${isDisable}`)
}
