/**
 * 作废、撤销医嘱
 * 
 */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, message } from 'antd';
import { Util } from 'tools/index';
import { $http } from 'containers/config/https';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;

class ShowStopAdviceReasonModal extends Component {

    constructor(props) {
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        super(props);
        this.state = {
            userData: userData
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.setState({
                stopStatus: nextProp.stopStatus
            })
            this.loadStopReason()
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    loadStopReason() {
        let data = {
            params: [{
                language: this.state.userData.language
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060027",
            data: data,
            success: (res) => {
                if (res.errorCode == '0') {
                    var stopReason = res.result;
                    var reasonID;
                    if (!Util.isEmpty(stopReason)) {
                        for (var i = 0; i < stopReason.length; i++) {
                            if (stopReason[i].defaultFlag == 'Y') {
                                reasonID = stopReason[i].id
                                break;
                            }
                        }
                    }
                    this.setState({
                        stopReason: res.result,
                        reasonID: reasonID
                    })
                }
            }
        })
    }

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

    //清空输入框
    cleanInput() {
        this.setState({
            reasonID: undefined
        })
    }

    commonSelectOption(name, e) {
        this.setState({
            [name]: e
        })
    }

    //提交
    onSubmit() {
        if (Util.isEmpty(this.state.reasonID)) {
            if (this.state.stopStatus == '1') {
                message.error('作废原因不能为空，请选择！')
                return
            } else if (this.state.stopStatus == '1') {
                message.error('撤销原因不能为空，请选择！')
                return
            }
        }
        let { onSubmit } = this.props;
        onSubmit && onSubmit(this.state.reasonID);
    }

    render() {

        var ordStopReasonOption = []
        this.state.stopReason && this.state.stopReason.map((item, index) => {
            ordStopReasonOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
        })

        return <Modal className='unawares-modal'
            title={this.state.stopStatus == '1' ? '作废原因' : '撤销原因'}
            onCancel={this.closeModal.bind(this)}
            onOk={this.onSubmit.bind(this)}
            maskClosable={false}
            visible={this.props.isShow}
        >
            {this.state.stopStatus == '1' ? '作废原因：' : '撤销原因：'}<Select style={{ width: '200px' }} allowClear showSearch
                placeholder='请选择医嘱原因' value={this.state.reasonID}
                onChange={this.commonSelectOption.bind(this, 'reasonID')}>{ordStopReasonOption}</Select>

        </Modal>
    }
}

export default Form.create()(ShowStopAdviceReasonModal);