import React, {useState} from 'react';
import {WingBlank, List, InputItem, WhiteSpace, DatePicker, Toast, ActivityIndicator} from 'antd-mobile'
import Document from 'react-document-title';
import moment from 'moment'
import axios from  'axios'
import './index.less'

import { createForm } from 'rc-form';
import PeopleInfo from "./PeopleInfo";

const Send = (props) => {
    const {getFieldProps} = props.form;


    const onSubmit = () => {
        props.form.validateFields({ force: true }, (error) => {
            if (!error) {
                const values = props.form.getFieldsValue();
                const keys = Object.keys(values);
                keys.map(item => {
                    !values[item] && (delete values[item]);
                    Array.isArray(values[item]) && values[item].length === 1 && (values[item] = values[item][0]);
                    values[item] instanceof Date && ( values[item] = moment(values[item]).valueOf());
                    return null;
                });
                setLoading(true);
                values.toUid = sP.value;
                values.uid = 'cgcPcPoo';
                axios.patch('/demo/initiateApproval', values).then(res => {
                    setLoading(false);
                    res.data.success && Toast.success('提交成功')
                })
            }
        });
    };

    const [sP, setSP] = useState({});
    const [personVisible, setPersonVisible] = useState(false);
    const [loading, setLoading] = useState(false);

    return (
        <Document title="请假申请">
            <WingBlank>
                <PeopleInfo
                    visible={personVisible}
                    onClose={setPersonVisible}
                    peopleData={sP}
                    onSelect={setSP}
                />
                <WhiteSpace/>
                <form>
                    <List>
                        <InputItem {...getFieldProps('title')}>标题</InputItem>
                        <InputItem value={'cgcPcPoo'}>发送人</InputItem>
                        <DatePicker mode="date" className="send-date-style"{...getFieldProps('startTime')}>
                            <List.Item arrow="horizontal">请假开始时间</List.Item>
                        </DatePicker>
                        <DatePicker mode="date" className="send-date-style"{...getFieldProps('entTime')}>
                            <List.Item arrow="horizontal">请假结束时间</List.Item>
                        </DatePicker>
                        <List.Item className="auto-flex" extra={
                            <div className="pos-aline">
                                <div className="meet-select-btn" onClick={() => setPersonVisible(true)}>选择审核人员</div>
                                <div>{sP.name}</div>
                            </div>
                        }>审核人员</List.Item>
                        {/*<InputItem {...getFieldProps('gzdh')}>工作电话</InputItem>*/}
                    </List>
                </form>
                <div className="event-footer">
                    <div className="event-footer-button" onClick={onSubmit}>提交</div>
                </div>
                {loading && (
                    <ActivityIndicator
                        toast
                        text="提交中..."
                        animating={loading}
                    />
                )}
            </WingBlank>
        </Document>
    );
};

export default createForm()(Send);
