/*
* 查看过期号源
* */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, Button, Row, Col, Table, Card, message } from 'antd';
import AddMechanism from 'components/addMechanism';
import { $http } from 'containers/config/https';
import { Util } from 'tools/index';
import '../style/index.less';

const Option = Select.Option;
const FormItem = Form.Item;

class ShowDisableModal extends Component {

    constructor(props) {
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        super(props);
        this.state = {
            dataSource: [],
            data: [{}],
            saveData: [],
            userData: userData
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.setState({
                resourceType: nextProp.resourceType,
                resourceTypeID: nextProp.resourceTypeID,
            },()=>{
                this.loadLocResDetail(nextProp.locResDetailId, nextProp.locResModDetailId)
                this.initData()
            })
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    componentDidMount() {
        // this.initData()
        this.getDoctorRoom()
    }

    //获取诊室
    getDoctorRoom() {
        var params = {
            params: [{
                hospitalId: this.state.hospitalId,
                locID:this.state.userData.locID,
                resourceTypeID: this.state.resourceTypeID
            }]
        }
        // var params = []
        $http.post('urlDeault', this, {
            code: "01040023",
            data: params,
            success: function (res) {
                if (res.errorCode === '0') {
                    this.setState({
                        doctorRoom: res.room
                    })
                }
            }
        });
    }

    loadLocResDetail(id, locResModDetailId) {
        var params = {
            params: [{
                locResDetailID: id || '',
                locResModDetailID: locResModDetailId || '',
                flag:'',
                resourceTypeID: this.state.resourceTypeID
            }]
        }
        // var params = []
        $http.post('urlDeault', this, {
            code: "03020041",
            data: params,
            success: function (res) {
                if (res.errorCode === '0') {
                    this.setState({
                        dataSource: res.data
                    })
                }
            }
        });
    }

    initData() {
        this.columns = [
            {
                title: '时间段',
                dataIndex: 'timeDetailStr',
                width: 120,
                align: 'center'
            }, {
                title: '号源数',
                dataIndex: 'registNum',
                width: 120,
                align: 'center',
                render: (text, data) => {
                    return <span>
                        <span>{data.registNum}</span>
                        <span>{'(已使用' + data.registedNum + ')'}</span>
                    </span>
                }
            }, {
                title: '新号源',
                dataIndex: 'newNum',
                width: 120,
                align: 'center',
                render: (text, data, index) => {
                    return <Input placeholder='请输入新号源' disabled={true} onChange={this.changeNewNum.bind(this, text, data, index)}
                        style={{ width: '80' }} />
                }
            }, {
                title: '原预约挂号数',
                dataIndex: 'appointNum',
                width: 120,
                align: 'center',
                render: (text, data) => {
                    return <span>
                        <span>{data.appointNum}</span>
                        <span>{'(已使用' + data.appointedNum + ')'}</span>
                    </span>
                }
            }, {
                title: '新预约挂号数',
                dataIndex: 'newRegNum',
                width: 120,
                align: 'center',
                render: (text, data, index) => {
                    return <Input placeholder='请输入新预约挂号数' disabled={true} onChange={this.changeRegNum.bind(this, text, data, index)}
                        style={{ width: '80' }} />
                }
            },
        ]
    }

    loadData() {

    }

    //获取新号源参数
    changeNewNum(text, data, index, e) {
        var datas = e.target.value;
        if (!(/(^[1-9]\d*$)/.test(datas)) && datas.length > 0) {
            message.error('请输入整数')
            return
        }
        var { dataSource } = this.state;
        for (var i = 0; i < dataSource.length; i++) {
            if (data.locResModTimeId == dataSource[i].locResModTimeId) {
                this.state.dataSource[i].registNum = e.target.value
                this.state.dataSource[i].newNum = e.target.value
            }
        }
        // this.state.newNum = e.target.value;
        // this.setState({newNum:e.target.value})
    }

    //获取新预约挂号数
    changeRegNum(text, data, index, e) {
        var datas = e.target.value;
        if (!(/(^[1-9]\d*$)/.test(datas)) && datas.length > 0) {
            message.error('请输入整数')
            return
        }
        var { dataSource } = this.state;
        for (var i = 0; i < dataSource.length; i++) {
            if (data.locResModTimeID == dataSource[i].locResModTimeID) {
                this.state.dataSource[i].appointNum = e.target.value
                this.state.dataSource[i].newRegNum = e.target.value
            }
        }
        // this.setState({newRegNum:e.target.value})
        // this.state.newRegNum = e.target.value;
    }

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

    //清空输入框
    cleanInput() {
        this.props.form.resetFields();
        this.setState({
            newNum: '',
            newRegNum: '',
            dataSource: []
        })
    }

    //提交
    onSubmit() {
      var {onHide} = this.props;
      onHide && onHide()
    }

    render() {
        let { getFieldDecorator, getFieldValue } = this.props.form;
        let formItemLayout = {
            labelCol: { span: 10 },
            wrapperCol: { span: 19 }
        };

        let merchantOption = [];
        //坐诊诊室
        let doctorRoomOption = []
        this.state.doctorRoom && this.state.doctorRoom.map((item, index) => {
            doctorRoomOption.push(<Option key={index} value={item.id + ''}>{item.descripts}</Option>)
        })
        return <Modal className='departModalStyle'
            title={'设置医生排班'}
            maskClosable={false}
            footer={null}
            visible={this.props.isShow}
            onCancel={this.closeModal.bind(this)}
            onOk={this.onSubmit.bind(this)}>
            <div>
                <div className="modal-body">
                    <span>
                        <span>
                            {this.props.modalData ? this.props.modalData.userName : ''}{'/'}
                            {this.props.modalData ? this.props.modalData.regTitle : ''}{'/'}
                            {this.props.modalData ? this.props.modalData.dateDetail : ''}
                        </span>
                        {/* <span>医生：{this.props.modalData ? this.props.modalData.userName : ''}</span>
                        <span style={{ marginLeft: '10px' }}>排班类型：{this.props.modalData ? this.props.modalData.regTitle : ''}</span>
                        <span style={{ marginLeft: '10px' }}>坐诊时间：{this.props.modalData ? this.props.modalData.dateDetail : ''}</span> */}
                        <span style={{ marginLeft: '10px' }}>坐诊诊室：<Select placeholder="请选择坐诊诊室" style={{ width: '150px' }}
                            onChange={Util.handleSelectChange.bind(this, 'roomDr')} disabled={true}
                            defaultValue={!Util.isEmpty(this.state.doctorRoom)?(this.props.modalData ? this.props.modalData.roomID : ''):''}
                            getPopupContainer={triggerNode => triggerNode.parentNode}>
                            {doctorRoomOption}
                        </Select></span>

                    </span>
                    <div>
                        <Table columns={this.columns} style={{ width: '100%', height: 200 }}
                            dataSource={this.state.dataSource}
                            loadData={this.loadData.bind(this)}
                            bordered
                            rowKey={(data) => {
                                return data.id
                            }}
                            pagination={false}></Table>
                        <div style={{ marginTop: '20px' }}>
                            {/* <AddMechanism showBtn={true} data={this.state.data}></AddMechanism> */}
                        </div>
                    </div>
                </div>
            </div>
        </Modal>
    }
}

export default Form.create()(ShowDisableModal);