<template>
  <div class="node" :class="{ root: isRoot || !show, 'node-error-state': showError }">
    <div
      v-if="show"
      class="node-body"
      :class="{ error: showError }"
      @click="onSelectNode"
    >
      <div>
        <div class="node-body-header" :style="{ backgroundColor: headerBgc }">
          <i
            v-if="headerIcon"
            :class="headerIcon"
            class="x-iconfont inline-block mr-[5px] align-baseline leading-none"
          ></i>
          <a-tooltip :title="title">
            <span class="name">{{ title }}</span>
          </a-tooltip>
          <i
            v-if="!isRoot && !isEnd && diagramMode !== 'viewer'"
            class="x-icon-close x-iconfont float-right"
            @click.stop="$emit('delNode')"
          ></i>
        </div>
        <div class="node-body-content">
          <i v-if="leftIcon" :class="leftIcon" class="x-iconfont"></i>
          <span v-if="!content || content.trim() === ''" class="placeholder">{{ placeholder }}</span>
          <a-tooltip v-else :title="content">
            <span class="ellipsis-text">{{ content }}</span>
          </a-tooltip>
          <i
            v-if="diagramMode !== 'viewer' && !isEnd"
            class="x-icon-arrow-right x-iconfont"
          ></i>
        </div>
        <div v-if="showError" class="node-error">
          <a-tooltip placement="topLeft" :title="errorInfo">
            <i class="x-icon-warning-outline x-iconfont" style="font-size: 24px"></i>
          </a-tooltip>
        </div>
      </div>
    </div>
    <div class="node-footer" v-if="!isEnd">
      <div class="btn">
        <div v-show="diagramMode !== 'viewer' && !isEnd">
          <InsertButton @insertNode="handleInsertNode" />
        </div>
      </div>
    </div>
    <div class="ffffff" v-else>

    </div>
  </div>
</template>

<script>
import InsertButton from '../../InsertButton.vue';

export default {
  name: 'Node',
  props: {
    isRoot: {
      type: Boolean,
      default: false
    },
    isEnd: {
      type: Boolean,
      default: false
    },
    show: {
      type: Boolean,
      default: true
    },
    content: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: '标题'
    },
    placeholder: {
      type: String,
      default: '请设置'
    },
    leftIcon: {
      type: String,
      default: ''
    },
    headerIcon: {
      type: String,
      default: ''
    },
    headerBgc: {
      type: String,
      default: '#576a95'
    },
    showError: {
      type: Boolean,
      default: false
    },
    errorInfo: {
      type: String,
      default: '无信息'
    }
  },
  components: {
    InsertButton
  },
  computed: {
    diagramMode() {
      return this.$store.state.workflow.diagramMode;
    }
  },
  methods: {
    onSelectNode() {
      if(!this.isEnd){
        this.$emit('selected')
      }
    },
    handleInsertNode(type) {
      this.$emit('insertNode', type)
    }
  }
};
</script>

<style scoped lang="less">
.ellipsis-text {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.x-iconfont {
  font-size: 14px;
}
.root {
  &:before {
    display: none !important;
  }
}
.node-error-state {
  .node-body {
    box-shadow: 0 0 5px 0 #f56c6c !important;
  }
}
.node {
  box-sizing: content-box;
  padding: 0 50px;
  width: 220px;
  position: relative;
  &:before {
    content: '';
    position: absolute;
    top: -12px;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    border-style: solid;
    border-width: 8px 6px 4px;
    border-color: #cacaca transparent transparent;
    background: #f5f5f7;
  }

  .node-body {
    cursor: pointer;
    max-height: 120px;
    position: relative;
    border-radius: 5px;
    background-color: white;
    box-shadow: 0 0 5px 0 #d8d8d8;

    &:hover {
      box-shadow: 0 0 3px 0 @primaryColor;

      .node-body-header {
        .x-icon-close {
          display: inline;
          font-size: medium;
        }
      }
    }

    .node-body-header {
      border-top-left-radius: 5px;
      border-top-right-radius: 5px;
      padding: 5px 15px;
      color: white;
      font-size: xx-small;

      .x-icon-close {
        display: none;
      }

      .name {
        height: 14px;
        width: 150px;
        display: inline-block;
      }
    }

    .node-body-content {
      padding: 18px;
      color: #656363;
      font-size: 14px;

      i {
        position: absolute;
        top: 55%;
        right: 5px;
        font-size: medium;
      }

      .placeholder {
        color: #8c8c8c;
      }
    }

    .node-error {
      position: absolute;
      right: -40px;
      top: 20px;
      font-size: 25px;
      color: #f56c6c;
    }
  }

  .node-footer {
    position: relative;

    .btn {
      width: 100%;
      display: flex;
      padding: 20px 0 32px;
      justify-content: center;
    }

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      z-index: -1;
      margin: auto;
      width: 2px;
      height: 100%;
      background-color: #cacaca;
    }
  }
  .ffffff {
    margin-bottom: 30px;
  }
}
</style>
