import {treeScollx} from "../../utils/global";
import settingConfig from "@/settings";

let tmixins = {};
tmixins = {
  props: ['visiable'],
  data() {
    return {
      showDialog: true,
      advancedData: {script: '', json: ''},
      showCodeEditor: false,
      result: '',
      serviceIds: [],
      serviceId: null
    };
  },
  mounted() {
    this.$nextTick(() => {
      treeScollx({
        target: this, type: 'default', nextMin: 200, callback: () => {
          this.$refs.editor1?.aceEditor?.resize();
          this.$refs.editor2?.aceEditor?.resize();
          this.$refs.editor3?.resultEditor?.resize();
        }
      })
      ;
    })
  },
  created() {
    // this.advancedData = Object.assign({}, this.formData);
    this.advancedData.script = this.$attrs.script;
    this.$nextTick(() => {
      this.showCodeEditor = true;
    })
    this.initServiceIds();
  },
  methods: {
    dialogClose(event) {
      this.$emit('update:visiable', false);
    },
    exc() {
      if (!this.serviceId) {
        this.$message({
          message: this.$t2('请选择服务名', 'components.scriptTest.warnMsg1'),
          type: 'error',
          duration: 300,
        });
        return;
      }
      let conditions = this.advancedData.json ? JSON.parse(this.advancedData.json) : {};
      let data = {
        conditions: conditions,
        script: this.advancedData.script
      }
      let aceEditor = this.$refs.resultEditor.aceEditor
      this.result = '';
      aceEditor.setValue('');
      let url = settingConfig.scriptTestUrl || "/script/test";
      this.$http({
        url: '/' + this.serviceId + url,
        method: `post`,
        data: data,
        isLoading: true,
        aes: true,
        success: res => {
          this.result = res.objx || '';
          aceEditor.setValue(this.result);
        }
      });
    },
    initServiceIds() {
      if (this.syncService === false) {
        if (this.serviceId) {
          this.serviceIds = [this.serviceId]
        }
        return;
      } else {
        this.$http({
          url: USER_PREFIX + `/wf_obj_config/wfServiceIds`,
          method: `post`,
          data: {},
          isLoading: true,
          success: res => {
            let serviceIds = res.objx || [];
            if (serviceIds.length) {
              this.serviceId = serviceIds[0];
            }
            this.serviceIds = serviceIds;
          }
        });
      }
    }
  }
};
export const mixins = tmixins
