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

let {Option} = Select;

class AddNodeDirectory extends React.Component {
	constructor() {
		super();
		var userData = JSON.parse(sessionStorage.getItem('userData'));
		this.state = {
			userInfo: {},
			flag: "",
			rowData: {},
			code: "",
			knowledgeData: {},
			departmentSelectData: [],
			userData
		}
	}

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

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

	// 打开弹窗
	showModal = (flag, code, knowledgeBaseTreeSelectData, userInfo, rowData) => {
		this.setState({flag, code, knowledgeData: knowledgeBaseTreeSelectData, rowData, visible: true}, () => {
			if (!(this.state.departmentSelectData&&this.state.departmentSelectData.length > 0)) {
				this.getDepartmentInformation();
			}
			if (flag === 'add') {
				this.setState({userInfo: {}})
			} else {
				this.setState({userInfo})
			}
		})
	}

	// 保存
	handleSave = () => {
		let {code, knowledgeData, rowData} = this.state;
		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 (code === '06020020') {
					obj.parentID = rowData&&rowData.ID ? rowData.ID : "";
					obj.knowledgeID = knowledgeData&&knowledgeData.ID ? knowledgeData.ID : "";
				} else if (code === '06020021') {
					obj.ID = rowData&&rowData.ID ? rowData.ID : "";
				} else if (code === '06020027') {
					obj.knowledgeNodeID = rowData.ID;
					obj.nodeContent = "";
				} else if (code === '06020028') {
					obj.ID = rowData.ID;
					obj.nodeContent = "";
					obj.dataSource = [];
				}

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

	// 重置表单
	resetFields() {
		this.props.form.resetFields();
	}
  
	// 3.13.23.获取科室信息
	getDepartmentInformation = () => {
		let {userData} = this.state;
		let data = {
			params: [
				{ 
					hospID: userData&&userData.hospID ? userData.hospID : '',
					type: "I^O^E^OP^OI^OR",
				}
			]
		}
		$http.post('urlS', this, {
			code: "04020008",
			data: data,
			success: function (res) {
				if (+res.errorCode === 0) {
					this.setState({ departmentSelectData: res.result.Data})
				} else {
					this.setState({ departmentSelectData: [] })
				}
			}
		})
  }

	render() {
		const { getFieldDecorator } = this.props.form;
		let {visible, flag, userInfo, code, departmentSelectData} = this.state;
		
    // 科室
    let departmentOption = departmentSelectData&&departmentSelectData.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={flag === 'add' ? (code === "06020020" ? "新增知识库节点目录" : "新增知识库节点数据") : (code === "06020021" ? "编辑知识库节点目录" : "编辑知识库节点数据")}
				okText="保存"
				onCancel={this.handleCancel}
				onOk={this.handleSave}
				width={600}
			>
				<Form>
					{(code === "06020020" || code === "06020021") ? (<Form.Item label="所属科室：">
						{getFieldDecorator('nodeLocIDs', {
							initialValue: userInfo.nodeLocIDs ? userInfo.nodeLocIDs : undefined,
							rules: [{ required: true, message: '所属科室不能为空' }]
						})(
						<Select showSearch optionFilterProp="seatchprop" allowClear placeholder="请选择" mode="multiple">
							{departmentOption}
						</Select>
						)}
					</Form.Item>) : ""}
					<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 AddNodeDirectoryForm = Form.create()(AddNodeDirectory);
export default AddNodeDirectoryForm;