import React, { useState, useEffect } from 'react'; import { Table, Button, Form, Input, Space, message, Popconfirm, Drawer, Select, Segmented, Badge } from 'antd'; import { PlusOutlined, FileTextOutlined, ProjectOutlined, CheckSquareOutlined } from '@ant-design/icons'; import { supabaseService } from '@/hooks/supabaseService'; const Classify = ({activeType,typeList}) => { const [data, setData] = useState([]); const [loading, setLoading] = useState(false); const [editingKey, setEditingKey] = useState(''); const [form] = Form.useForm(); const [filterType, setFilterType] = useState('all'); // 'all', 'common', 'current' const fetchCategories = async (type = activeType, filterTypeValue = filterType) => { setLoading(true); try { let filterCondition; switch (filterTypeValue) { case 'common': filterCondition = { eq: 'common' }; break; case 'current': filterCondition = { eq: type }; break; default: filterCondition = { in: `(${type},common)` }; } const { data: categories } = await supabaseService.select('resources', { filter: { 'type': { eq: 'categories' }, 'attributes->>template_type': filterCondition }, order: { column: 'created_at', ascending: false } }); setData(categories || []); } catch (error) { message.error('获取分类数据失败'); console.error(error); } finally { setLoading(false); } }; useEffect(() => { fetchCategories(activeType, filterType); }, [activeType]); // 新增分类 const handleAdd = (type) => { const newData = { id: Date.now().toString(), attributes: { name: '', template_type: type }, isNew: true }; setData([newData, ...data]); setEditingKey(newData.id); form.setFieldsValue(newData.attributes); }; // 保存分类数据 const handleSave = async (record) => { try { const values = await form.validateFields(); if (record.isNew) { await supabaseService.insert('resources', { type: 'categories', attributes: { name: values.name, template_type: values.template_type }, schema_version: 1 }); } else { await supabaseService.update('resources', { id: record.id }, { attributes: { name: values.name, template_type: values.template_type }, updated_at: new Date().toISOString() } ); } message.success('保存成功'); setEditingKey(''); fetchCategories(); } catch (error) { message.error('保存失败'); console.error(error); } }; // 删除分类 const handleDelete = async (record) => { try { await supabaseService.delete('resources', { id: record.id }); message.success('删除成功'); fetchCategories(); } catch (error) { message.error('删除失败'); console.error(error); } }; const columns = [ { title: '分类名称', dataIndex: ['attributes', 'name'], render: (text, record) => { const isEditing = record.id === editingKey; return isEditing ? (