import React, { useState, useEffect } from 'react';
import { Input, Button, Select, message, Modal, Form } from 'antd';
import { SearchOutlined, PlusOutlined, DeleteOutlined, EditOutlined } from '@ant-design/icons';
import StandardTable from '@/components/StandardTable';
import styles from './LawsAndRegulations.less';

const { Option } = Select;

const LawsAndRegulations = () => {
    const [loading, setLoading] = useState(false);
    const [selectedRowKeys, setSelectedRowKeys] = useState([]);
    const [searchValue, setSearchValue] = useState('');
    const [pagination, setPagination] = useState({
        current: 1,
        pageSize: 10,
        total: 48,
        showSizeChanger: true,
        showQuickJumper: true,
        showTotal: (total, range) => `共${total}条`,
    });

    // 模拟数据
    const [dataSource, setDataSource] = useState([
        {
            key: '1',
            name: '大气污染物综合排放标准',
            type: '国家标准',
            file: 'SLAB USER(CN) .pdf',
            remark: '无',
        },
        {
            key: '2',
            name: '危险化学品安全管理条例',
            type: '行业与领域规范',
            file: 'SLAB USER(CN) .pdf',
            remark: '针对危险化学品的生产、储存、使用、经营、运输等环节提出了具体要求。',
        },
        {
            key: '3',
            name: '安全生产事故应急预案管理办法',
            type: '行业与领域规范',
            file: 'SLAB USER(CN) .pdf',
            remark: '细化了生产经营单位需要安全生产责任的具体内容。',
        },
    ]);

    // 表格列配置
    const columns = [
        {
            title: '法规名称',
            dataIndex: 'name',
            key: 'name',
            width: 250,
        },
        {
            title: '法规类型',
            dataIndex: 'type',
            key: 'type',
            width: 150,
        },
        {
            title: '附件',
            dataIndex: 'file',
            key: 'file',
            width: 180,
            render: (text) => (
                <a href="#">{text}</a>
            ),
        },
        {
            title: '备注',
            dataIndex: 'remark',
            key: 'remark',
            width: 400,
        },
    ];

    // 搜索处理
    const handleSearch = () => {
        setLoading(true);
        // 模拟搜索请求
        setTimeout(() => {
            setLoading(false);
            message.success('查询完成');
        }, 1000);
    };

    // 新增处理
    const handleAdd = () => {
        message.info('新增功能待实现');
    };

    // 批量删除处理
    const handleBatchDelete = () => {
        if (selectedRowKeys.length === 0) {
            message.warning('请选择要删除的数据');
            return;
        }
        Modal.confirm({
            title: '确认删除',
            content: `确定要删除选中的 ${selectedRowKeys.length} 条数据吗？`,
            onOk() {
                setDataSource(dataSource.filter(item => !selectedRowKeys.includes(item.key)));
                setSelectedRowKeys([]);
                message.success('删除成功');
            },
        });
    };

    // 编辑处理
    const handleEdit = (record) => {
        message.info(`编辑 ${record.name} 的信息`);
    };

    // 删除处理
    const handleDelete = (record) => {
        Modal.confirm({
            title: '确认删除',
            content: `确定要删除 ${record.name} 吗？`,
            onOk() {
                setDataSource(dataSource.filter(item => item.key !== record.key));
                message.success('删除成功');
            },
        });
    };

    // 分页处理
    const handleTableChange = (pagination) => {
        setPagination(pagination);
    };

    return (
        <div className={styles.container}>
            {/* 页面标题 */}
            <div className={styles.header}>
                <div className={styles.titleBar}></div>
                <h2 className={styles.title}>法律法规</h2>
            </div>

            {/* 搜索和操作区域 */}
            <Form
                layout="inline"
                className={styles.searchBar}
                onFinish={handleSearch}
            >
                <Form.Item label="" name="searchValue">
                    <Input
                        placeholder='请输入法规名称'
                        style={{ width: 200 }}
                        value={searchValue}
                        onChange={(e) => setSearchValue(e.target.value)}
                        allowClear
                        className={styles.searchInput}
                    />
                </Form.Item>
                <Form.Item>
                    <Button 
                        type="primary" 
                        icon={<SearchOutlined />}
                        htmlType="submit"
                        loading={loading}
                        className={styles.searchButton}
                    >
                        查询
                    </Button>
                </Form.Item>
            </Form>

            {/* 数据表格 */}
            <div className={styles.tableContainer}>
                <StandardTable
                    columns={columns}
                    data={{
                        list: dataSource,
                        pagination: pagination
                    }}
                    rowKey="key"
                    selectedRows={selectedRowKeys}
                    onSelectRow={setSelectedRowKeys}
                    onChange={handleTableChange}
                    loading={loading}
                />
            </div>
        </div>
    );
};

export default LawsAndRegulations;
