import React, {useState} from 'react';
import {WingBlank, List, InputItem, TextareaItem, WhiteSpace, Toast, DatePicker, Picker} from 'antd-mobile'
import Document from 'react-document-title';
import '../index.less';
import fileRequest from '../../utils/fileRequest';
import Upload from 'rc-upload';


import { createForm } from 'rc-form';


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

    const [loading, setLoading] = useState(false);


    const onSubmit = () => {
        props.form.validateFields({ force: true }, (error) => {
            const values = props.form.getFieldsValue();
            const jsonParam = {...values,  _mail_option: 'send', _mail_has_blob: upload.length > 0};
            setLoading(true);
            const formData = new FormData();
            upload.length > 0 && upload.forEach(file => {
                formData.append('blob', file);
            });
            if (!error) {
                fileRequest({
                    method: 'post',
                    url: '/idtAppServiceV6/jspmail/sendMail',
                    body: {jsonParam: JSON.stringify(jsonParam)},
                    payload: formData
                }).then(res => {
                    Toast.success('发送成功');
                    props.history.push('/blank/mail');
                    setLoading(true)
                });
            }else {
                Toast.fail('请输入完整的主题、收件人、内容')
            }
        });
    };
    const [upload, setUpload] = useState([]);

    const uploaderProps = {
        multiple: true,
        beforeUpload(file) {
            const newUpload = [...upload, file];
            setUpload(newUpload);
            return false
        }
    };
    const uploadDelete = uid => {
        const newData = [...upload];
        const Index = newData.findIndex(item => item.uid === uid);
        Index > -1 && newData.splice(Index, 1);
        setUpload(newData)
    };

    const userInfo = localStorage.getItem('userInfo')
    return (
        <Document title="内部邮局">
            <WingBlank>
                <WhiteSpace/>
                <form>
                    <List>
                        {/*<WingBlank>*/}
                            {/*{upload.length > 0 && upload.map((item, index) => (*/}
                                {/*<div>1111</div>*/}
                            {/*))}*/}
                        {/*</WingBlank>*/}
                        {/*<Upload className="auto-upload" {...uploaderProps}>*/}
                            {/*<a className="auto-upload">上传附件 +</a>*/}
                        {/*</Upload>*/}

                        <InputItem disabled={true} value="11111">姓名</InputItem>
                        <InputItem disabled={true} value="11111">账号</InputItem>
                        <Picker data={[{label: '男', value: '男'}, {label: '女', value: '女'}]} cols={1} {...getFieldProps('xb')}>
                            <List.Item arrow="horizontal">性别</List.Item>
                        </Picker>
                        <DatePicker mode="date" className="send-date-style"{...getFieldProps('gzrq')}>
                            <List.Item arrow="horizontal">工作日期</List.Item>
                        </DatePicker>
                        <DatePicker mode="date" className="send-date-style"{...getFieldProps('csrq')} onChange={val => console.log(val)}>
                            <List.Item arrow="horizontal">出生日期</List.Item>
                        </DatePicker>
                        <InputItem{...getFieldProps('gzdh')}>工作电话</InputItem>
                        <InputItem{...getFieldProps('yddh')}>手机号码</InputItem>
                        <InputItem{...getFieldProps('sjxh')}>手机小号</InputItem>
                        <InputItem{...getFieldProps('email')}>邮箱地址</InputItem>
                    </List>
                </form>
                <div className="event-footer">
                    <div className="event-footer-button" onClick={onSubmit}>保存</div>
                </div>
            </WingBlank>
        </Document>
    );
};

export default createForm()(Send);
