<template>
	<el-dialog
		:visible.sync="visible"
		:width="'40%'"
		: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="80px"
			@keyup.enter.native="dataFormSubmit()">
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="物料名称" prop="materialId">
						<el-select
							v-model="dataForm.materialId"
							placeholder="请选择物料"
							clearable
							filterable
							@change="setCode"
							style="width: 100%"
						>
							<el-option
								v-for="dict in materialList"
								:key="dict.id"
								:label="dict.name"
								:value="dict.id" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="物料编码" prop="materialCode">
						<el-input
							v-model="dataForm.materialCode"
							clearable
							disabled
							placeholder="请输入物料编码" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="单位" prop="unit">
						<el-select
							v-model="dataForm.unit"
							style="width: 100%"
							disabled
							placeholder="请选择单位">
							<el-option
								v-for="dict in getDictDatas(DICT_TYPE.UNIT_DICT)"
								:key="dict.value"
								:label="dict.label"
								:value="dict.value" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="数量" prop="num">
					<el-input-number v-model="dataForm.num" controls-position="right" clearable placeholder="请输入数量" style="width: 100%" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" clearable placeholder="请输入备注" />
			</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 { getMaterialList } from '@/api/base/material';
import { createMaterialPBDet, updateMaterialPBDet, getMaterialPBDet } from "@/api/base/materialProductBom";

export default {
	props: {
		bomId: {
			type: String,
			default: '',
		},
	},
	data() {
		return {
			visible: false,
			dataForm: {
				id: undefined,
				materialId: '',
				num: 0,
				materialCode: undefined,
				unit: undefined,
				remark: '',
			},
			materialList: [],
			dataRule: {
				materialId: [{ required: true, message: '物料名称不能为空', trigger: 'change' }],
				num: [{ required: true, message: '数量不能为空', trigger: 'blur' }]
			},
		};
	},
	mounted() {
		this.getDict()
	},
	methods: {
		async getDict() {
			// 物料列表
			const res = await getMaterialList();
			this.materialList = res.data;
		},
		init(id) {
			this.dataForm.id = id || '';
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.dataForm.id) {
					getMaterialPBDet(this.dataForm.id).then((res) => {
						this.dataForm = res.data
						this.setCode()
					});
				}
			});
		},
		setCode() {
			const tempMaterial = this.materialList.filter(item =>{
				return item.id === this.dataForm.materialId
			})
			this.dataForm.materialCode = tempMaterial[0]?.code
			this.dataForm.unit = tempMaterial[0].unit === undefined ? undefined : String(tempMaterial[0]?.unit)
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					// 修改的提交
					if (this.dataForm.id) {
						updateMaterialPBDet({
							...this.dataForm,
							bomId: this.bomId,
						}).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.visible = false;
							this.$emit('refreshDataList');
						});
						return;
					}
					// 添加的提交
					createMaterialPBDet({
						...this.dataForm,
						bomId: this.bomId,
					}).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>
