<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="申请人" prop="applicant">
        <el-input
            v-model="queryParams.applicant"
            placeholder="请输入申请人"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType">
        <el-select
            v-model="queryParams.businessType"
            placeholder="业务类型"
            clearable
            style="width: 240px"
        >
          <el-option
              key="t_metadata_supp_info"
              label="元数据信息补录"
              value="t_metadata_supp_info"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-tabs type="border-card" v-model="flowTab" @tab-change="handleQuery">
      <el-tab-pane name="waiting" label="待审批">
        <template v-if="flowTab==='waiting'">
          <el-table :data="flowList">
            <el-table-column label="业务类型" align="center" prop="businessType">
              <template #default="scope">
                <span v-if="scope.row.businessType === 't_metadata_supp_info'">元数据信息补录</span>
              </template>
            </el-table-column>
            <el-table-column label="业务编号" align="center" prop="businessId" :show-overflow-tooltip="true" width="280">
              <template #default="scope">
                <el-link type="primary" @click="showBusinessDataDialog(scope.row)" :underline="false">{{ scope.row.businessId }}</el-link>
              </template>
            </el-table-column>
            <el-table-column label="申请人" align="center" prop="applicant" :show-overflow-tooltip="true" />
            <el-table-column label="申请时间" align="center" prop="applyTime" :show-overflow-tooltip="true" />
            <el-table-column label="当前状态" align="center" prop="configType">
              <template #default="scope">
                <span v-if="scope.row.status === 'waiting'">未审批</span>
                <span v-if="scope.row.status === 'pending'">未审批</span>
                <span v-if="scope.row.status === 'succeed'">已审批</span>
                <span v-if="scope.row.status === 'rejected'">已驳回</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-button link type="primary" icon="View" @click="showFlow(scope.row)">查看流程</el-button>
                <el-button link type="success" icon="Select" @click="agree(scope.row)">同意</el-button>
                <el-button link type="danger" icon="CloseBold" @click="reject(scope.row)">驳回</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
              v-show="total > 0"
              :total="total"
              v-model:page="queryParams.pageNum"
              v-model:limit="queryParams.pageSize"
              @pagination="getList"
          />
        </template>
      </el-tab-pane>
      <el-tab-pane name="over" label="已审批">
        <template v-if="flowTab==='over'">
          <el-table :data="flowList">
            <el-table-column label="业务类型" align="center" prop="businessType">
              <template #default="scope">
                <span v-if="scope.row.businessType === 't_metadata_supp_info'">元数据信息补录</span>
              </template>
            </el-table-column>
            <el-table-column label="业务编号" align="center" prop="businessId" :show-overflow-tooltip="true" width="280">
              <template #default="scope">
                <el-link type="primary" @click="showBusinessDataDialog(scope.row)" :underline="false">{{ scope.row.businessId }}</el-link>
              </template>
            </el-table-column>
            <el-table-column label="申请人" align="center" prop="applicant" :show-overflow-tooltip="true" />
            <el-table-column label="申请时间" align="center" prop="applyTime" :show-overflow-tooltip="true" />
            <el-table-column label="当前状态" align="center" prop="configType">
              <template #default="scope">
                <span v-if="scope.row.status === 'waiting'">未审批</span>
                <span v-if="scope.row.status === 'pending'">已审批</span>
                <span v-if="scope.row.status === 'succeed'">已审批</span>
                <span v-if="scope.row.status === 'rejected'">已驳回</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-button link type="success" icon="View" @click="showFlow(scope.row)">查看流程</el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
              v-show="total > 0"
              :total="total"
              v-model:page="queryParams.pageNum"
              v-model:limit="queryParams.pageSize"
              @pagination="getList"
          />
        </template>
      </el-tab-pane>
      <el-tab-pane name="applying" label="我的申请">
        <template v-if="flowTab==='applying'">
          <el-table :data="flowList">
            <el-table-column label="业务类型" align="center" prop="businessType">
              <template #default="scope">
                <span v-if="scope.row.businessType === 't_metadata_supp_info'">元数据信息补录</span>
              </template>
            </el-table-column>
            <el-table-column label="业务编号" align="center" prop="businessId" :show-overflow-tooltip="true" width="280">
              <template #default="scope">
                <el-link type="primary" @click="showBusinessDataDialog(scope.row)" :underline="false">{{ scope.row.businessId }}</el-link>
              </template>
            </el-table-column>
            <el-table-column label="申请人" align="center" prop="applicant" :show-overflow-tooltip="true" />
            <el-table-column label="申请时间" align="center" prop="applyTime" :show-overflow-tooltip="true" />
            <el-table-column label="当前状态" align="center" prop="configType">
              <template #default="scope">
                <span v-if="scope.row.status === 'waiting'">未审批</span>
                <span v-if="scope.row.status === 'pending'">审批中</span>
                <span v-if="scope.row.status === 'succeed'">已审批</span>
                <span v-if="scope.row.status === 'rejected'">已驳回</span>
                <span v-if="scope.row.status === 'canceled'">已撤回</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
              <template #default="scope">
                <el-button link type="success" icon="View" @click="showFlow(scope.row)">查看流程</el-button>
                <template v-if="scope.row.status === 'waiting' || scope.row.status === 'pending'">
                  <el-popconfirm title="确定撤销申请吗？">
                    <template #reference>
                      <el-button  link type="danger" icon="Delete" @click="cancelFlow(scope.row)">撤销申请</el-button>
                    </template>
                  </el-popconfirm>
                </template>
              </template>
            </el-table-column>
          </el-table>
          <pagination
              v-show="total > 0"
              :total="total"
              v-model:page="queryParams.pageNum"
              v-model:limit="queryParams.pageSize"
              @pagination="getList"
          />
        </template>
      </el-tab-pane>
    </el-tabs>
    <el-dialog
        v-model="showFlowDialog"
        title="审批流程"
        width="50%"
    >
      <div id="flowContainer" style="width: 100%;height: 100%"></div>
    </el-dialog>
    <el-dialog
        v-model="businessDialog"
        width="80%"
    >
      <el-descriptions
          class="margin-top"
          title="修改前数据"
          :column="3"

          border
      >
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              系统英文名
            </div>
          </template>
          {{ oldTableInfo.ssysCd }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              模式名称
            </div>
          </template>
          {{ oldTableInfo.mdlName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象英文名
            </div>
          </template>
          {{ oldTableInfo.tabEngName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象中文名
            </div>
          </template>
          {{ oldTableInfo.tabCnName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              记录数
            </div>
          </template>
          {{ oldTableInfo.tabRecNum }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              补录对象名称
            </div>
          </template>
          <span v-if="oldTableInfo.tabCrrctName === newTableInfo.tabCrrctName">{{ oldTableInfo.tabCrrctName }}</span>
          <span v-else style="color: red">{{ oldTableInfo.tabCrrctName }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象类型
            </div>
          </template>
          {{ oldTableInfo.tabType }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象治理标志
            </div>
          </template>
          <span v-if="oldTableInfo.govFlag === newTableInfo.govFlag">{{ oldTableInfo.govFlag }}</span>
          <span v-else style="color: red">{{ oldTableInfo.govFlag }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              负责人
            </div>
          </template>
          <span v-if="oldTableInfo.pic === newTableInfo.pic">{{ oldTableInfo.pic }}</span>
          <span v-else style="color: red">{{ oldTableInfo.pic }}</span>
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template #label>
            <div class="cell-item">
              对象标签
            </div>
          </template>
          <template v-if="oldTableInfo.showTabClas && oldTableInfo.showTabClas.length > 0">
            <template  v-for="item in oldTableInfo.showTabClas">
              <el-tag style="margin:5px" v-if="item.clasEffFlag && item.clasEffFlag === '1'">{{item.clasName}}</el-tag>
              <el-tag style="margin:5px" type="danger" v-if="item.clasEffFlag === '0'">{{item.clasName}}</el-tag>
            </template>
          </template>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              补录对象描述
            </div>
          </template>
          <span v-if="oldTableInfo.tabDesc === newTableInfo.tabDesc">{{ oldTableInfo.tabDesc }}</span>
          <span v-else style="color: red">{{ oldTableInfo.tabDesc }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <div v-if="oldColumnList.length > 0" >
        <div style="margin-top: 16px;margin-bottom: 16px">
          <span style="font-size: 14px;font-weight: bold;margin: 8px 11px">字段内容:</span>
        </div>
        <el-table :data="oldColumnList" height="100%">
          <el-table-column label="字段英文名" width="100" align="center" prop="fldEngName"></el-table-column>
          <el-table-column label="字段中文名" align="center" prop="fldCnName"></el-table-column>
          <el-table-column label="字段类型" width="170" align="center" prop="fldType" ></el-table-column>
          <el-table-column label="主键标志" width="100" align="center" prop="pkFlag">
            <template #default="scope">
              <span v-if="scope.row.pkFlag === '1' || scope.row.pkFlag === 'Y' || scope.row.pkFlag === true">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <!--                  <el-table-column label="字段描述" width="250" align="center" prop="fldDesc"></el-table-column>-->
          <el-table-column label="是否必填" width="70" align="center" prop="requireFlag">
            <template #default="scope">
              <span v-if="scope.row.requireFlag === '1' || scope.row.requireFlag === 'Y' || scope.row.requireFlag === true">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column label="是否索引" width="150" align="center" prop="idxFlag">
            <template #default="scope">
              <span v-if="scope.row.idxFlag === '1' || scope.row.idxFlag === 'Y' || scope.row.idxFlag === true">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column label="字段补录名" width="150" align="center" prop="fldCrrctName">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].fldCrrctName === newColumnList[scope.$index].fldCrrctName">{{scope.row.fldCrrctName}}</span>
              <span v-else style="color: red">{{scope.row.fldCrrctName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="补录主键" align="center" prop="crrctPkFlag">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].crrctPkFlag === newColumnList[scope.$index].crrctPkFlag">{{scope.row.crrctPkFlag?'是':'否'}}</span>
              <span v-else style="color: red">{{scope.row.crrctPkFlag?'是':'否'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="补录字段描述" align="center" prop="fldDesc">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].fldDesc === newColumnList[scope.$index].fldDesc">{{scope.row.fldDesc}}</span>
              <span v-else style="color: red">{{scope.row.fldDesc}}</span>
            </template>
          </el-table-column>
          <el-table-column label="负责人" align="center" prop="pic">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].pic === newColumnList[scope.$index].pic">{{scope.row.pic}}</span>
              <span v-else style="color: red">{{scope.row.pic}}</span>
            </template>
          </el-table-column>
          <el-table-column label="字段标签" width="150" align="center" prop="fldClas">
            <template #default="scope">
              <template v-if="scope.row.showColClas && scope.row.showColClas.length > 0">
                <div  v-for="item in scope.row.showColClas">
                  <el-tag v-if="item.clasEffFlag === '1'">{{item.clasName}}</el-tag>
                  <el-tag type="danger" v-if="item.clasEffFlag === '0'">{{item.clasName}}</el-tag>
                </div>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="字段空值率" align="center" prop="fldNullRate">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].fldNullRate === newColumnList[scope.$index].fldNullRate">{{scope.row.fldNullRate}}</span>
              <span v-else style="color: red">{{scope.row.fldNullRate}}</span>
            </template>
          </el-table-column>
          <el-table-column label="引用字典/标准" align="center"></el-table-column>
        </el-table>
      </div>
      <br><br>
      <el-descriptions
          class="margin-top"
          title="修改后数据"
          :column="3"
          border
      >
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              系统英文名
            </div>
          </template>
          {{ newTableInfo.ssysCd }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              模式名称
            </div>
          </template>
          {{ newTableInfo.mdlName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象英文名
            </div>
          </template>
          {{ newTableInfo.tabEngName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象中文名
            </div>
          </template>
          {{ newTableInfo.tabCnName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              记录数
            </div>
          </template>
          {{ newTableInfo.tabRecNum }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              补录对象名称
            </div>
          </template>
          <span v-if="oldTableInfo.tabCrrctName === newTableInfo.tabCrrctName">{{ newTableInfo.tabCrrctName }}</span>
          <span v-else style="color: red">{{ newTableInfo.tabCrrctName }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象类型
            </div>
          </template>
          {{ newTableInfo.tabType }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              对象治理标志
            </div>
          </template>
          <span v-if="oldTableInfo.govFlag === newTableInfo.govFlag">{{ newTableInfo.govFlag }}</span>
          <span v-else style="color: red">{{ newTableInfo.govFlag }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              负责人
            </div>
          </template>
          <span v-if="oldTableInfo.pic === newTableInfo.pic">{{ newTableInfo.pic }}</span>
          <span v-else style="color: red">{{ newTableInfo.pic }}</span>
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template #label>
            <div class="cell-item">
              对象标签
            </div>
          </template>
          <template v-if="newTableInfo.showTabClas && newTableInfo.showTabClas.length > 0">
            <template  v-for="item in newTableInfo.showTabClas">
              <el-tag style="margin:5px" v-if="item.clasEffFlag === '1'">{{item.clasName}}</el-tag>
              <el-tag style="margin:5px" type="danger" v-if="item.clasEffFlag === '0'">{{item.clasName}}</el-tag>
            </template>
          </template>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              补录对象描述
            </div>
          </template>
          <span v-if="oldTableInfo.tabDesc === newTableInfo.tabDesc">{{ newTableInfo.tabDesc }}</span>
          <span v-else style="color: red">{{ newTableInfo.tabDesc }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <div >
        <div style="margin-top: 16px;margin-bottom: 16px">
          <span style="font-size: 14px;font-weight: bold;margin: 8px 11px">字段内容:</span>
        </div>
        <el-table :data="newColumnList" height="100%">
          <el-table-column label="字段英文名" width="100" align="center" prop="fldEngName"></el-table-column>
          <el-table-column label="字段中文名" align="center" prop="fldCnName"></el-table-column>
          <el-table-column label="字段类型" width="170" align="center" prop="fldType" ></el-table-column>
          <el-table-column label="主键标志" width="100" align="center" prop="pkFlag">
            <template #default="scope">
              <span v-if="scope.row.pkFlag === '1' || scope.row.pkFlag === 'Y' || scope.row.pkFlag === true">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <!--                  <el-table-column label="字段描述" width="250" align="center" prop="fldDesc"></el-table-column>-->
          <el-table-column label="是否必填" width="70" align="center" prop="requireFlag">
            <template #default="scope">
              <span v-if="scope.row.requireFlag === '1' || scope.row.requireFlag === 'Y' || scope.row.requireFlag === true">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column label="是否索引" width="150" align="center" prop="idxFlag">
            <template #default="scope">
              <span v-if="scope.row.idxFlag === '1' || scope.row.idxFlag === 'Y' || scope.row.idxFlag === true">是</span>
              <span v-else>否</span>
            </template>
          </el-table-column>
          <el-table-column label="字段补录名" width="150" align="center" prop="fldCrrctName">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].fldCrrctName === newColumnList[scope.$index].fldCrrctName">{{scope.row.fldCrrctName}}</span>
              <span v-else style="color: red">{{scope.row.fldCrrctName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="补录主键" align="center" prop="crrctPkFlag">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].crrctPkFlag === newColumnList[scope.$index].crrctPkFlag">{{scope.row.crrctPkFlag?'是':'否'}}</span>
              <span v-else style="color: red">{{scope.row.crrctPkFlag?'是':'否'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="补录字段描述" align="center" prop="fldDesc">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].fldDesc === newColumnList[scope.$index].fldDesc">{{scope.row.fldDesc}}</span>
              <span v-else style="color: red">{{scope.row.fldDesc}}</span>
            </template>
          </el-table-column>
          <el-table-column label="负责人" align="center" prop="pic">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].pic === newColumnList[scope.$index].pic">{{scope.row.pic}}</span>
              <span v-else style="color: red">{{scope.row.pic}}</span>
            </template>
          </el-table-column>
          <el-table-column label="字段标签" width="150" align="center" prop="fldClas">
            <template #default="scope">
              <template v-if="scope.row.showColClas && scope.row.showColClas.length>0">
                <div v-for="item in scope.row.showColClas">
                  <el-tag v-if="item.clasEffFlag === '1'">{{item.clasName}}</el-tag>
                  <el-tag type="danger" v-if="item.clasEffFlag === '0'">{{item.clasName}}</el-tag>
                </div>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="字段空值率" align="center" prop="fldNullRate">
            <template #default="scope">
              <span v-if="oldColumnList[scope.$index].fldNullRate === newColumnList[scope.$index].fldNullRate">{{scope.row.fldNullRate}}</span>
              <span v-else style="color: red">{{scope.row.fldNullRate}}</span>
            </template>
          </el-table-column>
          <el-table-column label="引用字典/标准" align="center"></el-table-column>
        </el-table>
      </div>
    </el-dialog>
        <!-- 弹窗 -->
        <el-dialog v-model="stdMainVisible" title="数据标准变更详情" width="80%">
      <DataStdMainTable :flowId="selectedFlowId" :stdMainVisible="stdMainVisible"/>
    </el-dialog>
    <el-dialog v-model="stdDictVisible" title="数据字典变更详情" width="80%" >
      <DataStdDictTable :flowId="selectedFlowId" :stdDictVisible="stdDictVisible"/>
    </el-dialog>
    <el-dialog v-model="stdCodeVisible" title="标准代码变更详情" width="80%" >
      <DataStdCodeTable :flowId="selectedFlowId" :stdCodeVisible="stdCodeVisible"/>
    </el-dialog>
    <el-dialog v-model="dataAssetMainVisible" title="数据资产变更详情" width="80%" >
      <DataAssetMainAppr :flowId="selectedFlowId" :dataAssetMainVisible="dataAssetMainVisible"/>
    </el-dialog>
  </div>
</template>

<script setup>
import {getFlowConfList, getApprovalList, operateProcess, cancelMyFlow,getWaitingFlowCount } from "@/api/flow/flow"
import { getMetaInfoApplyBusinessDetail } from "@/api/meta/metaInfo"
import {v4 as uuid} from 'uuid'
import DataStdMainTable from '@/views/system/flow/dataStdMainAppr.vue'; // 路径根据实际调整
import DataStdDictTable from '@/views/system/flow/dataStdDictAppr.vue'; // 路径根据实际调整
import DataStdCodeTable from '@/views/system/flow/dataStdCodeAppr.vue'; // 路径根据实际调整
import DataAssetMainAppr from "@/views/system/flow/dataAssetMainAppr.vue"; // 路径根据实际调整

const stdMainVisible = ref(false);
const stdDictVisible = ref(false);
const stdCodeVisible = ref(false);
const dataAssetMainVisible = ref(false);
const { proxy } = getCurrentInstance();
import { ref, nextTick, computed, watch, reactive, onMounted } from 'vue'
import cache from "../../../plugins/cache.js";
import {Graph} from "@antv/x6";

const flowTab = ref('waiting')
const selectedFlowId = ref('');

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applicant: undefined,
    businessType: undefined,
    status:'pending'
  },
});
const showFlowDialog = ref(false);
const businessDialog = ref(false);
const oldTableInfo = ref([]);
const oldColumnList = ref([]);
const newTableInfo = ref([]);
const newColumnList = ref([]);
const total = ref(0);
const { queryParams } = toRefs(data);
const flowList = ref([]);

/** 查询参数列表 */
function getList() {
  getApprovalList(queryParams.value).then(res=>{
    flowList.value = res.data.rows
    total.value = res.data.total
  })
}
function register(){
  Graph.registerNode(
      'activity',
      {
        inherit: 'rect',
        markup: [
          {
            tagName: 'rect',
            selector: 'body',
          },
          {
            tagName: 'image',
            selector: 'img',
          },
          {
            tagName: 'text',
            selector: 'label',
          },
        ],
        attrs: {
          body: {
            rx: 6,
            ry: 6,
            stroke: '#5F95FF',
            fill: '#EFF4FF',
            strokeWidth: 1,
          },
          img: {
            x: 6,
            y: 6,
            width: 16,
            height: 16,
            'xlink:href': '/x6-user.png',
          },
          label: {
            fontSize: 12,
            fill: '#262626',
          },
        },
      },
      true,
  )
  Graph.registerNode(
      'activity-success',
      {
        inherit: 'rect',
        markup: [
          {
            tagName: 'rect',
            selector: 'body',
          },
          {
            tagName: 'image',
            selector: 'img',
          },
          {
            tagName: 'text',
            selector: 'label',
          },
        ],
        attrs: {
          body: {
            rx: 6,
            ry: 6,
            stroke: '#67C23A',
            fill: 'rgb(239.8, 248.9, 235.3)',
            strokeWidth: 1,
          },
          img: {
            x: 6,
            y: 6,
            width: 16,
            height: 16,
            'xlink:href': '/checkbox-circle-line.png',
          },
          label: {
            fontSize: 12,
            fill: '#67C23A',
          },
        },
      },
      true,
  )
  Graph.registerNode(
      'activity-reject',
      {
        inherit: 'rect',
        markup: [
          {
            tagName: 'rect',
            selector: 'body',
          },
          {
            tagName: 'image',
            selector: 'img',
          },
          {
            tagName: 'text',
            selector: 'label',
          },
        ],
        attrs: {
          body: {
            rx: 6,
            ry: 6,
            stroke: '#F56C6C',
            fill: 'rgb(254, 240.3, 240.3)',
            strokeWidth: 1,
          },
          img: {
            x: 6,
            y: 6,
            width: 16,
            height: 16,
            'xlink:href': '/close-circle-line.png',
          },
          label: {
            fontSize: 12,
            fill: '#F56C6C',
          },
        },
      },
      true,
  )

  Graph.registerEdge(
      'bpmn-edge',
      {
        inherit: 'edge',
        attrs: {
          line: {
            stroke: '#C71E1EFF',
            strokeWidth: 2,
          },
        },
      },
      true,
  )
}

function getIconUrlAndShape(data,row){
  for (let i = 0; i < data.length; i++) {
    data[i].operator = ""
    data[i].iconUrl = '/x6-user.png'
    data[i].shape = 'activity'
  }
  let approvalFlow = JSON.parse(row.approvalFlow)
  if (approvalFlow && approvalFlow.length>0){
    for (let i = 0; i < approvalFlow.length; i++) {
      for (let j = 0; j < data.length; j++) {
        if (approvalFlow[i].confFlowId === data[j].id){
          if (approvalFlow[i].operate === 'success'){
            data[j].operator = approvalFlow[i].operator
            data[j].shape = 'activity-success'
            data[j].iconUrl = '/checkbox-circle-line.png'
          }else if (approvalFlow[i].operate === 'reject'){
            data[j].operator = approvalFlow[i].operator
            data[j].iconUrl = '/close-circle-line.png'
            data[j].shape = 'activity-reject'
          }
        }
      }
    }
  }
  return data
}
function cancelFlow(row){
  cancelMyFlow(row.id).then(res=>{
        proxy.$modal.msgSuccess("撤销成功");

    getList()
  })
}
function showFlow(row){
  showFlowDialog.value = true
  getFlowConfList(row.businessType).then(res=>{
    let resData = res.data
    if (resData.length > 0){
      for (let i = 0; i < resData.length; i++) {
        resData[i].parent = JSON.parse(resData[i].parent)
      }
    }
    let data = getIconUrlAndShape(resData,row)
    let array = []
    register()
    let graph = new Graph({
      container: document.getElementById('flowContainer'),
      width: 800,
      height: 500,
      grid:true,
      autoResize: true,
      connecting: {
        router: 'manhattan'
      },
    })
    data.forEach((item) => {
      let str = ""
      if (item.operator !== ''){
        str += "\r\n审批员："+item.operator
      }else {
        str +="\r\n未审批"
      }
      let node = {
        id: item.id,
        code: item.code,
        attrs: {text:{text: item.text + str}},
        position:{x:item.x,y:item.y},
        type: item.type,
        width: 100,
        height: 60,
        shape: item.shape,
        iconUrl: item.iconUrl
      }
      array.push(node)
    })
    data.forEach((item) => {
      if (item.parent && item.parent.length > 0){
        item.parent.forEach((parentNodeId) =>{
          let node = {
            id: uuid(),
            shape: 'edge',
            source: {cell: parentNodeId},
            target: {cell: item.id}
          }
          array.push(node)
        })
      }
    })
    console.log(array)
    graph.fromJSON(array)
    graph.zoomToFit({ padding:10, maxScale: 1 })
  })

}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  queryParams.value.status = flowTab.value
  getList();
}
function resetQuery(){
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    applicant: undefined,
    businessType: undefined,
  }
  getList()
}
function showBusinessDataDialog(row){

  if (row.businessType === 'metaDataInfo'){
    businessDialog.value = true
    let param = {
      businessId: row.businessId
    }
    getMetaInfoApplyBusinessDetail(param).then(res=>{
      let data = JSON.parse(JSON.stringify(res.data))
      let table = JSON.parse(data.table.oldTableData)
      let batchTabClas = data.table.batchTabClas
      let showOldTabClas = []
      if (batchTabClas && batchTabClas.length>0){
        showOldTabClas = JSON.parse(JSON.stringify(batchTabClas))
        if (table.tabClas && table.tabClas !== '[]' && table.tabClas !== 'null'){
          let oldTabClas = JSON.parse(table.tabClas)
          for (let i = 0; i < oldTabClas.length; i++) {
            for (let j = 0; j < showOldTabClas.length; j++) {
              if (showOldTabClas[j].clasPriClas === oldTabClas[i].clasPriClas
                  && showOldTabClas[j].clasScdClas === oldTabClas[i].clasScdClas
                  && showOldTabClas[j].clasThreClas === oldTabClas[i].clasThreClas
                  && showOldTabClas[j].clasName === oldTabClas[i].clasName
              ){
                showOldTabClas[j] = oldTabClas[i]
              }
            }
          }
        }
      }else {
        showOldTabClas = JSON.parse(table.tabClas)
      }
      table.showTabClas = showOldTabClas
      oldTableInfo.value = JSON.parse(JSON.stringify(table))
      newTableInfo.value = {
        applyStatus:data.table.applyStatus,
        applyTime:data.table.applyTime,
        crrctVerNum:data.table.crrctVerNum,
        govFlag:data.table.govFlag,
        mdlName:data.table.mdlName,
        onum:data.table.onum,
        pic:data.table.pic,
        recStat:data.table.recStat,
        recSubmPrsn:data.table.recSubmPrsn,
        ssysCd:data.table.ssysCd,
        tabClas:data.table.tabClas,
        tabCrrctName:data.table.tabCrrctName,
        tabDesc:data.table.tabDesc,
        tabEngName:data.table.tabEngName,
        updTime:data.table.updTime,
        tabCnName:table.tabCnName,
        tabRecNum:table.tabRecNum,
        tabType:table.tabType,
      }
      let showNewTabClas = []
      if (batchTabClas && batchTabClas.length>0){
        showNewTabClas = JSON.parse(JSON.stringify(batchTabClas))
        if (newTableInfo.value.tabClas && newTableInfo.value.tabClas !== '[]' && newTableInfo.value.tabClas !== 'null'){
          let newTabClas = JSON.parse(newTableInfo.value.tabClas)
          for (let i = 0; i < newTabClas.length; i++) {
            for (let j = 0; j < showNewTabClas.length; j++) {
              if (showNewTabClas[j].clasPriClas === newTabClas[i].clasPriClas
                  && showNewTabClas[j].clasScdClas === newTabClas[i].clasScdClas
                  && showNewTabClas[j].clasThreClas === newTabClas[i].clasThreClas
                  && showNewTabClas[j].clasName === newTabClas[i].clasName
              ){
                showNewTabClas[j] = newTabClas[i]
              }
            }
          }
        }
      }else {
        showNewTabClas = JSON.parse(data.table.tabClas)
      }
      newTableInfo.value.showTabClas = showNewTabClas
      newColumnList.value = []
      oldColumnList.value = []
      if (data.column && data.column.length > 0){
        for (let i = 0; i < data.column.length; i++) {
          let col = data.column[i]
          let column = JSON.parse(col.oldColumnData)
          let batchColClas = col.batchColClas
          let showOldColClas = []
          if (batchColClas && batchColClas.length>0){
            showOldColClas = JSON.parse(JSON.stringify(batchColClas))
            if (column.fldClas && column.fldClas !== '[]' && column.fldClas !== 'null'){
              let oldColClas = JSON.parse(column.fldClas)
              for (let i = 0; i < oldColClas.length; i++) {
                for (let j = 0; j < showOldColClas.length; j++) {
                  if (showOldColClas[j].clasPriClas === oldColClas[i].clasPriClas
                      && showOldColClas[j].clasScdClas === oldColClas[i].clasScdClas
                      && showOldColClas[j].clasThreClas === oldColClas[i].clasThreClas
                      && showOldColClas[j].clasName === oldColClas[i].clasName
                  ){
                    showOldColClas[j] = oldColClas[i]
                  }
                }
              }
            }
          }else {
            showOldColClas = JSON.parse(column.fldClas)
          }
          column.showColClas = showOldColClas
          oldColumnList.value.push(column)
          let showNewColClas = []
          if (batchColClas && batchColClas.length>0){
            showNewColClas = batchColClas
            if (col.fldClas && col.fldClas !== '[]' && col.fldClas !== 'null'){
              let newColClas = JSON.parse(col.fldClas)
              for (let i = 0; i < newColClas.length; i++) {
                for (let j = 0; j < showNewColClas.length; j++) {
                  if (showNewColClas[j].clasPriClas === newColClas[i].clasPriClas
                      && showNewColClas[j].clasScdClas === newColClas[i].clasScdClas
                      && showNewColClas[j].clasThreClas === newColClas[i].clasThreClas
                      && showNewColClas[j].clasName === newColClas[i].clasName
                  ){
                    showNewColClas[j] = newColClas[i]
                  }
                }
              }
            }
          }else {
            showNewColClas = JSON.parse(col.fldClas)
          }
          newColumnList.value.push({
            applyStatus:col.applyStatus,
            applyTime:col.applyTime,
            crrctPkFlag:col.crrctPkFlag,
            crrctVerNum:col.crrctVerNum,
            fldClas:col.fldClas,
            fldCrrctName:col.fldCrrctName,
            fldDesc:col.fldDesc,
            fldEngName:col.fldEngName,
            fldNullRate:col.fldNullRate,
            mdlName:col.mdlName,
            onum:col.onum,
            pic:col.pic,
            recStat:col.recStat,
            recSubmPrsn:col.recSubmPrsn,
            ssysCd:col.ssysCd,
            tabEngName:col.tabEngName,
            updTime:col.updTime,
            fldCnName:column.fldCnName,
            fldType:column.fldType,
            showColClas: showNewColClas
          })
        }
      }
    })
  }else if(row.businessType ==="dataStdMain"){
    selectedFlowId.value = row.businessId;
    stdMainVisible.value = true;
  }else if(row.businessType ==="dataStdDict"){
    selectedFlowId.value = row.businessId;
    stdDictVisible.value = true;
  }else if(row.businessType ==="dataStdCode"){
    selectedFlowId.value = row.businessId;
    stdCodeVisible.value = true;
  }else if (row.businessType === 'dataAssetMain'){
    dataAssetMainVisible.value = true;
    selectedFlowId.value = row.businessId;
  }


}
function agree(row){
  //获取用户当前的nodeId
  let data = {
    flowId: row.id,
    operateType: 'success',
    operateComment: ''
  }
  operateProcess(data).then(res=>{
    proxy.$modal.msgSuccess("操作成功");
    getWaitingFlowCount().then(res=>{
      cache.local.set("waitingTotal",res.data)
    })
    getList()
  })
}
function reject(row){
  let data = {
    flowId: row.id,
    operateType: 'reject',
    operateComment: ''
  }
  operateProcess(data).then(res=>{
    proxy.$modal.msgSuccess("操作成功");
    getWaitingFlowCount().then(res=>{
      cache.local.set("waitingTotal",res.data)
    })
    getList()
  })
}
onMounted(()=>{
  handleQuery()
})
</script>
