<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="160px"
				>
					<el-row>
						<el-col :md="12" :sm="24">
							<el-form-item label="检验类型" prop="testitem">
								<el-select
									v-model="form.testitem"
									placeholder="检验类型"
									clearable
									style="width: 100%"
									disabled
								>
									<el-option
										v-for="item in behaviors"
										: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="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="样品编号" prop="sample_number">
								<el-input
									v-model="form.sample_number"
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '透过率'">
							<el-form-item label="样品厚度/mm" prop="val_hd">
								<el-input-number
									v-model="form.val_hd"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col
							:md="12"
							:sm="24"
							v-if="
								activeName == '中温粘度' || activeName == '膨胀'
							"
						>
							<el-form-item label="规格型号" prop="specification">
								<el-input
									v-model="form.specification"
								></el-input>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '析晶'">
							<el-form-item label="样品数量" prop="sample_count">
								<el-input-number
									v-model="form.sample_count"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col
							:md="12"
							:sm="24"
							v-if="activeName == '高温粘度'"
						>
							<el-form-item
								label="样品密度"
								prop="sample_density"
							>
								<el-input
									v-model="form.sample_density"
								></el-input>
							</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" v-if="activeName == '析晶'">
							<el-form-item
								label="实验温度及保温时间"
								prop="sample_density"
							>
								<el-input
									v-model="form.sample_number"
								></el-input>
							</el-form-item>
						</el-col>
						<el-col
							:md="12"
							:sm="24"
							v-if="activeName == '高温粘度'"
						>
							<el-form-item
								label="升至最高温度/℃"
								prop="val_zgwd"
							>
								<el-input-number
									v-model="form.val_zgwd"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24">
							<el-form-item label="仪器状态" prop="equip_state">
								<el-select
									v-model="form.equip_state"
									placeholder="仪器状态"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in stateOptios"
										:key="item.id"
										:label="item.name"
										:value="item.id"
									>
									</el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '折射率'">
							<el-form-item label="折射率(589nm)" prop="val_zsl">
								<el-input-number
									v-model="form.val_zsl"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '透过率'">
							<el-form-item label="透过率(550nm)" prop="val_tgl">
								<el-input-number
									v-model="form.val_tgl"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col
							:md="12"
							:sm="24"
							v-if="activeName == '中温粘度'"
						>
							<el-form-item label="Ts/℃" prop="val_ts">
								<el-input-number
									v-model="form.val_ts"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '膨胀'">
							<el-form-item
								label="30-300℃膨胀系数"
								prop="val_pzxs"
							>
								<el-input-number
									v-model="form.val_pzxs"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '膨胀'">
							<el-form-item label="Tg/℃" prop="val_tg">
								<el-input-number
									v-model="form.val_tg"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '膨胀'">
							<el-form-item label="Tf/℃" prop="val_tf">
								<el-input-number
									v-model="form.val_tf"
									:min="0"
									style="width: 100%"
									controls-position="right"
								>
								</el-input-number>
							</el-form-item>
						</el-col>
						<el-col :md="12" :sm="24" v-if="activeName == '析晶'">
							<el-form-item label="析晶状态" prop="val_xj">
								<el-select
									v-model="form.val_xj"
									placeholder="析晶状态"
									clearable
									style="width: 100%"
								>
									<el-option
										v-for="item in xjOptios"
										: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-input
									v-model="form.note"
									placeholder="备注"
								/>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-main>
			<el-footer v-if="this.mode !== 'show'">
				<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"],
	props: {
		activeId: { type: String, default: "" },
		activeName: { type: String, default: "" },
	},
	data() {
		return {
			loading: false,
			form: {},
			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",
					},
				],
			},
			stateOptios: [
				{ id: "R", name: "正常" },
				{ id: "T", name: "异常" },
			],
			xjOptios: [
				{ id: "S", name: "析晶" },
				{ id: "K", name: "不析晶" },
				{ id: "θ", name: "未化" },
			],
			visible: false,
			isSaveing: false,
			userList: [],
			options: [],
			processOptions: [],
			deptOptions: [],
			selectionFilters: [],
			setFiltersVisible: false,
			belong_dept_options: [],
			group: [],
			behaviors: [],
		};
	},
	mounted() {
		console.log(this.activeId);
		console.log(this.activeName);
		this.form.testitem = this.activeId;
		this.getTextItem();
		this.getProcess();
		this.getUserList();
		this.getDeptOptions();
	},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//获取检验项目
		getTextItem() {
			let that = this;
			that.$API.qm.getTestItem
				.get({ tag: "performance", page: 0 })
				.then((res) => {
					that.behaviors = res;
				});
		},
		//获取部门列表
		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__name: "检验" })
				.then((res) => {
					that.userList = res;
				});
		},
		getProcess() {
			let that = this;
			this.$API.mtm.process.list.req({ page: 0 }).then((res) => {
				that.processOptions = res;
			});
		},
		handleClick() {},
		//提交
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					try {
						var res;
						if (this.mode == "add") {
							res = await this.$API.qm.ptest.create.req(
								this.form
							);
						} else if (this.mode == "edit") {
							res = await this.$API.qm.ptest.update.req(
								this.form.id,
								this.form
							);
						}
						this.isSaveing = false;
						this.$emit("success");
						this.visible = false;
						this.$message.success("操作成功");
					} catch (err) {
						//可以处理校验错误
						this.isSaveing = false;
						return err;
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, data);
			this.form.test_group = this.form.split(",");
		},
		//设置过滤项
		setFilters(filters) {
			this.selectionFilters = filters;
			this.setFiltersVisible = true;
		},
	},
};
</script>

<style></style>
