import React, { useState } from 'react'; import { redirect, type MetaFunction, type ActionFunctionArgs, type LoaderFunctionArgs } from '@remix-run/node'; import { useLoaderData, useActionData, Form, useSubmit, useNavigate } from '@remix-run/react'; import { Button } from '~/components/ui/Button'; import { Card } from '~/components/ui/Card'; import { Breadcrumb } from '~/components/layout/Breadcrumb'; import type { Rule, RuleType, RulePriority } from '~/models/rule'; export const meta: MetaFunction = () => { return [ { title: "中国烟草AI合同及卷宗审核系统 - 评查规则详情" }, { name: "description", content: "评查规则详情编辑页面" } ]; }; export const handle = { breadcrumb: '编辑评查点' }; interface LoaderData { rule: Rule; ruleTypes: { label: string; value: RuleType }[]; rulePriorities: { label: string; value: RulePriority }[]; groupOptions: { label: string; value: string }[]; } export async function loader({ params }: LoaderFunctionArgs) { const { ruleId } = params; // 判断是否为新建规则 const isNewRule = ruleId === 'new'; // 模拟数据,实际项目中应从API获取 const rule: Rule = isNewRule ? { id: '', name: '', description: '', content: '', type: 'text', priority: 'medium', groupId: '', groupName: '', isActive: true, createdAt: '', updatedAt: '' } : { id: ruleId, name: '许可证编号格式检查', description: '检查烟草专卖零售许可证编号是否符合"烟零许(年份)序号号"的标准格式', content: '许可证编号应当符合"烟零许(年份)序号号"的标准格式,如"烟零许(2023)12345号"', type: 'regex', priority: 'high', groupId: '1', groupName: '专卖许可证规则组', isActive: true, createdAt: '2023-10-15 09:30', updatedAt: '2023-12-10 14:20' }; // 规则类型选项 const ruleTypes = [ { label: '文本匹配', value: 'text' }, { label: '正则表达式', value: 'regex' }, { label: '数值范围', value: 'range' }, { label: '日期检查', value: 'date' }, { label: 'AI智能检查', value: 'ai' } ]; // 规则优先级选项 const rulePriorities = [ { label: '低', value: 'low' }, { label: '中', value: 'medium' }, { label: '高', value: 'high' }, { label: '关键', value: 'critical' } ]; // 规则组选项 const groupOptions = [ { label: '专卖许可证规则组', value: '1' }, { label: '合同协议规则组', value: '2' }, { label: '财务票据规则组', value: '3' }, { label: '采购订单规则组', value: '4' }, { label: '销售报表规则组', value: '5' } ]; return Response.json({ rule, ruleTypes, rulePriorities, groupOptions }); } interface ActionData { success?: boolean; errors?: { name?: string; description?: string; content?: string; type?: string; priority?: string; groupId?: string; general?: string; }; } export async function action({ request, params }: ActionFunctionArgs) { const { ruleId } = params; const formData = await request.formData(); const isNewRule = ruleId === 'new'; // 获取表单数据 const name = formData.get('name')?.toString() || ''; const description = formData.get('description')?.toString() || ''; const content = formData.get('content')?.toString() || ''; const type = formData.get('type')?.toString() || ''; const priority = formData.get('priority')?.toString() || ''; const groupId = formData.get('groupId')?.toString() || ''; const isActive = formData.get('isActive') === 'true'; // 表单验证 const errors: ActionData['errors'] = {}; if (!name.trim()) { errors.name = '规则名称不能为空'; } if (!content.trim()) { errors.content = '规则内容不能为空'; } if (!type) { errors.type = '必须选择规则类型'; } if (!priority) { errors.priority = '必须选择规则优先级'; } if (!groupId) { errors.groupId = '必须选择规则所属组'; } if (Object.keys(errors).length > 0) { return Response.json({ errors }); } // 模拟API保存操作,实际项目中应调用API try { // 在这里调用API进行保存 console.log('保存规则:', { id: isNewRule ? 'new-id' : ruleId, name, description, content, type, priority, groupId, isActive }); // 成功后重定向到规则列表页 return redirect('/rules'); } catch (error) { return Response.json({ errors: { general: '保存规则失败,请重试' } }); } } export default function RuleDetail() { const { rule, ruleTypes, rulePriorities, groupOptions } = useLoaderData(); const actionData = useActionData(); const navigate = useNavigate(); const submit = useSubmit(); const [formData, setFormData] = useState({ name: rule.name, description: rule.description, content: rule.content, type: rule.type, priority: rule.priority, groupId: rule.groupId, isActive: rule.isActive }); const isNewRule = !rule.id; const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target; setFormData(prev => ({ ...prev, [name]: value })); }; const handleSwitchChange = (name: string, checked: boolean) => { setFormData(prev => ({ ...prev, [name]: checked })); }; const handleCancel = () => { navigate('/rules'); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); // 使用useSubmit提交表单 const formElement = e.currentTarget; submit(formElement, { method: 'post' }); }; return (

{isNewRule ? '新增评查规则' : '编辑评查规则'}

{actionData?.errors?.general && (
{actionData.errors.general}
)}

基本信息

{actionData?.errors?.name && (
{actionData.errors.name}
)}
{actionData?.errors?.groupId && (
{actionData.errors.groupId}
)}

规则设置

{actionData?.errors?.type && (
{actionData.errors.type}
)}
{actionData?.errors?.priority && (
{actionData.errors.priority}
)}
{formData.isActive ? '启用' : '禁用'}
{actionData?.errors?.content && (
{actionData.errors.content}
)} {formData.type === 'regex' && (
输入正则表达式,用于匹配文档内容
)} {formData.type === 'ai' && (
请使用自然语言描述规则检查的要求,AI将自动理解并执行检查
)}

测试工具

); }