import request from '@/utils/request'
import { usingExternalUploadService } from "@/api/system/config";

//查询已上传附件
export const attachmentList = (id) => {
  return request({
    url: `api/home/xixia/asset/zcDetail/${id}`,
    method: 'get'
  })
}

//删除已上传附件
export function systemAttachment (ids) {
  if (ids != undefined) {
    return request({
      url: '/api/system/attachment/remove/' + ids,
      method: 'get'
    })
  }
}


//上传凭证附件
export function voucherUpload (data) {
  return request({
    url: '/api/finance/voucher/upload',
    method: 'post',
    header: { "Content-Type": 'application/x-www-form-urlencoded' },
    data: data
  })
}

//上传全局方法附件

export function commonAttach (data) {
  return request({
    url: '/api/common/attach',
    method: 'post',
    header: { "Content-Type": 'application/x-www-form-urlencoded' },
    data: data
  })
}

// 上传功能对象(外部/内部)
export const extUploader = function () {
  this._remoteHost = null; // 外部上传主机地址
  this._token = null; // 外部上传服务身份标识
  this._supportExternal = null; // 是否支持外部上传
  this._usingExternal = false; // 是否启用外部上传

  // 内部方式上传
  this._Upload_inner = (url, data) => {
    return request({
      url: url,
      method: 'post',
      header: { "Content-Type": 'application/x-www-form-urlencoded' },
      data: data,
    });
  };

  // 外部方式上传
  this._Upload_external = (url, data) => {
    if (data instanceof FormData)
      data.append('token', this._token);
    else
      data.token = this._token;
    return request({
      url: url,
      method: 'post',
      header: { "Content-Type": 'application/x-www-form-urlencoded' },
      data: data,
      baseURL: this._remoteHost,
    });
  };

  // 检查是否启用外部上传
  this._CheckExternalEnabled = () => {
    return request({
      url: '/api/system/config/usingExternalUploadService',
      method: 'get'
    }).then((resp) => {
      if (resp.enabled) {
        this._token = resp.data;
        this._remoteHost = resp.fileServerAddress;
        this._usingExternal = true;
      }
      else {
        this._token = null;
        this._remoteHost = null;
        this._usingExternal = false;
      }
      this._supportExternal = true;
    }).catch(() => {
      this._token = null;
      this._remoteHost = null;
      this._usingExternal = false;
      this._supportExternal = false;
    });
  };

  // 初始化
  this.Init = () => {
    return this._CheckExternalEnabled();
  };

  // 反初始化
  this.Deinit = () => {
    this._token = null;
    this._remoteHost = null;
    this._usingExternal = false;
    this._supportExternal = null;
  };

  // 是否初始化
  this.IsInit = () => {
    return this._supportExternal !== null;
  };

  // 当前是否使用外部上传
  this._UsingExternal = () => {
    return this._usingExternal;
  };

  // 获取上传地址
  this.GetUploadUrl = (innerUrl, extUrl) => {
    return this._UsingExternal() ? (this._remoteHost + extUrl) : (process.env.VUE_APP_BASE_API + innerUrl);
  };

  // 处理文件上传(外部调用, 自动初始化)
  this.Process = (innerUrl, extUrl, data) => {
    if (this.IsInit())
      return this.Upload(innerUrl, extUrl, data);
    else {
      return this.Init().then(() => { // 使用外部上传
        return this.Upload(innerUrl, extUrl, data);
      }).catch(() => {
        return this._Upload_inner(innerUrl, data);
      });
    }
  };

  // 文件上传(外部调用, 必须先初始化)
  this.Upload = (innerUrl, extUrl, data) => {
    if (!this.IsInit())
      throw '请先调用Init进行初始化';
    if (this._UsingExternal()) {
      return this._Upload_external(extUrl, data).catch(() => {
        this.ForceDisableExternal();
        return this._Upload_inner(innerUrl, data);
      });
    }
    else
      return this._Upload_inner(innerUrl, data);
  };

  // 强制禁用外部上传(外部上传失败时启用内部上传)
  this.ForceDisableExternal = () => {
    this._usingExternal = false;
  };

  // 获取外部文件服务身份标识
  this.Token = () => {
    return this._token;
  };
};
