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

import iconsc from '@/assets/yjzygl/iconsc.svg';

const { Option } = Select;

const Yjwz = () => {
    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',
            number: '01',
            unitName: '文登市兴文新材料有限公司',
            materialCategory: '应急救援装备',
            materialName: '正压式空气呼吸器',
            specification: 'DEV2024001',
            unit: '北粮农业股份有限公司',
            quantity: 2,
            storageLocation: '一楼监控室',
            name: '李世敏',
            contactNumber: '18353452884',
        },
        {
            key: '2',
            number: '02',
            unitName: '文登市兴文新材料有限公司',
            materialCategory: '应急救援装备',
            materialName: '过滤式防毒面具',
            specification: 'DEV2024001',
            unit: '北京新态互动科贸有限公司',
            quantity: 7,
            storageLocation: '一楼监控室',
            name: '赵小瑞',
            contactNumber: '18847168496',
        },
        {
            key: '3',
            number: '03',
            unitName: '合鸿新材科技有限公司',
            materialCategory: '应急救援装备',
            materialName: '连体防护服',
            specification: 'DEV2024001',
            unit: '北京达斯可恩食品有限公司',
            quantity: 8,
            storageLocation: '一楼监控室',
            name: '钱珈艺',
            contactNumber: '18437766264',
        },
        {
            key: '4',
            number: '04',
            unitName: '山东万图高分子材料股份有限公司',
            materialCategory: '个人防护装备',
            materialName: '防爆背心',
            specification: 'DEV2024001',
            unit: '腾大科技发展(北京)有限公司',
            quantity: 23,
            storageLocation: '一楼监控室',
            name: '赵雾瑕',
            contactNumber: '15014254724',
        },
        {
            key: '5',
            number: '05',
            unitName: '合鸿新材科技有限公司',
            materialCategory: '应急物资',
            materialName: '逃生面具',
            specification: 'DEV2024001',
            unit: '北京长友物业管理有限公司',
            quantity: 12,
            storageLocation: '一楼监控室',
            name: '李彤运',
            contactNumber: '15151314652',
        },
        {
            key: '6',
            number: '06',
            unitName: '山东万图高分子材料股份有限公司',
            materialCategory: '应急救援装备',
            materialName: '移动式防爆应急灯',
            specification: 'DEV2024001',
            unit: '北京金益邦科技有限公司',
            quantity: 9,
            storageLocation: '一楼监控室',
            name: '钱唯',
            contactNumber: '13322876802',
        },
    ]);

    // 表格列配置
    const columns = [
        {
            title: '编号',
            dataIndex: 'number',
            key: 'number',
            width: 80,
        },
        {
            title: '单位名称',
            dataIndex: 'unitName',
            key: 'unitName',
            width: 200,
        },
        {
            title: '物资类别',
            dataIndex: 'materialCategory',
            key: 'materialCategory',
            width: 120,
        },
        {
            title: '物资名称',
            dataIndex: 'materialName',
            key: 'materialName',
            width: 150,
        },
        {
            title: '规格型号',
            dataIndex: 'specification',
            key: 'specification',
            width: 120,
        },
        {
            title: '单位',
            dataIndex: 'unit',
            key: 'unit',
            width: 150,
        },
        {
            title: '数量',
            dataIndex: 'quantity',
            key: 'quantity',
            width: 80,
        },
        {
            title: '存放地点',
            dataIndex: 'storageLocation',
            key: 'storageLocation',
            width: 120,
        },
        {
            title: '姓名',
            dataIndex: 'name',
            key: 'name',
            width: 100,
        },
        {
            title: '联系电话',
            dataIndex: 'contactNumber',
            key: 'contactNumber',
            width: 130,
        },
        {
            title: '操作',
            key: 'action',
            width: 120,
            render: (text, record) => (
                <div className={styles.actionButtons}>
                    <Button 
                        type="link" 
                        size="small" 
                        // icon={<EditOutlined />}
                        onClick={() => handleEdit(record)}
                    >
                        修改
                    </Button>
                    <Button 
                        type="link" 
                        size="small" 
                        danger
                        // icon={<DeleteOutlined />}
                        onClick={() => handleDelete(record)}
                    >
                        删除
                    </Button>
                </div>
            ),
        },
    ];

    // 搜索处理
    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>

            {/* 搜索和操作区域 */}
            <div className={styles.searchBar}>
                <div className={styles.searchLeft}>
                    <Select
                        placeholder="请选择单位名称"
                        value={searchValue}
                        onChange={setSearchValue}
                        style={{width: 180, height: 30, borderRadius: 2}}
                        allowClear
                    >
                        <Option value="文登市兴文新材料有限公司">文登市兴文新材料有限公司</Option>
                        <Option value="合鸿新材科技有限公司">合鸿新材科技有限公司</Option>
                        <Option value="山东万图高分子材料股份有限公司">山东万图高分子材料股份有限公司</Option>
                    </Select>
                    <Input
                        placeholder="请输入物资名称"
                        // value={searchValue}
                        // onChange={setSearchValue}
                        style={{width: 180, height: 30, borderRadius: 2}}
                        allowClear
                    />
  
                    <Button 
                        type="primary" 
                        icon={<SearchOutlined />}
                        onClick={handleSearch}
                        loading={loading}
                        className={styles.customButton}
                    >
                        查询
                    </Button>
                </div>
                <div className={styles.searchRight}>
                    <Button 
                        type="primary" 
                        icon={<PlusOutlined />}
                        onClick={handleAdd}
                        className={styles.customButton}
                    >
                        新增
                    </Button>
                    <Button 
                        danger
                        style={{width: 70, height: 30, borderRadius: 2, display: 'flex', alignItems: 'center', justifyContent: 'center'}}
                        icon={<img src={iconsc} alt="delete"  style={{width: 14, height: 14, marginTop: -2}}/>}
                        onClick={handleBatchDelete}
                    >
                        删除
                    </Button>
                </div>
            </div>

            {/* 数据表格 */}
            <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 Yjwz;
