<template>
  <div class="flow_region">
    <div class="flow_left" v-if="sourceType == '1'">
      <div class="flow_left_item">
        <el-scrollbar style="height: 100%">
          <div class="nodes-wrap">
            <div class="left-tab-container">
              <div class="primary-tabs">
                <div
                  v-for="(tab, index) in nodeTypeList"
                  @click="activePrimary = index; activeSecondary = 0"
                  :class="{ 'active': activePrimary === index }"
                  :key="tab.name"
                >
                  {{tab.name}}
                </div>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <div class="flow_left_item">
        <el-scrollbar style="height: 100%">
          <div class="nodes-wrap">
            <div class="left-tab-container">
              <div class="secondary-tabs" v-if="nodeTypeList!= null && nodeTypeList.length > 0">
                <div
                  v-for="(subTab, subIndex) in nodeTypeList[activePrimary].children"
                  @click="activeSecondary = subIndex"
                  :class="{ 'active': activeSecondary === subIndex }"
                  :key="subTab.typeId"
                  :draggable="true"
                  @dragstart="drag($event, {nodeName: subTab.name, workTypeId: subTab.typeId, type: subTab.type})"
                >
                  {{subTab.name}}
                </div>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </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"
          :isEdit="isEdit"
          :selectOperator="selectOperator"
          :sourceType="sourceType"
          @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 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
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    // 节点类型
    nodeTypeList: {
      type: Array,
      default: () => []
    },
    sourceType: {
      type: String,
      default: ''
    }
  },
  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
      },
      activePrimary: 0,       // 当前激活的一级标签索引
      activeSecondary: 0      // 当前激活的二级标签索引
    }
  },
  watch: {
    nodeData (val) {
      console.log('nodeData watch', val)
      this.initNode()
      this.fixNodesPosition()
      this.$nextTick(() => {
        this.jsPlumb.deleteEveryConnection()
        // this.jsPlumb.deleteEveryEndpoint();
        // this.deleteAllNode();
        this.init()
      })
    },
    disabled (val) {
    },
    isEdit (val) {},
    nodeTypeList (val) {
      console.log('watch', val)
    }
  },
  mounted () {
    // console.log('mounted')
    this.jsPlumb = jsPlumb.getInstance()
    this.initNodeTypeObj()
    this.initNode()
    this.fixNodesPosition()
    this.$nextTick(() => {
      this.init()
    })
    // window.addEventListener('scroll', this.init)
  },
  methods: {
    ...methods,
    // 重置
    resetNodeData () {
      this.data = {
        nodeList: [],
        lineList: []
      }
    },
    initNodeTypeObj () {
      this.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
    },
    dataChange () {
      // 通知上层数据变更
      this.$emit('dataChange')
    }
  }
}
</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 {
    display: flex;
  }

  .flow_left_item {
    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: 100px;
    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;
  }
}

/* 全局或组件内样式 */
.el-scrollbar__wrap {
  overflow-x: hidden !important; /* 隐藏横向滚动条 */
}

.left-tab-container{
  display: flex;
}

/* 左侧列：固定宽度，垂直排列 */
.primary-tabs {
  width: 100%;
  display: flex;
  flex-direction: column; /* 垂直布局 */
  border-right: 1px solid #ddd;
}
.primary-tabs > div {
  padding: 12px;
  // cursor: pointer;
  margin: 2px 2px;
  border-radius: 3px;
}
.primary-tabs > div.active {
  color: #ffffff;
  background: #f78989 !important;
}

/* 二级标签：水平排列 */
.secondary-tabs {
  display: flex;
  flex: 1;
  flex-direction: column;
}
.secondary-tabs > div {
  padding: 12px;
  cursor: pointer;
  margin: 2px 2px;
  background: #85ce61;
  border-radius: 3px;
}
.secondary-tabs > div.active {
  color: #ffffff;
  background: #66b1ff !important;
}
</style>
