// 分娩登记
import React from 'react';
import { Form, Input, Select, message, Modal, DatePicker, Row, Col, Button } from 'antd';
import FormItem from 'antd/lib/form/FormItem';
import { $http } from 'containers/config/https';
import moment from 'moment';
import PatientInfo from '../Patient/PatientInfo';
import "./CSS/ChildbirthRegister.less"
const formItemLayout = {    // label 和input宽度
    labelCol: { span: 8 },
    wrapperCol: { span: 16 },
};
const formItemLayoutDay = {
    labelCol: { span: 8 },
    wrapperCol: { span: 17 },
};
const formItemLayoutRe = {
    labelCol: { span: 6 },
    wrapperCol: { span: 12 },
};
// 用户数据
let userData;
const ChildbirthRegister = Form.create()(
    class extends React.Component {
        constructor(props) {
            userData = sessionStorage.patientData ? JSON.parse(sessionStorage.patientData) : {};
            super(props);
            this.state = {
                visible: false, // 弹框显示状态
                showLoading: false,
                disableFlag: false,
                deliveryRoomData: [],
                formDefault: {},
                childBirthBloodGroupArr: [],
                childBirthRHBloodGroupArr: [],
                childBirthThalassemiaArr: [],
                childBirthHBsAArr: [],
                childBirthHIVArr: [],
                childBirthG6PDArr: [],
                childBirthGBSArr: []
            }
        }

        componentWillReceiveProps(nextProps) {
            if (nextProps.visible) {
                this.getRegInfo();  //test  
            }
        }
        componentDidMount() {
            this.getDeliveryRoom();
            // this.props.onRef(this);
            this.getSelectData();
        }
        // 显示弹框
        showModal = () => {
            // console.log(this.props.params)
            this.setState({
                visible: true,
            }, () => this.getRegInfo());
        }

        // 获取登记信息
        getRegInfo = () => {
            this.setState({
                showLoading: true
            });
            let data = {
                'params': [{
                    admID: this.props.admID,
                }]
            };
            $http.post('urlADS', this, {
                code: "04110003",
                data: data,
                success: function (res) {
                    if (+res.errorCode == '0') {
                        let regData = res.result;
                        this.setState({
                            disableFlag: regData.childbirthRegStatusCode === 'REG' ? true : false,
                            showLoading: false,
                            formDefault: regData
                        })

                    } else {
                        this.props.onCancel();
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                    this.setState({
                        showLoading: false
                    })
                }
            });
        }

        // 保存登记信息
        saveRegInfo = (code) => {
            this.setState({
                showLoading: true
            });
            // 表单数据验证
            this.props.form.validateFields((err, value) => {
                if (err) {
                    this.setState({
                        showLoading: false
                    });
                }
            });
            const formData = this.props.form.getFieldsValue();
            let data = {
                'params': [{
                    admID: this.props.admID,
                    childbirthRegStatusCode: code,
                    childbirthRegCreateDate: formData.childbirthRegCreateDate,
                    childbirthRegLocID: formData.childbirthRegLocID,
                    childbirthRegWeek: formData.childbirthRegWeek,
                    childbirthRegDay: formData.childbirthRegDay,
                    childbirthRegGravida: formData.childbirthRegGravida,
                    childbirthRegDeliveryTimes: formData.childbirthRegDeliveryTimes,
                    userID: userData.userID,
                    childbirthRegBloodGroupID: formData.childbirthRegBloodGroupID,
                    childbirthRegRHBloodGroupID: formData.childbirthRegRHBloodGroupID,
                    childbirthRegThalassemiaID: formData.childbirthRegThalassemiaID,
                    childbirthRegHBsAID: formData.childbirthRegHBsAID,
                    childbirthRegHIVID: formData.childbirthRegHIVID,
                    childbirthRegG6PDID: formData.childbirthRegG6PDID,
                    childbirthRegGBSID: formData.childbirthRegGBSID,
                }]
            };
            // console.log(data)  
            $http.post('urlADS', this, {
                code: "04110002",
                data: data,
                success: function (res) {
                    if ((+res.errorCode == '0') && (code != 'FINISH')) {
                        message.success(res.errorMessage);
                        //this.props.handthis.onCancel();
                        this.setState({
                            showLoading: false
                        }, () => {
                            this.getRegInfo()
                        });
                    } else if ((+res.errorCode == '0') && (code == 'FINISH')) {
                        // this.props.handthis.onCancel();
                        message.success(res.errorMessage);
                        this.setState({
                            showLoading: false,
                            visible: false
                        }, () => {
                            if (this.props.handthis) {
                                this.props.handthis.getPatients();
                            }
                            this.props.onCancel();
                        });
                    } else {
                        this.setState({
                            showLoading: false
                        })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                    this.setState({
                        showLoading: false
                    })
                }
            });
        }
        // 获取产房下拉数据
        getDeliveryRoom = () => {
            let data = {
                'params': [{
                    hospID: userData.hospID
                }]
            };
            $http.post('urlADS', this, {
                code: "04110001",
                data: data,
                success: function (res) {
                    if (+res.errorCode == '0') {
                        this.setState({
                            deliveryRoomData: res.result
                        });
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 获取下拉数据
        getSelectData = () => {
            let data = {
                'params': [{}]
            };
            $http.post('urlADS', this, {
                code: "04110028",
                data: data,
                success: function (res) {
                    if (+res.errorCode == '0') {
                        this.setState({
                            childBirthBloodGroupArr: res.result.childBirthBloodGroup,
                            childBirthRHBloodGroupArr: res.result.childBirthRHBloodGroup,
                            childBirthThalassemiaArr: res.result.childBirthThalassemia,
                            childBirthHBsAArr: res.result.childBirthHBsA,
                            childBirthHIVArr: res.result.childBirthHIV,
                            childBirthG6PDArr: res.result.childBirthG6PD,
                            childBirthGBSArr: res.result.childBirthGBS
                        });
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                }
            });
        }
        // 关闭弹框
        handleCancel = () => {
            this.setState({
                visible: true,
            });
        }

        render() {
            const { getFieldDecorator } = this.props.form;
            const formDefault = this.state.formDefault;
            return (
                <Modal
                    className="childDom"
                    width={'60%'}
                    title="分娩登记"
                    visible={this.props.visible}
                    onCancel={this.props.onCancel}
                    footer={null}
                    maskClosable={false}
                >
                    <div className='colors'>

                        <Row style={{ width: '100%', height: '36px', textAlign: 'center', lineHeight: '36px', borderBottom: '1px solid #cccccc', marginBottom: "11px" }}>
                            <PatientInfo admID={this.props.admID} />
                        </Row>
                        <Form>
                            <Row>
                                <Col span={8}>
                                    <FormItem {...formItemLayout} label="登记状态:">
                                        {getFieldDecorator('childbirthRegStatusDesc', {
                                            initialValue: formDefault.childbirthRegStatusDesc
                                        })(
                                            <Input disabled />
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={8}>
                                    <FormItem {...formItemLayout} label="登记日期:">
                                        {getFieldDecorator('childbirthRegCreateDate', {
                                            initialValue: moment(moment(), 'YYYY-MM-DD')
                                        })(
                                            <DatePicker style={{ width: '100%' }} disabled={false}></DatePicker>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={8}>
                                    <FormItem {...formItemLayout} label="产房:">
                                        {getFieldDecorator('childbirthRegLocID', {
                                            initialValue: formDefault.childbirthRegLocID,
                                            rules: [{ required: true, message: '请选择产房' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.deliveryRoomData.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span={5} style={{ position: "relative" }}>
                                    <FormItem {...formItemLayoutRe} label="孕">
                                        {getFieldDecorator('childbirthRegWeek', {
                                            initialValue: formDefault.childbirthRegWeek,
                                            getValueFromEvent: (event) => {
                                                return event.target.value.replace(/[^0-9]/g, '')
                                            },
                                        })(
                                            <Input />
                                        )}
                                    </FormItem>
                                    <span style={{ position: "absolute", top: "9px", right: "11px" }}>周</span>
                                </Col>
                                {/* <Col span={1}>
                                    <FormItem>
                                        <span>周</span>
                                    </FormItem>
                                </Col> */}
                                <Col span={3} style={{ position: "relative" }}>
                                    <FormItem {...formItemLayoutDay} label='' >
                                        {getFieldDecorator('childbirthRegDay', {
                                            initialValue: formDefault.childbirthRegDay,
                                            getValueFromEvent: (event) => {
                                                return event.target.value.replace(/[^0-9]/g, '')
                                            },
                                        })(
                                            <Input />
                                        )}
                                    </FormItem>
                                    <span style={{ position: "absolute", top: "9px", right: "3px" }}>天</span>
                                </Col>
                                {/* <Col span={1}>
                                    <FormItem>
                                        <span>天</span>
                                    </FormItem>
                                </Col> */}
                                <Col span={8}>
                                    <FormItem {...formItemLayout} label="孕次:">
                                        {getFieldDecorator('childbirthRegGravida', {
                                            initialValue: formDefault.childbirthRegGravida,
                                            getValueFromEvent: (event) => {
                                                return event.target.value.replace(/[^0-9]/g, '')
                                            },
                                        })(
                                            <Input />
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={8}>
                                    <FormItem {...formItemLayout} label="产次:">
                                        {getFieldDecorator('childbirthRegDeliveryTimes', {
                                            initialValue: formDefault.childbirthRegDeliveryTimes,
                                            getValueFromEvent: (event) => {
                                                return event.target.value.replace(/[^0-9]/g, '')
                                            },
                                        })(
                                            <Input />
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span={6} style={{ position: "relative" }}>
                                    <FormItem {...formItemLayout} label="血型:">
                                        {getFieldDecorator('childbirthRegBloodGroupID', {
                                            initialValue: '',
                                            rules: [{ message: '请选择' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.childBirthBloodGroupArr.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={6}>
                                    <FormItem {...formItemLayout} label="Rh(D):">
                                        {getFieldDecorator('childbirthRegRHBloodGroupID', {
                                            initialValue: '',
                                            rules: [{ message: '请选择' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.childBirthRHBloodGroupArr.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={6}>
                                    <FormItem {...formItemLayout} label="地中海贫血:">
                                        {getFieldDecorator('childbirthRegThalassemiaID', {
                                            initialValue: '',
                                            rules: [{ message: '请选择' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.childBirthThalassemiaArr.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={6} style={{ position: "relative" }}>
                                    <FormItem {...formItemLayout} label="HBsA:">
                                        {getFieldDecorator('childbirthRegHBsAID', {
                                            initialValue: '',
                                            rules: [{ message: '请选择' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.childBirthHBsAArr.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row>
                                <Col span={6} style={{ position: "relative" }}>
                                    <FormItem {...formItemLayout} label="HIV:">
                                        {getFieldDecorator('childbirthRegHIVID', {
                                            initialValue: '',
                                            rules: [{ message: '请选择' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.childBirthHIVArr.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={6}>
                                    <FormItem {...formItemLayout} label="G-6-PD:">
                                        {getFieldDecorator('childbirthRegG6PDID', {
                                            initialValue: '',
                                            rules: [{ message: '请选择' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.childBirthG6PDArr.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={6}>
                                    <FormItem {...formItemLayout} label="GBS检查:">
                                        {getFieldDecorator('childbirthRegGBSID', {
                                            initialValue: '',
                                            rules: [{ message: '请选择' }]
                                        })(
                                            <Select allowClear
                                                showSearch
                                                optionFilterProp='role'
                                                disabled={false}
                                            >
                                                {this.state.childBirthGBSArr.map(data => (
                                                    <Select.Option role={data.descripts + '^' + data.descriptsSPCode} key={data.id}>{data.descripts}</Select.Option>
                                                ))}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row type="flex" justify="center" style={{ margin: "2px 0" }}>
                                <Col span={3}>
                                    <FormItem>
                                        <Button
                                            type='primary'
                                            htmlType="submit"
                                            onClick={this.saveRegInfo.bind(this, 'REG')}
                                            disabled={this.state.disableFlag || this.state.showLoading}
                                        >登记
                                        </Button>
                                    </FormItem>
                                </Col>
                                <Col span={4}>
                                    <FormItem>
                                        <Button
                                            type='primary'
                                            htmlType="submit"
                                            onClick={this.saveRegInfo.bind(this, 'CANCEL')}
                                            disabled={(!this.state.disableFlag) || this.state.showLoading}
                                        >取消登记
                                        </Button>
                                    </FormItem>
                                </Col>
                                <Col span={3}>
                                    <FormItem>
                                        <Button
                                            type='primary'
                                            htmlType="submit"
                                            onClick={this.saveRegInfo.bind(this, 'default')}
                                            disabled={(!this.state.disableFlag) || this.state.showLoading}
                                        >修改
                                        </Button>
                                    </FormItem>
                                </Col>
                                <Col span={3}>
                                    <FormItem>
                                        <Button
                                            type='primary'
                                            htmlType="submit"
                                            onClick={this.saveRegInfo.bind(this, 'FINISH')}
                                            disabled={(!this.state.disableFlag) || this.state.showLoading}
                                        >完成
                                        </Button>
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </div>
                </Modal>
            )
        }
    }
)
export default ChildbirthRegister;