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

<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 { postBpmProcessExpressionDelete, postBpmProcessExpressionPage } from '@/api/modules/bpmDefinition/bpMliuchengbiaodashibiaokongzhiqi';
import { ACTION, statusOptions } from '../enum';
import DrawerExpression from './drawerExpression.vue';

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

const drawerExpressionRef = ref<InstanceType<typeof DrawerExpression>>();

const tableRef = ref<typeof ProTable>();

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

function onAction(key: string, record?: API.BpmProcessExpressionRespVO): void {
  switch (key) {
    case ACTION.ADD:
      drawerExpressionRef.value?.open(ACTION.ADD);
      break;
    case ACTION.EDIT:
      drawerExpressionRef.value?.open(ACTION.EDIT, record);
      break;
    case ACTION.REMOVE:
      Modal.confirm({
        title: '确定删除该表达式吗?',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          postBpmProcessExpressionDelete({ 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 === 0" 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>
    <DrawerExpression ref="drawerExpressionRef" @submit="reloadData" />
  </div>
</template>
