import React, { useState } from 'react';
import { Button, Col, Form, Input, Row, message, Select, Space, DatePicker } from 'antd';
import { ClearOutlined, SearchOutlined, ExpandOutlined, CompressOutlined } from '@ant-design/icons';
import styles from "../StaffInfo.less";

const FormItem = Form.Item;
const { Option } = Select; 
const StaffInfoRenderSimpleForm = (props) => {
    const [form] = Form.useForm();
    const [expandForm, setExpandForm] = useState(false);
    const { submitButtons, handleSearch, handleFormReset, toggleForm, params } = props;
    
    React.useEffect(() => {
        form.setFieldsValue({
            name: params?.name,
            phone: params?.phone,
            idCard: params?.idCard,
            department: params?.department,
            position: params?.position,
            status: params?.status,
        });
    }, [params]);

    const onFinish = values => {
        const searchParams = {
            ...values,
            entryDateStart: values.entryDateStart ? values.entryDateStart.format('YYYY-MM-DD') : undefined,
            entryDateEnd: values.entryDateEnd ? values.entryDateEnd.format('YYYY-MM-DD') : undefined,
        };
        handleSearch && handleSearch(searchParams);
    };

    const myhandleFormReset = () => {
        form.resetFields();
        handleFormReset && handleFormReset();
    };

    const toggleExpandForm = () => {
        setExpandForm(!expandForm);
    };

    const onEndDateChange = (value) => {
        const startDate = form.getFieldValue("entryDateStart");
        if (startDate && value && value.isBefore(startDate, "day")) {
            message.error("结束日期不能小于开始日期！");
            form.setFieldsValue({ entryDateEnd: undefined });
        }
    };

    return (
        <div className={styles.searchFormContainer}>
            <Form
                form={form}
                onFinish={onFinish}
                layout="vertical"
                className={styles.searchForm}
            >
                <Row gutter={16}>
                    <Col span={6}>
                        <Form.Item
                            name="name"
                            label="姓名"
                        >
                            <Input
                                placeholder="请输入姓名"
                                allowClear
                            />
                        </Form.Item>
                    </Col>
                    <Col span={6}>
                        <Form.Item
                            name="phone"
                            label="电话"
                        >
                            <Input
                                placeholder="请输入电话"
                                allowClear
                            />
                        </Form.Item>
                    </Col>
                    <Col span={6}>
                        <Form.Item
                            name="department"
                            label="部门"
                        >
                            <Select placeholder="请选择部门" allowClear>
                                <Option value="技术部">技术部</Option>
                                <Option value="产品部">产品部</Option>
                                <Option value="运营部">运营部</Option>
                                <Option value="财务部">财务部</Option>
                                <Option value="人事部">人事部</Option>
                            </Select>
                        </Form.Item>
                    </Col>
                    <Col span={6}>
                        <Form.Item label=" " colon={false}>
                            <Space size="middle">
                                <Button
                                    type="primary"
                                    htmlType="submit"
                                    icon={<SearchOutlined />}
                                    className={styles.searchButton}
                                >
                                    查询
                                </Button>
                                <Button
                                    onClick={myhandleFormReset}
                                    icon={<ClearOutlined />}
                                    className={styles.resetButton}
                                >
                                    重置
                                </Button>
                                <Button
                                    type="link"
                                    onClick={toggleExpandForm}
                                    icon={expandForm ? <CompressOutlined /> : <ExpandOutlined />}
                                    className={styles.expandButton}
                                >
                                    {expandForm ? '收起' : '展开'}
                                </Button>
                            </Space>
                        </Form.Item>
                    </Col>
                </Row>

                {/* 展开的表单项 */}
                {expandForm && (
                    <Row gutter={16}>
                        <Col span={6}>
                            <Form.Item
                                name="idCard"
                                label="身份证号"
                            >
                                <Input
                                    placeholder="请输入身份证号"
                                    allowClear
                                />
                            </Form.Item>
                        </Col>
                        <Col span={6}>
                            <Form.Item
                                name="position"
                                label="岗位"
                            >
                                <Select placeholder="请选择岗位" allowClear>
                                    <Option value="高级工程师">高级工程师</Option>
                                    <Option value="产品经理">产品经理</Option>
                                    <Option value="运营专员">运营专员</Option>
                                    <Option value="会计">会计</Option>
                                    <Option value="HR专员">HR专员</Option>
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={6}>
                            <Form.Item
                                name="status"
                                label="状态"
                            >
                                <Select placeholder="请选择状态" allowClear>
                                    <Option value="1">在职</Option>
                                    <Option value="0">离职</Option>
                                    <Option value="2">试用期</Option>
                                </Select>
                            </Form.Item>
                        </Col>
                        <Col span={6}>
                            <Form.Item
                                name="entryDateStart"
                                label="入职开始日期"
                            >
                                <DatePicker
                                    placeholder="请选择开始日期"
                                    style={{ width: '100%' }}
                                />
                            </Form.Item>
                        </Col>
                    </Row>
                )}
            </Form>
        </div>
    );

};

export default StaffInfoRenderSimpleForm;
