// 库存项---基本信息
import React from 'react';
import { Form, Select, Input, Row, Col, Card, Modal, Button, message, Switch } from 'antd';
import { $http } from 'containers/config/https';

const FormItem = Form.Item;
let { Option } = Select;
let timeout;

class AIIEssentialInformation extends React.Component {
	constructor() {
		super();
		this.state = {
			medItemData: [],
			visible: false,
			prefixText: '', //库存项最大代码
		}
	}

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

	handleValidateFields = () => {
		const { form: { validateFields } } = this.props;
		validateFields((errors, values) => {
			// 验证成功
			if (errors === null) {
				let obj = JSON.parse(JSON.stringify(values));
				obj.errors = true;
				obj.uniformPurchFlag = obj.uniformPurchFlag ? 'Y' : 'N';
				obj.medID = obj.medID === this.props.userInfo.medDesc ? this.props.userInfo.medID : obj.medID;
				this.props.receiveSubcomponentsValue(obj, 'essentialInformation')
			} else {
				let obj = JSON.parse(JSON.stringify(values));
				obj.uniformPurchFlag = obj.uniformPurchFlag ? 'Y' : 'N';
				obj.errors = false;
				this.props.receiveSubcomponentsValue(obj, 'essentialInformation')
			}
		})
	}

	// 重置数据
	resetFields = () => {
		this.props.form.resetFields();
		this.setState({ prefixText: '' })
	}

	//医嘱项
	handleMedSearch = (value) => {
		this.fetchMed(value, medItemData => this.setState({ medItemData }));
	}

	fetchMed = (value, callback) => {
		value = value.replace(/^\s+/, "").replace(/\s+$/, "");
		if (value === "") {
			let data = [];
			callback(data);
			return;
		}
		if (timeout) {
			clearTimeout(timeout);
			timeout = null;
		}
		function fakeMed() {
			let data = {}
			data = {
				params: [{
					alias: value,
				}],
			}
			$http.post('urlS', this, {
				code: "02020070",
				data: data,
				success: function (res) {
					let data = [];
					if (res.errorCode === '0') {
						var j;
						for (j = 0; j < res.result.length; j++) {
							data.push({
								value: res.result[j].id,
								text: res.result[j].descripts,
							});
						}
						callback(data);
					}
				}
			})
		}
		timeout = setTimeout(fakeMed, 300);
	}

	//获取最大代码
	saveMaxInventory = () => {
		let stkSubCatIDPopup = this.props.form.getFieldValue('stkSubCatIDPopup');
		let prefix = this.props.form.getFieldValue('AllEprefix');
		if (!prefix) {
			message.error('请输入前缀');
			return
		}
		let data = {
			params: [{
				preCode: prefix ? prefix : '',//前缀
				stkSubCatID: stkSubCatIDPopup ? stkSubCatIDPopup : '', //库存子类
			}]
		};
		$http.post('urlS', this, {
			code: "02020319",
			data: data,
			success: function (res) {
				if (res.errorCode == 0) {
					this.props.form.setFieldsValue({ stockCode: res.result })
					message.success(res.errorMessage)
					this.handleCancel();
				} else {
					this.setState({ prefixText: '' });
					message.error(res.errorMessage);
				}
			}
		});
	};

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

	render() {
		const { getFieldDecorator } = this.props.form;
		const formItemItem = {
			labelCol: { span: 11 },
			wrapperCol: { span: 13 }
		};
		const formItemItemPopup = {
			labelCol: { span: 9 },
			wrapperCol: { span: 13 }
		};
		const formItemMedID = {
			labelCol: { span: 6 },
			wrapperCol: { span: 18 }
		}
		let userInfo = this.props.userInfo;
		let selectData = this.props.selectData;
		let ChineseEnglishSize = this.props.ChineseEnglishSize;

		// 入库单位
		let purUnitIDOption = selectData && selectData.unit && selectData.unit.map((item, index) => {
			return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
		})
		// 基本单位
		let baseUnitIDOption = selectData && selectData.unit && selectData.unit.map((item, index) => {
			return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
		})
		// 库存分类
		let stkSubCatIDOption = selectData && selectData.stkSubCat && selectData.stkSubCat.map((item, index) => {
			return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
		})
		// 质量层次
		let quaLevIDOption = selectData && selectData.qualityLevel && selectData.qualityLevel.map((item, index) => {
			return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
		})
		// 定价类型
		let priceTypeIDOption = selectData && selectData.priceType && selectData.priceType.map((item, index) => {
			return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
		})
		// 进口类型
		let criticalOption = selectData && selectData.importType && selectData.importType.map((item, index) => {
			return (<Option key={index} value={item.id} title={item.descripts} seatchprop={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>)
		})
		// 医嘱项
		let optionsMed = this.state.medItemData.map(d => <Option title={d.text} key={d.value}>{d.text}</Option>);
		return (
			<Card
				type="inner"
				title={ChineseEnglishSize.DrugMaintenancessentialInfo ? ChineseEnglishSize.DrugMaintenancessentialInfo.descripts : '基本信息'}
				size="small"
				className='allRssential'
			>
				<Form style={{ padding: '0 12px' }}>
					<Row>
						<Col span={8} className='maxInventory'>
							<FormItem label={ChineseEnglishSize.DrugMaintenancestockCode ? ChineseEnglishSize.DrugMaintenancestockCode.descripts : '库存项代码'} {...formItemItem}>
								{getFieldDecorator('stockCode', {
									initialValue: userInfo.stockCode ? userInfo.stockCode : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancestockCode ? ChineseEnglishSize.DrugMaintenancestockCode.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancestockCode ? ChineseEnglishSize.DrugMaintenancestockCode.message : '请输入库存项代码' }]
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
							<a className='max' onClick={() => { this.setState({ visible: true }) }}>{ChineseEnglishSize.MaterialInventoryItemMaintenanmaxCode ? ChineseEnglishSize.MaterialInventoryItemMaintenanmaxCode.descripts : '最大码'}</a>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancestockDesc ? ChineseEnglishSize.DrugMaintenancestockDesc.descripts : '库存项描述'} {...formItemItem}>
								{getFieldDecorator('stockDesc', {
									initialValue: userInfo.stockDesc ? userInfo.stockDesc : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancestockDesc ? ChineseEnglishSize.DrugMaintenancestockDesc.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancestockDesc ? ChineseEnglishSize.DrugMaintenancestockDesc.message : '请输入库存项描述' }]
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancestockSpec ? ChineseEnglishSize.DrugMaintenancestockSpec.descripts : '包装规格'} {...formItemItem}>
								{getFieldDecorator('stockSpec', {
									initialValue: userInfo.stockSpec ? userInfo.stockSpec : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancestockSpec ? ChineseEnglishSize.DrugMaintenancestockSpec.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancestockSpec ? ChineseEnglishSize.DrugMaintenancestockSpec.message : '请输入包装规格' }]
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancepurUnitID ? ChineseEnglishSize.DrugMaintenancepurUnitID.descripts : '入库单位'} {...formItemItem}>
								{getFieldDecorator('purUnitID', {
									initialValue: userInfo.purUnitID ? userInfo.purUnitID : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancepurUnitID ? ChineseEnglishSize.DrugMaintenancepurUnitID.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancepurUnitID ? ChineseEnglishSize.DrugMaintenancepurUnitID.message : '请选择入库单位' }]
								})(
									<Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
										{purUnitIDOption}
									</Select>
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancebaseFac ? ChineseEnglishSize.DrugMaintenancebaseFac.descripts : '转换系数'} {...formItemItem}>
								{getFieldDecorator('baseFac', {
									initialValue: userInfo.baseFac ? userInfo.baseFac : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancebaseFac ? ChineseEnglishSize.DrugMaintenancebaseFac.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancebaseFac ? ChineseEnglishSize.DrugMaintenancebaseFac.message : '请输入转换系数' }]
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancebaseUnitID ? ChineseEnglishSize.DrugMaintenancebaseUnitID.descripts : '基本单位'} {...formItemItem}>
								{getFieldDecorator('baseUnitID', {
									initialValue: userInfo.baseUnitID ? userInfo.baseUnitID : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancebaseUnitID ? ChineseEnglishSize.DrugMaintenancebaseUnitID.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancebaseUnitID ? ChineseEnglishSize.DrugMaintenancebaseUnitID.message : '请选择基本单位' }]
								})(
									<Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
										{baseUnitIDOption}
									</Select>
								)}
							</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenanceimportTypeID ? ChineseEnglishSize.DrugMaintenanceimportTypeID.descripts : '进口类型'} {...formItemItem}>
								{getFieldDecorator('importTypeID', {
									initialValue: userInfo.importTypeID ? userInfo.importTypeID : undefined,
								})(
									<Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
										{criticalOption}
									</Select>
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancebillFac ? ChineseEnglishSize.DrugMaintenancebillFac.descripts : '转换系数(计价)'} {...formItemItem}>
								{getFieldDecorator('billFac', {
									initialValue: userInfo.billFac ? userInfo.billFac : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancebillFac ? ChineseEnglishSize.DrugMaintenancebillFac.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancebillFac ? ChineseEnglishSize.DrugMaintenancebillFac.message : '请输入转换系数(计价)' }]
								})(
									<Input style={{ width: '100%' }} type="number" className="input-number" />
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenanceBillUnitID ? ChineseEnglishSize.DrugMaintenanceBillUnitID.descripts : '计价单位'} {...formItemItem}>
								{getFieldDecorator('billUnitID', {
									initialValue: userInfo.billUnitID ? userInfo.billUnitID : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenanceBillUnitID ? (ChineseEnglishSize.DrugMaintenanceBillUnitID.required == 'Y' ? true : false) : false, message: ChineseEnglishSize.DrugMaintenanceBillUnitID ? ChineseEnglishSize.DrugMaintenanceBillUnitID.message : '请选择' }]
								})(
									<Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear disabled>
										{baseUnitIDOption}
									</Select>
								)}
							</FormItem>
						</Col>
					</Row>
					{/* 计价单位（下拉不可编辑），进价（不可编辑），售价（标志控制） */}
					<Row>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancebarCode ? ChineseEnglishSize.DrugMaintenancebarCode.descripts : '条码'} {...formItemItem}>
								{getFieldDecorator('barCode', {
									initialValue: userInfo.barCode ? userInfo.barCode : undefined,
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenanceRP ? ChineseEnglishSize.DrugMaintenanceRP.descripts : '进价'} {...formItemItem}>
								{getFieldDecorator('rp', {
									initialValue: (userInfo.rp || +userInfo.rp === 0) ? userInfo.rp : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenanceRP ? (ChineseEnglishSize.DrugMaintenanceRP.required == 'Y' ? true : false) : false, message: ChineseEnglishSize.DrugMaintenanceRP ? ChineseEnglishSize.DrugMaintenanceRP.message : '请输入' }]
								})(
									<Input style={{ width: '100%' }} type="number" className="input-number" disabled />
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenanceSP ? ChineseEnglishSize.DrugMaintenanceSP.descripts : '售价'} {...formItemItem}>
								{getFieldDecorator('sp', {
									initialValue: (userInfo.sp || +userInfo.sp === 0) ? userInfo.sp : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenanceSP ? (ChineseEnglishSize.DrugMaintenanceSP.required == 'Y' ? true : false) : false, message: ChineseEnglishSize.DrugMaintenanceSP ? ChineseEnglishSize.DrugMaintenanceSP.message : '请输入' }]
								})(
									<Input style={{ width: '100%' }} type="number" className="input-number" disabled={(userInfo && userInfo.priceFlag && (+userInfo.priceFlag === 1)) ? false : (+userInfo.priceFlag === 0 ? true : (selectData && selectData.priceFlag && (+selectData.priceFlag === 1) ? false : true))} />
								)}
							</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancestkSubCatID ? ChineseEnglishSize.DrugMaintenancestkSubCatID.descripts : '库存分类'} {...formItemItem}>
								{getFieldDecorator('stkSubCatID', {
									initialValue: userInfo.stkSubCatID ? userInfo.stkSubCatID : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancestkSubCatID ? ChineseEnglishSize.DrugMaintenancestkSubCatID.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancestkSubCatID ? ChineseEnglishSize.DrugMaintenancestkSubCatID.message : '请选择库存分类' }]
								})(
									<Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
										{stkSubCatIDOption}
									</Select>
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancequaLevID ? ChineseEnglishSize.DrugMaintenancequaLevID.descripts : '质量层次'} {...formItemItem}>
								{getFieldDecorator('quaLevID', {
									initialValue: userInfo.quaLevID ? userInfo.quaLevID : undefined,
								})(
									<Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
										{quaLevIDOption}
									</Select>
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancequalityNo ? ChineseEnglishSize.DrugMaintenancequalityNo.descripts : '质标编号'} {...formItemItem}>
								{getFieldDecorator('qualityNo', {
									initialValue: userInfo.qualityNo ? userInfo.qualityNo : undefined,
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancepriceTypeID ? ChineseEnglishSize.DrugMaintenancepriceTypeID.descripts : '定价类型'} {...formItemItem}>
								{getFieldDecorator('priceTypeID', {
									initialValue: userInfo.priceTypeID ? userInfo.priceTypeID : undefined,
								})(
									<Select style={{ width: '100%' }} showSearch optionFilterProp="seatchprop" allowClear>
										{priceTypeIDOption}
									</Select>
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancemaxSp ? ChineseEnglishSize.DrugMaintenancemaxSp.descripts : '最高售价'} {...formItemItem}>
								{getFieldDecorator('maxSp', {
									initialValue: userInfo.maxSp ? userInfo.maxSp : undefined,
								})(
									<Input style={{ width: '100%' }} type="number" className="input-number" />
								)}
							</FormItem>
						</Col>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancepriceFileNo ? ChineseEnglishSize.DrugMaintenancepriceFileNo.descripts : '物价文件号'} {...formItemItem}>
								{getFieldDecorator('priceFileNo', {
									initialValue: userInfo.priceFileNo ? userInfo.priceFileNo : undefined,
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancedrugUseInfo ? ChineseEnglishSize.DrugMaintenancedrugUseInfo.descripts : '用药说明'} {...formItemItem}>
								{getFieldDecorator('drugUseInfo', {
									initialValue: userInfo.drugUseInfo ? userInfo.drugUseInfo : undefined,
								})(
									<Input style={{ width: '100%' }} />
								)}
							</FormItem>
						</Col>
						<Col span={16}>
							<FormItem label={ChineseEnglishSize.DrugMaintenancemedID ? ChineseEnglishSize.DrugMaintenancemedID.descripts : '医嘱项'} {...formItemMedID}>
								{getFieldDecorator('medID', {
									initialValue: userInfo.medDesc ? userInfo.medDesc : undefined,
									rules: [{ required: ChineseEnglishSize.DrugMaintenancemedID ? ChineseEnglishSize.DrugMaintenancemedID.required == 'Y' ? true : false : false, message: ChineseEnglishSize.DrugMaintenancemedID ? ChineseEnglishSize.DrugMaintenancemedID.message : '请选择医嘱项' }]
								})(
									<Select
										allowClear={true}
										showSearch
										style={{ width: '100%' }}
										defaultActiveFirstOption={false}
										showArrow={false}
										filterOption={false}
										onSearch={this.handleMedSearch}
										notFoundContent={null}
									>
										{optionsMed}
									</Select>
								)}
							</FormItem>
						</Col>
					</Row>
					<Row>
						<Col span={8}>
							<FormItem label={ChineseEnglishSize.DrugMaintenanceuniformPurchFlag ? ChineseEnglishSize.DrugMaintenanceuniformPurchFlag.descripts : '集采标志'} {...formItemItem}>
								{getFieldDecorator('uniformPurchFlag', {
									valuePropName: 'checked',
									initialValue: userInfo?.uniformPurchFlag === 'Y' ? true : false,
								})(
									<Switch checkedChildren="是" unCheckedChildren="否" />
								)}
							</FormItem>
						</Col>
					</Row>
				</Form>
				<Modal
					title={ChineseEnglishSize.MaterialInventoryItemMaintenangetMaxCode ? ChineseEnglishSize.MaterialInventoryItemMaintenangetMaxCode.descripts : '获取最大代码'}
					visible={this.state.visible}
					onCancel={this.handleCancel}
					width={400}
					footer={null}
				>
					<div>
						<Row style={{ marginBottom: '24px' }}>
							<Row>
								<Col span={21}>
									<FormItem {...formItemItemPopup} label={ChineseEnglishSize.MaterialInventoryItemMaintenanprefix ? ChineseEnglishSize.MaterialInventoryItemMaintenanprefix.descripts : '前缀'}>
										{getFieldDecorator('AllEprefix', {
											initialValue: 'XYS',
										})(
											<Input />
										)}
									</FormItem>
								</Col>
							</Row>
							<Row>
								<Col span={21}>
									<FormItem {...formItemItemPopup} label={ChineseEnglishSize.MaterialInventoryItemMaintenanstkSubCatID ? ChineseEnglishSize.MaterialInventoryItemMaintenanstkSubCatID.descripts : '库存子类'}>
										{getFieldDecorator('stkSubCatIDPopup', {
											initialValue: undefined,
											rules: [{ required: false, message: '库存子类不能为空' }]
										})(
											<Select allowClear style={{ width: '100%' }}>
												{selectData.stkSubCat && selectData.stkSubCat.map(item => {
													return <Option value={item.id} key={item.id} title={item.descripts} seatchprop={item.descripts}>{item.descripts}</Option>
												})}
											</Select>
										)}
									</FormItem>
								</Col>
							</Row>
						</Row>
						<Row style={{ textAlign: 'center' }}>
							<Button onClick={this.saveMaxInventory} style={{ background: 'rgba(255,255,255,1)' }}>{ChineseEnglishSize.MaterialInventoryItemMaintenandetermine ? ChineseEnglishSize.MaterialInventoryItemMaintenandetermine.descripts : '确认'}</Button>
						</Row>
					</div>
				</Modal>
			</Card>
		)
	}
};
let AIIEssentialInformationForm = Form.create()(AIIEssentialInformation);
export default AIIEssentialInformationForm;