/*
* 设置表单
* */
import React, { Component } from 'react';
import { Switch, Form, Input, Row, Col, Button, message, DatePicker, Select, Icon } from 'antd';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import moment from 'moment';
import { $http } from '../../containers/config/https';
import SetFormOrderTable from './SetFormOrderTable';
import BatchInformation from './BatchInformation';

const Option = Select.Option;
const { TextArea } = Input;

const InventoryFormModal = Form.create()(
	class extends Component {
		constructor(props) {
			super(props);
			this.state = {
				editField: [],
				fetching: false,
				data: [],
				value: "",
				selectData: {},
				detailInfo: {}, //双击药品获取的数据
				rowId: "",
				ColNum: 6,
				className: "",
				operateAuth: "",
				ModalRowData: {},  //双击带过来的行数据
				ModalDataList: {},
				userInfo: {}, //点击右边表格行数据
				modalData: [], // 弹框列表数据
				unitData: [],
				rowKey: '',
				fieldDesc: '',
				parameter: '', //录入顺序的数据参数
				inputFocus: true,  //默认光标
				pageSize: 10,
				page: 1,
				showdetailList: false,
				getBatchDetail: {}, //点击右边表格显示录入表格数据
				detailWidthTotal: '',
				detailColumns: [], //表头数据
				listLength: '',
				modalColumns: [],
				languageSize: {}, //录入翻译
				language: JSON.parse(window.sessionStorage.userData),
				columnsData: [], //公共表头
				loading: false
			};
			this.modalColumns = [
				{
					title: '加锁', dataIndex: 'lockFlag', key: '16', width: '150px', align: "center",
					render: (text, record) => (
						<span> {record.lockFlag ? <Icon type="check" /> : <Icon type="close" />}</span>
					)
				},
				{ title: '代码', dataIndex: 'stockCode', key: '1', width: '150px', align: "center" },
				{ title: '药品名称', dataIndex: 'stockDesc', key: '2', width: '150px', align: "center" },
				{ title: '规格', dataIndex: 'stockSpec', key: '3', width: '150px', align: "center" },
				{ title: '剂型', dataIndex: 'stockForm', key: '4', width: '150px', align: "center" },
				{ title: '货位码', dataIndex: 'stkLocBin', key: '5', width: '150px', align: "center" },
				{ title: '基本单位库存', dataIndex: 'locBQty', key: '6', width: '150px', align: "center" },
				{ title: '入库单位库存', dataIndex: 'locPQty', key: '8', width: '150px', align: "center" },
				{ title: '占用数', dataIndex: 'dirQty', key: '9', width: '150px', align: "center" },
				{ title: '进价', dataIndex: 'rp', key: '11', width: '150px', align: "center" },
				{ title: '进价金额', dataIndex: 'rpAmt', key: '12', width: '150px', align: "center" },
				{ title: '售价', dataIndex: 'sp', key: '13', width: '150px', align: "center" },
				{ title: '售价金额', dataIndex: 'spAmt', key: '14', width: '150px', align: "center" },
			];
			this.detailColumns = [
				{
					title: '医嘱激活', dataIndex: 'ordActive', key: '1', width: '150px', align: "center",
					render: (text, record) => (
						<span> {record.ordActive === 1 ? <Icon type="check" /> : <Icon type="close" />}</span>
					)
				},
				{
					title: '库存激活', dataIndex: 'stkActive', key: '2', width: '150px', align: "center",
					render: (text, record) => (
						<span> {record.stkActive === 1 ? <Icon type="check" /> : <Icon type="close" />}</span>
					)
				},
				{ title: '批号', dataIndex: 'batNo', key: '3', width: '150px', align: "center" },
				{ title: '有效期', dataIndex: 'expDate', key: '4', width: '150px', align: "center" },
				{ title: '生产日期', dataIndex: 'produceDate', key: '5', width: '150px', align: "center" },
				{ title: '库存数', dataIndex: 'batQty', key: '6', width: '150px', align: "center" },
				{ title: '占用数', dataIndex: 'dirQty', key: '7', width: '150px', align: "center" },
				{ title: '进价(基本单位)', dataIndex: 'baseRp', key: '9', width: '150px', align: "center" },
				{ title: '售价(基本单位)', dataIndex: 'baseSp', key: '10', width: '150px', align: "center" },
				{ title: '进价(售价单位)', dataIndex: 'purRp', key: '11', width: '150px', align: "center" },
				{ title: '售价(售价单位)', dataIndex: 'purSp', key: '12', width: '150px', align: "center" },
				{ title: '生产企业', dataIndex: 'manfDesc', key: '13', width: '200px', align: "center" },
				{ title: '供应商', dataIndex: 'vendorDesc', key: '14', width: '200px', align: "center" },
			];
		}

		componentDidMount() {
			this.props.onRef(this);
			this.getEditField();
			this.getDetailTableColumns();
			this.getDetailListTableColumns();
		}

		componentWillReceiveProps(props) {
			this.setState({ getBatchDetail: props.getBatchDetail, userInfo: props.userInfo ? props.userInfo : {}, rowKey: props.rowKey, unitData: props.unitData, SelectData: props.SelectData, parameter: props.parameter })
		}

		componentDidUpdate(prevProps, prevState) {
			if (this.state.editField.length > 0 && !this.props.form.getFieldValue(this.state.editField[0].code) && this.state.inputFocus) {
				this.input.focus();
			}
		}

		// 获取明系列表头数据
		getDetailTableColumns = () => {
			let data = {
				params: [{
					compontName: 'stockDescPopupList',
					type: 'C',
				}]
			}
			$http.post('urlS', this, {
				code: '01040073',
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						let arr = res.result.C && res.result.C.length > 0 ? JSON.parse(JSON.stringify(res.result.C)).map((item, index) => {
							return {
								...item,
								key: index + 1,
								title: <div onDoubleClick={() => { this.columnRef.onOpenModel() }}>{item.title}</div>,
								render: (text, record) => (
									<span>
										{item.code == 'lockFlag' ?
											record.lockFlag ? <Icon type="check" /> : <Icon type="close" />
											: text
										}
									</span>
								)
							}
						}) : '';
						let filterArr = arr.filter(function (item) { return item.display === 'Y' });
						this.setState({ modalColumns: filterArr, listLength: res.totalWidth })
					}
				}
			})
		}

		// 获取明系列表头数据
		getDetailListTableColumns = () => {
			let data = {
				params: [{
					compontName: 'stockSescPopupDetailList',
					type: 'C',
				}]
			}
			$http.post('urlS', this, {
				code: '01040073',
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						let arr = res.result.C && res.result.C.length > 0 ? JSON.parse(JSON.stringify(res.result.C)).map((item, index) => {
							return {
								...item,
								key: index + 1,
								title: <div onDoubleClick={() => { this.columnRef.onOpenModel() }}>{item.title}</div>,
								render: (text, record) => (
									<span>
										{item.code == 'ordActive' ?
											(record.ordActive == 1 ? <Icon type="check" /> : <Icon type="close" />)
											: item.code == 'stkActive' ?
												(record.stkActive === 1 ? <Icon type="check" /> : <Icon type="close" />)
												: text
										}
									</span>
								)
							}
						}) : '';
						let filterArr = arr.filter(function (item) { return item.display === 'Y' });
						this.setState({ detailColumns: filterArr, detailWidthTotal: res.totalWidth })
					}
				}
			})
		}

		onRefStockDesc = (ref) => {
			this.childStock = ref;
		}

		//组件权限编辑器
		onColumnRef = (ref) => {
			this.columnRef = ref;
		}

		//获取表单的模板数据
		getEditField = () => {
			let { language } = this.state;
			let data = {
				params: [
					{ compontName: this.props.parameter ? this.props.parameter : 'purchasePlan', }
				]
			};
			$http.post('urlS', this, {
				code: "02020206",
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						this.setState({
							editField: language.language == 'EN' ? res.result[0].ENRows : res.result[0].CNRows,
							ColNum: res.result[0].columns,
							typeID: res.result[0].typeID,
							columnsData: res.result[0].componet.C,
							languageSize: language.language == 'EN' ? res.result[0].componet.language.EN : res.result[0].componet.language.CN
						})
					}
				}
			})
		}

		// 获取描述列表数据
		getModalListData(alias) {
			let data = {
				params: [
					{
						stockAlias: alias, // 库存项描述别名
						stkSubCatID: this.props.stkCatID ? this.props.stkCatID : undefined,
						locID: this.props.inOpLocDr ? this.props.inOpLocDr : undefined,
						vendorID: this.props.parameter == 'MaterialSalesReturnDetailList' ? this.props.vendorID : undefined,
					}
				],
				pagination: [
					{
						pageSize: this.state.pageSize,
						currentPage: this.state.page,
					}
				],
			};
			$http.post('urlS', this, {
				code: this.props.APICode ? this.props.APICode : "02020103",
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						for (var i = 0; i < res.result.rows.length; i++) {
							res.result.rows[i].key = i + 1
						}
						this.setState({ modalData: res.result.rows, rowId: res.result.rows[0].key })
					} else {
						this.setState({ modalData: [] })
					}
				}
			})
		}

		// 操作弹框
		onClickRowPublic = (record) => {
			return {
				// 单击行选中
				onClick: () => {
					if (this.state.rowId === '') {
						// 保存行数据以及ID
						this.setState({ rowId: record.key })
					} else {
						if (this.state.rowId !== record.key) {
							this.setState({ rowId: record.key })
						}
					}
				},
				onDoubleClick: () => {
					this.setState({ modalData: [] }, () => {
						this.confirmSelection(record);
					})
				}
			}
		}

		// 双击选中行数据的回调
		confirmSelection = (record) => {
			// this.resetFields();
			if (!this.state.showdetailList) {
				this.setState({
					ModalDataList: record
				})
			}
			this.setState({ ModalRowData: record }, () => {
				// 需要根据后台返回回来的默认值判断是否需要二次弹窗
				const selectData = this.props && this.props.SelectData ? this.props.SelectData : {};
				// 如果双击时所在的输入框是药品名称并且 adjPriceSpEdit === 1的时候直接返回不用二次弹窗
				if ((selectData && selectData.commonParams && selectData.commonParams.adjPriceSpEdit && +selectData.commonParams.adjPriceSpEdit === 1 && this.state.fieldDesc === 'stockDesc') || this.state.showdetailList) {
					// 关闭弹窗
					this.childStock.handleCancel();
					let newShowdetailList = this.state.showdetailList;
					let id = newShowdetailList ? (record && record.stkBatID ? record.stkBatID : "") : (record && record.stockID ? record.stockID : "")
					this.setState({ showdetailList: false, modalData: [] }, () => {
						// 获取单位下拉框数据
						let stkLocFlag = record && record.stkLocBatID ? true : false; // 除去调价单录入界面的
						this.getDrugData(record && record.stkLocBatID ? record.stkLocBatID : id, newShowdetailList, stkLocFlag);
					})
				} else {
					this.setState({ showdetailList: true, rowId: '', detailModalRecord: record, page: 1 }, () => {
						this.getDetailModalData();
					})
				}
				// 关闭弹窗
				// this.childStock.handleCancel();
				// 获取单位下拉框数据
				// this.getDrugData(record.stockID);
				// this.props.form.setFieldsValue({stockDesc: record.stockDesc});
			})
		}

		// 获取明细弹窗数据
		getDetailModalData = () => {
			this.setState({ loading: true });
			let record = this.state.detailModalRecord || {};
			let stkLocFlag = record && record.stkLocID ? true : false; // 除去调价单录入界面的
			let data = {
				params: [{
					stockID: stkLocFlag ? undefined : record.stockID, // 库存项Id
					stkLocID: stkLocFlag ? record.stkLocID : undefined,
				}],
				pagination: [{
					pageSize: this.state.pageSize,
					currentPage: this.state.page,
				}
				],
			};
			$http.post('urlS', this, {
				code: this.props.DetailCode ? this.props.DetailCode : "02020104",
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						for (var i = 0; i < res.result.rows.length; i++) {
							res.result.rows[i].key = i + 1;
						}
						this.setState({ modalData: res.result.rows, total: res.result.total, rowId: res.result.rows[0].key, loading: false });
					} else {
						this.setState({ modalData: [], total: 0, loading: false })
					}
				},
				error: function () {
					this.setState({ modalData: [], total: 0, loading: false })
				}
			})
		}

		// 双击获取药品详细信息
		getDrugData(id, newShowdetailList, stkLocFlag) {
			let data = {
				params: [
					{
						stkBatID: newShowdetailList ? id : undefined,
						stockID: newShowdetailList ? undefined : id,
						stkLocBatID: stkLocFlag ? id : undefined,
						toLocID: this.props.toLocDr ? this.props.toLocDr : undefined,
					}
				]
			};
			$http.post('urlS', this, {
				code: this.props.formCode ? this.props.formCode : "02020108",
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						this.setState({ detailInfo: res.result, showdetailList: false }, () => {
							this.props.form.setFieldsValue({ stockDesc: res.result.stockDesc });
							if (this.props.parameter == 'SalesReturnDetailList') {
								this.props.handelThis.vendorDataList(res.result.vendorID)
							}
							// this.props.form.setFieldsValue({'unit': res.result.unit[0].descripts})
							let fieldArr = JSON.parse(JSON.stringify(this.state.editField));
							let fieldArrNewData = fieldArr.filter((g) => { return g.readOnly == 'N' });
							let fieldArrNew = fieldArrNewData.filter((v) => { return v.enterJump == 'Y' });
							for (var i = 0; i < fieldArrNew.length; i++) {
								if (this.state.fieldDesc === fieldArrNew[i].code) {
									if (fieldArrNew[i + 1]) {
										var fieldObj = this.props.form.getFieldInstance(fieldArrNew[i + 1].code);
										setTimeout(() => { fieldObj.focus() }, 300)
									}
								}
							}
						});
					} else {
						this.setState({ detailInfo: [] })
					}
				}
			})
		}

		// 药品名称表格行的类名
		setRowClassNamePublic = (record) => {
			return record.key === this.state.rowId ? 'clickRowStyle' : '';
		}

		handleOnKeydown = (e) => {
			var e = window.event || e;
			let scroll_height = 50;
			let id = '';
			let oneArr = this.state.modalData && this.state.modalData.length > 0 ? this.state.modalData : [];
			switch (e.keyCode) {
				case 13:
					if (this.state.rowId) {
						let obj = {}
						for (var i = 0; i < oneArr.length; i++) {
							if (oneArr[i].key === this.state.rowId) {
								obj = i > 0 ? oneArr[i] : oneArr[0];
								break;
							}
						}
						this.setState({ modalData: [] }, () => {
							this.confirmSelection(obj);
						})
					} else {
						message.info("请选择一条需要操作的数据")
					}
					break;
				case 38: //上
					if (this.state.rowId) {
						for (var i = 0; i < oneArr.length; i++) {
							if (oneArr[i].key === this.state.rowId) {
								id = i > 0 ? oneArr[i - 1].key : oneArr[0].key;
								break;
							}
						}
						this.setState({ rowId: id })
						setTimeout(() => {
							var gundong = document.getElementById("gundong");
							gundong.scrollTop = gundong.scrollTop - scroll_height;
						}, 300)
					} else {
						this.setState({ rowId: oneArr[0].key })
					}
					break;
				case 40: //下
					if (this.state.rowId) {
						for (let i = 0; i < oneArr.length; i++) {
							if (oneArr[i].key === this.state.rowId) {
								id = i + 1 >= oneArr.length ? oneArr[oneArr.length - 1].key : oneArr[i + 1].key;
								break;
							}
						}
						this.setState({ rowId: id })
						setTimeout(() => {
							var gundong = document.getElementById("gundong");
							gundong.scrollTop = gundong.scrollTop + scroll_height;
						}, 300)
					} else {
						this.setState({ rowId: oneArr[0].key })
					}
					break;
			}
		}

		//字段跳转方法和回车显示药品名称弹窗
		FieldOnKeyDown = (e, field, type, dateString) => {
			if (e.keyCode === 13 || type == 'Select' || type == 'DatePicker') {
				if (field == 'stockDesc') {
					let val = e.target?.value || '';
					if (this.props.form.getFieldValue('stockDesc')) {
						let top = document.getElementById('stock-desc').getBoundingClientRect().top + 40 + 'px';
						let left = document.getElementById('stock-desc').getBoundingClientRect().left + 'px';
						this.setState({
							top: top,
							left: left,
							fieldDesc: field,
							modalData: [],
							page: 1
						}, () => {
							this.getModalListData(val);
						});
						this.resetKeyDown();
						setTimeout(() => {
							var gundong = document.getElementById("gundong");
							gundong.scrollTop = 0;
						}, 300)
						this.childStock.showModal();
						// this.FieldOnKeyDown(e,field, type)
					} else {
						message.info("药品名称不能为空");
					}
				} else {
					let fieldArr = JSON.parse(JSON.stringify(this.state.editField));
					let fieldArrNewData = fieldArr.filter((g) => { return g.readOnly == 'N' });
					let fieldArrNew = fieldArrNewData.filter((v) => { return v.enterJump == 'Y' });
					for (var i = 0; i < fieldArrNew.length; i++) {
						if (field === fieldArrNew[i].code) {
							if (fieldArrNew[i].enterSave == 'Y' && field !== 'DatePicker') {
								this.handelSave(dateString);
							} else if (fieldArrNew[i + 1]) {
								if (this.props.form.getFieldValue(fieldArrNew[i].code) || fieldArrNew[i].required == '' || fieldArrNew[i].required == 'N') {
									var fieldObj = this.props.form.getFieldInstance(fieldArrNew[i + 1].code);
									setTimeout(() => { fieldObj.focus(); }, 100)
								} else if (type == 'Select') {
									return ''
								} else {
									message.error(fieldArrNew[i].desc + '不能为空！')
								}
							}
						}
					}
				}
			}
		}

		// 当描述失去焦点的时候
		stastockDescOnBlure = (e) => {
			this.setState({ inputFocus: false }); //失去默认焦点
		}

		//药品名称为空时清除表单数据
		clearData = (val, field, data) => {
			if (field == 'stockDesc') {
				if (!val.target.value) {
					this.setState({ userInfo: {}, detailInfo: {} });
					this.resetFields();
				}
			}
			//输入的数量大于可用数量提示
			if (field == 'qty' && this.props.parameter !== 'AdjustmentDetailList') {
				let unitAvaQtry = this.props.form.getFieldValue('unit');
				let avaData = data.unit ? data.unit.filter((item) => { return item.id == unitAvaQtry }) : []
				if (avaData.length > 0 && avaData[0].avaQty < val.target.value) {
					message.error('输入的数量不能大于可用数量');
					setTimeout(() => {
						this.props.form.setFieldsValue({ qty: undefined })
					}, 300)
				}
			}

			setTimeout(() => {
				let qtyMoney = this.props.form.getFieldValue('qty') ? this.props.form.getFieldValue('qty') : 0;
				let RPMoney = this.props.form.getFieldValue('rp') ? this.props.form.getFieldValue('rp') : 0;
				let SPMoney = this.props.form.getFieldValue('sp') ? this.props.form.getFieldValue('sp') : 0;
				this.props.form.setFieldsValue({ rpAmt: (RPMoney * qtyMoney).toFixed(2), spAmt: (SPMoney * qtyMoney).toFixed(2) });
			}, 300)
		}

		disabledDate = (current) => {
			// Can not select days before today and today
			return current && current < moment().endOf('day');
		}

		GetCom = (fieldItem, key) => {
			let { languageSize } = this.state;
			const { getFieldDecorator } = this.props.form;
			let detailInfo = this.state.detailInfo ? this.state.detailInfo : {};
			let userInfo = this.state.userInfo ? this.state.userInfo : {};
			var code = detailInfo[fieldItem.code] ? detailInfo[fieldItem.code] : userInfo[fieldItem.code] ? userInfo[fieldItem.code] : '';
			var type = fieldItem.componentType;
			var ReadOnlyFlag = fieldItem.readOnly;
			var disabled = false;
			if (ReadOnlyFlag === "Y") disabled = true;
			if (type === "Input" || type == 'InputNumber') {
				return (
					{
						...getFieldDecorator(fieldItem.code, {
							initialValue: code ? code : '',
							rules: [{ required: fieldItem.required == "Y" ? true : false, message: languageSize.EnterSequenceinputTips ? (languageSize.EnterSequenceinputTips.descripts + ' ' + fieldItem.desc) : ('请输入' + fieldItem.desc) }],
						})(<Input
							type={type == 'InputNumber' ? "number" : null}
							allowClear={!disabled}
							disabled={disabled}
							ref={key == 0 ? (input) => this.input = input : ''}
							style={{ width: '100%' }}
							placeholder={disabled ? '' : languageSize.EnterSequenceinputTips ? (languageSize.EnterSequenceinputTips.descripts + ' ' + fieldItem.desc) : ('请输入' + fieldItem.desc)}
							onKeyDown={(e) => {
								if (!disabled) {
									this.FieldOnKeyDown(e, fieldItem.code)
								}
							}}
							onBlur={this.stastockDescOnBlure}
							onChange={(e) => this.clearData(e, fieldItem.code, detailInfo)}
						/>)
					}
				)
			} else if (type === "Radio") {
				return (

					{
						...getFieldDecorator(fieldItem.code, {
							initialValue: code === "Y" ? true : false,
							valuePropName: 'checked',
							rules: [{ required: fieldItem.required == "Y" ? true : false }],
							normalize: (value, prevValue) => {
								return value
							}
						})(<Switch disabled={disabled} checkedChildren="是" unCheckedChildren="否" />)
					}
				)
			} else if (type === "DatePicker") {
				var date = ""
				if (fieldItem.code === "StartDate") {
					date = code ? moment(code, 'YYYY-MM-DD') : moment(new Date(), 'YYYY-MM-DD')
				} else if (this.props.parameter == 'PriceAdjustment' && fieldItem.code == 'preExeDate') {
					let tomorrow = moment(new Date(), 'YYYY-MM-DD').add(1, 'days');
					date = code ? moment(code, 'YYYY-MM-DD') : tomorrow
				} else {
					date = code ? moment(code, 'YYYY-MM-DD') : null
				}
				return (
					{
						...getFieldDecorator(fieldItem.code, {
							initialValue: date,
							rules: [{ required: fieldItem.required == "Y" ? true : false, message: languageSize.EnterSequencepleaseChoose ? (languageSize.EnterSequencepleaseChoose.descripts + ' ' + fieldItem.desc) : ('请选择' + fieldItem.desc) }],
						})(<DatePicker
							style={{ width: '100%' }}
							disabledDate={this.props.parameter == 'PriceAdjustment' ? this.disabledDate : ''}
							disabled={disabled}
							format="YYYY-MM-DD"
							placeholder={disabled ? '' : languageSize.EnterSequencepleaseChoose ? (languageSize.EnterSequencepleaseChoose.descripts + ' ' + fieldItem.desc) : ('请选择' + fieldItem.desc)}
							onChange={(e, dateString) => {
								if (dateString && this.state.ModalRowData.stockDesc) {
									this.FieldOnKeyDown(e, fieldItem.code, type, dateString)
								}
							}}
						/>)
					}
				)
			} else if (type === "Select") {
				return this.GetSelectOther(fieldItem, detailInfo, type);
			} else if (type === "Text") {
				return (
					{
						...getFieldDecorator(fieldItem.code, {
							initialValue: code,
							rules: [{ required: fieldItem.required == "Y" ? true : false, message: languageSize.EnterSequenceinputTips ? (languageSize.EnterSequenceinputTips.descripts + ' ' + fieldItem.desc) : ('请输入' + fieldItem.desc) }],
						})(<TextArea rows={1} style={{ width: '100%' }} placeholder={languageSize.EnterSequenceinputTips ? (languageSize.EnterSequenceinputTips.descripts + ' ' + fieldItem.desc) : ('请输入' + fieldItem.desc)} />)
					}
				)
			} else {
				return (
					{
						...getFieldDecorator(fieldItem.code, {
							initialValue: code,
							rules: [{ required: fieldItem.required == "Y" ? true : false, message: languageSize.EnterSequenceinputTips ? (languageSize.EnterSequenceinputTips.descripts + ' ' + fieldItem.desc) : ('请输入' + fieldItem.desc) }],
						})(<Input disabled={disabled} placeholder={languageSize.EnterSequenceinputTips ? (languageSize.EnterSequenceinputTips.descripts + ' ' + fieldItem.desc) : ('请输入' + fieldItem.desc)} />)
					}
				)
			}
		}

		GetSelectOther = (fieldItem, detailInfo, type) => {
			let { languageSize, language } = this.state;
			const selectData = this.props.SelectData;
			const { getFieldDecorator } = this.props.form;
			let userInfo = this.state.userInfo ? this.state.userInfo : {};
			var code = detailInfo[fieldItem.code + 'ID'] ? detailInfo[fieldItem.code + 'ID'].toString() : userInfo[fieldItem.code + 'ID'] ? userInfo[fieldItem.code + 'ID'].toString() : fieldItem.code == 'hosp' ? language.hospID.toString() : '';
			var ReadOnlyFlag = fieldItem.readOnly;
			var disabled = false;
			if (ReadOnlyFlag === "Y") disabled = true;
			return (
				{
					...getFieldDecorator(fieldItem.code, {
						initialValue: code ? code : '',
						rules: [{ required: fieldItem.required == "Y" ? true : false, message: languageSize.EnterSequencepleaseChoose ? (languageSize.EnterSequencepleaseChoose.descripts + ' ' + fieldItem.desc) : ('请选择' + fieldItem.desc) }],
						normalize: (value, prevValue) => {
							if (+value === 0) {
								value = ""
							}
							return value
						}
					})(
						<Select
							allowClear
							showSearch
							optionFilterProp="seatchprop"
							disabled={disabled}
							filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
							style={{ width: '100%' }}
							placeholder={disabled ? '' : languageSize.EnterSequencepleaseChoose ? (languageSize.EnterSequencepleaseChoose.descripts + ' ' + fieldItem.desc) : ('请选择' + fieldItem.desc)}
							onChange={this.changeUnit}
						>
							{fieldItem.code == 'unit' ? detailInfo.unit ?
								detailInfo.unit && detailInfo.unit.length > 0 && detailInfo.unit.map((g) => {
									return <Option onClick={(e) => this.FieldOnKeyDown(e, fieldItem.code, type)} key={g.id} value={g.id}>{g.descripts}</Option>
								})
								:
								this.props.unitData && this.props.unitData.length > 0 ? this.props.unitData.map((item) => {
									return <Option onClick={(e) => this.FieldOnKeyDown(e, fieldItem.code, type)} key={item.id} value={item.id}>{item.descripts}</Option>
								})
									:
									userInfo.unit && userInfo.unit > 0 && userInfo.unit.map((v) => {
										return <Option onClick={(e) => this.FieldOnKeyDown(e, fieldItem.code, type)} key={v.id} value={v.id}>{v.descripts}</Option>
									})
								:
								selectData[fieldItem.code] && selectData[fieldItem.code].length && selectData[fieldItem.code].map(d => { return <Option onClick={(e) => this.FieldOnKeyDown(e, fieldItem.code, type)} key={d.id} value={d.id} seatchprop={d.descriptsSPCode + d.descripts}>{d.descripts}</Option> })
							}
						</Select>
					)
				}
			);
		}

		//选择单位后改变进价和售价
		changeUnit = (e, event) => {
			if (this.state.detailInfo && this.state.detailInfo.unit) {
				let g = this.state.detailInfo.unit.filter((v) => v.id == e);
				if (g.length > 0) {
					this.props.form.setFieldsValue({ rp: g[0].rp });
					this.props.form.setFieldsValue({ sp: g[0].sp });
				}
			} else if (this.state.userInfo && this.state.userInfo.unit) {
				let arr = this.state.userInfo.unit.filter((v) => v.id == e);
				if (arr.length > 0) {
					this.props.form.setFieldsValue({ rp: arr[0].rp });
					this.props.form.setFieldsValue({ sp: arr[0].sp });
				}
			}
			this.clearData();
		}

		shouldComponentUpdate(nextProps, nextState) {
			if (nextState === this.state) {
				//return false
			}
			return true
		}

		// 重置表单
		resetFields = (isFocus) => {
			this.setState({
				rowId: "",
				detailInfo: {},
				userInfo: {},
				ModalRowData: {},
				inputFocus: isFocus ? false : true,
				showdetailList: false
			});
			this.props.form.resetFields(); //清空所有
			// this.props.handelThis.resetFields();
			if (this.props.parameter == 'SalesReturnDetailList') {
				this.props.handelThis.vendorDataList('')
			}
		}

		resetKeyDown = () => {
			this.setState({
				modalData: []
			});
		}

		//保存
		handelSave = (time) => {
			let qtyMoney = this.props.form.getFieldValue('qty') ? this.props.form.getFieldValue('qty') : 0;
			let RPMoney = this.props.form.getFieldValue('rp') ? this.props.form.getFieldValue('rp') : 0;
			let SPMoney = this.props.form.getFieldValue('sp') ? this.props.form.getFieldValue('sp') : 0;
			this.props.form.setFieldsValue({ rpAmt: (RPMoney * qtyMoney).toFixed(2), spAmt: (SPMoney * qtyMoney).toFixed(2) });
			const { form: { validateFields } } = this.props;
			validateFields((errors, values) => {
				if (errors === null) {
					let obj = JSON.parse(JSON.stringify(values));
					// 将开关的值转化
					obj.preDate = obj.preDate ? moment(obj.preDate).format('YYYY-MM-DD') : time ? moment(time).format('YYYY-MM-DD') : '' // 到货日期
					if (obj.preExeDate) {
						obj.preExeDate = obj.preExeDate ? moment(obj.preExeDate).format('YYYY-MM-DD') : ''
					}
					if (this.state.userInfo && this.state.userInfo.stock) {
						obj.stock = this.state.userInfo.stock;
					}
					if (this.state.detailInfo && this.state.detailInfo.stock) {
						obj.stock = this.state.detailInfo.stock;
					}
					if (this.props.SelectData && this.props.SelectData.manf) {
						for (var i = 0; i < this.props.SelectData.manf.length; i++) {
							if (this.props.SelectData.manf[i].id === obj.manf) {
								obj.manfDesc = this.props.SelectData.manf[i].descripts;
								obj.manfID = this.props.SelectData.manf[i].id;
								obj.manf = this.props.SelectData.manf;
							} else if (this.props.SelectData.manf[i].descripts == obj.manf) {
								obj.manfDesc = this.props.SelectData.manf[i].descripts;
								obj.manfID = this.props.SelectData.manf[i].id;
								obj.manf = this.props.SelectData.manf;
							}
						}
					}
					if (this.props.SelectData && this.props.SelectData.vendor) {
						for (var i = 0; i < this.props.SelectData.vendor.length; i++) {
							if (this.props.SelectData.vendor[i].id === obj.vendor) {
								obj.vendorID = this.props.SelectData.vendor[i].id;
								obj.vendorDesc = this.props.SelectData.vendor[i].descripts;
								obj.vendor = this.props.SelectData.vendor;
							} else if (this.props.SelectData.vendor[i].descripts == obj.vendor) {
								obj.vendorID = this.props.SelectData.vendor[i].id;
								obj.vendorDesc = this.props.SelectData.vendor[i].descripts;
								obj.vendor = this.props.SelectData.vendor;
							}
						}
					}
					if (this.state.detailInfo && this.state.detailInfo.unit) {
						for (var i = 0; i < this.state.detailInfo.unit.length; i++) {
							if (this.state.detailInfo.unit[i].id === obj.unit) {
								obj.unitDesc = this.state.detailInfo.unit[i].descripts;
								obj.unitID = this.state.detailInfo.unit[i].id;
								obj.unit = this.state.detailInfo.unit;
							} else if (this.state.detailInfo.unit[i].descripts === obj.unit) {
								obj.unitDesc = this.state.detailInfo.unit[i].descripts;
								obj.unitID = this.state.detailInfo.unit[i].id;
								obj.unit = this.state.detailInfo.unit;
							}
						}
					} else if (this.state.unitData && this.state.unitData.length > 0) {
						for (var i = 0; i < this.state.unitData.length; i++) {
							if (this.state.unitData[i].descripts === obj.unit) {
								obj.unitDesc = this.state.unitData[i].descripts;
								obj.unitID = this.state.unitData[i].id;
								obj.unit = this.state.unitData;
							} else if (this.state.unitData[i].id === obj.unit) {
								obj.unitDesc = this.state.unitData[i].descripts;
								obj.unitID = this.state.unitData[i].id;
								obj.unit = this.state.unitData;
							}
						}
					}

					if (this.props.SelectData && this.props.SelectData.reason) {
						for (var k = 0; k < this.props.SelectData.reason.length; k++) {
							if (this.props.SelectData.reason[k].id == obj.reasonID) {
								obj.reasonDesc = this.props.SelectData.reason[k].descripts;
								obj.reasonID = this.props.SelectData.reason[k].id;
								obj.reason = this.props.SelectData.reason;
							} else if (this.props.SelectData.reason[k].id == obj.reason) {
								obj.reasonDesc = this.props.SelectData.reason[k].descripts;
								obj.reasonID = this.props.SelectData.reason[k].id;
								obj.reason = this.props.SelectData.reason;
							} else if (this.props.SelectData.reason[k].descripts == obj.reasonDesc) {
								obj.reasonDesc = this.props.SelectData.reason[k].descripts;
								obj.reasonID = this.props.SelectData.reason[k].id;
								obj.reason = this.props.SelectData.reason;
							}
						}
					}
					let p = this.state.ModalRowData ? this.state.ModalRowData : undefined;
					let H = this.state.ModalDataList ? this.state.ModalDataList : undefined;
					if (this.state.rowKey) {
						obj.stockID = this.state.userInfo ? (/^[0-9]*$/g.test(this.state.userInfo.StockItemDr) ? this.state.userInfo.StockItemDr : this.state.userInfo.stockID) : p.stockID;
						obj.inOpDetailID = this.state.userInfo.inOpDetailID;
						obj.key = this.state.rowKey;
						obj.stockDesc = this.state.userInfo.stockDesc;
						obj.PPDetailID = this.state.userInfo.PPDetailID;
						obj.stkLocBatID = this.state.userInfo.stkLocBatID;
						obj.stkScrapDetailID = this.state.userInfo.stkScrapDetailID;
						obj.stkAdjDetailID = this.state.userInfo.stkAdjDetailID;
						obj.transDetailID = this.state.userInfo.transDetailID;
						obj.stkReturnDetailID = this.state.userInfo.stkReturnDetailID;
						/**
						* @ByShizi: 晶体度数/柱镜度数显示处理
						* 需求 #1001
						* Create 2021-10-18
						* */
						obj.degreesDesc = this.state.userInfo?.degreesDesc || ''; // 晶体度数
						obj.clyDegreesDesc = this.state.userInfo?.clyDegreesDesc || ''; // 柱镜度数
					} else {
						obj.stkLocBatID = p.stkLocBatID;
						obj.spAmt = obj.spAmt ? obj.spAmt + '' : (SPMoney * qtyMoney).toFixed(2);
						obj.rpAmt = obj.rpAmt ? obj.rpAmt + '' : (RPMoney * qtyMoney).toFixed(2);
						obj.stockDesc = H.stockDesc;
						obj.stockID = this.state.detailInfo.stockID ? this.state.detailInfo.stockID : (obj && obj.stockID ? obj.stockID : "");
						obj.stkBatID = this.state.detailInfo.stkBatID ? this.state.detailInfo.stkBatID : (obj && obj.stkBatID ? obj.stkBatID : "");
						obj.reasonID = this.state.detailInfo.reasonID ? this.state.detailInfo.reasonID : (obj && obj.reasonID ? obj.reasonID : "");
						obj.degreesDesc = this.state.detailInfo?.degreesDesc || ''; // 晶体度数
						obj.clyDegreesDesc = this.state.detailInfo?.clyDegreesDesc || ''; // 柱镜度数
					}

					console.log(obj)
					if (this.props.parameter == 'PriceAdjustment' && this.state.rowKey) {
						let info = this.state.userInfo;
						info.reasonDesc = obj.reasonDesc;
						info.reasonID = obj.reasonID;
						info.reason = obj.reason;
						info.unitID = obj.unitID;
						info.unitDesc = obj.unitDesc;
						info.preExeDate = obj.preExeDate;
						// info.reasonID = obj.reason;
						info.warrentNO = obj.warrentNO;
						info.preDate = obj.preDate;
						info.remark = obj.remark;
						info.resultSp = obj.resultSp;
						this.props.handelThis.addHandle(info);
					} else {
						this.props.handelThis.addHandle(obj);
					}
					// this.setState({inputFocus: true})
				}
			})
		}

		onRef = (ref) => {
			this.child = ref;
		}

		// 分页重查
		compilePage = (page, pageSize) => {
			// 修改page，或者pageSize
			this.setState({ page, pageSize }, () => {
				// 重新调用查询方法
				this.getDetailModalData();
			})
		}

		render() {
			let { languageSize, showdetailList } = this.state;
			let ColWidth = Math.ceil(24 / this.state.ColNum);
			const formItemInput = {
				labelCol: {
					span: 8
				},
				wrapperCol: {
					span: 16
				},
			};
			let param = {
				width: '900px',
				page: this.state.page,
				top: this.state.top,
				left: this.state.left,
				total: this.state.total,
				loading: this.state.loading,
				columns: this.state.showdetailList ?
					(this.state.detailColumns.length > 0 ?
						this.state.detailColumns : this.detailColumns)
					: (this.state.modalColumns.length > 0 ?
						this.state.modalColumns : this.modalColumns),
				data: this.state.modalData,
				isOnRow: true,
				TableClassName: 'tab-placeholder',
				rowClassName: true,
				showPagination: showdetailList || false
			};

			return (
				<div>
					<Form onSubmit={this.handleSubmit} layout="vertical" className="FormTitle inventoryFormModal">
						<Row gutter={24} className="RowTitle" >
							{
								this.state.editField.length > 0 ? this.state.editField.map((value, key) => {
									return (
										<Col span={ColWidth} key={key} style={{ paddingRight: '0', paddingLeft: '0' }} className={value.display == "N" ? 'ColTitle display' : 'ColTitle'} id={value.code == 'stockDesc' ? 'stock-desc' : ''}>
											<Form.Item key={key} label={value.desc + '： '} style={{ minHeight: '40px' }}  {...formItemInput} >
												{this.GetCom(value, key)}
											</Form.Item>
										</Col>
									)
								}) : ""
							}
						</Row>
						<Row style={{ textAlign: 'right' }}>
							<Col span={6} >
								<Button onClick={this.handelSave} type="primary">{languageSize.EnterSequencesave ? languageSize.EnterSequencesave.descripts : '保存'}</Button>
							</Col>
							<Col span={6} >
								<Button type="primary" onClick={this.resetFields}>{languageSize.EnterSequencereset ? languageSize.EnterSequencereset.descripts : '重置'}</Button>
							</Col>
							<Col span={6} >
								<Button type="primary" onClick={() => { this.child.showModal(); }} >{languageSize.EnterSequenceentry ? languageSize.EnterSequenceentry.descripts : '录入顺序'}</Button>
							</Col>
						</Row>
					</Form>
					<SetFormOrderTable columnsData={this.state.columnsData} languageSize={this.state.languageSize} editField={this.state.editField} parameter={this.state.parameter} onRef={this.onRef} handleThis={this} compontName={this.state.parameter} configType="C" />
					<div
						tabIndex={1}
						onKeyDown={this.handleOnKeydown}
						style={{ outline: 'none' }}
					>
						<BatchInformation
							compilePage={this.compilePage}
							onClickRowPublic={this.onClickRowPublic}
							setRowClassNamePublic={this.setRowClassNamePublic}
							param={param}
							onRef={this.onRefStockDesc}
							id="gundong"
							handelThis={this}
						/>
					</div>
					<ColumnAuthoritysModel
						clientWidth='800px'
						compontName={this.state.showdetailList ? 'stockSescPopupDetailList' : 'stockDescPopupList'}
						configType="C"
						onRef={this.onColumnRef}
						reloadData={() => {
							if (this.state.showdetailList) {
								this.getDetailListTableColumns()
							} else {
								this.getDetailTableColumns()
							}
						}
						}
					/>
				</div>
			);
		}
	});

export default InventoryFormModal;
