<script setup name="childFlow">
import { inject } from 'vue'
import store from '@/store'
defineProps({
  nodeData: Array
})
const { filedConditionsOptions } = store.dictStore.globalDicts || {}
// 注入父组件方法
const showApprovalDialog = inject('showApprovalDialog'),
  showConditionDialog = inject('showConditionDialog'),
  showConcurrentDialog = inject('showConcurrentDialog'),
  addNode = inject('addNode'),
  removeNode = inject('removeNode'),
  addCondition = inject('addCondition')
// 添加节点
const add = (arg) => {
    addNode(arg)
  },
  // 删除
  remove = (con_index, item, index, nodeData) => {
    removeNode(con_index, item, index, nodeData)
  },
  // 添加条件
  addCon = (branchs, type) => {
    addCondition(branchs, type)
  },
  // 配置节点
  choseNode = ({ key, type, item }) => {
    type === 'SUB' && showApprovalDialog({ key, type, item })
    type === 'CONDITION' && showConditionDialog({ key, type, item })
    type === 'CONCURRENT' && showConcurrentDialog({ key, type, item })
  }

</script>

<template>
  <div
    v-for="(item, index) in nodeData"
    :key="index"
    class="childFlow"
  >
    <!--    审批节点-->
    <div v-if="!item.branchs">
      <div class="node flex flex-col justify-center items-center">
        <div
          :class="`box box_${item.type} px-24 py-16 bg-white rounded-4`"
          @click="choseNode({ key: index, type: item.type, item: nodeData })"
        >
          <svg-icon
            name="delIcon"
            svg-class="delIcon"
            @click.stop="remove(index, nodeData)"
          />
          <p class="tit">{{ item.name }}</p>
          <div class="content">
            <template v-if="item.props&&item.props.assignedOrg?.length">
              <el-tag
                v-for="(i,key) in item.props.assignedOrg"
                :key="key"
                type="info"
              >
                {{ i.orgType==='TARGET_LABEL'?i.orgName:i.orgType==='TARGET_MANAGEMENT'?'主管单位':i.orgType==='TARGET_OWNER'?'发起单位':'上级条线主管单位' }}
              </el-tag>
            </template>
            <template v-else-if="item.props&&item.props.orgType&&item.props.orgType!=='TARGET_LABEL'">
              <el-tag
                type="info"
              >{{ item.props.orgType==='TARGET_MANAGEMENT'?'主管单位':item.props.orgType==='TARGET_OWNER'?'发起单位':'上级条线主管单位' }}</el-tag>
            </template>
            <span
              v-else
              class="text-info"
            >请选择审批单位</span>
          </div>
        </div>
        <el-popover
          v-if="item.type!=='CONDITIONS'&&item.type!='CONCURRENTS'"
          placement="right"
          :width="100"
          trigger="hover"
        >
          <template #reference>
            <el-icon class="add_icon mt-20 mb-20 cursor-pointer">
              <Plus />
            </el-icon>
          </template>
          <div class="flex flex-col">
            <p>
              <el-button
                class="mb-8 w-full"
                @click="add({ key: index, type: 'SUB', item:nodeData })"
              >
                单位
              </el-button>
            </p>
            <p>
              <el-button
                class="ml-1 w-full mb-8"
                @click="add({ key: index, type: 'CONDITIONS', item:nodeData })"
              >
                数据筛选
              </el-button>
            </p>
            <p>
              <el-button
                class="ml-1 w-full"
                @click="add({ key: index, type: 'CONCURRENTS', item:nodeData })"
              >
                并行分支
              </el-button>
            </p>
          </div>
        </el-popover>
      </div>
    </div>
    <!--    条件节点-->
    <template v-else>
      <div class="conNode">
        <p
          v-if="item.branchs"
          class="topLine"
        >
          <span
            class="text-primary cursor-pointer bg-white text-center text-12"
            @click="addCon(item.branchs,item.type)"
          >{{ item.type==='CONDITIONS'?'添加条件':'添加分支' }}</span>
        </p>
        <div class="conOut flex justify-between">
          <div
            v-for="(con, con_index) in item.branchs"
            :key="con_index"
            class="model conModel mr-8"
          >
            <div class="node flex flex-col justify-center items-center">
              <p class="line"></p>
              <div
                :class="`box box_${con.type} px-24 py-16 bg-white rounded-4`"
                @click="
                  choseNode({ key: con_index, type: con.type, item: con })
                "
              >
                <svg-icon
                  name="delIcon"
                  svg-class="delIcon"
                  @click.stop="remove(con_index, item, index, nodeData)"
                />
                <template v-if="con.props&&con.props.groups?.length">
                  <div
                    v-for="(orList, conIndex) in con.props.groups"
                    :key="conIndex"
                  >
                    <div class="tit">
                      <p
                        v-for="(andList, key) in orList.conditions"
                        :key="key"
                      >
                        <span>{{ andList.title }}{{ filedConditionsOptions.find(i=>andList.id===i.value).judges[andList.compare] }}{{ filedConditionsOptions.find(i=>andList.id===i.value).type==='bool_condition'?`${andList.value.join('、')*1?'是':'否'}`:filedConditionsOptions.find(i=>andList.id===i.value).type==='value_condition'?andList.value.map(i=>filedConditionsOptions.find(i=>andList.id===i.value).options[i]).join('、'):andList.value.join('、') }}</span>
                      </p>
                    </div>
                    <p v-if="conIndex < con.props.groups.length - 1">
                      或
                    </p>
                  </div>
                </template>
                <p v-else>{{ con.name }}</p>
                <p v-if="con.type==='CONDITION'" class="content">
                  <a>筛选数据</a>
                </p>

                <p v-else class="content">
                  <span class="text-info">并行任务（同时进行）</span>
                </p>
              </div>
              <el-popover
                v-if="con.type!=='CONDITIONS'&&con.type!=='CONCURRENTS'&&false"
                placement="right"
                :width="100"
                trigger="hover"
              >
                <template #reference>
                  <el-icon class="add_icon mt-20 mb-20 cursor-pointer">
                    <Plus />
                  </el-icon>
                </template>
                <div class="flex flex-col">
                  <p>
                    <el-button
                      class="mb-8 w-full"
                      @click="add({ key: index, type: 'SUB', item:con })"
                    >
                      单位
                    </el-button>
                  </p>
                  <p>
                    <el-button
                      class="ml-1 w-full mb-8"
                      @click="add({ key: con_index, type: 'CONDITIONS', item:con })"
                    >
                      数据筛选
                    </el-button>
                  </p>
                  <p>
                    <el-button
                      class="ml-1 w-full"
                      @click="add({ key: con_index, type: 'CONCURRENTS', item:con })"
                    >
                      并行分支
                    </el-button>
                  </p>
                </div>
              </el-popover>
            </div>
            <p class="spec_line"></p>
            <child-flow
              v-if="con.children"
              :node-data="con.children"
            />
          </div>
        </div>
        <template v-if="item.branchs">
          <p class="bot_line"></p>
        </template>
      </div>
      <div v-if="item.type!=='CONDITIONS'&&item.type!=='CONCURRENTS'" class="flex justify-center items-center">
        <el-popover
          placement="right"
          :width="100"
          trigger="hover"
        >
          <template #reference>
            <el-icon class="add_icon mt-20 mb-20 cursor-pointer">
              <Plus />
            </el-icon>
          </template>
          <div class="flex flex-col">
            <p>
              <el-button
                class="mb-8 w-full"
                @click="add({ key: index, type: 'SUB', item:nodeData })"
              >
                单位
              </el-button>
            </p>
            <p>
              <el-button
                class="ml-1 w-full mb-8"
                @click="add({ key: index, type: 'CONDITIONS', item:nodeData })"
              >
                数据筛选
              </el-button>
            </p>
            <p>
              <el-button
                class="ml-1 w-full"
                @click="add({ key: index, type: 'CONCURRENTS', item:nodeData })"
              >
                并行分支
              </el-button>
            </p>

          </div>
        </el-popover>
      </div>
    </template>
  </div>
</template>

<style lang="less">
.childFlow{
  width: 100%;
  .node{
    font-size: 14px;
    .line {
      width: 4px;
      height: 20px;
      background: #dcdee2;
    }
    .box{
      z-index: 2;
      background: #ffffff;
      box-shadow: 0px 4px 8px 0px rgba(9, 103, 253, 0.1);
      border-radius: 4px;
      border: 1px solid rgba(9, 103, 253, 0.06);
      width: 200px;
      padding: 8px;
      cursor: pointer;
      position: relative;
      //&:after{
      //  width: 4px;
      //  height: 20px;
      //  background: #dcdee2;
      //  content: '';
      //  display: block;
      //  position: absolute;
      //  left: 50%;
      //  transform: translate(-50%, 100%);
      //  bottom: 0;
      //}
      .tit{
        padding: 4px 8px;
        border-radius: 4px;
        font-weight: 600;
        line-height: 20px;
        margin-bottom: 8px;
        position: relative;
      }
      &.box_SUB {
        .tit {
          background: linear-gradient(270deg, #ffae00 0%, #ff8600 100%);
          color: #ffffff;
          padding-right: 29px;
        }
      }
      &.box_CONDITION {
        .tit {
          background: #f5f8fa;
          color: #333333;
        }
      }
      &.box_EMPTY,&.box_AUTO_PASS {
        display: none;
      }
      &:hover {
        .delIcon {
          display: block;
        }
      }
    }
  }
  .conNode{
    position: relative;
    .conOut{
      background: #fff;
    }
    .topLine{
      height: 4px;
      background: #dcdee2;
      position: absolute;
      left: 0;
      right: 0;
      span {
        display: inline-block;
        position: absolute;
        z-index: 10;
        left: 50%;
        transform: translateX(-50%);
        top: -10px;
        width: 88px;
        height: 24px;
        line-height: 24px;
        border: 1px solid #1b71e3;
        border-radius: 4px;
      }
    }
    &:after{
      height: 4px;
      background: #dcdee2;
      position: absolute;
      left: 0;
      right: 0;
      bottom:0
    }
    .conModel{
      position: relative;
      &:nth-of-type(1) {
        background: #fff;
        z-index: 3;
        &:after {
          content: '';
          position: absolute;
          top: 0;
          width: 50%;
          height: 4px;
          background: #dcdee2;
          right: 0;
        }
        &:before {
          bottom: 0;
          content: '';
          background: #dcdee2;
          height: 4px;
          width: 50%;
          right: 0;
          position: absolute;
        }
      }
      &:last-of-type {
        background: #fff;
        margin-right: 0;
        z-index: 3;
        &:after {
          content: '';
          position: absolute;
          top: 0;
          width: 50%;
          height: 4px;
          background: #dcdee2;
          left: 0;
        }
        &:before {
          bottom: 0;
          content: '';
          background: #dcdee2;
          height: 4px;
          width: 50%;
          left: 0;
          position: absolute;
        }
      }
      &:after{
        content: '';
        position: absolute;
        top: 0;
        width: 50%;
        height: 4px;
        background: #dcdee2;
        right: 0;
      }
    }
    .bot_line{
      height: 4px;
      background: #dcdee2;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      z-index:2
    }
    .spec_line{
      width: 4px;
      height: 100%;
      position: absolute;
      top: 0;
      left: 50%;
      transform: translateX(-50%);
      background: #dcdee2;
      z-index: 0;
    }
  }
  .delIcon{
    position: absolute;
    width: 24px;
    height: 24px;
    right: -12px;
    top: -12px;
    display: none;
    z-index:99
  }
}

</style>
