<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:size="1000"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-container v-loading="loading">
			<el-main style="padding: 0 20px 20px 20px">
				<el-form
					ref="dialogForm"
					:model="form"
					:rules="rules"
					label-position="right"
					label-width="80px"
					style="padding: 0 10px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="名称" prop="name">
								<el-input
									v-model="form.name"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="标识">
								<el-input
									v-model="form.code"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="类型">
								<el-select
									v-model="form.type"
									placeholder="类型"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in typeOptions"
										:key="item.value"
										:label="item.label"
										:value="item.value"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="检测项目">
								<el-select
									v-model="form.testitems"
									placeholder="检测项目"
									clearable
									multiple
									style="width: 100%"
								>
									<el-option
										v-for="item in options"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="已到工序">
								<el-select
									v-model="form.process"
									placeholder="已到工序"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in processOptions"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									/>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="计量单位">
								<el-input
									v-model="form.unit"
									clearable
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="排序">
								<el-input-number
									v-model="form.sort"
									clearable
								></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit"
					>保存</el-button
				>
				<el-button @click="visible = false">取消</el-button>
			</el-footer>
		</el-container>
	</el-dialog>
</template>

<script>
const defaultForm = {
	id: "",
	name: "",
	code: "",
	type: "",
	sort: 1,
	unit: "",
	testitems: [],
};
export default {
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			mode: "add",
			titleMap: {
				add: "新增产品",
				edit: "编辑产品",
				show: "查看产品",
			},
			//表单数据
			form: defaultForm,
			//验证规则
			rules: {
				name: [
					{ required: true, message: "请输入名称", trigger: "blur" },
				],
			},
			visible: false,
			isSaveing: false,
			options: [],
			typeOptions: [
				{ label: "电/水/气", value: 0 },
				{ label: "成品", value: 10 },
				{ label: "半成品", value: 20 },
				{ label: "主要原料", value: 30 },
				{ label: "辅助材料", value: 40 },
				{ label: "加工工具", value: 50 },
				{ label: "辅助工装", value: 60 },
			],
			processOptions: [],
			setFiltersVisible: false,
		};
	},
	mounted() {
		this.getTestItem(); //获取部门
		this.getProcessOptions();
	},
	methods: {
		getTestItem() {
			this.$API.qm.getTestItem.get({ page: 0 }).then((res) => {
				this.options = res;
			});
		},
		getProcessOptions() {
			this.$API.mtm.process.list.req({ page: 0 }).then((res) => {
				this.processOptions = res;
			});
		},
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
		},
		//表单提交方法
		submit() {
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					if (that.mode === "add") {
						that.$API.mtm.material.create
							.req(that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success", that.form, that.mode);
								that.visible = false;
								that.$message.success("操作成功");
							})
							.catch((res) => {
								that.isSaveing = false;
							});
					} else {
						res = that.$API.mtm.material.update
							.req(that.form.id, that.form)
							.then((res) => {
								that.isSaveing = false;
								that.$emit("success", that.form, that.mode);
								that.visible = false;
								that.$message.success("操作成功");
							})
							.catch((res) => {
								that.isSaveing = false;
							});
					}
				}
			});
		},

		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
