<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="material">
				<el-select
					v-model="dataForm.material"
					filterable
					style="width: 100%"
					placeholder="请选择原料">
					<el-option
						v-for="dict in getDictDatas('material')"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="来源" prop="origin">
				<el-select
					v-model="dataForm.origin"
					filterable
					style="width: 100%"
					placeholder="请选择来源">
					<el-option
						v-for="dict in originList"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="供应商" prop="supplierId">
				<el-select
					v-model="dataForm.supplierId"
					filterable
					style="width: 100%"
					placeholder="请选择供应商">
					<el-option
						v-for="dict in supplierList"
						:key="dict.id"
						:label="dict.name"
						:value="dict.id" />
				</el-select>
			</el-form-item>
			<el-form-item label="批次号" prop="batch">
				<el-input
					v-model="dataForm.batch"
					clearable
					placeholder="请输入规格" />
			</el-form-item>
			<el-form-item label="数量" prop="num">
				<el-input-number
					v-model="dataForm.num"
					clearable
					style="width: 100%"
					placeholder="请输入数量" />
			</el-form-item>
			<el-form-item label="单位" prop="unit">
				<el-select
					v-model="dataForm.unit"
					filterable
					style="width: 100%"
					placeholder="请选择单位">
					<el-option
						v-for="dict in getDictDatas('unit_dict')"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value" />
				</el-select>
			</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 {
	createCoreWOMa,
	updateCoreWOMa,
	getCoreWOMa
} from '@/api/base/coreWorkOrder';
import { getSupplierList } from "@/api/base/material";
import { getDictDatas} from "@/utils/dict";

export default {
	props: {
		workOrderId: {
			type: String,
			default: '',
		},
	},
	data() {
		return {
			visible: false,
			dataForm: {
				id: undefined,
				material: '',
				origin: undefined,
				supplierId: undefined,
				batch: undefined,
				num: 0,
				unit: undefined
			},
			originList: [
				{ label: '内部', value: 1},
				{ label: '采购', value: 2}
			],
			supplierList: [],
			dataRule: {
				material: [{ required: true, message: '物料不能为空', trigger: 'blur' }],
				num: [{ required: true, message: '数量不能为空', trigger: 'blur' }]
			},
		};
	},
	mounted() {
		this.getDict()
	},
	methods: {
		async getDict() {
			// 供应商列表
			const supplierRes = await getSupplierList();
			this.supplierList = supplierRes.data;
		},
		init(id) {
			this.dataForm.id = id || '';
			console.log('1', this.dataForm.id)
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.dataForm.id) {
					getCoreWOMa(this.dataForm.id).then((res) => {
						this.dataForm = res.data
					});
				}
			});
		},
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					// 修改的提交
					if (this.dataForm.id) {
						updateCoreWOMa({
							...this.dataForm,
							workOrderId: this.workOrderId,
						}).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.visible = false;
							this.$emit('refreshDataList');
						});
						return;
					}
					// 添加的提交
					createCoreWOMa({
						...this.dataForm,
						workOrderId: this.workOrderId,
					}).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>
