<template>
	<el-dialog
		:visible.sync="visible"
		:width="'35%'"
		:append-to-body="true"
		:close-on-click-modal="false"
		class="dialog">
		<template #title>
			<slot name="title">
				<div class="titleStyle">
					{{ !dataForm.id ? '新增' : '编辑' }}
				</div>
			</slot>
		</template>

		<el-form
			ref="dataForm"
			:model="dataForm"
			:rules="dataRule"
			label-width="100px"
			@keyup.enter.native="dataFormSubmit()">
			<el-form-item label="产品" prop="productInfo">
				<el-select
					v-model="dataForm.productInfo"
					style="width: 100%"
					filterable
					remote
					:remote-method="remoteMethod"
					value-key="id"
					placeholder="请选择产品"
					:loading="loading">
					<el-option
						v-for="item in productArr"
						:key="item.id"
						:label="item.goodSpecificationName"
						:value="item"
						:disabled="!item.deactivate" />
				</el-select>
			</el-form-item>
			<el-form-item label="品质" prop="grade">
				<el-input
					v-model="dataForm.grade"
					placeholder="请输入品质"
					clearable
					:style="{ width: '100%' }" />
			</el-form-item>
			<el-form-item label="数量" prop="number">
				<el-input-number
					v-model="dataForm.number"
					:step="1"
					:min="1"
					step-strictly></el-input-number>
			</el-form-item>
		</el-form>

		<el-row style="text-align: right">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</el-row>
	</el-dialog>
</template>

<script>
import { getGoodSpecificationPage } from '@/api/asrs/goodSpecification';

export default {
	props: {
		warehouseId: {
			type: String,
			default: '',
		},
	},
	data() {
		return {
			visible: false,
			dataForm: {
				index: -1,
				productInfo: null,
				number: 1,
				grade: null,
			},
			productArr: [],
			loading: false,
			dataRule: {
				productInfo: [
					{ required: true, message: '产品不能为空', trigger: 'change' },
				],
				number: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
				grade: [{ required: true, message: '品质不能为空', trigger: 'change' }],
			},
		};
	},
	methods: {
		init(index) {
			if (index >= 0) {
				this.dataForm.index = index;
			}
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
			});
		},
		remoteMethod(query) {
			if (query !== '') {
				this.loading = true;
				const params = {
					pageSize: 100,
					pageNo: 1,
					goodSpecificationName: query,
					// warehouseId: this.warehouseId,
				};
				getGoodSpecificationPage(params).then((response) => {
					this.loading = false;
					this.productArr = response.data.list;
				});
			} else {
				this.productArr = [];
			}
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					this.dataForm.productInfo.goodSpecificationId =
						this.dataForm.productInfo.id;
					// 修改的提交
					if (this.dataForm.index >= 0) {
						this.$modal.msgSuccess('修改成功');
						this.visible = false;
						this.$emit('refreshDataList', this.dataForm);
						return;
					}
					// 添加的提交
					this.$modal.msgSuccess('新增成功');
					this.visible = false;
					this.$emit('refreshDataList', this.dataForm);
				}
			});
		},
	},
};
</script>

<style scoped>
.dialog >>> .el-dialog__body {
	padding: 30px 24px;
}
.dialog >>> .el-dialog__header {
	font-size: 16px;
	color: rgba(0, 0, 0, 0.85);
	font-weight: 500;
	padding: 13px 24px;
	border-bottom: 1px solid #e9e9e9;
}
.dialog >>> .el-dialog__header .titleStyle::before {
	content: '';
	display: inline-block;
	width: 4px;
	height: 16px;
	background-color: #0b58ff;
	border-radius: 1px;
	margin-right: 8px;
	position: relative;
	top: 2px;
}
</style>
