// 拒绝发药原因
import React from 'react';
import {Modal, Form, Select} from 'antd';
import { $http } from 'containers/config/https';

const FormItem = Form.Item;
let {Option} = Select;

class RejectDispensingCause extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false, // 模态框显示状况
        }
    }

    componentDidMount() {
        this.props.onRef(this);
    }

    // 确认
    handleConfirm = () => {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let ordDispen = this.props.getOrdDispenData().ordDispenArr ? this.props.getOrdDispenData().ordDispenArr : []
                let data = {
                    params: [{
                        reasonID: values.reasonID,
                        ordDispen
                    }]
                }
                $http.post('urlS', this, {
                    code: "02040052",
                    data: data,
                    success: function (res) {
                        if (+res.errorCode === 0) {
                            this.handleCancel(); // 关闭弹窗
                            this.props.handleQuery(); // 重新查询明细数据
                        }
                    }
                })
            }
        })
    }

    // 打开弹窗
    handleShowModal = () => {
        this.setState({visible: true})
    }

    // 关闭弹窗
    handleCancel = () => {
        this.setState({visible: false})
    }

    render() {
        let {visible} = this.state;
        const { getFieldDecorator } = this.props.form;
        const formItemItem  = {
            labelCol: {span: 6},
            wrapperCol: {span: 18}
        };

        // 拒绝发药原因
        let reasonOption = this.props.reasonSelectData&&this.props.reasonSelectData.length > 0 ? this.props.reasonSelectData.map((item, index) => {
			return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
        }) : []

        return (
            <Modal
                visible={visible}
                title="拒绝发药原因"
                onCancel={this.handleCancel}
                onOk={this.handleConfirm}
            >
                <Form style={{minHeight: '100px'}}>
                    <FormItem label="拒绝发药原因：" {...formItemItem}>
                        {getFieldDecorator('reasonID', {
                            initialValue: undefined,
                            rules: [{ required: true, message: '拒绝发药原因不能为空' }]
                        })(
                            <Select style={{width: '70%'}}>
                                {reasonOption}
                            </Select>
                        )}
                    </FormItem>
                </Form>
          </Modal>
        )
    }
};
const RejectDispensingCauseForm = Form.create()(RejectDispensingCause);
export default RejectDispensingCauseForm;