<template>
	<el-dialog title="添加库存" :close-on-click-modal="false" :visible.sync="visible" class="NCC-dialog NCC-dialog_center"
		lock-scroll width="800px">
		<el-form ref="elForm" :model="dataForm" size="small" label-width="120px" label-position="right" :rules="rules">
			<el-row :gutter="15">
				<el-col :span="12">
					<el-form-item label="产品" prop="productId">
						<el-select v-model="dataForm.productId" placeholder="请选择产品" clearable filterable
							:style='{"width":"100%"}' :disabled="!!defaultProductId" @change="onProductChange">
							<el-option v-for="item in productOptions" :key="item.id" :label="item.productName"
								:value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="数量" prop="quantity">
						<el-input-number v-model="dataForm.quantity" placeholder="请输入数量" :min="1" :precision="0"
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="入库时间" prop="stockInTime">
						<el-date-picker v-model="dataForm.stockInTime" type="date" placeholder="请选择入库时间"
							value-format="yyyy-MM-dd" format="yyyy-MM-dd" :style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="生产日期" prop="productionDate">
						<el-date-picker v-model="dataForm.productionDate" type="date" placeholder="请选择生产日期"
							value-format="yyyy-MM-dd" format="yyyy-MM-dd" :style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="保质期(天)" prop="shelfLife">
						<el-input-number v-model="dataForm.shelfLife" placeholder="请输入保质期" :min="1" :precision="0"
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="批次号" prop="batchNumber">
						<el-input v-model="dataForm.batchNumber" placeholder="请输入批次号" clearable
							:style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>

<script>
import request from '@/utils/request'

export default {
	data() {
		return {
			visible: false,
			btnLoading: false,
			defaultProductId: '',
			dataForm: {
				productId: '',
				quantity: 1,
				stockInTime: '',
				productionDate: '',
				shelfLife: 365,
				batchNumber: ''
			},
			productOptions: [],
			rules: {
				productId: [
					{ required: true, message: '请选择产品', trigger: 'change' }
				],
				quantity: [
					{ required: true, message: '请输入数量', trigger: 'blur' }
				],
				stockInTime: [
					{ required: true, message: '请选择入库时间', trigger: 'change' }
				]
			}
		}
	},
	methods: {
		init(productId) {
			this.visible = true
			this.defaultProductId = productId || ''
			this.dataForm = {
				productId: productId || '',
				quantity: 1,
				stockInTime: '',
				productionDate: '',
				shelfLife: 365,
				batchNumber: ''
			}
			this.initProductOptions()
		},
		initProductOptions() {
			// 如果传入了产品ID，只显示该产品；否则只显示上架的产品
			const query = {
				currentPage: 1,
				pageSize: 1000
			}
			// 如果传入了产品ID，则查询该产品；否则只查询上架的产品
			if (this.defaultProductId) {
				query.id = this.defaultProductId
			} else {
				query.onShelfStatus = 1  // 只获取上架的产品
			}
			request({
				url: '/api/Extend/LqProduct/GetList',
				method: 'GET',
				data: query
			}).then(res => {
				if (res.code == 200 && res.data && res.data.list) {
					if (this.defaultProductId) {
						// 如果传入了产品ID，只显示该产品
						this.productOptions = res.data.list
					} else {
						// 只显示上架的产品
						this.productOptions = res.data.list.filter(product => product.onShelfStatus === 1)
					}
				}
			}).catch(() => {
				this.productOptions = []
			})
		},
		onProductChange() {
			// 产品选择变化时的处理
		},
		dataFormSubmit() {
			this.$refs.elForm.validate((valid) => {
				if (!valid) {
					return false
				}
				this.btnLoading = true
				request({
					url: '/api/Extend/LqInventory/Create',
					method: 'POST',
					data: {
						productId: this.dataForm.productId,
						quantity: this.dataForm.quantity,
						stockInTime: this.dataForm.stockInTime,
						productionDate: this.dataForm.productionDate,
						shelfLife: this.dataForm.shelfLife,
						batchNumber: this.dataForm.batchNumber
					}
				}).then(res => {
					this.btnLoading = false
					this.$message({
						type: 'success',
						message: res.msg || '添加成功',
						onClose: () => {
							this.visible = false
							this.$emit('refresh')
						}
					})
				}).catch(() => {
					this.btnLoading = false
				})
			})
		}
	}
}
</script>

<style lang="scss" scoped>
</style>

