<template>
<div>
  <a-row  :gutter="16">
    <a-col :span="6" v-for="(type) in dataType" :key="type.FK_FlowSort">
      <a-collapse v-model:activeKey="activeKey">
        <a-collapsePanel :key="type.FK_FlowSort" :header="type.FK_FlowSortText">
          <div v-for="(start) in dataFlow" :key="start.No" class="set_Start">
            <div v-if="type.FK_FlowSort == start.FK_FlowSort" style="margin-bottom:10px;">
              {{ start.No }}&nbsp;
              <a @click="StartFlow(start.No)">{{start.Name }}</a>
            </div>
          </div>
        </a-collapsePanel>
      </a-collapse>
    </a-col>
  </a-row>

  <a-drawer
      title="节点流程"
      placement="right"
      width="70%"
      :closable="true"
      :visible="visible"
      :get-container="false"
      :wrap-style="{ position: 'absolute'}"
      :body-style="{ padding: '0px' }"
      @close="onCloseDrawer">

      <div style="width: 100%;height: 100%;">
        <iframe :src="url" scrolling="auto" frameborder="no" style="width: 100%; height: 100%" />
      </div>
      
  </a-drawer>
</div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

import { useUserStoreWithOut } from '/@/store/modules/user';

import { defHttp } from '/@/utils/http/axios';

const user = useUserStoreWithOut();

const apiUrl = '/workflow/start';

const dataType = ref<any>([]);  //类型
const dataFlow = ref<any>([]);  //发起流程
//const userStore = useUserStore();
const activeKey = ref<string[]>([]);
const visible = ref<boolean>(false);
const url = ref();

async function setList() {
  const res = await defHttp.get({ url: apiUrl});
  if(res.code==200) {
    dataType.value = []
    let tmpAry = [];
    let newAry = [{}]
    let retData = JSON.parse(res.data);
    dataFlow.value = retData.sort(compare('No')); 

    for(let i=0;i<dataFlow.value.length;i++){
      let item = dataFlow.value[i];
      if (tmpAry.indexOf(item.FK_FlowSort) == -1) {
        tmpAry.push(item.FK_FlowSort);
        dataType.value.push({
          FK_FlowSort: item.FK_FlowSort,
          FK_FlowSortText: item.FK_FlowSortText
        })
        activeKey.value.push(item.FK_FlowSort);
      } 
    }
  }
}



const StartFlow = (flowNo: string) => {
  visible.value = true;
  url.value = import.meta.env.VITE_APP_JFLOW_CORE_ADDR + "/#/WF/Port?DoWhat=Start&FK_Flow=001&Token=" + user.getJflowToken
}


const compare = (property) => {
  return (a, b) => {
    var value1 = a[property];
    var value2 = b[property];
    return value1 - value2;
  }
}

function onCloseDrawer() {
  visible.value = false
}


setList()
</script>
