// 病历模板分组目录树
import React from 'react';
import {Form, Input, Modal, message } from 'antd';
import { $http } from 'containers/config/https';

class GroupedCatalogTree extends React.Component {
    constructor() {
        super();
        this.state = {
            userInfo: {},
            flag: 'add',
            HBEMRWTCParRef: '',
        }
    }

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

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

    // 打开弹窗
    showModal = (flag, rowId, rowDaata, dataRef) => {
        if (flag === 'add') {
            this.resetFields()
        }
		this.setState({visible: true, flag, HBEMRWTCParRef: rowId, rowDaata, userInfo: dataRef})
    }

    // 保存
    handleSave = () => {
        let { flag } = this.state;
        if (flag === 'compile') {
            this.handelInterface('06030008', '编辑');
        } else {
            this.handelInterface('06030007', '新增');
        }
    }

    handelInterface = (code, msg) => {
        let {HBEMRWTCParRef, rowDaata} = this.state;
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = JSON.parse(JSON.stringify(values));
                obj.HBEMRWTCParRef = msg === '新增' ? HBEMRWTCParRef : undefined;
                obj.parentID = msg === '新增' ? rowDaata.ID : undefined;
                obj.ID = msg === '编辑' ? rowDaata.ID : undefined;

                // 处理需要传递的obj
                data.params.push(obj);
                $http.post('urlADS', this, {
                    code: code,
                    data: data,
                    success: function (res) { 
                        if (+res.errorCode === 0) {
                            // 成功提示
                            message.success(msg + '成功');
                            // 新增或编辑成功重置form表单
                            this.resetFields();
                            // 关闭弹框
                            this.handleCancel();
                            // 刷新列表数据
                            this.props.handleQuery();
                        }
                    }
                })
            }
        });
    }

    // 重置表单
    resetFields() {
      	this.props.form.resetFields();
    }

    render() {
        const { getFieldDecorator } = this.props.form;
        let {visible, flag, userInfo} = this.state;
        return (
            <Modal
                visible={visible}
                title={flag === 'add' ? '新增病历模板分组目录' : '编辑病历模板分组目录'}
                okText="保存"
                onCancel={this.handleCancel}
                onOk={this.handleSave}
                width={600}
            >
                <Form>
					<Form.Item label="目录代码：">
						{getFieldDecorator('code', {
							initialValue: userInfo.code ? userInfo.code: undefined,
							rules: [{ required: true, message: '目录代码不能为空' }]
						})(
							<Input placeholder="请输入" />
						)}
					</Form.Item>
					<Form.Item label="目录名称：">
						{getFieldDecorator('descripts', {
							initialValue: userInfo.descripts ? userInfo.descripts: undefined,
							rules: [{ required: true, message: '目录名称不能为空' }]
						})(
							<Input placeholder="请输入" />
						)}
					</Form.Item>
                </Form>
            </Modal>
        )
    }
};
const GroupedCatalogTreeForm = Form.create()(GroupedCatalogTree);
export default GroupedCatalogTreeForm;