<template>
  <div style="height: 100%">
    <w-grid
      ref="gridRef"
      title="待办任务"
      :fetch-data-url="Environment.apiContextPath('api/system/process/task/waitTaskAllList')"
      :sort-no="true"
      :checkbox-selection="false"
      :query-form-cols-num="4"
      :query-form-fields="grid.queryFormFields"
      :columns="grid.columns"
      :toolbar-actions="grid.buttons"
      :pagination="{
        reqPageStart: 1,
        sortBy: 'CREATE_TIME',
        descending: true,
      }"
    ></w-grid>
    <RatingDialog ref="ratingDialogRef" @refresh="refresh"></RatingDialog>
    <CognizanceDialog ref="cognizanceDialogRef" @refresh="refresh"></CognizanceDialog>
    <RebirthDialog ref="rebirthDialogRef" @refresh="refresh"></RebirthDialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Environment, EnumTools, DictionaryTools, Options, Formater, axios, NotifyManager } from 'platform-core';
import RatingDialog from '@/views/custRating/RatingDialog.vue';
import CognizanceDialog from '@/views/default/CognizanceDialog.vue';
import RebirthDialog from '@/views/default/RebirthDialog.vue';
import { ProcessDefKey } from './Task.ts';

const gridRef = ref();
const ratingDialogRef = ref();
const cognizanceDialogRef = ref();
const rebirthDialogRef = ref();

const grid = {
  queryFormFields: [
    { label: '业务流水号', name: 'BUSINESS_ID', type: 'text' },
    { label: '流程名称', name: 'PROC_NAME', type: 'text' },
    { label: '任务节点', name: 'TASK_NAME', type: 'text' },
    { label: '任务描述', name: 'TASK_DESC', type: 'text' },
  ],
  buttons: [
    'query',
    'reset',
    'separator',
    {
      name: 'handle',
      label: '办理',
      icon: 'home',
      enableIf: (args) => {
        if (args.selected) {
          return true;
        }
        return false;
      },
      click: (args) => {
        if (args.selected['PROC_DEF_KEY'] === ProcessDefKey.RATING) {
          // 打开评级流程窗口
          openRatingDialog(args.selected);
        } else if (args.selected['PROC_DEF_KEY'] === ProcessDefKey.DEFAULT_COGNIZANCE_F) {
          // 打开违约认定流程窗口
          openCognizanceDialog(args.selected);
        } else if (args.selected['PROC_DEF_KEY'] === ProcessDefKey.DEFAULT_REBIRTH_F) {
          // 打开违约重生流程窗口
          openRebirthDialog(args.selected);
        }
      },
    },
    'separator',
  ],
  columns: [
    { label: '业务流水号', name: 'BUSINESS_ID' },
    { label: '流程名称', name: 'PROC_NAME' },
    { label: '任务节点', name: 'TASK_NAME' },
    { label: '任务描述', name: 'TASK_DESC' },
    { label: '任务开始时间', name: 'CREATE_TIME' },
  ],
};

const openRatingDialog = (task) => {
  const urlSearchParams = new URLSearchParams();
  urlSearchParams.append('criteria', JSON.stringify({ fieldName: 'id', operator: 'equals', value: task['BUSINESS_ID'] }));
  axios
    .get(Environment.apiContextPath('api/irbs/companyRating?pageable=false'), { params: urlSearchParams })
    .then((resp) => {
      if (resp.data?.content) {
        ratingDialogRef.value.show({ ...resp.data.content[0], taskId: task['ID'] });
      }
    })
    .catch((error) => {
      console.info('error====', error);
    });
};
const openCognizanceDialog = (task) => {
  const urlSearchParams = new URLSearchParams();
  urlSearchParams.append('criteria', JSON.stringify({ fieldName: 'id', operator: 'equals', value: task['BUSINESS_ID'] }));
  axios
    .get(Environment.apiContextPath('api/irbs/defaultCognizance?pageable=false'), { params: urlSearchParams })
    .then((resp) => {
      if (resp.data?.content) {
        cognizanceDialogRef.value.show({ ...resp.data.content[0], taskId: task['ID'] });
      }
    })
    .catch((error) => {
      console.info('error====', error);
    });
};
const openRebirthDialog = (task) => {
  const urlSearchParams = new URLSearchParams();
  urlSearchParams.append('criteria', JSON.stringify({ fieldName: 'id', operator: 'equals', value: task['BUSINESS_ID'] }));
  axios
    .get(Environment.apiContextPath('api/irbs/defaultRebirth?pageable=false'), { params: urlSearchParams })
    .then((resp) => {
      if (resp.data?.content) {
        rebirthDialogRef.value.show({ ...resp.data.content[0], taskId: task['ID'] });
      }
    })
    .catch((error) => {
      console.info('error====', error);
    });
};

const refresh = () => {
  gridRef.value.refresh();
};
</script>
