<!--
文件: flow-type/index.vue
功能: 流程类型（Flow Type）列表页面
主要逻辑:
- 使用 `ProTable` 展示流程类型列表，支持分页与搜索，通过 `postBpmCategoryPage` 获取数据并适配给表格组件。
- 提供新增、编辑、删除操作：
  - 新增/编辑：通过 `DrawerFlowType` 抽屉组件进行表单操作，提交后触发父组件 `reloadData` 刷新列表。
  - 删除：调用 `postBpmCategoryDelete` 接口删除记录，删除成功后提示并刷新表格。
- 在 `columns` 中定义表格列与搜索项（如 id、name、code、description、status、sort、createTime、action 等）。
- `request` 方法负责将 ProTable 的分页参数映射为后端参数并返回 { success, data, total } 格式的数据。
- `onAction` 根据 `ACTION` 分发操作并与 `drawerFlowTypeRef` 进行交互。
-->

<script setup lang="tsx">
import type { ProColumns } from '@pubinfo/pro-components';
import { ProTable } from '@pubinfo/pro-components';
import { message, Modal } from 'ant-design-vue';
import { postBpmCategoryDelete, postBpmCategoryPage } from '@/api/modules/bpmDefinition/bpMliuchengfenleikongzhiqi';
import { ACTION, statusOptions } from '../enum';
import DrawerFlowType from './drawerFlowType.vue';

defineOptions({
  name: 'FlowTypeListPage',
});

const drawerFlowTypeRef = ref<InstanceType<typeof DrawerFlowType>>();

const tableRef = ref<typeof ProTable>();

async function request(params: any) {
  const { current, pageSize, ...rest } = params;
  const res = await postBpmCategoryPage({
    doSearchTotal: true,
    currentPage: current,
    pageSize,
    ...rest,
  });
  return {
    success: res.success,
    data: res.data?.records ?? [],
    total: res.data?.total ?? 0,
  };
}
const columns: ProColumns<API.BpmCategoryRespVO> = [
  {
    hideInSearch: true,
    title: '分类编号',
    dataIndex: 'id',
    ellipsis: true,
  },
  {
    valueType: 'a-input',
    bind: 'value',
    title: '分类名',
    dataIndex: 'name',
    ellipsis: true,
    fieldProps: {
      placeholder: '请输入分类名',
      allowClear: true,
    },
  },
  {
    valueType: 'a-input',
    bind: 'value',
    title: '分类标志',
    dataIndex: 'code',
    ellipsis: true,
    fieldProps: {
      placeholder: '请输入分类标志',
      allowClear: true,
    },
  },
  {
    hideInSearch: true,
    title: '分类描述',
    dataIndex: 'description',
    ellipsis: true,
  },
  {
    valueType: 'a-select',
    title: '状态',
    dataIndex: 'status',
    ellipsis: true,
    fieldProps: {
      placeholder: '请选择状态',
      allowClear: true,
      options: statusOptions,
    },
  },
  {
    hideInSearch: true,
    title: '分类排序',
    dataIndex: 'sort',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    title: '创建时间',
    dataIndex: 'createTime',
    ellipsis: true,
  },
  {
    hideInSearch: true,
    width: 200,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
  },
];
function reloadData() {
  tableRef.value?.reload();
}

// const router = useRouter();
function onAction(key: string, record?: API.BpmCategoryRespVO): void {
  switch (key) {
    case ACTION.ADD:
      drawerFlowTypeRef.value?.open(ACTION.ADD);
      break;
    case ACTION.EDIT:
      drawerFlowTypeRef.value?.open(ACTION.EDIT, record);
      break;
    case ACTION.REMOVE:
      Modal.confirm({
        title: '确定删除该流程类型吗?',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          postBpmCategoryDelete({ id: record!.id }).then((res: API.ResponseDataBoolean) => {
            if (res.success) {
              message.success('删除成功！');
              reloadData();
            }
          });
        },
        onCancel() {},
      });
      break;
  }
}
</script>

<template>
  <div
    py-10px
    px-10px
    w-full
    h-full
  >
    <ProTable
      ref="tableRef"
      row-key="id"
      :request="request"
      :columns="columns"
      auto-height
      :scroll="{ x: 1100 }"
    >
      <template #toolbar>
        <a-button type="primary" @click="onAction(ACTION.ADD)">
          <PlusCircleOutlined />
          {{ ACTION.ADD }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'status'">
          <a-badge v-if="record.status === 1" status="success" text="开启" />
          <a-badge v-else status="default" text="关闭" />
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-space>
            <a class="flex items-center" @click="onAction(ACTION.EDIT, record)">
              <span class="ml-4px">{{ ACTION.EDIT }}</span>
            </a>
            <a class="flex items-center hover:opacity-80" @click="onAction(ACTION.REMOVE, record)">
              <span class="ml-4px text-red-500!">{{ ACTION.REMOVE }}</span>
            </a>
          </a-space>
        </template>
      </template>
    </ProTable>
    <DrawerFlowType ref="drawerFlowTypeRef" @submit="reloadData" />
  </div>
</template>
