/*
* 拒绝接诊理由
* */

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

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

class ShowRefuseReasonModal extends Component {

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

    loadData() {
        var params = {
            params: [{}]
        }
        $http.post('urlDeault', this, {
            code: "03100067",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    var data = res.result
                    var reasonValue = '',reasonCode = ''
                    if (!Util.isEmpty(data)) {
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].defaultValueFlag == 'Y') {
                                reasonValue = data[i].id
                                reasonCode = data[i].code
                            }
                        }
                    }
                    this.setState({
                        dataSource: res.result,
                        reasonValue: reasonValue,
                        reasonCode: reasonCode,
                    })
                }
            }
        });
    }

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

    //清空输入框
    cleanInput() {
        this.setState({
            dataSource: [],
            reasonCode: undefined,
            reasonValue: undefined,
            reason: undefined
        })
    }

    loadRefuseMsg() {
        var refuseOption = []
        this.state.dataSource && this.state.dataSource.map((item, index) => {
            refuseOption.push(<Radio key={index} value={item.id} code={item.code}>{item.descripts}</Radio>)
        })
        return refuseOption
    }

    changeRadioInput(e) {
        if (e.target.code == '04') {
            this.setState({
                reasonCode: e.target.code,
                reasonValue: e.target.value,
            })
        } else {
            this.setState({
                reasonCode: e.target.code,
                reasonValue: e.target.value,
                reason: undefined
            })
        }
    }

    //提交
    onSubmit() {
        let { onSubmit } = this.props;
        onSubmit && onSubmit(this.state.reasonValue, this.state.reason);
    }

    render() {
        return <div>
            <Modal
                title={'请选择拒绝接诊原因'}
                maskClosable={false} bodyStyle={{ height: '20vh' }}
                visible={this.props.isShow}
                okText='确定拒绝'
                onCancel={this.closeModal.bind(this)}
                onOk={this.onSubmit.bind(this)}>
                <div>
                    <Radio.Group onChange={this.changeRadioInput.bind(this)} value={this.state.reasonValue}>
                        {this.loadRefuseMsg()}
                    </Radio.Group>

                    {this.state.reasonCode == '04' ?
                        <TextArea rows={4} value={this.state.reason} onChange={Util.commonInputChange.bind(this, 'reason')}
                            style={{ marginTop: 10 }} />
                        : null}
                </div>
            </Modal></div>
    }
}

export default Form.create()(ShowRefuseReasonModal);