import React, { Component } from 'react';
import { Modal,Row, message, Input, Button, Select, Radio, Icon, Table, Col ,Form} from 'antd';
import store from 'store';
import { $http } from '../../../containers/config/https';

const FormItem = Form.Item;
const { Option } = Select;
var user ;

const formItemLayout = {
    labelCol: {
      xs: { span: 24 },
      sm: { span: 8 },
    },
    wrapperCol: {
      xs: { span: 24 },
      sm: { span: 16 },
    },
  };
const formItemLayoutSelect = {
    labelCol: { span: 6 },
    wrapperCol: { span: 18 },
}

class DeviceBind extends Component {
    constructor(props) {
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        super(props);
        var date = new Date()
        this.state = {
            visible: false,
            bedDevice:false,
            hospitalArr:[],
            locArr:[],
            bedArr:[],
            locID:"",
            hospID:"",
            bedID:"",
        };
    }

    //初始化
    componentDidMount()
    {
        if(this.props.onRef)
        {
            this.props.onRef(this);
        }
        this.handlDeviceBasicData()
    }

    handlDeviceBasicData = () => {
        let { page2, pageSize2 } = this.state;
        let data = {
            params: [{
                locID:this.state.locID,
                hospID:this.state.hospID,
            }],
        }
        $http.post('urlS', this, {
            code: "04210003",
            data: data,
            success: function (res) {
               if(res.errorCode == 0){
                    this.setState({
                        hospitalArr:res.result.hospitalArr,
                        locArr:res.result.locInfoObj,
                        bedArr:res.result.bedArr,
                        deviceTypeArr:res.result.deviceTypeArr,
                    })
               }
            }
        })
    }

    hide = () => {
        this.setState({
            visible: false,
        })
    }

        //打开消息框
    showModal = () => {
        this.setState({
            visible: true,
            bedDevice:this.props.that?(this.props.that.state.clickRow?this.props.that.state.clickRow.typeCode=="BedCar":false):false  //判断绑定的设备是不是床头卡
        })
        this.handlDeviceBasicData()
    }

    deviceBind = () =>{
        var clickRow = this.props.that?this.props.that.state.clickRow:"";
        if (clickRow != ""){
            const formData = this.props.form.getFieldsValue();
            this.props.form.validateFields((err, value) => {
                if (err) {
                    this.setState({
                        clickFlag: false,
                        disabledAll: false
                    });
                }else{
                    let data = {
                        params: [{
                            ID:clickRow.id,
                            locID:formData.loc?formData.loc:"",
                            bedID:formData.bed?formData.bed:"",
                            hospCode:formData.hospital?formData.hospital:"",
                            //deviceDesc:"",useStatus
                            useStatus:"Y"
                        }]
                    }
                    $http.post('urlS', this, {
                        code: "04210001",
                        data: data,
                        success: function (res) {
                            if(res.errorCode==0){
                                message.success("绑定成功")
                            }
                            this.hide()
                            this.props.that.handlDeviceData()
                        }
                    })
                }
            })
        }     
    }

    selectHosp = (e) =>{
        this.props.form.setFieldsValue({loc:undefined,bed:undefined})
        this.setState({
            hospID:e,
        },()=>{
            this.handlDeviceBasicData()
        })
    }

    selectLoc = (e) =>{
        this.props.form.setFieldsValue({bed:undefined})
        this.setState({
            locID:e,
        },()=>{
            this.handlDeviceBasicData()
        })
    }

    selectBed = (e) =>{
        this.setState({
            bedID:e,
        })
    }

    render() {

        const { getFieldDecorator } = this.props.form;

        let hospitalArr = [];
        this.state.hospitalArr?this.state.hospitalArr.map((item) => {
            hospitalArr.push(
            <Option key={item.code} value={item.code} >
              {item.descripts}
            </Option>
          )
        }):""

        let locArr = [];
        this.state.locArr?this.state.locArr.map((item) => {
            locArr.push(
            <Option key={item.code} value={item.code} >
              {item.descripts}
            </Option>
          )
        }):""

        let bedArr = [];
        this.state.bedArr?this.state.bedArr.map((item) => {
            bedArr.push(
            <Option key={item.code} value={item.code} >
              {item.descripts}
            </Option>
          )
        }):""

        return (
            <Modal
                className="DeviceBind"
                title="设备绑定"
                visible={this.state.visible}
                destroyOnClose
                maskClosable={false}
                onCancel={this.hide}
                footer={null}
                width="600px"
                height="400px"
            >
                 <Form>
                        <Row style={{ margin: '23px 0 0 0' }}>
                            <Col span={24} >
                                <FormItem {...formItemLayout} label="所属医院">
                                    {getFieldDecorator('hospital', {
                                        //initialValue: params && params.healBureauID ? String(params.healBureauID) : undefined,
                                        rules: [{ required: false, message: '请填写设变编号' }],
                                    })(
                                        <Select
                                            style={{ width: '70%' }}
                                            allowClear={true}
                                            optionFilterProp="role"
                                            showSearch={true}
                                            onChange={this.selectHosp}
                                        >
                                            {hospitalArr}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            <Col span={24} >
                                <FormItem {...formItemLayout} label="科室">
                                    {getFieldDecorator('loc', {
                                        //initialValue: params && params.healBureauID ? String(params.healBureauID) : undefined,
                                        rules: [{ required: false, message: '请填写设变编号' }],
                                    })(
                                        <Select
                                            style={{ width: '70%' }}
                                            allowClear={true}
                                            optionFilterProp="role"
                                            showSearch={true}
                                            onChange={this.selectLoc}
                                        >
                                            {locArr}
                                        </Select>
                                    )}
                                </FormItem>
                            </Col>
                            {this.state.bedDevice? (
                                <Col span={24} >
                                    <FormItem {...formItemLayout} label="床号">
                                        {getFieldDecorator('bed', {
                                            //initialValue: params && params.healBureauID ? String(params.healBureauID) : undefined,
                                            rules: [{ required: false, message: '请填写设变编号' }],
                                        })(
                                            <Select
                                            style={{ width: '70%' }}
                                            allowClear={true}
                                            optionFilterProp="role"
                                            showSearch={true}
                                            onChange={this.selectBed}
                                        >
                                            {bedArr}
                                            </Select>
                                        )}
                                    </FormItem>
                                </Col>
                            ) : ""}
                            
                        </Row>  
                        <div style={{marginLeft:"30%"}}>
                            <Button type="primary" onClick={this.hide}  style={{ marginLeft:20 ,marginRight:20}}>取消</Button>
                            <Button type="primary"  onClick={this.deviceBind}>确定</Button>
                        </div>
                    </Form>
            </Modal>
        )
    }
}
export default Form.create()(DeviceBind);