<template>
  <Node
    :title="config.name"
    :show-error="showError"
    :content="content"
    :error-info="errorInfo"
    placeholder="请设置延时时间"
    header-bgc="#f25643"
    header-icon="x-icon-clock"
    @selected="$emit('selected')"
    @del-node="$emit('delNode')"
    @insertNode="onInsertNode"
  />
</template>

<script>
import Node from './Node.vue';
//延迟节点
export default {
  name: 'DelayNode',
  components: {
    Node
  },
  props: {
    config: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      showError: false,
      errorInfo: ''
    };
  },
  computed: {
    content() {
      const props = this.config.props || {};
      if (props.type === 'FIXED') {
        return `等待 ${props.time} ${this.getName(props.unit)}`;
      } else if (props.type === 'AUTO') {
        return `至当天 ${props.dateTime}`;
      }
      return '-';
    }
  },
  methods: {
    getName(unit) {
      switch (unit) {
        case 'D':
          return '天';
        case 'H':
          return '小时';
        case 'M':
          return '分钟';
        default:
          return '未知';
      }
    },
    // 用于流程图设计器中的合法性校验逻辑
    validate(errList) {
      this.showError = false;
      try {
        const props = this.config.props || {};
        if (props.type === 'AUTO') {
          if ((props.dateTime || '') === '') {
            this.showError = true;
            this.errorInfo = '请选择时间点';
          }
        } else if (props.time <= 0) {
          this.showError = true;
          this.errorInfo = '请设置延时时长';
        }
      } catch (e) {
        this.showError = true;
        this.errorInfo = '配置出现问题';
      }

      if (this.showError) {
        errList.push(`${this.config.name} 未设置延时规则`);
      }
      return !this.showError;
    },
    onInsertNode(type) {
      this.$emit('insertNode', type);
    }
  }
};
</script>

<style scoped></style>
