import React from 'react'
import PropTypes from 'prop-types';
import { $http } from '../../containers/config/https';
import { Util } from '../../tools';
import { Modal, Form, DatePicker,message,Input, Col} from 'antd';
let user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
import moment from 'moment'


const FormHandel = Form.create()(
    class extends React.Component {
        state = {
            visible: false,
            userInfo: {},
            flag: ''
        };

        //初始化数据
        componentDidMount() {
            this.props.onRef(this)
        }


        // 显示弹出层
        showModal = (flag, userInfo) => {
            if(flag === 'compile') {
                this.setState({userInfo, flag:'compile'})
            } else {
                this.setState({flag:'add'})
            }
            this.setState({
                visible: true,
            });
        }

        // 取消
        handleCancel = () => {
            this.setState({
                visible: false,
            }, ()=> {
                if(this.state.flag==='compile') {
                    this.resetFields();
                }
            })
        }

        // 重置表单
        resetFields() {
            this.props.form.resetFields(); //清空所有
        }

        // 确定
        handleCreate = () => {
            let { flag } = this.props;
            this.handelFunction('05100086');
        }

        //更新
        handelFunction(code) {
            const { form: { validateFields } } = this.props;
            validateFields((errors, values) => {
                // 验证成功
                if (errors === null) {
                    let data = {
                        params: []
                    }
                    let obj = values;
                    obj.ID = this.state.userInfo.ID;
                    obj.hospID=this.state.userInfo.hospID
                    obj.createUserDr=user.groupID
                    obj.userID=JSON.parse(window.sessionStorage.userData).userID
                    // 处理需要传递的obj
                    data.params.push(Util.disposeData(obj));
                    $http.post('urlADS', this, {
                        code: code,
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                // 提示
                                message.success('编辑成功');
                                // 关闭弹框
                                this.handleCancel();
                                // 刷新列表数据
                                this.props.handelThis.handleQuery();
                            } else {
                                if (this.props.flag === 'compile') {
                                    // 编辑可能失败，但还是需要重置表单
                                    this.resetFields();
                                }
                            }
                        },
                        error: function () {
                            if (this.props.flag === 'compile') {
                                // 编辑可能失败，但还是需要重置表单
                                this.resetFields();
                            }
                        }
                    })
                }
            });
        }

        render() {
            const { getFieldDecorator } = this.props.form;
            let userInfo = this.state.userInfo ? this.state.userInfo : {}
            return (
                <Modal
                    visible={this.state.visible}
                    title="编辑"
                    okText="确定"
                    onCancel={this.handleCancel}
                    onOk={this.handleCreate}
                    width='600px'
                >
                    <Form layout="inline">
                        <Col>
                        <Form.Item label="医院名称">
                            {getFieldDecorator('hospDesc', {
                                initialValue: userInfo.hospDesc
                            })(
                                <Input disabled/>
                            )}
                        </Form.Item>
                        </Col>
                        <Col>
                        <Form.Item label="公司地址">
                            {getFieldDecorator('address', {
                                initialValue: userInfo.address
                            })(
                                <Input/>
                            )}
                        </Form.Item>
                        <Form.Item label="联系方式">
                            {getFieldDecorator('telephone', {
                                initialValue: userInfo.telephone
                            })(
                                <Input/>
                            )}
                        </Form.Item>
                        <Form.Item label="生效日期">
                            {getFieldDecorator('startDate', {
                                initialValue: userInfo.startDate ?  moment(userInfo.startDate) : undefined
                            })(
                                <DatePicker/>
                            )}
                        </Form.Item>
                        <Form.Item label="失效日期">
                            {getFieldDecorator('stopDate', {
                                 initialValue: userInfo.stopDate ?  moment(userInfo.stopDate) : undefined
                            })(
                                <DatePicker/>
                            )}
                        </Form.Item>
                        <Form.Item label="代&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码">
                            {getFieldDecorator('code', {
                                initialValue: userInfo.code
                            })(
                                <Input/>
                            )}
                        </Form.Item>
                        <Form.Item label="法&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;人">
                            {getFieldDecorator('legalPerson', {
                                initialValue: userInfo.legalPerson
                            })(
                                <Input/>
                            )}
                        </Form.Item>
                        <Form.Item label="合作协作医院名称">
                            {getFieldDecorator('descripts', {
                                initialValue: userInfo.descripts
                            })(
                                <Input style={{width:'393px'}}/>
                            )}
                        </Form.Item>
                        <Form.Item label="统一社会信用代码">
                            {getFieldDecorator('socialCode', {
                                initialValue: userInfo.socialCode
                            })(
                                <Input style={{width:'393px'}}/>
                            )}
                        </Form.Item>
                        </Col>
                    </Form>
                </Modal>
            )
        }
    }
)
export default FormHandel;

FormHandel.propTypes = {
    // 验证userInfo的属性
    userInfo: PropTypes.object
}