<template>
  <el-card class="box-card">
    <byTable
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :searchConfig="searchConfig"
      highlight-current-row
      @get-list="getList"
      @clickReset="clickReset">
    </byTable>
  </el-card>
</template>

<script setup name="DealWith">
import byTable from "/src/components/byTable/index";

const loading = ref(false);
const sourceList = ref({
  data: [],
  pagination: {
    total: 3,
    pageNum: 1,
    pageSize: 10,
    status: 1,
  },
});
const { proxy } = getCurrentInstance();
const searchConfig = computed(() => {
  return [
    {
      type: "select",
      prop: "status",
      label: "流程状态",
      data: [
        {
          dictKey: 1,
          dictValue: "待处理",
        },
        {
          dictKey: 2,
          dictValue: "已通过",
        },
        {
          dictKey: 3,
          dictValue: "相关流程",
        },
      ],
      clearable: true,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "流程类型",
        prop: "flowName",
      },
    },
    {
      attrs: {
        label: "流程标题",
        prop: "title",
      },
    },
    {
      attrs: {
        label: "流程状态",
        width: 100,
        prop: "status",
      },
      render(status) {
        return status == 0 || status == 1 ? "待处理" : status == 2 ? "已通过" : "已驳回";
      },
    },
    {
      attrs: {
        label: "发起人",
        prop: "createUserName",
      },
    },
    {
      attrs: {
        label: "发起时间",
        prop: "createTime",
      },
    },
    {
      attrs: {
        label: "操作",
        width: "200",
        align: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: (row.status != 1 && row.status != 0) || sourceList.value.pagination.status != 1 ? "查看" : "办理",
              type: "primary",
              text: true,
              bg: true,
              disabled: false,
            },
            el: "button",
            click() {
              if ((row.status != 1 && row.status != 0) || sourceList.value.pagination.status != 1) {
                proxy.$router.push({
                  path: "/platform_manage/process/processApproval",
                  query: {
                    flowKey: row.flowKey,
                    id: row.businessId,
                    flowId: row.id,
                    processType: 20,
                    version: row.version,
                  },
                });
                return;
              }
              proxy.post("flowExample/getApprovalRecord", { id: row.id }).then((res) => {
                if (res.recordList.length > 0) {
                  let data = res.recordList.filter((item) => item.status === 2);
                  let nodeType = 0;
                  if (data && data.length > 0) {
                    nodeType = data[0].nodeType;
                  }
                  proxy.$router.push({
                    path: "/platform_manage/process/processApproval",
                    query: {
                      flowKey: row.flowKey,
                      id: row.businessId,
                      flowId: row.id,
                      processType: nodeType == 1 ? 30 : 10,
                      businessId: row.businessId,
                      version: row.version,
                    },
                  });
                }
              });
            },
          },
        ];
      },
    },
  ];
});
const getUrlObj = {
  1: "/flowExample/getToBeProcessedPage",
  2: "/flowExample/getHaveInitiatedPage",
  3: "/flowExample/getProcessedPage",
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post(getUrlObj[sourceList.value.pagination.status], sourceList.value.pagination).then((message) => {
    sourceList.value.data = message.rows;
    sourceList.value.pagination.total = message.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
onMounted(() => {
  const route = useRoute();
  sourceList.value.pagination.status = route.query.type ? route.query.type : 1;
  getList();
});
const clickReset = () => {
  getList({ status: 1 });
};
</script>

<style lang="scss" scoped></style>
