<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="待审批">
        <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="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"
        />
      </el-tab-pane>
      <el-tab-pane name="over" label="已审批">
        <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"
        />
      </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-row :gutter="20">
        <el-col :span="12">
          <el-descriptions
              class="margin-top"
              title="修改前数据"
              :column="3"

              border
          >
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  系统英文名
                </div>
              </template>
              kooriookami
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  模式名称
                </div>
              </template>
              18100000000
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象英文名
                </div>
              </template>
              Suzhou
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象中文名
                </div>
              </template>
              <el-tag size="small">School</el-tag>
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  记录数
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  补录对象名称
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象类型
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象治理标志
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  负责人
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item :span="2">
              <template #label>
                <div class="cell-item">
                  对象标签
                </div>
              </template>
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  补录对象描述
                </div>
              </template>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
        <el-col :span="12">
          <el-descriptions
              class="margin-top"
              title="修改后数据"
              :column="3"

              border
          >
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  系统英文名
                </div>
              </template>
              kooriookami
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  模式名称
                </div>
              </template>
              18100000000
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象英文名
                </div>
              </template>
              Suzhou
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象中文名
                </div>
              </template>
              <el-tag size="small">School</el-tag>
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  记录数
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  补录对象名称
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象类型
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  对象治理标志
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  负责人
                </div>
              </template>

            </el-descriptions-item>
            <el-descriptions-item :span="2">
              <template #label>
                <div class="cell-item">
                  对象标签
                </div>
              </template>
            </el-descriptions-item>
            <el-descriptions-item>
              <template #label>
                <div class="cell-item">
                  补录对象描述
                </div>
              </template>
            </el-descriptions-item>
          </el-descriptions>

        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script setup>
import {getFlowConfList, getApprovalList, operateProcess } from "@/api/flow/flow"
import {v4 as uuid} from 'uuid'

const { proxy } = getCurrentInstance();
import { ref, nextTick, computed, watch, reactive, onMounted } from 'vue'
import {getWaitingFlowCount} from "../../../api/flow/flow.js";
import cache from "../../../plugins/cache.js";
import {Graph} from "@antv/x6";
const flowTab = ref('waiting')
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applicant: undefined,
    businessType: undefined,
    status:'pending'
  },
});
const showFlowDialog = ref(false);
const businessDialog = ref(false);
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 searchFlowData(){

}
function getIconUrlAndShape(data,row){
  let approvalFlow = JSON.parse(row.approvalFlow)
  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'
        }else {
          data[j].operator = ""
          data[j].iconUrl = '/x6-user.png'
          data[j].shape = 'activity'
        }
      }
    }
  }
  return data
}

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
      }
      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)
        })
      }
    })
    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){
  businessDialog.value = true
}
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)
    })
  })
}
function reject(row){

}
onMounted(()=>{
  handleQuery()
})
</script>
