/*
* 协议
* */

import React, { Component } from 'react';
import {
    Input, Form, Select, Modal, Button, Progress, Upload, message, Icon, Table,
    Card, Row, Col, DatePicker, Popconfirm, Checkbox, Radio, Calendar, Tabs
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import DiabetesPageTabPane from './DiabetesPageTabPane';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;
const Search = Input.Search;
const FormItem = Form.Item;
const TabPane = Tabs.TabPane;
const { TextArea } = Input;

class ShowSignDoctorListModal extends Component {

    constructor(props) {
        super(props);
        var date = new Date();
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        var patID = JSON.parse(sessionStorage.getItem('patID'))
        this.state = {
            leftData: [],
            rightData: [],
            userData: userData,
            activeKey: "1",
            patID: patID
        }
    }
    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {

        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    componentDidMount() {
        this.loadHospitalDactor()
        this.getDepartment(this.state.userData.hospID)
    }

    getDoctorList() {
        var params = {
            params: [{
                hospID: this.state.hospitalID,
                locID: this.state.locID,
            }]
        }
        $http.post('urlDeault', this, {
            code: "03070033",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        doctorData: res.result.data,
                    })
                }
            }
        });
    }

    getDepartment(e) {
        var params = {
            params: [{
                hospID: e ? e : this.state.hospitalID,
            }]
        }
        $http.post('urlS', this, {
            code: "03020046",
            data: params,
            success: function (res) {
                if (res.errorCode === '0') {
                    this.setState({
                        departmentData: res.result.Data
                    })
                }
            }
        })
    }


    //获取家庭医生
    loadHospitalDactor() {
        var params = {
            params: [{
                hospID: this.state.userData.hospID
            }]
        }
        $http.post('urlDeault', this, {
            code: "03070032",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    var source = res.result;
                    var code = []
                    code.push({
                        hospID: res.result[0].hospID,
                        hospDesc: res.result[0].hospDesc,
                    })
                    var newData = []
                    if (!Util.isEmpty(source)) {
                        var data = source[0].subHospList
                        if (!Util.isEmpty(data)) {
                            for (var i = 0; i < data.length; i++) {
                                newData.push({
                                    hospID: data[i].hospID,
                                    hospDesc: data[i].hospDesc,
                                })
                            }
                        }
                    }
                    this.setState({
                        familyDoctor: code.concat(newData),
                    })
                }
            }
        });
    }

    closeModal(name) {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.setState({
            hospitalID: undefined,
            locID: undefined,
            doctorID: undefined,
            docID:undefined
        })
    }

    handleSelectChange(name, e) {
        this.setState({
            [name]: e,
            locID: undefined,
            docID: undefined
        }, () => {
            this.getDepartment()
        })
    }

    handleSelectLocID(name, e) {
        this.setState({
            [name]: e,
            docID: undefined
        }, () => {
            this.getDoctorList()
        })
    }
    onSubmit() {
        var params = {
            hospitalID: this.state.hospitalID,
            locID: this.state.locID,
            doctorID: this.state.docID,
        }
        var { onSubmit } = this.props;
        onSubmit && onSubmit(params)
    }

    render() {
        let familyDoctorOption = []
        this.state.familyDoctor && this.state.familyDoctor.map((item, index) => {
            familyDoctorOption.push(<Option key={index} value={item.hospID}>{item.hospDesc}</Option>)
        })

        var departmentDataOption = []
        this.state.departmentData && this.state.departmentData.map((item, index) => {
            departmentDataOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })
        var doctorDataOption = []
        this.state.doctorData && this.state.doctorData.map((item, index) => {
            doctorDataOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })
        return <div>
            <Modal
                title={'新增健康管理团队成员'}
                maskClosable={false}
                visible={this.props.isShow}
                // footer={null}
                onCancel={this.closeModal.bind(this)}
                onOk={this.onSubmit.bind(this)}>
                <div style={{ textAlign: 'center' }}>
                    <div>
                        机构：<Select style={{ width: 220 }} onSelect={this.handleSelectChange.bind(this, 'hospitalID')}
                            value={this.state.hospitalID}>{familyDoctorOption}</Select>
                    </div>

                    <div style={{ marginTop: 5 }}>
                        科室：<Select style={{ width: 220 }} onSelect={this.handleSelectLocID.bind(this, 'locID')}
                            value={this.state.locID}>{departmentDataOption}</Select>
                    </div>

                    <div style={{ marginTop: 5 }}>
                        人员：<Select style={{ width: 220 }} onSelect={Util.handleSelectChange.bind(this, 'docID')}
                            value={this.state.docID}>{doctorDataOption}</Select>
                    </div>
                </div>
            </Modal>
        </div>
    }
}

export default Form.create()(ShowSignDoctorListModal);