/**
 * descripts：  ESI评级 用于急诊预检分级
*/
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';
// 用户数据
const userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
const ESIRateModel = Form.create()(
    class extends Component {
        constructor(props) {
            super(props);
            this.state = {
                patCriticallyIllFlag: false,   //濒危
                patWaitOrNotFlag: false,    //不能等
                patEmergencyCaseFlag: false,//急症
                patAbnormalSignsFlag: false,   //生命体征
                resourcesNumberIDFlag: false,  //急诊资源
                totalScore: 0,            //总评分
                patESIGrade: {},             //
                painScoreData: [],           //医疗资源数
                patESIGradeID: '',
                eSIRateSaved: false,         //是否点击过保存
                ModalData: []
            }
        }

        componentDidMount() {
            this.loadData()
            this.initColumns();
            this.loadColumn();
            this.initData();
            this.props.onRef(this);
        }
        componentWillReceiveProps(nextProps) {
            if ((nextProps.patGradePar !== this.props.patGradePar)) {
                // console.log(nextProps.data)
                this.setState({
                    ModalData: nextProps.patGradePar,
                }, () => {
                    
                })
            }
        }
        //根据评分记录ID查询详细评分数据
        loadFormData = () => {
            if ((Util.isEmpty(this.props.patESIGradeID)) || (!Util.isEmpty(this.state.patESIGrade))) { return }
            let data = {
                params: [{
                    power: 'ESIGrade',
                    patESIGradeID: this.props.patESIGradeID
                }]
            }
            $http.post('urlS', this, {
                code: "04140008",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        this.setState({
                            patESIGrade: res.result.ESIGrade,
                            totalScore: res.result.ESIGrade.score
                        }
                        )
                    }
                }
            })
        }
        //获取医疗资源数配置
        loadData = () => {
            const typeAry = ['ResourcesNumber']
            let data = {
                params: [{
                    typeAry: typeAry
                }]
            }
            $http.post('urlS', this, {
                code: "04140007",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        this.setState({
                            painScoreData: res.result.Data[0].ResourcesNumber,
                        }
                        )
                    }
                }
            })
        }

        //基础字典配置数
        loadColumn = () => {
            let data = {
                params: [{
                    compontName: "ESIVitalSignsItem",
                    type: this.state.tableType,
                }]
            }
            $http.post('urlS', this, {
                code: "01040073",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        let resData = res.result[this.state.tableType];
                        for (let index = 0; index < resData.length; index++) {
                            let columnCode = resData[index].code;
                        }
                        this.setState({
                            columns: resData,
                            tableWidth: res.totalWidth
                        }
                        )
                    }
                }
            })
        }

        initColumns() {
            const renderContent = (value, row, index) => {
                const obj = {
                    children: value,
                    props: {},
                };
                if (index === 3) {
                    obj.props.colSpan = 0;
                }
                return obj;
            };
            this.columns = [
                {
                    title: '项目',
                    dataIndex: 'item',
                    align: 'center',
                    width: 180,
                },
                {
                    title: '<3个月',
                    dataIndex: 'less3Month',
                    align: 'center',
                    width: 100,
                    render: (text, row, index) => {
                        if (index < 3) {
                            return (<span>{text}</span>)
                        }
                        return {
                            children: <span>{text}</span>,
                            props: {
                                colSpan: 6,
                            }
                        }
                    },
                },
                {
                    title: '3个月-3岁',
                    children: [
                        {
                            title: '3-6个月',
                            dataIndex: 'between3To6Month',
                            key: 'between3To6Month',
                            align: 'center',
                            width: 130,
                            render: (value, row, index) => {
                                const obj = {
                                    children: value,
                                    props: {},
                                };
                                if (index === 1) {
                                    obj.props.colSpan = 3;
                                }
                                if (index === 2) {
                                    obj.props.colSpan = 4;
                                }
                                if (index === 3) {
                                    obj.props.colSpan = 0;
                                }
                                return obj;
                            }
                        }, {
                            title: '6-12个月',
                            dataIndex: 'between6To12Month',
                            key: 'between6To12Month',
                            align: 'center',
                            width: 130,
                            render: (value, row, index) => {
                                const obj = {
                                    children: value,
                                    props: {},
                                };
                                if ((index === 1) || (index === 2)) {
                                    obj.props.colSpan = 0;
                                }
                                if (index === 3) {
                                    obj.props.colSpan = 0;
                                }
                                return obj;
                            }
                        }, {
                            title: '1-3岁',
                            dataIndex: 'between1To3Age',
                            key: 'between1To3Age',
                            align: 'center',
                            width: 130,
                            render: (value, row, index) => {
                                const obj = {
                                    children: value,
                                    props: {},
                                };
                                if ((index === 1) || (index === 2)) {
                                    obj.props.colSpan = 0;
                                }
                                if (index === 3) {
                                    obj.props.colSpan = 0;
                                }
                                return obj;
                            }
                        },
                    ]
                },
                {
                    title: '3-8岁',
                    dataIndex: 'between3To8Age',
                    align: 'center',
                    width: 100,
                    render: (value, row, index) => {
                        const obj = {
                            children: value,
                            props: {},
                        };
                        if (index === 2) {
                            obj.props.colSpan = 0;
                        }
                        if (index === 3) {
                            obj.props.colSpan = 0;
                        }
                        return obj;
                    }
                },
                {
                    title: '>8岁',
                    dataIndex: 'greater8Age',
                    render: renderContent,
                    align: 'center',
                    width: 100,
                },
            ];
        }

        initData() {
            this.data = [
                {
                    key: 1,
                    item: '心率',
                    less3Month: '>180或<100',
                    between3To6Month: '>160或<90',
                    between6To12Month: '>160或<80',
                    between1To3Age: '>160或<70',
                    between3To8Age: '>140或<60',
                    greater8Age: '>120或<20'
                },
                {
                    key: 2,
                    item: '呼吸',
                    less3Month: '>50或<30',
                    between3To6Month: '>40或<25',
                    between6To12Month: '',
                    between1To3Age: '',
                    between3To8Age: '>30或<20',
                    greater8Age: '>20或<14'
                },
                {
                    key: 3,
                    item: '血压-收缩压(mmHg)',
                    less3Month: '>85或<65',
                    between3To6Month: '>90+年龄×2 或 <70+年龄×2',
                    between6To12Month: '',
                    between1To3Age: '',
                    between3To8Age: '',
                    greater8Age: '>140或<90'
                },
                {
                    key: 4,
                    item: '指测脉搏氧饱和度',
                    less3Month: '<=92%',
                    between3To6Month: '',
                    between6To12Month: '',
                    between1To3Age: '',
                    between3To8Age: '',
                    greater8Age: ''
                },
            ];
        }

        save = () => {
            const formData = this.props.form.getFieldsValue();
            let data = {
                params: [{
                    patID: this.props.patID,
                    patESIGradeID: this.props.patTraumaGradeID,
                    patCriticallyIll: formData.patCriticallyIll,
                    patWaitOrNot: formData.patWaitOrNot,
                    patEmergencyCase: formData.patEmergencyCase,
                    patAbnormalSigns: formData.patAbnormalSigns,
                    resourcesNumberID: formData.resourcesNumberID,
                    updateUserID: userData.userID,
                }]
            }
            let ESIRateScore={
                totalScore:this.state.totalScore
            }
            this.props.handleOk({ ESIRateInfo: data, eSIRate: false, eSIRateSaved: true }, this.state.totalScore);
        }

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

        HandleCancel = () => {
            this.props.form.resetFields(); // 清空表单
            this.props.handleOk({ ESIRateInfo: {}, eSIRate: false });
        }

        radioChange(type, e) {
            this.props.form.setFieldsValue({ [type]: e.target.value })
            let itemAry = ['patWaitOrNot', 'patEmergencyCase', 'patAbnormalSigns', 'resourcesNumberID']
            let value = e.target.value
            for (let i = 0; i < itemAry.length; i++) {
                const element = itemAry[i];
                if (element == type) continue;
                if (type == 'patCriticallyIll') {
                    if (value == 'Y') {
                        this.props.form.setFieldsValue({ [element]: '' });
                        this.setState({
                            [element + 'Flag']: true,
                            totalScore: 1,
                        })
                    } else {
                        this.setState({
                            [element + 'Flag']: false,
                            totalScore: 0,
                        })
                    }
                }
                if (type == 'patWaitOrNot') {
                    if (value == 'Y') {
                        this.props.form.setFieldsValue({ [element]: '' });
                        this.setState({
                            [element + 'Flag']: true,
                            totalScore: 2,
                        })
                    } else {
                        this.setState({
                            [element + 'Flag']: false,
                            totalScore: 0,
                        })
                    }
                }
                if (type == 'patEmergencyCase') {
                    if (element == 'patWaitOrNot') continue;
                    this.props.form.setFieldsValue({ patAbnormalSigns: '' });
                    if (value == 'Y') {
                        this.props.form.setFieldsValue({ resourcesNumberID: this.state.painScoreData[1].id });
                        this.setState({
                            resourcesNumberIDFlag: true,
                            patAbnormalSignsFlag: false,
                            totalScore: 1,
                        })
                    } else {
                        this.props.form.setFieldsValue({ resourcesNumberID: '' });
                        this.setState({
                            patAbnormalSignsFlag: true,
                            resourcesNumberIDFlag: false,
                            totalScore: 0,
                        })
                    }
                }
            }
            if (type == 'patAbnormalSigns') {
                if (value == 'Y') {
                    this.setState({
                        totalScore: 2,
                    })
                } else {
                    this.setState({
                        totalScore: 3,
                    })
                }
            }
            if (type == 'resourcesNumberID') {
                if (value == 'Y') {
                    this.setState({
                        totalScore: 4,
                    })
                } else {
                    this.setState({
                        totalScore: 3,
                    })
                }
            }
        }
        render() {
            const { getFieldDecorator } = this.props.form;
            let patESIGrade = this.state.patESIGrade;
            const formData = this.props.form.getFieldsValue();

            return (
                <Modal
                    title="ESI评级"
                    width='900px'
                    visible={this.props.visible}
                    onOk={this.handleOk}
                    onCancel={this.HandleCancel}
                    maskClosable={false}
                >
                    <Form layout='vertical' className='Preliminary-form'>
                        <Form.Item>
                            {getFieldDecorator('patCriticallyIll', {
                                initialValue: Util.isEmpty(patESIGrade) ? undefined : patESIGrade.patCriticallyIll,
                            })(
                                <Card title='病人是否濒危?' headStyle={{ backgroundColor: '#509DE1' }} bodyStyle={{ padding: '8px' }}>
                                    <Radio.Group value={formData.patCriticallyIll || patESIGrade.patCriticallyIll} disabled={this.state.patCriticallyIllFlag} onChange={this.radioChange.bind(this, 'patCriticallyIll')}>
                                        <Radio value='Y'>是</Radio>
                                        <Radio value='N'>否</Radio>
                                    </Radio.Group>
                                </Card>
                            )}
                        </Form.Item>
                        <Form.Item>
                            {getFieldDecorator('patWaitOrNot', {
                                initialValue: Util.isEmpty(patESIGrade) ? undefined : patESIGrade.patWaitOrNot,
                            })(
                                <Card title='病人是否不能等?' headStyle={{ backgroundColor: '#509DE1' }} bodyStyle={{ padding: '8px' }}>
                                    <Radio.Group value={formData.patWaitOrNot || patESIGrade.patWaitOrNot} disabled={this.state.patWaitOrNotFlag} onChange={this.radioChange.bind(this, 'patWaitOrNot')}>
                                        <Radio value='Y'>是</Radio>
                                        <Radio value='N'>否</Radio>
                                    </Radio.Group>
                                </Card>
                            )}
                        </Form.Item>
                        <Form.Item>
                            {getFieldDecorator('patEmergencyCase', {
                                initialValue: Util.isEmpty(patESIGrade) ? undefined : patESIGrade.patEmergencyCase,
                            })(
                                <Card title='急症病人?' headStyle={{ backgroundColor: '#509DE1' }} bodyStyle={{ padding: '8px' }}>
                                    <Radio.Group value={formData.patEmergencyCase || patESIGrade.patEmergencyCase} disabled={this.state.patEmergencyCaseFlag} onChange={this.radioChange.bind(this, 'patEmergencyCase')}>
                                        <Radio value='Y'>是</Radio>
                                        <Radio value='N'>否</Radio>
                                    </Radio.Group>
                                </Card>
                            )}
                        </Form.Item>
                        <Form.Item>
                            {getFieldDecorator('patAbnormalSigns', {
                                initialValue: Util.isEmpty(patESIGrade) ? undefined : patESIGrade.patAbnormalSigns,
                            })(
                                <Card title='生命体征有无异常?' headStyle={{ backgroundColor: '#509DE1' }} bodyStyle={{ padding: '8px' }}>
                                    <Table
                                        columns={this.columns}
                                        dataSource={this.data}
                                        bordered
                                        pagination={false}
                                    >
                                    </Table>
                                    <Radio.Group value={formData.patAbnormalSigns || patESIGrade.patAbnormalSigns} disabled={this.state.patAbnormalSignsFlag} onChange={this.radioChange.bind(this, 'patAbnormalSigns')}>
                                        <Radio value='Y'>是</Radio>
                                        <Radio value='N'>否</Radio>
                                    </Radio.Group>
                                </Card>
                            )}
                        </Form.Item>
                        <Form.Item>
                            {getFieldDecorator('resourcesNumberID', {
                                initialValue: Util.isEmpty(patESIGrade) ? undefined : patESIGrade.resourcesNumberID,
                            })(
                                <Card title='病人需要多少急诊资源?（放射/实验室检查/专家会诊/心电图等）' headStyle={{ backgroundColor: '#509DE1' }} bodyStyle={{ padding: '8px' }}>
                                    <Radio.Group value={formData.resourcesNumberID || patESIGrade.resourcesNumberID} disabled={this.state.resourcesNumberIDFlag} onChange={this.radioChange.bind(this, 'resourcesNumberID')}>
                                        {this.state.painScoreData.map((item, index) => {
                                            return (
                                                <Radio value={item.id} key={item.id}>{item.descripts}</Radio>
                                            )
                                        })}
                                    </Radio.Group>
                                </Card>
                            )}
                        </Form.Item>
                    </Form>
                    级别：<label style={{ color: '#FF0000' }}>{this.state.totalScore}级</label>
                </Modal>
            )
        }
    }
)
export default ESIRateModel