<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="equipmentId">
				<el-select
					style="width: 100%"
					v-model="dataForm.equipmentId"
					@change="setEquipment"
					placeholder="请选择设备">
					<el-option
						v-for="item in equipmentArr"
						:key="item.id"
						:label="item.equipmentName"
						:value="item.id"></el-option>
				</el-select>
			</el-form-item> -->
			<el-form-item label="物料" prop="materialId">
				<el-select
					style="width: 100%"
					v-model="dataForm.materialId"
					filterable
					@change="setMaterial"
					placeholder="请选择物料">
					<el-option
						v-for="item in productArr"
						:key="item.id"
						:label="
							item.materialCode + '-' + item.materialName + '-' + item.material
						"
						:value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="数量" prop="materialNumber">
				<el-input-number
					v-model="dataForm.materialNumber"
					:step="1"
					step-strictly
					:min="0"></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 {
	getProductPage,
	createProcess,
	getProcess,
	updateProcess,
} from '@/api/ssdl/product&recipe';

export default {
	data() {
		return {
			visible: false,
			dataForm: {
				id: 0,
        processId: '', // 配方id
				processName: '', // 配方型号
				processSize: '', // 配方规格
				processDesc: '', // 配方描述
				materialId: '',
				material: '',
				materialName: '',
				materialNumber: '',
			},
			productArr: [],
			equipmentArr: [],
			dataRule: {
				materialId: [
					{ required: true, message: '物料不能为空', trigger: 'change' },
				],
				materialNumber: [
					{ required: true, message: '数量不能为空', trigger: 'blur' },
				],
			},
		};
	},
	methods: {
		init(id,data) {
			this.dataForm.id = id || '';
			this.dataForm.processId = data.id || '';
			this.dataForm.processName = data.processName || '';
			this.dataForm.processSize = data.processSize || '';
			this.dataForm.processDesc = data.processDesc || '';
			this.visible = true;
			getProductPage({ pageNo: 1, pageSize: 100 }).then((res) => {
				this.productArr = res.data.list;
			});
			// getEquipmentPage({ pageNo: 1, pageSize: 100 }).then((res) => {
			// 	this.equipmentArr = res.data.list;
			// });
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.dataForm.id) {
					getProcess(this.dataForm.id).then((res) => {
						this.dataForm = res.data;
					});
				}
			});
		},
		setMaterial() {
			const data = this.productArr.find(
				(i) => i.id === this.dataForm.materialId
			);
			this.dataForm.materialName = data.materialName;
			this.dataForm.material = data.material;
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					// 修改的提交
					if (this.dataForm.id) {
						updateProcess([{
							...this.dataForm,
						}]).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.visible = false;
							this.$emit('refreshDataList');
						});
						return;
					}
					// 添加的提交
					createProcess([{
						...this.dataForm,
					}]).then((response) => {
						this.$modal.msgSuccess('新增成功');
						this.visible = false;
						this.$emit('refreshDataList');
					});
				}
			});
		},
	},
};
</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>
