// import {initWf} from '../../../components/wf/wfUtil'
// import {getReportGlobalMap} from "../../../components/xform/utils/util";
import {decrypt} from "@base/utils/aes";


let modules = {};
modules = {
  methods: {
    /*formScriptHttp(opts){
      return this.formHttp({
        ...opts
      });
    },*/
    handleDecryptData(data) {
      if (data) {
        if (Array.isArray(data)) {
          data.forEach(item => {
            this.handleDecryptData(item)
          })
        } else if (Object.prototype.toString.call(data) === "[object Object]") {
          Object.keys(data).forEach(key => {

            let value = data[key];
            if (value !== null && value !== undefined) {
              if (Object.prototype.toString.call(data[key]) === "[object String]") {
                let str = "ATEN*-";
                if (value.startsWith(str)) {
                  data[key] = decrypt(value.slice(str.length));
                }
              } else {
                this.handleDecryptData(data[key])
              }
            }
          })
        } else if (Object.prototype.toString.call(data) === "[object String]") {
          let str = "ATEN*-";
          if (data.startsWith(str)) {
            data = decrypt(data.slice(str.length));
          }
        }
      }
      return data;
    },
    getHttpTarget() {
      return this.getFormRef ? this.getFormRef() : this;
    },
    formHttp(opts) {
      let data = opts.data;
      let scriptCode = opts.scriptCode ? opts.scriptCode.trim() : null;
      let formCode = opts.formCode ? opts.formCode.trim() : null;
      let isLoading = opts.isLoading ?? true;
      let success = opts.success;
      let fail = opts.fail;
      let error = opts.error;

      let that = this;
      // let reportTemplate = that.getFormRef ? that.getFormRef().reportTemplate : that.reportTemplate;
      if (scriptCode && !formCode) {
        let arr = scriptCode.split("/").filter(item => item)
        if (arr.length > 1) {
          formCode = arr[0];
          scriptCode = arr[1];
        }
      }


      delete opts.data
      delete opts.scriptCode
      delete opts.formCode
      delete opts.isLoading
      delete opts.success
      delete opts.fail
      delete opts.error


      let getReqUrl = (callback) => {

        if (!opts.url) {
          this.getHttpTarget().$http({
            aes: true,
            url: USER_PREFIX + `/formScript/getServiceName`,
            method: `post`,
            data: {
              formCode,
              scriptCode
            },
            isLoading,
            // loadingTarget: this.getLoadingTarget(),
            // modalStrictly: true,
            success: res => {
              let serviceName = res.objx;
              if (serviceName) {
                let url = `/${searviceName}/bd_api/${formCode}/${scriptCode}`;
                callback(url)
              } else {
                this.$baseAlert("服务名不存在")
              }
            }
          });
        } else {
          callback(opts.url)
        }

      }
      let toDo = () => {
        return new Promise((resolve, reject) => {
          getReqUrl(url => {
            let reqData;
            reqData = data;

            let reportGlobalMap = {};
            // let url = `/${searviceName}/bd_api/${formCode}/${scriptCode}`;
            // let opts.vue
            return opts.vue.$http({
              // aes: true,
              url: url,
              method: `post`,
              // loadingTarget: this.getLoadingTarget(),
              isLoading,
              ...opts,
              data: {
                // scriptCode: scriptCode,
                fixeData: reportGlobalMap,
                ...reqData
              },
              success: res => {
                if (res.objx) {
                  res.objx = this.handleDecryptData(res.objx);
                }
                if (opts.successMsg) {
                  this.$message({
                    message: res.content,
                    type: 'success',
                    duration: 1000
                  });
                }
                success && success(res);
                resolve(res);
              },
              fail: res => {
                fail && fail(res);
                resolve(res);
              },
              error: e => {
                error && error(e);
                reject(e);
              }
            });
          })
        })
      }
      if (opts.isConfirm) {
        this.$baseConfirm(opts.confirmText).then(() => {
          return toDo();
        });
      } else {
        return toDo();
      }
    }
  }
}
export default modules;
