import React from 'react';
import PropTypes from 'prop-types';
import { $http } from '../../containers/config/https';
import { Util } from '../../tools';
import { Modal, Form, Input, message, Select, Icon } from 'antd';
const { Option } = Select;
const FormItem = Form.Item;
const FormHandel = Form.create()(
	// eslint-disable-next-line
	class extends React.Component {
		state = {
			visible: false,
			dataArr: [],
			HospitalArr: []
		};

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

		//医院
		selectHospitalData() {
			let data = {
				params: [{
					hospID: "",
					logonGroupID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).groupID : "",
					logonHospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
				}],
			}
			$http.post('urlS', this, {
				code: '01040001',
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						this.setState({
							hospArr: res.result
						})
					}
				}
			})
		}

		//单位/显示值
		selectInitData() {
			$http.post("urlS", this, {
				code: '05100058',
				data: { params: [] },
				success: function (res) {
					if (+res.errorCode === 0) {
						this.setState({
							dataArr: res.dataArr
						})
					}
				}
			});
		}

		// 显示弹出层
		showModal = () => {
			this.setState({
				visible: true,
			});
		}

		// 取消
		handleCancel = () => {
			this.setState({
				visible: false,
			})
		}

		// 重置表单
		resetFields() {
			// console.log("触发了")
			this.props.form.resetFields(); //清空所有
		}

		handelFunction(code, msg) {
			const { form: { validateFields } } = this.props;
			validateFields((errors, values) => {
				// 验证成功
				if (errors === null) {
					let data = {
						params: []
					}
					let obj = values;
					obj.id = this.props.userInfo.configid;
					// 处理需要传递的obj
					data.params.push(Util.disposeData(obj));
					$http.post('urlADS', this, {
						code: code,
						data: data,
						success: function (res) {
							if (+res.errorCode === 0) {
								// 提示
								message.success(msg + '成功');
								// 关闭弹框
								this.handleCancel();
								// 刷新列表数据
								this.props.handelThis.selectTableData();
								this.props.handelThis.selectMaxData()
							} else {
								if (this.props.flag === 'compile') {
									// 提示
									message.error(msg + '失败');
									// 编辑可能失败，但还是需要重置表单
									this.resetFields();
								}
							}
						},
						error: function () {
							if (this.props.flag === 'compile') {
								// 提示
								message.error(msg + '失败');
								// 编辑可能失败，但还是需要重置表单
								this.resetFields();
							}
						}
					})
				}
			});
		}

		// 确定
		handleCreate = () => {
			let { flag } = this.props;
			this.handelFunction('05100056', '编辑');
		}

		render() {
			const { getFieldDecorator } = this.props.form;
			let userInfo = this.props.userInfo ? this.props.userInfo : {}
			//医院
			let hospArrOption = []
			this.state.hospArr && this.state.hospArr.map((item, index) => {
				hospArrOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
			})
			//单位显示值
			let dataArrOption = []
			this.state.dataArr && this.state.dataArr.map((item, index) => {
				dataArrOption.push(<Option key={index} value={item.id}>{item.descripts}</Option>)
			})
			return (
				<Modal
					visible={this.state.visible}
					title="编辑"
					okText="确定"
					onCancel={this.handleCancel}
					onOk={this.handleCreate}
				>
					<Form layout="vertical">
						<FormItem label="医院名称">
							{getFieldDecorator('hospID', {
								initialValue: userInfo.hospID,
							})(
								<Select disabled>
									<Option value="">公用</Option>
									{hospArrOption}
								</Select>
							)}
						</FormItem>
						<FormItem label="操作符1">
							{getFieldDecorator('stOperate', {
								initialValue: userInfo.stOperate,
							})(
								<Select placeholder="请选择">
									<Option value="<="><Icon type='left' />=</Option>
									<Option value=">="><Icon type='right' />=</Option>
								</Select>
							)}
						</FormItem>
						<FormItem label="开始数值">
							{getFieldDecorator('stData', {
								initialValue: userInfo.stData,
							})(
								<Input disabled />
							)}
						</FormItem>
						<FormItem label="开始单位">
							{getFieldDecorator('stUomID', {
								initialValue: userInfo.stUomID,
							})(
								<Select>
									{dataArrOption}
								</Select>
							)}
						</FormItem>
						<FormItem label="显示值">
							{getFieldDecorator('showUomID', {
								initialValue: userInfo.showUomID,
							})(
								<Select>
									{dataArrOption}
								</Select>
							)}
						</FormItem>
						<FormItem label="操作符2">
							{getFieldDecorator('endOperate', {
								initialValue: userInfo.endOperate,
							})(
								<Select placeholder="请选择">
									<Option value="<"><Icon type='left' /></Option>
									<Option value=">"><Icon type='right' /></Option>
								</Select>
							)}
						</FormItem>
						<FormItem label="结束数值">
							{getFieldDecorator('endData', {
								initialValue: userInfo.endData,
							})(
								<Input />
							)}
						</FormItem>
						<FormItem label="结束单位">
							{getFieldDecorator('endUomID', {
								initialValue: userInfo.endUomID,
							})(
								<Select>
									{dataArrOption}
								</Select>
							)}
						</FormItem>
					</Form>
				</Modal>
			);
		}
	}
);
export default FormHandel;

FormHandel.propTypes = {
	// 验证userInfo的属性
	userInfo: PropTypes.object
}