<template>
  <Node
    :title="config.name"
    :show-error="showError"
    :content="content"
    :error-info="errorInfo"
    placeholder="请设置触发器"
    header-bgc="#47bc82"
    header-icon="x-icon-trigger"
    @insertNode="handleInsertNode"
  />
</template>

<script>
import Node from '@views/workFlow/designer/common/process/nodes/Node.vue'
//触发器
export default {
  name: 'TriggerNode',
  components: { Node },
  props: {
    config: {
      type: Object,
      default: function () {
        return {};
      }
    }
  },
  data() {
    return {
      showError: false,
      errorInfo: ''
    };
  },
  computed: {
    content() {
      return JSON.stringify(this.config);
    }
  },
  methods: {
    isNotEmpty(obj) {
      return obj !== undefined && obj !== null && obj !== '' && obj !== 'null';
    },
    // 校验数据配置的合法性
    validate(err) {
      this.showError = false;

      if (this.config.props && this.config.props.type === 'WEBHOOK') {
        if (this.isNotEmpty(this.config.props.http && this.config.props.http.url)) {
          this.showError = false;
        } else {
          this.showError = true;
          this.errorInfo = '请设置WEBHOOK的URL地址';
        }
      } else if (this.config.props && this.config.props.type === 'EMAIL') {
        const email = this.config.props.email || {};
        if (
          !this.isNotEmpty(email.subject) ||
          !(Array.isArray(email.to) && email.to.length > 0) ||
          !this.isNotEmpty(email.content)
        ) {
          this.showError = true;
          this.errorInfo = '请设置邮件发送配置';
        } else {
          this.showError = false;
        }
      }

      if (this.showError) {
        err.push(`${this.config.name} 触发动作未设置完善`);
      }
      return !this.showError;
    },
    handleInsertNode(type) {
      this.$emit('insertNode', type)
    }
  }
};
</script>

<style scoped></style>
