Component({
  mixins: [],
  data: {
    params: null,
    eventResult: null
  },
  props: {
    isCumulative: {
      type: Boolean,
      value: true
    },
    number: {
      type: String,
      value: ""
    },
    name: {
      type: String,
      value: "调用事件"
    },
    tips: {
      type: String,
      value: ""
    },
    aromeCode: {
      type: String,
      value: "ampeHHCommunication"
    },
    initParams: {
      type: Object,
      value: {
        action: "xxx",
        event: "xxx",
        taskId: "88888888",
        params: {}
      }
    }
  },
  didMount() {
    this.reset()
  },
  didUpdate() { },
  didUnmount() { },
  methods: {
    doEvent() {
      let that = this;
      let params = null;
      try {
        params = JSON.parse(that.data.params)
      } catch (error) {
        my.showToast({
          type: 'fail',
          content: 'JSON格式有误',
          duration: 1500,
        });
        return;
      }
      console.log(that.props.aromeCode, 55, params)
      if (that.props.aromeCode) {
        my.on(that.props.aromeCode, res => {
          if (that.props.isCumulative) {
            res = (that.data.eventResult || "") + "\n" + JSON.stringify(res)
          } else {
            res = JSON.stringify(res)
          }
          that.setData({
            eventResult: res
          })
        });
      } else {
        my.call("ampeHHCommunication", params, res => {
          console.log('ampeHHCommunication', res)
          if (that.props.isCumulative) {
            res = (that.data.eventResult || "") + "\n" + JSON.stringify(res)
          } else {
            res = JSON.stringify(res)
          }
          console.log(res)
          that.setData({
            eventResult: res
          })
        });
      }
    },
    handleChange(e) {
      let json = e.detail.value
      this.setData({
        params: json
      })
    },
    close() {
      this.setData({
        eventResult: null
      })
    },
    reset() {
      let that = this;
      let params = JSON.parse(JSON.stringify(that.props.initParams))
      this.setData({
        params: JSON.stringify(params),
        eventResult: null
      })
    }
  },
});
