/**
 * descripts：  创伤评级 用于急诊预检分级
*/
import React, { Component } from 'react';
import { Modal, Form, Card, Table, Radio } from 'antd';
import './style/index.less';
import { $http } from '../../../containers/config/https';
import { Util } from 'tools/index';

let userData = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
const TraumaRateModel = Form.create()(
    class extends Component {
        constructor(props) {
            super(props);
            this.state = {
                endangeredFlag: false,   //濒危
                neverWaitFlag: false,    //不能等
                emergencyCaseFlag: false,//急症
                vitalSignsFlag: false,   //生命体征
                emergencySourceFlag: false,  //急诊资源
                totalScore: 0,            //总评分
                traumaRateData:[],        //配置数据
                patTraumaGrade: {},         //表单数据
                traumaRateSaved: false,     //保存标记
            }
        }

        componentDidMount() {
            // this.loadColumn();
            this.loadData();
            this.props.onRef(this);
        }
        // UNSAFE_componentWillReceiveProps(nextProps) {
        //     if (nextProps.visible) {
        //         this.loadFormData();
        //     }
        // }

        //查询列头数据
        loadData = () => {
            let data = {
                params: [{
                }]
            }
            $http.post('urlS', this, {
                code: "04140005",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        let resData = res.result.traumaRateData;
                        for (let index = 0; index < resData.length; index++) {
                            resData[index].key = index;
                            for (let j = 0; j < resData[index].length; j++) {
                                resData[index][j].key = j;
                                
                            }
                        }
                        this.setState({
                            traumaRateData:res.result.traumaRateData
                        }
                        )
                    }
                }
            })
        }

        //根据评分记录ID查询详细评分数据
        loadFormData = () => {
            if ((Util.isEmpty(this.props.patTraumaGradeID)) || (!Util.isEmpty(this.state.patTraumaGrade))) { return }
            let data = {
                params: [{
                    power:'TraumaGrade',
                    patTraumaGradeID: this.props.patTraumaGradeID
                }]
            }
            $http.post('urlS', this, {
                code: "04140008",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        this.setState({
                            patTraumaGrade:res.result.TraumaGrade,
                            totalScore:res.result.TraumaGrade.score,
                        }
                        )
                    }
                }
            })
        }
        
    
        radioChange=(type, e)=>{
            if (!Util.isEmpty(this.state.traumaRateData)){
                let totalScore=0
                for (let i = 0; i < this.state.traumaRateData.length; i++) {
                    const element = this.state.traumaRateData[i].code;
                    if (element==type) continue
                    let checkValue=this.props.form.getFieldValue(element)
                    if (!Util.isEmpty(checkValue)){
                        for (let j = 0; j < this.state.traumaRateData[i].data.length; j++) {
                            const item = this.state.traumaRateData[i].data[j].id;
                            if (item==checkValue){
                                totalScore=totalScore+Number.parseInt(this.state.traumaRateData[i].data[j].value)
                            }
                        }
                    }
                }
                this.setState({totalScore:totalScore+Number.parseInt(e.target.score)})
            }
        }
        HandleCancel = () => {
            this.props.form.resetFields(); // 清空表单
            this.props.handleOk({ TraumaRateInfo: {}, traumaRate: false });
        }

        save = () => {
            const formData = this.props.form.getFieldsValue();
            let TraumaRateInfo={
                params:[{
                    patID:this.props.patID,
                    patTraumaGradeID:this.props.patTraumaGradeID,
                    patComaScore:formData.patComaScore,
                    patRespiratoryRate:formData.patRespiratoryRate,
                    patDyspnea:formData.patDyspnea,
                    patSBP:formData.patSBP,
                    patCapillaryFilling:formData.patCapillaryFilling,
                    updateUserID:userData.userID,
                }]
            } 

            let TraumaRateScore={
                totalScore:this.state.totalScore
            }
            this.props.handleOk({ TraumaRateInfo: TraumaRateInfo, traumaRate: false, traumaRateSaved: true },this.state.totalScore);
        }

        handleOk = (e) => {
            this.save();
        };

        render() {
            const { getFieldDecorator } = this.props.form;
            let patTraumaGrade=this.state.patTraumaGrade
            const formData = this.props.form.getFieldsValue();
            const tipString=" 总分越小，伤情越重；总分14-16分，生存率>96%;总分4-13分，抢救效果显著；总分1-3分，死亡率>96%, 一般TS<12分作为重伤标准"
            return (
                <Modal
                    title="创伤评级"
                    width='900px'
                    visible={this.props.visible}
                    onOk={this.handleOk}
                    onCancel={this.HandleCancel}
                    maskClosable={false}
                >
                    <Form layout='vertical' className='Preliminary-form'>
                        {Util.isEmpty(this.state.traumaRateData)?null:(
                            this.state.traumaRateData.map((item,index)=>{
                                return (
                                    <Form.Item 
                                        key={item.code}
                                    >
                                        {getFieldDecorator(item.code, {
                                                initialValue: Util.isEmpty(patTraumaGrade) ? undefined : patTraumaGrade[item.code],
                                            })(
                                        <Card title={item.descripts} headStyle={{ backgroundColor: '#509DE1' }} bodyStyle={{ padding: '8px' }}>
                                            <Radio.Group 
                                                value={formData[item.code] || patTraumaGrade[item.code]}
                                                disabled={this.state.endangeredFlag} 
                                                onChange={this.radioChange.bind(this, item.code)}>
                                                {Util.isEmpty(item.data)?null:(
                                                    item.data.map((itemData,itemIndex)=>{
                                                        return(
                                                            <Radio value={itemData.id}
                                                                key={itemData.id}
                                                                score={itemData.value}
                                                            >{itemData.descripts+"("+itemData.value+"分)"}</Radio>
                                                        )
                                                    })
                                                )}
                                            </Radio.Group>
                                        </Card>
                                        )}
                                    </Form.Item>
                                )
                            })
                        )}
                    </Form>
                    总分：
                    <label style={{color:'#FF0000'}}>{this.state.totalScore}分</label>
                    <br/><label style={{color:'#CBAEFF'}}>{tipString}</label>
                </Modal>
            )
        }
    }
)
export default TraumaRateModel