import { type MetaFunction } from "@remix-run/node";
import { useState, useEffect } from "react";
import { BasicInfo } from "~/components/rules/new/BasicInfo";
import { ExtractionSettings } from "~/components/rules/new/ExtractionSettings";
import { ReviewSettings } from "~/components/rules/new/ReviewSettings";
import { RuleContext } from "~/contexts/RuleContext";
import { ActionButtons } from "~/components/rules/new/ActionButtons";
import { PageHeader } from "~/components/rules/new/PageHeader";
import rulesStyles from "~/styles/rules.css?url";
import { useNavigate, useLocation } from "@remix-run/react";

export const meta: MetaFunction = () => {
  return [
    { title: "评查点管理 - 中国烟草AI合同及卷宗审核系统" },
    { 
      name: "description", 
      content: "创建或修改评查点，设置规则参数" 
    }
  ];
};

export function links() {
  return [{ rel: "stylesheet", href: rulesStyles }];
}

export const handle = {
  breadcrumb: "评查点管理"
};

// 定义类型
interface RegexField {
  id: string;
  fieldName: string;
  regex: string;
}

interface ApiRegexField {
  field: string;
  pattern: string;
}

interface ApiVlmField {
  name: string;
  type: string;
}

interface PromptSetting {
  type: string;
  template: string;
}

interface ExtactionConfigType {
  llm_ocr: {
    fields: string[];
    prompt_setting: PromptSetting;
  };
  llm_vl: {
    fields: string[];
    prompt_setting: PromptSetting;
  };
  ocr_regex: {
    fields: RegexField[];
  };
}

interface ApiExtactionConfigType {
  llm?: {
    fields?: string[];
    prompt_setting?: {
      type?: string;
      template?: string;
    }
  };
  vlm?: {
    fields?: ApiVlmField[];
    prompt_setting?: {
      type?: string;
      template?: string;
    }
  };
  regex?: {
    fields?: ApiRegexField[];
  };
}

interface RuleConfigType {
  [key: string]: unknown;
}

interface Rule {
  id: string;
  type: string;
  config: RuleConfigType;
}

interface EvaluationConfigType {
  logicType: string;
  customLogic: string;
  rules: Rule[];
}

interface FormDataType {
  id?: number;
  name: string;
  code: string;
  risk: string;
  is_enabled: boolean;
  description: string;
  references_laws: {
    name: string;
    articles: string[];
    content: string;
  };
  evaluation_point_groups_id: number | null;
  extraction_config: ExtactionConfigType;
  evaluation_config: EvaluationConfigType;
  pass_message: string;
  fail_message: string;
  suggestion_message: string;
  suggestion_message_type: string;
  post_action: string;
  action_config: string;
  type: string;
  evaluation_point_groups_pid: number | null;
  score: number;
  scoreDisplay?: string;
}

interface ApiPointData {
  id: number;
  name: string;
  code: string;
  risk: string;
  is_enabled: boolean;
  description: string;
  references_laws: {
    name: string;
    articles: string[];
    content: string;
  };
  evaluation_point_groups_id: number | null;
  evaluation_point_groups_pid?: number | null;
  extraction_config: ApiExtactionConfigType;
  evaluation_config: {
    logicType?: string;
    customLogic?: string;
    rules?: Array<{
      id: string;
      type: string;
      config: Record<string, unknown>;
    }>;
  };
  pass_message: string;
  fail_message: string;
  suggestion_message: string;
  suggestion_message_type: string;
  post_action: string;
  action_config: string;
  type?: string;
  meta?: {
    type: string;
    pid?: number;
    [key: string]: unknown;
  };
  score?: number;
  scoreDisplay?: string;
}

// API响应数据类型
interface ApiResponse {
  code?: number;
  msg?: string;
  data?: Array<Record<string, unknown>> | Record<string, unknown>;
  [key: string]: unknown;
}

export default function RuleNew() {
  const navigate = useNavigate();
  const location = useLocation();
  const [extractionFields, setExtractionFields] = useState<string[]>([]);
  const [isEditMode, setIsEditMode] = useState<boolean>(false);
  const [isLoading, setIsLoading] = useState<boolean>(false);
  const [evaluationPointGroups, setEvaluationPointGroups] = useState<Array<{id: number, pid: number, code: string, name: string, is_enabled: boolean}>>([]);
  const [formData, setFormData] = useState<FormDataType>({
    // 基本信息字段
    name: '',
    code: '',
    risk: 'medium',
    is_enabled: true,
    description: '',
    references_laws: {
      name: '',
      articles: [],
      content: ''
    },
    evaluation_point_groups_id: null,
    type: '',
    evaluation_point_groups_pid: null,
    
    // 抽取设置
    extraction_config: {
      llm_ocr: {
        fields: [],
        prompt_setting: {
          type: 'system',
          template: ''
        }
      },
      llm_vl: {
        fields: [],
        prompt_setting: {
          type: 'system',
          template: ''
        }
      },
      ocr_regex: {
        fields: [{ id: '1', fieldName: '', regex: '' }]
      }
    },
    
    // 评查设置
    evaluation_config: {
      logicType: 'and',
      customLogic: '',
      rules: []
    },
    
    // 评查结果消息
    pass_message: '文档检查通过，符合规范要求。',
    fail_message: '文档存在以下问题，请修改后重新提交。',
    suggestion_message: '',
    suggestion_message_type: 'warning',
    
    // 评查后动作
    post_action: 'none',
    action_config: '',
    
    // 分数
    score: 0
  });

  // 页面加载时检查URL中是否有ID参数，如果有则为编辑模式
  useEffect(() => {
    const searchParams = new URLSearchParams(location.search);
    const id = searchParams.get('id');
    
    if (id) {
      setIsEditMode(true);
      fetchEvaluationPoint(parseInt(id));
    }

    // 获取评查点组数据
    fetchEvaluationPointGroups();
  }, [location.search]);

  // 获取评查点组数据
  const fetchEvaluationPointGroups = async () => {
    try {
      console.log("获取评查点组数据");
      const response = await fetch("http://127.0.0.1:9000/admin/evaluation_point_groups", {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        }
      });

      if (!response.ok) {
        const errorText = await response.text();
        console.error(`API响应错误: ${response.status}`, errorText);
        throw new Error(`获取评查点组数据失败: ${response.status} - ${errorText}`);
      }

      const responseData = await response.json();
      console.log("评查点组数据原始响应:", responseData);
      
      // 适配API响应格式
      let groupsData: Array<{id: number, pid: number, code: string, name: string, is_enabled: boolean}> = [];
      if (responseData && typeof responseData === 'object') {
        if (responseData.code === 0 && responseData.data) {
          // 新API格式
          groupsData = Array.isArray(responseData.data) ? responseData.data : [responseData.data];
        } else if (Array.isArray(responseData)) {
          // 旧API格式
          groupsData = responseData;
        } else if (responseData.data && Array.isArray(responseData.data)) {
          // 旧API格式
          groupsData = responseData.data;
        }
      }
      
      // 确保所有项都有必需的字段
      groupsData = groupsData.filter(item => 
        item && typeof item === 'object' && 
        'id' in item && 
        'pid' in item && 
        'code' in item && 
        'name' in item
      );
      
      console.log("处理后的评查点组数据:", groupsData);
      console.log("根级评查点类型:", groupsData.filter(group => group.pid === 0));
      setEvaluationPointGroups(groupsData);
      
      // 如果表单数据已加载但类型未设置，尝试根据evaluation_point_groups_pid设置类型
      if (formData.id && !formData.type && formData.evaluation_point_groups_pid) {
        console.log("评查点组数据加载后更新类型，当前pid:", formData.evaluation_point_groups_pid);
        
        const typeGroup = groupsData.find(group => group.id === formData.evaluation_point_groups_pid);
        if (typeGroup) {
          console.log("找到对应的类型组:", typeGroup);
          setFormData(prevData => ({
            ...prevData,
            type: typeGroup.code
          }));
          console.log("根据评查点类型ID更新类型为:", typeGroup.code);
        } else if (formData.evaluation_point_groups_id) {
          // 通过规则组查找类型
          const selectedGroup = groupsData.find(group => group.id === formData.evaluation_point_groups_id);
          if (selectedGroup && selectedGroup.pid !== 0) {
            const parentTypeGroup = groupsData.find(group => group.id === selectedGroup.pid);
            if (parentTypeGroup) {
              console.log("通过规则组找到类型组:", parentTypeGroup);
              setFormData(prevData => ({
                ...prevData,
                type: parentTypeGroup.code,
                evaluation_point_groups_pid: parentTypeGroup.id
              }));
              console.log("根据规则组更新类型为:", parentTypeGroup.code);
            }
          }
        }
      }
    } catch (error) {
      console.error('获取评查点组数据失败:', error);
      // 显示错误提示但不影响应用继续使用
      alert(`获取评查点组数据失败: ${error instanceof Error ? error.message : '未知错误'}\n将使用默认数据`);
    }
  };

  // 获取评查点数据
  const fetchEvaluationPoint = async (id: number) => {
    setIsLoading(true);
    try {
      console.log(`获取评查点数据，ID: ${id}`);
      const response = await fetch(`http://127.0.0.1:9000/admin/evaluation_points?id=eq.${id}`, {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json'
        }
      });

      if (!response.ok) {
        const errorText = await response.text();
        console.error(`API响应错误: ${response.status}`, errorText);
        throw new Error(`获取评查点数据失败: ${response.status} - ${errorText}`);
      }

      const responseData = await response.json();
      console.log("API响应数据:", responseData);
      
      // 新的API响应格式适配
      if (responseData && typeof responseData === 'object') {
        if (responseData.code === 0 && responseData.data) {
          // 符合新的API响应格式
          if (Array.isArray(responseData.data)) {
            if (responseData.data.length > 0) {
              console.log("处理数组数据的第一项");
              processPointData(responseData.data[0]);
            } else {
              console.error("数据数组为空");
              throw new Error(`未找到ID为${id}的评查点数据`);
            }
          } else if (typeof responseData.data === 'object') {
            console.log("处理单个对象数据");
            processPointData(responseData.data);
          }
        } else if (responseData.code !== 0) {
          // API返回错误
          throw new Error(responseData.msg || `获取数据失败，错误码: ${responseData.code}`);
        } else if (Array.isArray(responseData)) {
          // 处理旧API格式：数组形式
          if (responseData.length > 0) {
            console.log("数组格式响应，使用第一项");
            processPointData(responseData[0]);
          } else {
            console.error("数组为空，未找到数据");
            throw new Error(`未找到ID为${id}的评查点数据`);
          }
        } else if (responseData.data) {
          // 处理旧API格式：包含data字段的对象
          console.log("包装对象格式响应，使用data字段");
          if (Array.isArray(responseData.data)) {
            if (responseData.data.length > 0) {
              processPointData(responseData.data[0]);
            } else {
              console.error("data数组为空");
              throw new Error(`未找到ID为${id}的评查点数据`);
            }
          } else if (typeof responseData.data === 'object') {
            processPointData(responseData.data);
          } else {
            console.error("data字段格式不正确");
            throw new Error('数据格式不正确: data字段不是预期的格式');
          }
        } else if (responseData.id) {
          // 处理旧API格式：直接返回的对象
          console.log("单对象格式响应");
          processPointData(responseData);
        } else {
          console.error("无法识别的对象格式", responseData);
          throw new Error('未找到评查点数据或数据格式不正确');
        }
      } else {
        console.error("响应格式无法识别", responseData);
        throw new Error('未找到评查点数据或数据格式不正确');
      }
    } catch (error) {
      console.error('获取评查点数据失败:', error);
      alert(`获取评查点数据失败: ${error instanceof Error ? error.message : '未知错误'}`);
      // 获取数据失败时返回上一页
      navigate(-1);
    } finally {
      setIsLoading(false);
    }
  };
  
  // 处理获取到的评查点数据
  const processPointData = (pointData: ApiPointData) => {
    console.log("处理评查点数据:", pointData);
    
    if (!pointData) {
      console.error("评查点数据为空");
      throw new Error("评查点数据为空");
    }
    
    try {
      // 转换API数据为表单数据格式
      const extractionConfig = pointData.extraction_config || {};
      const evaluationConfig = pointData.evaluation_config || {};
      
      console.log("提取配置:", extractionConfig);
      console.log("评查配置:", evaluationConfig);
      console.log("分数:", pointData.score);
      
      // 提取字段列表，用于规则设置
      const extractedFields: string[] = [];
      
      // 处理LLM字段
      if (extractionConfig.llm && extractionConfig.llm.fields) {
        extractedFields.push(...extractionConfig.llm.fields);
      }
      
      // 处理VLM字段
      if (extractionConfig.vlm && extractionConfig.vlm.fields) {
        extractedFields.push(...(extractionConfig.vlm.fields || []).map((field) => {
          if (typeof field === 'object' && field.name) {
            return field.type ? `${field.name}_${field.type}` : field.name;
          }
          return '';
        }).filter(Boolean));
      }
      
      // 处理正则字段
      if (extractionConfig.regex && extractionConfig.regex.fields) {
        extractedFields.push(...(extractionConfig.regex.fields || []).map((field) => field.field || '').filter(Boolean));
      }
      
      console.log("提取的字段:", extractedFields);
      setExtractionFields(extractedFields);
      
      // 提取评查点类型ID
      let pointGroupPid: number | null = null;
      if (pointData.evaluation_point_groups_pid !== undefined && pointData.evaluation_point_groups_pid !== null) {
        // 直接使用字段值
        pointGroupPid = typeof pointData.evaluation_point_groups_pid === 'number' 
          ? pointData.evaluation_point_groups_pid 
          : null;
        console.log("从evaluation_point_groups_pid获取类型ID:", pointGroupPid);
      } else if (pointData.meta && typeof pointData.meta === 'object' && pointData.meta.pid !== undefined) {
        // 从meta中提取
        pointGroupPid = typeof pointData.meta.pid === 'number' 
          ? pointData.meta.pid 
          : null;
        console.log("从meta.pid获取类型ID:", pointGroupPid);
      }
      
      console.log("最终确定的类型ID:", pointGroupPid);
      
      // 处理分数
      let pointScore = 0;
      if (pointData.score !== undefined && pointData.score !== null) {
        pointScore = typeof pointData.score === 'number' ? pointData.score : 0;
        console.log("从score字段获取分数:", pointScore);
      } else if (pointData.meta && typeof pointData.meta === 'object' && pointData.meta.score !== undefined) {
        pointScore = typeof pointData.meta.score === 'number' ? pointData.meta.score : 0;
        console.log("从meta.score获取分数:", pointScore);
      }
      
      // 构建表单数据
      const newFormData: FormDataType = {
        id: pointData.id,
        name: pointData.name || '',
        code: pointData.code || '',
        risk: pointData.risk || 'medium',
        is_enabled: pointData.is_enabled !== undefined ? pointData.is_enabled : false,
        description: pointData.description || '',
        references_laws: pointData.references_laws || {
          name: '',
          articles: [],
          content: ''
        },
        evaluation_point_groups_id: pointData.evaluation_point_groups_id || null,
        evaluation_point_groups_pid: pointGroupPid,
        type: '', // 先置空，稍后根据pid推断
        
        // 将API数据格式转换为内部使用的格式
        extraction_config: {
          llm_ocr: {
            fields: extractionConfig.llm?.fields || [],
            prompt_setting: {
              type: extractionConfig.llm?.prompt_setting?.type || 'system',
              template: extractionConfig.llm?.prompt_setting?.template || ''
            }
          },
          llm_vl: {
            fields: (extractionConfig.vlm?.fields || []).map((field) => {
              if (typeof field === 'object' && field.name) {
                return field.type ? `${field.name}_${field.type}` : field.name;
              }
              return '';
            }).filter(Boolean),
            prompt_setting: {
              type: extractionConfig.vlm?.prompt_setting?.type || 'system',
              template: extractionConfig.vlm?.prompt_setting?.template || ''
            }
          },
          ocr_regex: {
            fields: (extractionConfig.regex?.fields || []).map((field, index) => ({
              id: String(index + 1),
              fieldName: field.field || '',
              regex: field.pattern || ''
            }))
          }
        },
        
        evaluation_config: {
          logicType: evaluationConfig.logicType || 'and',
          customLogic: evaluationConfig.customLogic || '',
          rules: (evaluationConfig.rules || []).map((rule) => {
            // 将API规则格式转换为UI使用的格式
            let config: RuleConfigType = {};
            
            try {
              switch (rule.type) {
                case 'exists':
                  config = {
                    selectedFields: rule.config?.fields || [],
                    logicRelation: rule.config?.logic || 'and'
                  };
                  break;
                case 'consistency':
                  config = {
                    pairs: rule.config?.pairs || [],
                    logicRelation: rule.config?.logic || 'and'
                  };
                  break;
                case 'format':
                  config = {
                    field: rule.config?.field || '',
                    formatType: rule.config?.formatType || '',
                    parameters: rule.config?.parameters || ''
                  };
                  break;
                case 'logic':
                  config = {
                    conditions: rule.config?.conditions || [],
                    logicRelation: rule.config?.logic || 'and'
                  };
                  break;
                case 'regex':
                  config = {
                    field: rule.config?.field || '',
                    pattern: rule.config?.pattern || '',
                    matchType: rule.config?.matchType || 'match'
                  };
                  break;
                case 'ai':
                  config = {
                    model: rule.config?.model || 'qwen14b',
                    temperature: rule.config?.temperature || 0.1,
                    prompt: rule.config?.prompt || ''
                  };
                  break;
                case 'code':
                  config = {
                    language: rule.config?.language || 'javascript',
                    code: rule.config?.code || ''
                  };
                  break;
                default:
                  config = rule.config || {};
              }
            } catch (error) {
              console.error(`处理规则[${rule.id}]配置时出错:`, error);
              config = rule.config || {};
            }
            
            return {
              id: rule.id,
              type: rule.type,
              config
            };
          })
        },
        
        pass_message: pointData.pass_message || '文档检查通过，符合规范要求。',
        fail_message: pointData.fail_message || '文档存在以下问题，请修改后重新提交。',
        suggestion_message: pointData.suggestion_message || '',
        suggestion_message_type: pointData.suggestion_message_type || 'warning',
        post_action: pointData.post_action || 'none',
        action_config: pointData.action_config || '',
        score: pointScore,
        scoreDisplay: pointData.scoreDisplay
      };
      
      console.log("设置表单数据:", newFormData);
      setFormData(newFormData);
      
      // 根据evaluation_point_groups_pid查找对应的评查点类型
      if (evaluationPointGroups.length > 0) {
        console.log("开始根据pid查找对应的评查点类型, pid:", newFormData.evaluation_point_groups_pid);
        console.log("当前评查点组数据:", evaluationPointGroups);
        
        // 如果有evaluation_point_groups_pid，直接查找对应的类型组
        if (newFormData.evaluation_point_groups_pid) {
          const typeGroup = evaluationPointGroups.find(group => group.id === newFormData.evaluation_point_groups_pid);
          if (typeGroup) {
            console.log("找到对应的类型组:", typeGroup);
            const updatedFormData = {
              ...newFormData,
              type: typeGroup.code
            };
            console.log("根据评查点类型ID设置类型:", typeGroup.code);
            setFormData(updatedFormData);
            return;
          }
        }
        
        // 如果评查点组ID存在，尝试查找对应的类型
        if (newFormData.evaluation_point_groups_id) {
          console.log("通过规则组ID查找对应的类型组");
          const selectedGroup = evaluationPointGroups.find(group => group.id === newFormData.evaluation_point_groups_id);
          console.log("找到的规则组:", selectedGroup);
          
          if (selectedGroup && selectedGroup.pid !== 0) {
            const typeGroup = evaluationPointGroups.find(group => group.id === selectedGroup.pid);
            console.log("找到的类型组:", typeGroup);
            
            if (typeGroup && typeGroup.code) {
              // 更新类型和评查点类型ID
              const updatedFormData = {
                ...newFormData,
                type: typeGroup.code,
                evaluation_point_groups_pid: typeGroup.id
              };
              console.log("根据规则组ID设置类型:", typeGroup.code, "和评查点类型ID:", typeGroup.id);
              setFormData(updatedFormData);
            }
          }
        }
      } else {
        console.log("无评查点组数据，无法推断类型");
        
        // 尝试从meta或type字段获取类型信息
        if (pointData.type) {
          console.log("从type字段获取类型:", pointData.type);
          const updatedFormData = {
            ...newFormData,
            type: pointData.type
          };
          setFormData(updatedFormData);
        } else if (pointData.meta && typeof pointData.meta === 'object' && 'type' in pointData.meta) {
          console.log("从meta.type字段获取类型:", pointData.meta.type);
          const updatedFormData = {
            ...newFormData,
            type: pointData.meta.type as string
          };
          setFormData(updatedFormData);
        }
      }
    } catch (error) {
      console.error("处理评查点数据时出错:", error);
      throw new Error(`处理评查点数据时出错: ${error instanceof Error ? error.message : '未知错误'}`);
    }
  };

  // 更新抽取字段列表，用于在评查规则中选择
  const updateExtractionFields = (fields: string[]) => {
    setExtractionFields(fields);
  };

  // 处理BasicInfo组件数据变更
  const handleBasicInfoChange = (data: Record<string, unknown>) => {
    setFormData(prevData => ({
      ...prevData,
      ...data
    }));
  };

  // 处理ExtractionSettings组件数据变更
  const handleExtractionSettingsChange = (data: Record<string, unknown>) => {
    setFormData(prevData => {
      // 获取数据
      const regexFields = data.regexFields as Array<{ id: string; fieldName: string; regex: string }>;
      const fields = data.fields as Record<string, string[]>;
      const allFields = data.allFields as string[];
      
      if (allFields && allFields.length > 0) {
        // 更新抽取字段列表，用于在规则设置中使用
        updateExtractionFields(allFields);
      }
      
      // 根据抽取方法更新对应字段
      const updatedExtractionConfig = { ...prevData.extraction_config };
      
      // 更新正则抽取字段 - 修改这里，只要有字段名就保存，不要求正则必须有内容
      if (regexFields) {
        updatedExtractionConfig.ocr_regex.fields = regexFields
          .filter(field => field.fieldName && field.fieldName.trim() !== '')
          .map(field => ({
            id: field.id || `${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
            fieldName: field.fieldName,
            regex: field.regex || ''  // 即使正则为空也保留
          }));
      }
      
      // 更新LLM字段
      if (fields) {
        if (fields.llm_ocr) {
          updatedExtractionConfig.llm_ocr.fields = fields.llm_ocr;
        }
        if (fields.llm) {
          updatedExtractionConfig.llm_vl.fields = fields.llm;
        }
      }
      
      // 更新提示词设置
      if (data.promptSettings) {
        const promptSettings = data.promptSettings as Record<string, Record<string, unknown>>;
        
        // 更新大模型抽取提示词设置
        if (promptSettings.llm_ocr) {
          updatedExtractionConfig.llm_ocr.prompt_setting = {
            type: promptSettings.llm_ocr.type as string || 'system',
            template: promptSettings.llm_ocr.content as string || ''
          };
        }
        
        // 更新多模态抽取提示词设置
        if (promptSettings.llm) {
          updatedExtractionConfig.llm_vl.prompt_setting = {
            type: promptSettings.llm.type as string || 'system',
            template: promptSettings.llm.content as string || ''
          };
        }
      } else {
        // 兼容旧的API
        if (data.promptType) {
          const promptType = data.promptType as Record<string, string>;
          if (promptType.llm_ocr) {
            updatedExtractionConfig.llm_ocr.prompt_setting.type = promptType.llm_ocr;
          }
          if (promptType.llm) {
            updatedExtractionConfig.llm_vl.prompt_setting.type = promptType.llm;
          }
        }
        
        if (data.promptContent) {
          const promptContent = data.promptContent as Record<string, string>;
          if (promptContent.llm_ocr) {
            updatedExtractionConfig.llm_ocr.prompt_setting.template = promptContent.llm_ocr;
          }
          if (promptContent.llm) {
            updatedExtractionConfig.llm_vl.prompt_setting.template = promptContent.llm;
          }
        }
      }
      
      // 记录状态更新到控制台以便调试
      console.log("抽取设置更新:", {
        fields: fields,
        regexFields: regexFields,
        extractionConfig: updatedExtractionConfig
      });
      
      return {
        ...prevData,
        extraction_config: updatedExtractionConfig
      };
    });
  };

  // 处理ReviewSettings组件数据变更
  const handleReviewSettingsChange = (data: Record<string, unknown>) => {
    setFormData(prevData => {
      const updatedData = { ...prevData };
      
      // 记录所有收到的数据
      console.log("评查设置更新数据:", data);
      
      // 更新规则
      if (data.rules) {
        updatedData.evaluation_config.rules = data.rules as Rule[];
      }
      
      // 更新组合逻辑
      if (data.combinationLogic !== undefined) {
        updatedData.evaluation_config.logicType = data.combinationLogic as string;
      }
      
      // 更新自定义逻辑
      if (data.customLogic !== undefined) {
        updatedData.evaluation_config.customLogic = data.customLogic as string;
      }
      
      // 更新通过/不通过/建议消息
      if (data.pass_message !== undefined) {
        updatedData.pass_message = data.pass_message as string;
      }
      
      if (data.fail_message !== undefined) {
        updatedData.fail_message = data.fail_message as string;
      }
      
      if (data.suggestion_message !== undefined) {
        updatedData.suggestion_message = data.suggestion_message as string;
      }
      
      if (data.suggestion_message_type !== undefined) {
        updatedData.suggestion_message_type = data.suggestion_message_type as string;
      }
      
      // 更新评查后动作
      if (data.post_action !== undefined) {
        updatedData.post_action = data.post_action as string;
      }
      
      if (data.action_config !== undefined) {
        updatedData.action_config = data.action_config as string;
      }
      
      // 更新分数
      if (data.score !== undefined) {
        const scoreValue = parseFloat(data.score as string);
        updatedData.score = isNaN(scoreValue) ? 0 : scoreValue;
      }
      
      // 更新分数显示值
      if (data.scoreDisplay !== undefined) {
        updatedData.scoreDisplay = data.scoreDisplay as string;
      }
      
      return updatedData;
    });
  };

  // 格式化数据，准备提交到接口
  const formatDataForApi = (formData: FormDataType, isDraft: boolean = false) => {
    // 转换提取配置为符合数据库格式的JSON
    const extractionConfig = {
      llm: {
        fields: formData.extraction_config.llm_ocr.fields || [],
        prompt_setting: {
          type: formData.extraction_config.llm_ocr.prompt_setting.type || 'system',
          template: formData.extraction_config.llm_ocr.prompt_setting.template || ''
        }
      },
      vlm: {
        fields: (formData.extraction_config.llm_vl.fields || []).map((field: string) => {
          // 处理带有类型后缀的字段名 (如 "字段名_类型")
          if (field.includes('_')) {
            const [name, type] = field.split('_');
            return {
              name,
              type: type || 'default'
            };
          }
          return { name: field, type: 'default' };
        }),
        prompt_setting: {
          type: formData.extraction_config.llm_vl.prompt_setting.type || 'system',
          template: formData.extraction_config.llm_vl.prompt_setting.template || ''
        }
      },
      regex: {
        fields: (formData.extraction_config.ocr_regex.fields || [])
          .filter(field => field.fieldName && field.fieldName.trim() !== '')
          .map((field: RegexField) => {
            return {
              field: field.fieldName || '',
              pattern: field.regex || ''
            };
          })
      }
    };
    
    // 转换评查配置为符合数据库格式的JSON
    const evaluationConfig = {
      logicType: formData.evaluation_config.logicType || 'and',
      customLogic: formData.evaluation_config.customLogic || '',
      rules: (formData.evaluation_config.rules || [])
        .filter((rule: Rule) => rule.type && rule.type.trim() !== '')
        .map((rule: Rule) => {
          let config = {};
          
          // 根据不同的规则类型生成对应的配置
          switch (rule.type) {
            case 'exists': 
              config = {
                fields: rule.config?.fields || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'consistency':
              config = {
                pairs: rule.config?.pairs || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'format':
              config = {
                field: rule.config?.field || '',
                formatType: rule.config?.formatType || '',
                parameters: rule.config?.parameters || ''
              };
              break;
            case 'logic':
              config = {
                conditions: rule.config?.conditions || [],
                logic: rule.config?.logicRelation || 'and'
              };
              break;
            case 'regex':
              config = {
                field: rule.config?.field || '',
                pattern: rule.config?.pattern || '',
                matchType: rule.config?.matchType || 'match'
              };
              break;
            case 'ai':
              config = {
                model: rule.config?.model || 'qwen14b',
                temperature: rule.config?.temperature || 0.1,
                prompt: rule.config?.prompt || ''
              };
              break;
            case 'code':
              config = {
                language: rule.config?.language || 'javascript',
                code: rule.config?.code || ''
              };
              break;
            default:
              config = { ...rule.config };
              // 清除辅助字段，避免发送无效数据
              if (typeof config === 'object' && config !== null) {
                delete (config as Record<string, unknown>).availableFields;
              }
          }
          
          return {
            id: rule.id,
            type: rule.type,
            config
          };
        })
    };

    // 构建完整的评查点数据
    const evaluationPointData: Record<string, unknown> = {
      code: formData.code,
      name: formData.name,
      evaluation_point_groups_id: formData.evaluation_point_groups_id,
      evaluation_point_groups_pid: formData.evaluation_point_groups_pid,
      risk: formData.risk,
      description: formData.description,
      is_enabled: isDraft ? false : formData.is_enabled,
      references_laws: formData.references_laws,
      extraction_config: extractionConfig,
      evaluation_config: evaluationConfig,
      pass_message: formData.pass_message || '文档检查通过，符合规范要求。',
      fail_message: formData.fail_message || '文档存在以下问题，请修改后重新提交。',
      suggestion_message: formData.suggestion_message || '',
      suggestion_message_type: formData.suggestion_message_type || 'warning',
      post_action: formData.post_action || 'none',
      action_config: formData.action_config || '',
      score: Number(formData.score) // 确保是数字类型
    };

    // 如果是编辑模式，添加ID
    if (isEditMode && formData.id) {
      evaluationPointData.id = formData.id;
    }
    
    // 打印最终发送的数据
    console.log("最终API数据格式:", JSON.stringify(evaluationPointData, null, 2));
    
    return evaluationPointData;
  };

  // 确定使用的HTTP方法和URL
  const getEndpointAndMethod = (id?: number) => {
    let method = 'POST';
    let endpoint = 'http://127.0.0.1:9000/admin/evaluation_points';
  
    // 如果是编辑模式，使用PATCH更新现有记录
    if (id) {
      method = 'PATCH';
      endpoint = `http://127.0.0.1:9000/admin/evaluation_points?id=eq.${id}`;
    }

    return { method, endpoint };
  };

  // 保存评查点
  const handleSave = async () => {
    try {
      setIsLoading(true);
      
      // 基本验证
      if (!formData.name || !formData.code) {
        alert("请填写评查点名称和编码，这些是必填项");
        setIsLoading(false);
        return;
      }
      
      // 检查评查点类型
      if (!formData.type) {
        alert("请选择评查点类型");
        setIsLoading(false);
        return;
      }
      
      // 检查所属规则组
      if (!formData.evaluation_point_groups_id) {
        alert("请选择所属规则组");
        setIsLoading(false);
        return;
      }
      
      // 检查评查规则
      if (!formData.evaluation_config.rules || formData.evaluation_config.rules.length === 0 ||
          !formData.evaluation_config.rules.some(rule => rule.type && rule.type.trim() !== '')) {
        alert("请至少添加一条有效的评查规则");
        setIsLoading(false);
        return;
      }
      
      const evaluationPointData = formatDataForApi(formData);
      console.log("保存数据:", evaluationPointData);
      
      const { method, endpoint } = getEndpointAndMethod(formData.id);
      console.log(`发送${method}请求到`, endpoint);
      
      // 发送数据到API
      const response = await fetch(endpoint, {
        method: method,
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        },
        body: JSON.stringify(evaluationPointData)
      });
      
      // 输出完整响应信息
      console.log("响应状态:", response.status, response.statusText);
      
      if (!response.ok) {
        const errorText = await response.text();
        console.error(`API响应错误: ${response.status}`, errorText);
        try {
          // 尝试解析错误响应为JSON
          const errorJson = JSON.parse(errorText);
          console.error("解析的错误JSON:", errorJson);
          throw new Error(`保存失败: ${errorJson.msg || response.statusText}`);
        } catch (parseError) {
          // 如果无法解析为JSON，使用原始文本
          throw new Error(`保存失败: ${response.status} - ${errorText}`);
        }
      }
      
      const contentType = response.headers.get('content-type');
      console.log("响应Content-Type:", contentType);
      
      // 检查是否是JSON响应
      if (contentType && contentType.includes('application/json')) {
        const responseData = await response.json();
        console.log("API响应数据:", responseData);
        
        // 处理响应
        await handleApiResponse(responseData, isEditMode);
      } else {
        // 非JSON响应
        const text = await response.text();
        console.log("非JSON响应:", text);
        alert("操作已完成!");
        navigate('/rules');
      }
    } catch (error) {
      console.error('保存失败:', error);
      alert(`保存失败: ${error instanceof Error ? error.message : '未知错误'}`);
    } finally {
      setIsLoading(false);
    }
  };
  
  // 保存为草稿
  const handleSaveDraft = async () => {
    try {
      setIsLoading(true);
      
      // 基本验证
      if (!formData.name || !formData.code) {
        alert("请填写评查点名称和编码，这些是必填项");
        setIsLoading(false);
        return;
      }
      
      // 检查评查点类型
      if (!formData.type) {
        alert("请选择评查点类型");
        setIsLoading(false);
        return;
      }
      
      // 检查所属规则组
      if (!formData.evaluation_point_groups_id) {
        alert("请选择所属规则组");
        setIsLoading(false);
        return;
      }
      
      const draftData = formatDataForApi(formData, true);
      console.log("保存草稿数据:", draftData);
      
      const { method, endpoint } = getEndpointAndMethod(formData.id);
      console.log(`发送${method}请求到`, endpoint);
      
      // 发送数据到API
      const response = await fetch(endpoint, {
        method: method,
        headers: {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        },
        body: JSON.stringify(draftData)
      });
      
      // 输出完整响应信息
      console.log("响应状态:", response.status, response.statusText);
      
      if (!response.ok) {
        const errorText = await response.text();
        console.error(`API响应错误: ${response.status}`, errorText);
        try {
          // 尝试解析错误响应为JSON
          const errorJson = JSON.parse(errorText);
          console.error("解析的错误JSON:", errorJson);
          throw new Error(`保存失败: ${errorJson.msg || response.statusText}`);
        } catch (parseError) {
          // 如果无法解析为JSON，使用原始文本
          throw new Error(`保存失败: ${response.status} - ${errorText}`);
        }
      }
      
      const contentType = response.headers.get('content-type');
      console.log("响应Content-Type:", contentType);
      
      // 检查是否是JSON响应
      if (contentType && contentType.includes('application/json')) {
        const responseData = await response.json();
        console.log("API响应数据:", responseData);
        
        // 处理响应
        await handleApiResponse(responseData, isEditMode, true);
      } else {
        // 非JSON响应
        const text = await response.text();
        console.log("非JSON响应:", text);
        alert("草稿已保存!");
        navigate('/rules');
      }
    } catch (error) {
      console.error('保存草稿失败:', error);
      alert(`保存草稿失败: ${error instanceof Error ? error.message : '未知错误'}`);
    } finally {
      setIsLoading(false);
    }
  };

  // 处理API响应
  const handleApiResponse = async (
    responseData: 
      | ApiResponse
      | Array<{ id: number; [key: string]: unknown }>,
    isEditMode: boolean, 
    isDraft: boolean = false
  ) => {
    // 适配新的API响应格式
    if (responseData && typeof responseData === 'object') {
      // 符合新的API规范
      if ('code' in responseData && responseData.code === 0) {
        console.log(`${isDraft ? '草稿' : ''}保存成功 (新API格式):`, responseData.data);
        alert(`${isDraft ? '草稿' : ''}保存成功!`);
        
        // 根据操作类型重定向
        if (isEditMode) {
          // 编辑模式，保留在当前编辑页面
          navigate(`/rules/new?id=${formData.id}`);
        } else {
          // 创建模式，跳转到新创建数据的编辑页面
          let newId: number | undefined;
          
          if (responseData.data && Array.isArray(responseData.data) && responseData.data.length > 0) {
            newId = responseData.data[0].id as number;
          } else if (responseData.data && typeof responseData.data === 'object' && 'id' in responseData.data) {
            newId = responseData.data.id as number;
          }
          
          if (newId) {
            navigate(`/rules/new?id=${newId}`);
          } else {
            // 无法获取ID，返回列表页
            navigate('/rules');
          }
        }
        return;
      } else if ('code' in responseData && responseData.code !== 0) {
        // API返回错误
        console.warn("API返回错误:", responseData.msg);
        throw new Error(responseData.msg as string || "操作失败");
      }
    }
    
    // 兼容处理旧的响应格式
    if (Array.isArray(responseData)) {
      if (responseData.length > 0) {
        console.log(`${isDraft ? '草稿' : ''}保存成功 (数组响应):`, responseData[0]);
        alert(`${isDraft ? '草稿' : ''}保存成功!`);
        
        // 如果是创建，跳转到编辑页面
        if (!isEditMode && responseData[0].id) {
          navigate(`/rules/new?id=${responseData[0].id}`);
        } else {
          navigate('/rules');
        }
      } else {
        console.warn("响应数组为空");
        alert("操作已完成，但服务器未返回数据");
        navigate('/rules');
      }
    } else if (responseData && typeof responseData === 'object') {
      if ('data' in responseData && responseData.data) {
        console.log(`${isDraft ? '草稿' : ''}保存成功 (带data字段):`, responseData.data);
        alert(`${isDraft ? '草稿' : ''}保存成功!`);
        
        let newId: number | undefined;
        if (Array.isArray(responseData.data) && responseData.data.length > 0 && 'id' in responseData.data[0]) {
          newId = responseData.data[0].id as number;
        } else if (typeof responseData.data === 'object' && 'id' in responseData.data) {
          newId = responseData.data.id as number;
        }
        
        if (!isEditMode && newId) {
          navigate(`/rules/new?id=${newId}`);
        } else {
          navigate('/rules');
        }
      } else if ('id' in responseData && responseData.id) {
        console.log(`${isDraft ? '草稿' : ''}保存成功 (直接对象):`, responseData);
        alert(`${isDraft ? '草稿' : ''}保存成功!`);
        
        if (!isEditMode) {
          navigate(`/rules/new?id=${responseData.id as number}`);
        } else {
          navigate('/rules');
        }
      } else {
        console.warn("响应对象格式不符合预期", responseData);
        alert("操作已完成，但返回的数据格式不符合预期");
        navigate('/rules');
      }
    } else {
      console.warn("响应不是数组或对象", responseData);
      alert("操作已完成，但返回的数据格式不符合预期");
      navigate('/rules');
    }
  };

  // 当评查点组数据和表单数据都加载完成后，确保类型信息被正确设置
  useEffect(() => {
    // 仅在编辑模式下，且表单数据已加载，评查点组数据也已加载的情况下执行
    if (
      isEditMode && 
      formData.id && 
      evaluationPointGroups.length > 0 && 
      (!formData.type || formData.type === '')
    ) {
      console.log("检测到编辑模式下类型未设置，尝试自动设置类型");
      
      // 首先尝试通过evaluation_point_groups_pid设置类型
      if (formData.evaluation_point_groups_pid) {
        const typeGroup = evaluationPointGroups.find(
          group => group.id === formData.evaluation_point_groups_pid
        );
        
        if (typeGroup) {
          console.log("通过评查点类型ID找到类型组:", typeGroup);
          setFormData(prevData => ({
            ...prevData,
            type: typeGroup.code
          }));
          console.log("自动设置类型为:", typeGroup.code);
          return;
        }
      }
      
      // 如果无法通过evaluation_point_groups_pid设置，尝试通过evaluation_point_groups_id设置
      if (formData.evaluation_point_groups_id) {
        const ruleGroup = evaluationPointGroups.find(
          group => group.id === formData.evaluation_point_groups_id
        );
        
        if (ruleGroup && ruleGroup.pid && ruleGroup.pid !== 0) {
          const parentGroup = evaluationPointGroups.find(
            group => group.id === ruleGroup.pid
          );
          
          if (parentGroup) {
            console.log("通过规则组找到父级类型组:", parentGroup);
            setFormData(prevData => ({
              ...prevData,
              type: parentGroup.code,
              evaluation_point_groups_pid: parentGroup.id
            }));
            console.log("自动设置类型为:", parentGroup.code);
          }
        }
      }
    }
  }, [isEditMode, formData.id, formData.type, formData.evaluation_point_groups_id, formData.evaluation_point_groups_pid, evaluationPointGroups]);

  return (
    <div className="container">
      <PageHeader 
        title={isEditMode ? "编辑评查点" : "新增评查点"} 
        onSave={handleSave} 
      />
      
      {isLoading ? (
        <div className="flex justify-center items-center p-12">
          <div className="loading-spinner"></div>
          <span className="ml-3">加载中...</span>
        </div>
      ) : (
        <>
          <div className="mb-8">
            <BasicInfo 
              onChange={handleBasicInfoChange} 
              initialData={formData} 
              evaluationPointGroups={evaluationPointGroups}
            />
          </div>
          
          <div className="mb-8">
            <RuleContext.Provider value={{ extractionFields, updateFields: updateExtractionFields }}>
              <ExtractionSettings 
                onChange={handleExtractionSettingsChange} 
                initialData={{
                  llm_ocr: formData.extraction_config.llm_ocr,
                  llm_vl: formData.extraction_config.llm_vl,
                  ocr_regex: formData.extraction_config.ocr_regex
                }} 
              />
            </RuleContext.Provider>
          </div>
          
          <div className="mb-8">
            <RuleContext.Provider value={{ extractionFields, updateFields: updateExtractionFields }}>
              <ReviewSettings 
                onChange={handleReviewSettingsChange} 
                initialData={{
                  rules: formData.evaluation_config.rules,
                  combinationLogic: formData.evaluation_config.logicType,
                  customLogic: formData.evaluation_config.customLogic,
                  pass_message: formData.pass_message,
                  fail_message: formData.fail_message,
                  suggestion_message: formData.suggestion_message,
                  suggestion_message_type: formData.suggestion_message_type,
                  post_action: formData.post_action,
                  action_config: formData.action_config,
                  score: formData.score,
                  scoreDisplay: formData.scoreDisplay
                }} 
              />
            </RuleContext.Provider>
          </div>
          
          <ActionButtons 
            onSave={handleSave}
            onSaveDraft={handleSaveDraft}
            isEditMode={isEditMode}
          />
        </>
      )}
    </div>
  );
}
