import React, { useState } from 'react';
import { Input, Button, Select, message, Modal, DatePicker } from 'antd';
import { SearchOutlined, PlusOutlined, DeleteOutlined } from '@ant-design/icons';
import StandardTable from '@/components/StandardTable';
import styles from './DutyLog.less';
import iconsc from '@/assets/yjzygl/iconsc.svg';

const { Option } = Select;
const { RangePicker } = DatePicker;

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

    // 模拟数据
    const [dataSource, setDataSource] = useState([
        {
            key: '1',
            number: '01',
            logDate: '2025-10-28 08:00',
            dutyPerson: '张三',
            dutyType: '白班',
            logContent: '正常巡检，未发现异常情况',
            weather: '晴',
            temperature: '18°C',
            recorder: '张三',
            status: '已提交',
        },
        {
            key: '2',
            number: '02',
            logDate: '2025-10-28 20:00',
            dutyPerson: '李四',
            dutyType: '夜班',
            logContent: '夜间巡检，设备运行正常',
            weather: '晴',
            temperature: '12°C',
            recorder: '李四',
            status: '已提交',
        },
        {
            key: '3',
            number: '03',
            logDate: '2025-10-27 08:00',
            dutyPerson: '王五',
            dutyType: '白班',
            logContent: '发现A区域照明设备故障，已报修',
            weather: '多云',
            temperature: '16°C',
            recorder: '王五',
            status: '已处理',
        },
    ]);

    // 表格列配置
    const columns = [
        {
            title: '编号',
            dataIndex: 'number',
            key: 'number',
            width: 80,
        },
        {
            title: '记录时间',
            dataIndex: 'logDate',
            key: 'logDate',
            width: 150,
        },
        {
            title: '值班人员',
            dataIndex: 'dutyPerson',
            key: 'dutyPerson',
            width: 120,
        },
        {
            title: '值班类型',
            dataIndex: 'dutyType',
            key: 'dutyType',
            width: 100,
        },
        {
            title: '日志内容',
            dataIndex: 'logContent',
            key: 'logContent',
            width: 250,
            ellipsis: true,
        },
        {
            title: '天气',
            dataIndex: 'weather',
            key: 'weather',
            width: 80,
        },
        {
            title: '温度',
            dataIndex: 'temperature',
            key: 'temperature',
            width: 80,
        },
        {
            title: '记录人',
            dataIndex: 'recorder',
            key: 'recorder',
            width: 100,
        },
        {
            title: '状态',
            dataIndex: 'status',
            key: 'status',
            width: 100,
        },
        {
            title: '操作',
            key: 'action',
            width: 150,
            render: (text, record) => (
                <div className={styles.actionButtons}>
                    <Button 
                        type="link" 
                        size="small" 
                        onClick={() => handleView(record)}
                    >
                        查看
                    </Button>
                    <Button 
                        type="link" 
                        size="small" 
                        onClick={() => handleEdit(record)}
                    >
                        修改
                    </Button>
                    <Button 
                        type="link" 
                        size="small" 
                        danger
                        onClick={() => handleDelete(record)}
                    >
                        删除
                    </Button>
                </div>
            ),
        },
    ];

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

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

    // 查看处理
    const handleView = (record) => {
        message.info(`查看日志详情：${record.logContent}`);
    };

    // 批量删除处理
    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(`编辑日志记录`);
    };

    // 删除处理
    const handleDelete = (record) => {
        Modal.confirm({
            title: '确认删除',
            content: `确定要删除该条日志记录吗？`,
            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}>
                    <RangePicker 
                        placeholder={['开始日期', '结束日期']}
                        style={{width: 240, height: 30, borderRadius: 2}}
                    />
                    <Select
                        placeholder="值班类型"
                        value={searchValue}
                        onChange={setSearchValue}
                        style={{width: 120, height: 30, borderRadius: 2}}
                        allowClear
                    >
                        <Option value="白班">白班</Option>
                        <Option value="夜班">夜班</Option>
                    </Select>
                    <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 DutyLog;

