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

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

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

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

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

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

/**
 * 附件列表
 * @param params
 * @returns {Promise | Promise<unknown>}
 * @private
 */
export const _selectAttachPage = (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(url, params) {
  return new Promise(function (resolve, reject) {
    HttpService.post(url, params || {}).then(res => {
      localStorage.removeItem(process.env.VUE_APP_TOKEN_KEY);
      localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY);
      localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY);
      localStorage.removeItem(process.env.VUE_APP_USER_ACCESS_KEY);
      localStorage.removeItem(process.env.VUE_APP_ROLES_KEY);
      localStorage.removeItem(process.env.VUE_APP_USER_KEY);
      removeAuthorization(AUTH_TYPE.BASIC);
      Session.clear();
      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: _deleteAttachFile,
  download: _download_file,
  attach_page: _selectAttachPage,
  query_constants: _query_constants,
  downloadCode: _download_code,
  orgUnitTreeList: orgUnitTreeList,
  selectMenuTreeList: selectMenuTreeList,
  autoLogin: _auto_login
}
