// MEWS评分
import React from 'react';
import { Form, message, Modal, Row, Col, Radio } from 'antd';
import FormItem from 'antd/lib/form/FormItem';
import { $http } from '../../../containers/config/https';
import { Util } from 'tools/index';
import './index.less';
const radioLayout = {    // label 和input宽度
    labelCol: { span: 3 },
    wrapperCol: { span: 21 },
};
// 用户数据
const userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
const MEWSRatingModal = Form.create()(
    class extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                visible: false,     // 弹框显示状态
                heartRate: [],      // 心率
                temperature: [],    // 体温
                consciousness: [],         // 意识
                SBP: [],            // 收缩压
                respiratoryRate: [],     // 呼吸频率
                mEWSRateSaved: false,   //保存标记
                totalScore: 0,            //总评分
            }
        }

        componentDidMount() {
            this.getBasicData();
            this.props.onRef(this);
        }
        // 显示弹框
        showModal = () => {
            // console.log(this.props.params)
            this.setState({
                visible: true,
            });
        }

        // 获取基础数据
        getBasicData = () => {
            let data = {
                'params': [{}]
            };
            $http.post('urlADS', this, {
                code: "04140003",
                data: data,
                success: function (res) {
                    if (+res.errorCode == '0') {
                        this.setState({
                            heartRate: res.heartRate,
                            temperature: res.temperature,
                            consciousness: res.awareness,
                            SBP: res.SBP,
                            respiratoryRate: res.breathRate
                        })
                    }
                },
                error: function (error) {
                    message.error('失败：' + error);
                    this.setState({
                        showLoading: false
                    })
                }
            });
        }

        //根据评分记录ID查询详细评分数据
        loadFormData = () => {
            let data = {
                params: [{
                    MEWSGradeID: this.props.patMEWSRateID
                }]
            }
            $http.post('urlS', this, {
                code: "04140010",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        this.setState({
                            MEWSGrade: res.result.MEWSGradeInfo,
                            totalScore: res.result.MEWSGradeInfo.score
                        }
                        )
                    }
                }
            })
        }

        // 保存信息
        save = () => {
            const formData = this.props.form.getFieldsValue();
            let MEWSGradeInfo = {
                patID: this.props.patID,
                heartRate: formData.heartRate,
                SBP: formData.SBP,
                respiratoryRate: formData.respiratoryRate,
                temperature: formData.temperature,
                consciousness: formData.consciousness,
            }
            this.props.handleOk({ MEWSGradeInfo: MEWSGradeInfo, mEWSRate: false, mEWSRateSaved: true },this.state.totalScore);
        }
        // 计算分数
        calculateScore = () => {

        }

        onChange = (type,e) => {
            let totalScore=0;
            const typeArr=['heartRate','SBP','respiratoryRate','temperature','consciousness'];
            for (let i=0;i<typeArr.length;i++)
            {
                const element=typeArr[i];
                if (element==type) continue
                let checkValue=this.props.form.getFieldValue(element)
                if (!Util.isEmpty(checkValue)){
                    for (let j = 0; j < this.state[element].length; j++) {
                        const item = this.state[element][j].id;
                        if (item==checkValue){
                            totalScore=totalScore+Number.parseInt(this.state[element][j].value)
                        }
                    }
                }
            }        
            this.setState({totalScore:totalScore+Number.parseInt(e.target.score)})
        }
        // 关闭弹框
        handleCancel = () => {
            this.props.form.resetFields(); // 清空表单
            this.props.handleOk({ MEWSGradeInfo: {}, mEWSRate: false });
        }
        handleOk = (e) => {
            this.save();
        };

        render() {
            const { getFieldDecorator } = this.props.form;
            const MEWSGrade = this.state.MEWSGrade
            return (
                <Modal
                    width={'60%'}
                    title="MEWS评分"
                    visible={this.props.visible}        //{this.props.visible}
                    onCancel={this.handleCancel}
                    onOk={this.handleOk}
                    maskClosable={false}
                    className='clearPadding'
                >
                    <div className='colors' style={{ width: '100%', height: '100%' }}>
                        <Form>
                            <Row type="flex" justify="center">
                                <Col span={24}>
                                    <FormItem {...radioLayout} label="心率">
                                        {getFieldDecorator('heartRate', {
                                            initialValue: Util.isEmpty(MEWSGrade) ? undefined : MEWSGrade.heartRate,
                                        })(
                                            <Radio.Group onChange={this.onChange.bind(this,'heartRate')}>
                                                {this.state.heartRate.map(item => (
                                                    <Radio value={item.id} score={item.value} >{item.descripts}</Radio>
                                                ))}
                                            </Radio.Group>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row type="flex" justify="center">
                                <Col span={24}>
                                    <FormItem {...radioLayout} label="收缩压">
                                        {getFieldDecorator('SBP', {
                                            initialValue: Util.isEmpty(MEWSGrade) ? undefined : MEWSGrade.SBP,
                                        })(
                                            <Radio.Group onChange={this.onChange.bind(this,'SBP')}>
                                                {this.state.SBP.map(item => (
                                                    <Radio value={item.id} score={item.value} >{item.descripts}</Radio>
                                                ))}
                                            </Radio.Group>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row type="flex" justify="center">
                                <Col span={24}>
                                    <FormItem {...radioLayout} label="呼吸频率">
                                        {getFieldDecorator('respiratoryRate', {
                                            initialValue: Util.isEmpty(MEWSGrade) ? undefined : MEWSGrade.respiratoryRate,
                                        })(
                                            <Radio.Group onChange={this.onChange.bind(this,'respiratoryRate')}>
                                                {this.state.respiratoryRate.map(item => (
                                                    <Radio value={item.id}  score={item.value} >{item.descripts}</Radio>
                                                ))}
                                            </Radio.Group>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row type="flex" justify="center">
                                <Col span={24}>
                                    <FormItem {...radioLayout} label="体温">
                                        {getFieldDecorator('temperature', {
                                            initialValue: Util.isEmpty(MEWSGrade) ? undefined : MEWSGrade.temperature,
                                        })(
                                            <Radio.Group onChange={this.onChange.bind(this,'temperature')}>
                                                {this.state.temperature.map(item => (
                                                    <Radio value={item.id} score={item.value} >{item.descripts}</Radio>
                                                ))}
                                            </Radio.Group>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row type="flex" justify="center">
                                <Col span={24}>
                                    <FormItem {...radioLayout} label="意识">
                                        {getFieldDecorator('consciousness', {
                                            initialValue: Util.isEmpty(MEWSGrade) ? undefined : MEWSGrade.consciousness,
                                        })(
                                            <Radio.Group onChange={this.onChange.bind(this,'consciousness')}>
                                                {this.state.consciousness.map(item => (
                                                    <Radio value={item.id} score={item.value} >{item.descripts}</Radio>
                                                ))}
                                            </Radio.Group>
                                        )}
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                        分数：<label style={{ color: '#FF0000' }}>{this.state.totalScore}分</label>
                    </div>
                </Modal>
            )
        }
    }
)
export default MEWSRatingModal;