// 新建知识库
import React from 'react';
import {Form, Input, Modal, message, Select } from 'antd';
import { $http } from 'containers/config/https';

let {Option} = Select;

class AddAddKnowledgeBase extends React.Component {
    constructor() {
        super();
        this.state = {
            userInfo: {},
            knowledgeBaseTreeSelectData: {},
            flag: 'add',
			selectCodeData: []
        }
    }

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

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

    // 打开弹窗
    showModal = (obj) => {
        if ((this.state.flag === 'add') && (obj.knowledgeBaseTreeSelectData.ID !== this.state.knowledgeBaseTreeSelectData.ID)) {
            this.resetFields();
        }
        this.setState({
            visible: true, 
            knowledgeBaseTreeSelectData: obj&&obj.knowledgeBaseTreeSelectData ? obj.knowledgeBaseTreeSelectData : {},
            flag: obj&&obj.flag ? obj.flag : 'add',
        }, () => {
			if (!(this.state.selectCodeData&&this.state.selectCodeData.length > 0)) {
				this.getSelectCodeData()
			}
            if (this.state.flag === 'compile') {
                this.setState({userInfo: this.state.knowledgeBaseTreeSelectData})
                this.resetFields();
            } else {
                this.setState({userInfo: {}})
            }
        })
    }

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

    handelInterface = (code, msg) => {
        const { form: { validateFields } } = this.props;
        validateFields((errors, values) => {
            // 验证成功
            if (errors === null) {
                let data = {
                    params: []
                }
                let obj = JSON.parse(JSON.stringify(values));
                // 当增加时点击复制把ID值传给后天然后后台根据ID值去复制权限，编辑时则根据ID去保存修改
                if (msg === '编辑') {
                    obj.ID = this.state.knowledgeBaseTreeSelectData.ID
                }
                obj.catalogID = this.state.knowledgeBaseTreeSelectData&&this.state.knowledgeBaseTreeSelectData.ID ? this.state.knowledgeBaseTreeSelectData.ID : '';

                // 处理需要传递的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();
    }
	
	// 获取病历章节列表 --- 代码下拉数据
	getSelectCodeData = () => {
		let data = {
            params: [{}]
        }
        $http.post('urlS', this, {
        code: "06010085",
        data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({selectCodeData: res.result})
                }
            }
        })
	}

	handleDescriptsChange = e => {
		for(var i = 0; i < this.state.selectCodeData.length; i++) {
			if (this.state.selectCodeData[i].eMRChapterDesc === e) {
				this.props.form.setFieldsValue({
					code: this.state.selectCodeData[i].eMRChapterCode
				})
				return;
			}
		}
	}

    render() {
        const { getFieldDecorator } = this.props.form;
        let {visible, flag, userInfo, selectCodeData} = this.state;
        
		// 模板章节名称
		let descriptsOption = selectCodeData&&selectCodeData.map((item, index) => {
			return (<Option key={index} value={item.eMRChapterDesc} title={item.eMRChapterDesc} >{item.eMRChapterDesc}</Option>)
		})
        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 />
                        )}
                    </Form.Item>
                    <Form.Item label="知识库名称：">
                        {getFieldDecorator('descripts', {
                            initialValue: userInfo.descripts ? userInfo.descripts: undefined,
                            rules: [{ required: true, message: '知识库名称不能为空' }]
                        })(
                            <Input />
                        )}
                    </Form.Item>
                    {/* <Form.Item label="模板章节代码：">
                        {getFieldDecorator('code', {
                            initialValue: userInfo.code ? userInfo.code: undefined,
                            rules: [{ required: true, message: '模板章节代码不能为空' }]
                        })(
                            <Input disabled />
                        )}
                    </Form.Item>
                    <Form.Item label="模板章节名称：">
                        {getFieldDecorator('descripts', {
                            initialValue: userInfo.descripts ? userInfo.descripts: undefined,
                            rules: [{ required: true, message: '模板章节名称不能为空' }]
                        })(
                            <Select showSearch optionFilterProp="seatchprop" allowClear placeholder="请选择" onChange={this.handleDescriptsChange}>
								{descriptsOption}
							</Select>
                        )}
                    </Form.Item> */}
                    {/* <Row>
                        <Col span={12}>
                            <Form.Item label="知识库类别：" {...formItemInput}>
                                {getFieldDecorator('knowledgeCategory', {
                                    initialValue: userInfo.knowledgeCategory ? userInfo.knowledgeCategory: undefined,
                                    rules: [{ required: true, message: '知识库类别不能为空' }]
                                })(
                                    <Select>
                                        <Option value='G'>表格知识库</Option>
                                        <Option value='T'>文本知识库</Option>
                                    </Select>
                                )}
                            </Form.Item>
                        </Col>
                    </Row> */}
                </Form>
            </Modal>
        )
    }
};
const AddAddKnowledgeBaseForm = Form.create()(AddAddKnowledgeBase);
export default AddAddKnowledgeBaseForm;