<template>
  <div class="flow_region">
    <div class="flow_left">
      <div class="help">
        <el-tooltip placement="bottom-start">
          <div slot="content">
            <ul>
              <li>
                <span>新增节点：</span
                ><span>从左侧拖动相应节点放到右侧画布中</span>
              </li>
              <li><span>编辑节点：</span><span>双击节点弹出编辑界面</span></li>
              <li>
                <span>删除节点：</span><span>鼠标右键节点弹出删除菜单</span>
              </li>
              <li>
                <span>删除连线：</span
                ><span
                  >鼠标移动到连线处，当连线变成红色时鼠标左键双击弹出删除界面</span
                >
              </li>
            </ul>
          </div>
          <el-button style="font-size: 10px; padding: 5px">操作指南</el-button>
        </el-tooltip>
      </div>
      <el-scrollbar style="height: 100%">
        <div class="nodes-wrap">
          <div
            v-for="item in nodeTypeList"
            :key="item.type"
            :class="nodeDisabled(item) ? 'node node-disabled' : 'node'"
            :draggable="!nodeDisabled(item)"
            @dragstart="drag($event, item)"
          >
            <div class="log">
              <img :src="item.logImg" alt="" />
            </div>
            <div class="name">{{ item.typeName }}</div>
          </div>
        </div>
      </el-scrollbar>
      <!-- <div class="flow_operation">
        <el-button type="primary" @click="saveFlow" :disabled="disabled">保存</el-button>
      </div> -->
    </div>
    <div
      id="flowWrap"
      ref="flowWrap"
      class="flow-wrap"
      @drop="drop($event)"
      @dragover="allowDrop($event)"
    >
      <div id="flow">
        <div
          v-show="auxiliaryLine.isShowXLine"
          class="auxiliary-line-x"
          :style="{
            width: auxiliaryLinePos.width,
            top: auxiliaryLinePos.y + 'px',
            left: auxiliaryLinePos.offsetX + 'px'
          }"
        ></div>
        <div
          v-show="auxiliaryLine.isShowYLine"
          class="auxiliary-line-y"
          :style="{
            height: auxiliaryLinePos.height,
            left: auxiliaryLinePos.x + 'px',
            top: auxiliaryLinePos.offsetY + 'px'
          }"
        ></div>
        <flowNode
          v-for="item in data.nodeList"
          :id="item.id"
          :key="item.id"
          :node="item"
          :disabled="disabled"
          :selectOperator="selectOperator"
          @setNode="setNode"
          @deleteNode="deleteNode"
          @changeLineState="changeLineState"
        ></flowNode>
      </div>
    </div>
  </div>
</template>

<script>
import cloneDeep from 'lodash/cloneDeep'
import { jsPlumb } from 'jsplumb'
import { nodeTypeList } from './config/init'
import {
  jsplumbSetting,
  jsplumbConnectOptions,
  jsplumbSourceOptions,
  jsplumbTargetOptions
} from './config/commonConfig'
import methods from './config/methods'
// import data from "./config/data.json";
import flowNode from './node-item'
export default {
  name: 'FlowEdit',
  components: {
    flowNode
  },
  props: {
    // 节点数据源
    nodeData: {
      type: Object,
      default: {
        nodeList: [],
        lineList: []
      }
    },
    // 是否仅查看
    disabled: {
      type: Boolean,
      default: false
    },
    // 是否选择操作人，当选择操作人时，其他字段不可编辑
    selectOperator: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      jsPlumb: null,
      currentItem: null,
      nodeTypeList: nodeTypeList,
      nodeTypeObj: {},
      data: {
        nodeList: [],
        lineList: []
      },
      selectedList: [],
      jsplumbSetting: jsplumbSetting,
      jsplumbConnectOptions: jsplumbConnectOptions,
      jsplumbSourceOptions: jsplumbSourceOptions,
      jsplumbTargetOptions: jsplumbTargetOptions,
      auxiliaryLine: { isShowXLine: false, isShowYLine: false }, // 对齐辅助线是否显示
      auxiliaryLinePos: {
        width: '100%',
        height: '100%',
        offsetX: 0,
        offsetY: 0,
        x: 20,
        y: 20
      },
      commonGrid: [5, 5], // 节点移动最小距离
      selectModuleFlag: false, // 多选标识
      rectAngle: {
        px: '', // 多选框绘制时的起始点横坐标
        py: '', // 多选框绘制时的起始点纵坐标
        left: 0,
        top: 0,
        height: 0,
        width: 0
      }
    }
  },
  watch: {
    nodeData (val) {
      this.initNode()
      this.fixNodesPosition()
      this.$nextTick(() => {
        this.jsPlumb.deleteEveryConnection()
        // this.jsPlumb.deleteEveryEndpoint();
        // this.deleteAllNode();
        this.init()
      })
    },
    disabled (val) {
    }
  },
  mounted () {
    // console.log('mounted')
    this.jsPlumb = jsPlumb.getInstance()
    this.initNodeTypeObj()
    this.initNode()
    this.fixNodesPosition()
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    ...methods,
    // 重置
    resetNodeData () {
      this.data = {
        nodeList: [],
        lineList: []
      }
    },
    initNodeTypeObj () {
      nodeTypeList.map(v => {
        this.nodeTypeObj[v.type] = v
      })
    },
    initNode () {
      this.resetNodeData()

      let tempData = cloneDeep(this.nodeData)

      this.data.lineList.push(...tempData.lineList)
      this.data.nodeList = []
      tempData.nodeList.map(v => {
        v.logImg = this.nodeTypeObj[v.type].logImg
        v.log_bg_color = this.nodeTypeObj[v.type].log_bg_color
        this.data.nodeList.push(v)
      })

      // this.$nextTick(() => {
      //   this.init()
      // })
    },
    nodeDisabled (node) {
      return (
        this.disabled ||
        this.selectOperator ||
        (this.data.nodeList.findIndex(t => t.type === 'start') > -1 &&
          node.type === 'start') ||
        (this.data.nodeList.findIndex(t => t.type === 'end') > -1 &&
          node.type === 'end') ||
        false
      )
    },
    // 保存流程图
    saveFlow () {
       // 去除流程图背景图片logImg和背景颜色字段log_bg_color
      // let data = JSON.parse(JSON.stringify(this.data))
      // data.nodeList = data.nodeList.map(item => {
      //   delete item.logImg
      //   delete item.log_bg_color
      //   return item
      // })
      // this.$emit('saveWorkFlow', data)
    },
    getFlowData () {
      let data = JSON.parse(JSON.stringify(this.data))
      data.nodeList = data.nodeList.map(item => {
        delete item.logImg
        delete item.log_bg_color
        return item
      })
      return data
    }
  }
}
</script>

<style lang="less" scoped>
.flow_region {
  display: flex;
  width: 90%;
  height: 90%;
  margin: 20px auto;
  border: 1px solid #ccc;
  position: relative;
  text-align: center;

  .flow_left {
    border-right: 1px solid #ccc;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .help {
      position: absolute;
      left: 150px;
      z-index: 99;
    }
  }

  .nodes-wrap {
    width: 150px;
    height: 90%;
    // border-right: 1px solid #ccc;
    .node {
      display: flex;
      height: 40px;
      width: 80%;
      margin: 5px auto;
      border: 1px solid #ccc;
      line-height: 40px;
      &:hover {
        cursor: grab;
      }
      &:active {
        cursor: grabbing;
      }
      .log {
        width: 40px;
        height: 40px;
      }
      .name {
        width: 0;
        flex-grow: 1;
      }
    }

    .node-disabled {
      &:hover {
        cursor: not-allowed;
      }
      &:active {
        cursor: not-allowed;
      }
    }
  }

  .flow_operation {
    // height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: 1px solid #ccc;
    padding: 10px 0;

    .el-button {
      width: 80%;
    }
  }

  .flow-wrap {
    height: 100%;
    position: relative;
    overflow: hidden;
    outline: none !important;
    flex-grow: 1;
    background-image: url("../../assets/point.png");
    #flow {
      position: relative;
      width: 100%;
      height: 100%;
      .auxiliary-line-x {
        position: absolute;
        border: 0.5px dashed #2ab1e8;
        z-index: 9999;
      }
      .auxiliary-line-y {
        position: absolute;
        border: 0.5px dashed #2ab1e8;
        z-index: 9999;
      }
    }
  }
}
::v-deep.el-scrollbar .el-scrollbar__wrap {
  overflow-x: hidden;
}
</style>
<style lang="less">
.jtk-connector.active {
  z-index: 9999;
  path {
    stroke: #150042;
    stroke-width: 1.5;
    animation: ring;
    animation-duration: 3s;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
    stroke-dasharray: 5;
  }
}
@keyframes ring {
  from {
    stroke-dashoffset: 50;
  }
  to {
    stroke-dashoffset: 0;
  }
}
</style>
