import HttpService from '@/utils/HttpService';
import { AUTH_TYPE, removeAuthorization } from "@/utils/authorization";

import SystemConfig from '@/utils/SystemConfig'

/**
 * 用户登录
 * @param params
 * @param config
 * @returns {Promise | Promise<unknown>}
 */
export const signIn = (params, config) => HttpService.post("/auth/login.json", params, Object.assign({}, { loading: false }, config || {}));

/**
 * 获取菜单信息
 * @param params
 * @returns {Promise | Promise<unknown>}
 */
export const getMenuAdmin = (params) => HttpService.post('/auth/select-auth-menus.json', params, { loading: false });

/**
 * 文件上传
 * @param params
 * @param config 配置
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _upload = (params, config) => HttpService.uploadFile("/api/base/system/attach/upload-file.json", params, Object.assign({}, { loading: true }, config || {}));

/**
 * 删除附件
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _delete = (params) => HttpService.deletes("/api/base/system/attach/delete.json", params);

/**
 * 下载附件
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _download = (params) => HttpService.post("/api/base/system/attach/download.json", params);

/**
 * 附件列表
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _page = (params) => HttpService.page("/api/base/system/attach/page.json", params);

/**
 * 查询常量集合
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _query_constants = (params) => HttpService.post("/api/base/interface/common/constants/value.json", params); // 删除

/**
 * 下载代码
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _download_code = (params) => HttpService.post("/api/base/generation/database/download-code.json", params);

/**
 * 根据组织ID查询单位信息
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const orgUnitTreeList = (params) => HttpService.post("/api/base/organ/consumer/select-orgunit-tree-list.json", params);

/**
 * 根据组织ID查询单位信息
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const selectMenuTreeList = (params) => HttpService.post("/api/organ/consumer/select-menu-tree-list.json", params);

/**
 * 退出登录
 */
export function logout (params) {
  return new Promise(function (resolve, reject) {
    HttpService.post("/auth/logout.json", params || {}).then(res => {
      localStorage.removeItem(SystemConfig.token);
      localStorage.removeItem(SystemConfig.adminUser);
      removeAuthorization(AUTH_TYPE.BASIC);
      resolve(res);
    }).catch(err => {
      reject(err);
    })
  });
}

/**
 * 第三方认证完成自动登录
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _auto_login = (params) => HttpService.get("/api/base/auto-login.json", params);

export default {
  signIn,
  logout,
  uploadFile: _upload,
  delete: _delete,
  download: _download,
  attach_page: _page,
  query_constants: _query_constants,
  downloadCode: _download_code,
  orgUnitTreeList: orgUnitTreeList,
  selectMenuTreeList: selectMenuTreeList,
  autoLogin: _auto_login
}
