import React, { useState, useEffect } from 'react';
import { Table, Button, Form, Input, Space, message, Popconfirm, Drawer, Select, Tabs, Badge, Radio } from 'antd';
import { PlusOutlined, FileTextOutlined, ProjectOutlined, CheckSquareOutlined } from '@ant-design/icons';
import { supabaseService } from '@/hooks/supabaseService';
const CategoryDrawer = () => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(false);
const [editingKey, setEditingKey] = useState('');
const [drawerVisible, setDrawerVisible] = useState(false);
const [activeType, setActiveType] = useState('quotation'); // 当前选中的类型
const [form] = Form.useForm();
// 模板类型配置
const TEMPLATE_TYPES = [
{
value: 'quotation',
label: '报价单模板',
icon: ,
color: 'blue',
description: '用于创建标准化的报价单'
},
{
value: 'project',
label: '专案模板',
icon: ,
color: 'green',
description: '用于创建项目流程模板'
},
{
value: 'task',
label: '任务模板',
icon: ,
color: 'purple',
description: '用于创建标准任务模板'
}
];
// 获取分类数据
const fetchCategories = async (type = activeType) => {
setLoading(true);
try {
const { data: categories } = await supabaseService.select('resources', {
filter: {
type: { eq: 'categories' },
'attributes->>type': { eq: type }
},
order: {
column: 'created_at',
ascending: false
}
});
setData(categories || []);
} catch (error) {
message.error('获取分类数据失败');
console.error(error);
} finally {
setLoading(false);
}
};
useEffect(() => {
if (drawerVisible) {
fetchCategories();
}
}, [drawerVisible, activeType]);
// 切换模板类型
const handleTypeChange = (type) => {
setActiveType(type);
setEditingKey(''); // 清除编辑状态
form.resetFields(); // 重置表单
};
// 新增分类
const handleAdd = (type) => {
const newData = {
id: Date.now().toString(),
attributes: {
name: '',
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,
type: values.type
},
schema_version: 1
});
} else {
// 更新
await supabaseService.update('resources',
{ id: record.id },
{
attributes: {
name: values.name,
type: values.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 ? (
) : (
{text}
);
},
},
{
title: '模板类型',
dataIndex: ['attributes', 'type'],
render: (text, record) => {
const isEditing = record.id === editingKey;
return isEditing ? (
) : (
{TEMPLATE_TYPES.find(t => t.value === text)?.label || text}
);
},
},
{
title: '操作',
render: (_, record) => {
const isEditing = record.id === editingKey;
return isEditing ? (
) : (
handleDelete(record)}
okText="确定"
cancelText="取消"
okButtonProps={{
className: "bg-red-500 hover:bg-red-600 border-red-500"
}}
>
);
},
},
];
return (
<>
setDrawerVisible(false)}
open={drawerVisible}
className="category-drawer"
>
{/* 类型选择器 */}
handleTypeChange(e.target.value)}
className="flex space-x-4"
>
{TEMPLATE_TYPES.map(type => (
{type.icon}
{type.label}
item.attributes.type === type.value).length}
className={`bg-${type.color}-100 text-${type.color}-600`}
/>
))}
{/* 当前类型说明 */}
t.value === activeType)?.color}-500 text-lg font-medium mb-2 flex items-center`}>
{TEMPLATE_TYPES.find(t => t.value === activeType)?.icon}
{TEMPLATE_TYPES.find(t => t.value === activeType)?.label}
{TEMPLATE_TYPES.find(t => t.value === activeType)?.description}
{/* 新增按钮 */}
{/* 分类列表 */}