import React from 'react';
import { Row, Col, Tabs, Form, Button, Select, Tree, Input, Radio, Checkbox, message } from 'antd';
import { $http } from 'containers/config/https';
import { Util, ArraySome } from '../../../tools';
import store from 'store';
const { TabPane } = Tabs;
const { TreeNode, DirectoryTree } = Tree;
const { Option } = Select;

const formItemLayout = {    // label 和input宽度
    labelCol: { span: 7 },
    wrapperCol: { span: 17 },
};
const formItemLayoutTime = { // label 和input宽度
    labelCol: { span: 8 },
    wrapperCol: { span: 14 },
}
const layoutForm = {
    labelCol: { span: 10 },
    wrapperCol: { span: 13, offset: 1 },
}
const radioLayout = {    // label 和input宽度
    labelCol: { span: 2 },
    wrapperCol: { span: 22 },
};
const FormItem = Form.Item;
let userData;
const Preliminaryright = Form.create()(
    class extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                value: '1',
                condition: [],//病情
                place: [],//去向
                recommendLevel: [],//推荐分级
                changeReason: [],//护士更改
                jobLeverArr: [],//号别
                ArrseleData: [],//科室
                selectData: [],//病区
                baseInfo: {},
                isShow: false,
                activityTabs: "1",
                templetTreeData: [],
                TreeData: []
            }
            userData = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
            this.setRightFormData = this.setRightFormData.bind(this)
           // this.setRightForm = this.setRightForm.bind(this)

        }
        componentDidMount() {
            this.loadButtonData();
            this.getLever();
            this.getselectData();
            this.getData();
            this.props.onRef(this);
            this.getSymptom();
        }


        componentWillUnmount() {
            this.setState = (state, callback) => {
                return
            }
        }
        getPropsTableData=(data)=>{
          //  console.log("rightdata",data)
            this.setState({conter:data})
        }
        //获取号别
        getLever() {
            var params = {
                params: [
                    { hospID: userData.hospID }
                ]
            }
            $http.post('urlDeault', this, {
                code: "03020004",
                data: params,
                success: function (res) {
                    if (res.errorCode === '0') {
                        this.setState({
                            jobLeverArr: res.result.Data,
                        })
                    }
                }
            });
        }
        //获取科室
        getData() {
            var params = {
                params: [
                    {
                        hospID: userData.hospID,
                        type: 'O'
                    }
                ]
            }
            $http.post('urlDeault', this, {
                code: "04020008",
                data: params,
                success: function (res) {
                    if (res.errorCode === '0') {
                        this.setState({
                            ArrseleData: res.result.Data,
                        })
                    }
                }
            });
        }
        //获取病区
        getselectData() {
            var params = {
                params: [
                    {
                        locID: userData.locID,

                    }
                ]
            }
            $http.post('urlDeault', this, {
                code: "01050011",
                data: params,
                success: function (res) {
                    if (res.errorCode === '0') {
                        this.setState({
                            selectData: res.result.Data,
                        })
                    }
                }
            });
        }
        // setRightForm =(data) =>{
        //     //console.log("111111",data)
        //     this.setState({conter:data})
        // }
        setRightFormData = (data) => {
            //console.log(data);
            this.props.form.setFieldsValue({
                recommendLevel: data.recommendLevel,
                greenChannel: data.greenChannel,
                condition: data.condition,
                conditionArea: data.conditionArea,
                nurseReason: data.nurseReason,
                triageLocID: data.triageLocID,
                triageResID: data.triageResID,
                rescueWardID: data.rescueWardID,
                effectiveTime: data.effectiveTime,
                referralLocID: data.referralLocID

            })
        }
        getConterFormValue = (values) => {
            // console.log(values);
            this.setState({ conterFormValues: values })
        }
        getCenter = (data) =>{
            this.setState({TableData:data})
        }
        // 保存信息表单提交
        handleSubmit = (e) => {
            this.props.getCenterFormValue();
            e.preventDefault(this.state.conterFormValues);
            this.props.form.validateFields((err, values) => {
                if (!err) {
                    let data = {
                        params: [{
                            patID: this.props.patID
                                || '',
                            patBasicInfo: this.state.conterFormValues,
                            preGradeInfo: {
                                triageLocID: values.triageLocID || '',
                                rescueWardID: values.rescueWardID || '',
                                recommendLevel: values.recommendLevel || '',
                                nurseLevel: values.nurseLevel || '',
                                nurseReason: values.nurseReason || '',
                                condition: values.condition || '',
                                conditionArea: values.conditionArea || '',
                                greenChannel: values.greenChannel || '',
                                effectiveTime: values.effectiveTime || '',
                                referralLocID: values.referralLocID || '',
                                inBatchesFlag: this.state.conterFormValues.inBatchesFlag ? this.state.conterFormValues.inBatchesFlag : '',
                                inBatchesNumber: this.state.conterFormValues.inBatchesNumber ? this.state.conterFormValues.inBatchesNumber : '',
                                pastHistory: this.state.conterFormValues.pastHistory ? this.state.conterFormValues.pastHistory : [],
                                patSource: this.state.conterFormValues.patSource ? this.state.conterFormValues.patSource : '',
                                specialPopulation: values.specialPopulation || '',
                                admMode: this.state.conterFormValues.admMode ? this.state.conterFormValues.admMode : '',
                                sixDiseaseSpecies: this.state.conterFormValues.sixDiseaseSpecies ? this.state.conterFormValues.sixDiseaseSpecies : [],
                                notes: this.state.conterFormValues.notes ? this.state.conterFormValues.notes : '',
                                ideology: this.state.conterFormValues.ideo,
                            },
                            vitalSignInfo: this.state.conterFormValues,
                            ESIRatingInfo: this.state.conterFormValues.ESIRateInfo,
                            painScoreInfo: this.state.conterFormValues.painRateInfo,
                            traumaGradeInfo: this.state.conterFormValues.TraumaRateInfo,
                            MEWSGradeInfo: this.state.conterFormValues.MEWSGradeInfo,
                            painGradeInfo: this.state.conterFormValues.painScoreInfo,
                            REMSGradeInfo: this.state.conterFormValues.REMSGradeInfo,
                            preGradeScoreInfo:this.state.conter

                        }]
                    };
                    $http.post('urlADS', this, {
                        code: "04140006",
                        data: data,
                        success: function (res) {
                            let errorCode = res.errorCode;
                            if (errorCode === '0') {
                                message.success(res.errorMessage);
                            }
                        }
                    });
                }

            });
        }

        //按钮显示数据
        loadButtonData = () => {
            let data = {
                params: [{

                }]
            }
            $http.post('urlS', this, {
                code: "04140001",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        this.setState({
                            condition: res.condition,
                            place: res.place,
                            recommendLevel: res.recommendLevel,
                            changeReason: res.changeReason
                        })
                    }
                }
            })
        }
        // 推荐分级改变事件
        changeSelectOption = () => {
            console.log(this.props.form.validateFields('recommendLevel'))
        }
        //根据症状分类
        getSymptom = () => {
            let data = {
                params: [{

                }]
            }
            $http.post('urlS', this, {
                code: "04140015",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        // console.log(res)
                        for (var i = 0; i < res.result.length; i++) {
                            res.result[i].key = i + 'key';
                        }
                        this.setState({
                            templetTreeData: res.result,
                        })
                    }
                }
            })
        }
        onTempletTree = (keys,e) => {
            let data = {
                params: [{
                    symptom: keys[0]
                }]
            }
            $http.post('urlS', this, {
                code: "04140016",
                data: data,
                success: (res) => {
                    if (+res.errorCode === 0) {
                        // console.log(res)
                        this.setState({
                            TreeData: res.result,
                            activityTabs: "2",
                        })
                    }
                }
            })
        }

        //切换面板
        onTabsChange = (activeKey) => {
            this.setState({
                activityTabs: activeKey
            })
        }
        // 改变复选框状态
        handleChange = () => {
            this.setState({
                isShow: !this.state.isShow

            })
        }
        //加载模版 
        loadTemplate() {
            var templateTree = []
            this.state.templetTreeData && this.state.templetTreeData.map((item, index) => {
                this.loadTemplate(item.data)
            })
            return templateTree
        }
        //展开模板节点
        loadTemplateNode(data) {
            var sunData = [];
            data && data.map((item, index) => {
                sunData.push(<TreeNode title={item.title} key={item.title} onSelect={() => {
                    if (item.data == 'undefined') {
                        this.onTempletTree(item);
                    }
                }}>
                    {this.loadTemplateNode(item.data)} 
                </TreeNode>)
            })
            return sunData
        }

        //展开右侧来诊主诉的节点 
        loadNode =(data)=> {
            var sunData = [];
            data && data.map((item, index) => {
                sunData.push(<TreeNode title={item.descripts} key={item.id}>
                    {item.descripts}
                </TreeNode>)
            })
            return sunData
        }

    
        handleSelect = (data, event) => {
            const {node} =event || {};
            const {props} = node || {}
            // debugger
            // console.log('...data', data)
            // console.log('...props', props)
            this.props.scoreInfo(data[0])

        }
      
        render() {
            // console.log(this.props.contentData);
            const { getFieldDecorator } = this.props.form;
            let { baseInfo } = this.state;
            const { jobLeverArr, ArrseleData, selectData } = this.state;
            var SelectOption = []
            this.state.recommendLevel && this.state.recommendLevel.map((item, index) => {
                SelectOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
            })
            var ReasonOption = []
            this.state.changeReason && this.state.changeReason.map((item, index) => {
                ReasonOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
            })
            //科室
            let LeverArrOp = []
            ArrseleData && ArrseleData.map(item => {
                LeverArrOp.push(<Option key={item.id} value={item.id}>{item.descripts}</Option>)
            })
            //病区
            let selectOp = []
            selectData && selectData.map(item => {
                selectOp.push(<Option key={item.id} value={item.id}>{item.descripts}</Option>)
            })
            //转科科室
            let ArrOp = []
            ArrseleData && ArrseleData.map(item => {
                ArrOp.push(<Option key={item.id} value={item.id}>{item.descripts}</Option>)
            })
            //号别
            let jobLeverArrOp = []
            jobLeverArr && jobLeverArr.map(item => {
                jobLeverArrOp.push(<Option key={item.id} value={item.id}>{item.descripts}</Option>)
            })
            // let tabsItem = [];
            return (
                <Row>
                    <Col>
                        <Tabs
                            activeKey={this.state.activityTabs}
                            onChange={this.onTabsChange}
                            type="card">
                            <TabPane tab="症状分类" key="1">
                                <Col>
                                    <Row style={{ height: "392px", overflow: 'auto' }}>
                                        {this.state.templetTreeData.length > 0 &&
                                            <DirectoryTree multiple defaultExpandAll onSelect={(keys, event) => {
                                                this.onTempletTree(keys, event);
                                            }}>
                                                {this.state.templetTreeData.length > 0 && this.state.templetTreeData.map((item, index) => {
                                                    return (<TreeNode title={item.title} key={item.key} onSelect={() => {
                                                        if (item.data == 'undefined') {
                                                            this.onTempletTree(item);
                                                        }
                                                    }}>
                                                        {item.data && item.data.length > 0 ? this.loadTemplateNode(item.data) : ''}
                                                    </TreeNode>)
                                                })}
                                            </DirectoryTree>
                                        }
                                    </Row>
                                    <Row>
                                        <Col span={3}>其他:</Col>
                                        <Col span={21}><Input /></Col>
                                    </Row>
                                </Col>
                            </TabPane>
                            <TabPane tab="来诊主诉" key="2">
                                <Col>
                                    <Row style={{ height: "392px", overflow: 'auto' }}>
                                        {this.state.TreeData.length>0 &&
                                            <DirectoryTree multiple defaultExpandAll onSelect={(data, event) => {this.handleSelect(data, event)}}>
                                                {this.loadNode(this.state.TreeData)}
                                            </DirectoryTree>
                                            }
                                    </Row>
                                    <Row>
                                        <Col span={3}>其他:</Col>
                                        <Col span={21}><Input /></Col>
                                    </Row>
                                </Col>
                            </TabPane>
                        </Tabs>
                    </Col>

                    <Col>
                        <Form style={{ borderTop: "1px solid #cccccc" }} onSubmit={this.handleSubmit}>
                            <span style={{ fontWeight: 'bold' }}>分诊</span>
                            <Row>
                                <Col span={12}>
                                    <FormItem {...formItemLayout} label="推荐分级">
                                        {getFieldDecorator('recommendLevel', {
                                            initialValue: "",
                                            rules: [{ required: false, message: '请输入推荐分级' }],
                                        })(
                                            <Select onChange={this.changeSelectOption()}>{SelectOption}</Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={12}>

                                    <FormItem {...formItemLayout} label="护士更改">
                                        {getFieldDecorator('nurseReason', {
                                            initialValue: "",
                                            rules: [{ required: false, message: '请输入卡号' }]
                                        })(
                                            <Select>{ReasonOption}</Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={24}>
                                    <FormItem {...radioLayout} label="病情:">
                                        {getFieldDecorator('condition', {
                                            initialValue: ''
                                        })(
                                            <Radio.Group>
                                                {
                                                    this.state.condition && this.state.condition.map((item, key) => {
                                                        return (
                                                            <Radio value={item.id} style={{ marginTop: '10px', color: item.value }}>{item.descripts}</Radio>
                                                        )
                                                    })
                                                }
                                            </Radio.Group>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={24}>
                                    <FormItem {...radioLayout} label="去向:">
                                        {getFieldDecorator('conditionArea', {
                                            initialValue: ''
                                        })(
                                            <Radio.Group>
                                                {
                                                    this.state.place && this.state.place.map((item, key) => {
                                                        return (
                                                            <Radio value={item.id} style={{ marginTop: '10px', color: item.value }}>{item.descripts}</Radio>
                                                        )
                                                    })
                                                }
                                            </Radio.Group>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={12}>
                                    <FormItem {...formItemLayout} label="分诊科室">
                                        {getFieldDecorator('triageLocID', {
                                            initialValue: "",
                                            rules: [{ required: false, message: '' }]
                                        })(
                                            <Select disabled={this.state.isShow}>{LeverArrOp}</Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={12}>
                                    <FormItem {...formItemLayout} label="号别">
                                        {getFieldDecorator('jobLeverId', {
                                            initialValue: "",
                                            rules: [{ required: false, message: '' }]
                                        })(
                                            <Select disabled={this.state.isShow}>{jobLeverArrOp}</Select>
                                        )}
                                    </FormItem>
                                </Col>
                                <Col span={24}>
                                    <Row>
                                        <Col span={8}>
                                            <FormItem {...layoutForm} label="抢救病区">
                                                {getFieldDecorator('rescueWardID', {
                                                    initialValue: "",
                                                    rules: [{ required: false, message: '' }]
                                                })(
                                                    <Select>{selectOp}</Select>
                                                )}
                                            </FormItem>
                                        </Col>
                                        <Col span={7} offset={1}>
                                            <FormItem style={{ display: 'flex', flexWrap: 'noWrap' }} label="绿色通道">
                                                {getFieldDecorator('greenChannel', {
                                                    initialValue: "",
                                                    rules: [{ required: false, message: '' }],
                                                })(
                                                    <Checkbox />
                                                )}
                                            </FormItem>
                                        </Col>
                                        <Col span={8}>
                                            <FormItem  {...layoutForm} label="小时有效">
                                                {getFieldDecorator('effectiveTime', {
                                                    initialValue: "",
                                                    rules: [{ required: false, message: '' }]
                                                })(
                                                    <Input />
                                                )}
                                            </FormItem>
                                        </Col>
                                    </Row>
                                </Col>
                                <Col span={24}>
                                    <Row>
                                        <Col span={12}>
                                            <FormItem {...formItemLayout} label="转科科室">
                                                {getFieldDecorator('referralLocID', {
                                                    initialValue: "",
                                                    rules: [{ required: false, message: '' }]
                                                })(
                                                    <Select disabled={!this.state.isShow}>{ArrOp}</Select>
                                                )}
                                            </FormItem>
                                        </Col>
                                        <Col span={7} offset={1}>
                                            <FormItem style={{ display: 'flex', flexWrap: 'noWrap' }} label="">
                                                {getFieldDecorator('ter')(
                                                    <Checkbox onChange={this.handleChange}></Checkbox>
                                                )}
                                            </FormItem>
                                        </Col>
                                    </Row>
                                </Col>
                            </Row>
                            <Row type="flex" justify="center">
                                <Col span={2}>
                                    <FormItem>
                                        <Button
                                            style={{ marginLeft: '-48px' }}
                                            type='primary'
                                            onClick={this.handleSubmit}
                                        >分诊
                                    </Button>
                                    </FormItem>
                                </Col>
                                <Col span={2}>
                                    <FormItem>
                                        <Button
                                            type='primary'
                                            htmlType="submit"
                                        >打印
                                    </Button>
                                    </FormItem>
                                </Col>

                            </Row>
                        </Form>
                    </Col>
                </Row>
            )
        }

    }
);
export default Preliminaryright;