<template>
	<el-dialog
		title="首件检验"
		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-width="100px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="检验日期" prop="test_date">
								<el-date-picker
									v-model="form.test_date"
									type="date"
									value-format="YYYY-MM-DD"
									style="width: 100%"
								/>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="抽检编号">
								<el-input v-model="form.test_numer"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-table
						:data="form.ftestitems"
						border
						placeholder="暂无数据"
					>
						<el-table-column
							prop="testitem"
							label="检测项"
							min-width="100"
							align="center"
						>
							<template #default="scope">
								<span>{{ scope.row.testitem_name }}</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="test_val"
							label="操作人检测值"
							min-width="120"
							align="center"
						>
							<template #default="scope">
								<el-input-number
									v-model="scope.row.test_val"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</template>
						</el-table-column>
						<el-table-column
							prop="check_val"
							label="专检人测量值"
							min-width="120"
							align="center"
						>
							<template #default="scope">
								<el-input-number
									v-model="scope.row.check_val"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</template>
						</el-table-column>
					</el-table>
					<el-row style="margin-top: 14px">
						<el-col :md="12" :sm="24">
							<el-form-item label="检验工序集" prop="test_group">
								<el-select
									v-model="form.test_group"
									placeholder="检验工序集"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in processOptions"
										:key="item"
										:label="item"
										:value="item"
									>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" prop="is_ok">
							<el-form-item label="合格判定" prop="is_ok">
								<el-select
									v-model="form.is_ok"
									placeholder="合格判定"
									clearable
									style="width: 100%"
								>
									<el-option label="合格" :value="true" />
									<el-option label="不合格" :value="false" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="操作人" prop="test_user">
								<el-select
									v-model="form.test_user"
									placeholder="操作人"
									clearable
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in userList"
										: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="专检人" prop="check_user">
								<el-select
									v-model="form.check_user"
									placeholder="专检人"
									clearable
									filterable
									style="width: 100%"
								>
									<el-option
										v-for="item in userList"
										: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.note"
									placeholder="备注"
								/>
							</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>
export default {
	emits: ["success", "closed"],
	data() {
		return {
			loading: false,
			form: {
				type: "first",
				ftestitems: [],
			},
			rules: {
				belong_dept: [
					{
						required: true,
						message: "请选择所属部门",
						trigger: "blur",
					},
				],
				test_date: [
					{
						required: true,
						message: "请选择检验日期",
						trigger: "blur",
					},
				],
				test_user: [
					{
						required: true,
						message: "请选择操作人",
						trigger: "blur",
					},
				],
				check_user: [
					{
						required: true,
						message: "请选择专检人",
						trigger: "blur",
					},
				],
				test_group: [
					{
						required: true,
						message: "请选择检测工序集",
						trigger: "blur",
					},
				],
				is_ok: [
					{
						required: true,
						message: "请选择是否合格",
						trigger: "blur",
					},
				],
			},
			visible: false,
			isSaveing: false,
			userList: [],
			options: [],
			processOptions: ["粗中细磨", "抛光", "半抛光"],
			deptOptions: [],
			selectionFilters: [],
			setFiltersVisible: false,
			belong_dept_options: [],
			group: [],
		};
	},
	mounted() {
		// this.getProcess();
		this.getUserList();
		this.getDeptOptions();
	},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			if (mode == "add") {
				this.getTextItem();
			}
			this.visible = true;
			return this;
		},
		//获取检验项目
		getTextItem() {
			let that = this;
			that.$API.qm.getTestItem
				.get({ tags__contains: "first", page: 0 })
				.then((res) => {
					console.log(res);
					let ftestitems = [];
					res.forEach((item) => {
						let obj = {};
						obj.testitem = item.id;
						obj.testitem_name = item.name;
						obj.test_val = 0;
						obj.check_val = 0;
						ftestitems.push(obj);
					});
					console.log(ftestitems);
					that.form.ftestitems = ftestitems;
				});
		},
		//获取部门列表
		getDeptOptions() {
			this.$API.system.dept.list
				.req({ page: 0, type__in: "dept" })
				.then((res) => {
					this.deptOptions = res;
				});
		},
		//获取员工
		getUserList() {
			let that = this;
			this.$API.system.user.list
			.req({ page: 0, posts__code__contains: "check" })
				.then((res) => {
					that.userList = res.results;
				});
		},
		// getProcess(){
		//   let that = this;
		//   this.$API.mtm.process.list.req({page:0}).then(res=>{
		//     let arr = [];
		//     res.forEach(item => {
		//       if(item.name=='粗中细磨'){
		//         arr.push(item);
		//       }
		//     })
		//     that.processOptions = arr;
		//   })
		// },
		//提交
		submit() {
			console.log("111111");
			let that = this;
			that.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					that.isSaveing = true;
					try {
						if (that.mode == "add") {
							that.$API.qm.ftest.create
								.req(that.form)
								.then((res) => {
									that.$emit("success");
									that.isSaveing = false;
									that.visible = false;
									that.$message.success("操作成功");
								});
						} else if (that.mode == "edit") {
							that.$API.qm.ftest.update
								.req(that.form.id, that.form)
								.then((res) => {
									that.$emit("success");
									that.isSaveing = false;
									that.visible = false;
									that.$message.success("操作成功");
								});
						}
					} catch (err) {
						//可以处理校验错误
						this.isSaveing = false;
						return err;
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			console.log("this.form", this.form);
			// this.form.test_group = this.form.split(',')
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
