<template>
	<el-dialog
		:visible.sync="visible"
		:width="'30%'"
		:append-to-body="true"
		:close-on-click-modal="false"
		@closed="$emit('closed')"
		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="90px"
			v-loading="formLoading"
			@keyup.enter.native="dataFormSubmit()">
			<el-form-item label="设备名称" prop="equipmentId">
				<el-select
					v-model="dataForm.equipmentId"
					filterable
					clearable
					placeholder="请选择设备名称"
					style="width: 100%">
					<el-option
						v-for="dict in equipmentOptions"
						:key="dict.value"
						:label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="巡检项目" prop="program">
				<el-input
					v-model="dataForm.program"
					placeholder="请输入巡检项目"
					clearable />
			</el-form-item>
			<el-form-item label="巡检结果" prop="checkResult">
				<el-input
					v-model="dataForm.checkResult"
					placeholder="请输入巡检结果"
					clearable />
			</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>
export default {
	props: ['orderId'],
	data() {
		return {
			visible: false,
			dataForm: {
				id: null,
				equipmentId: null,
				program: null,
				checkResult: null,
			},
			formLoading: false,
			equipmentOptions: [],
			dataRule: {
				equipmentId: [
					{ required: true, message: '设备不能为空', trigger: 'blur' },
				],
				program: [
					{ required: true, message: '巡检项目不能为空', trigger: 'blur' },
				],
				checkResult: [
					{ required: true, message: '巡检结果不能为空', trigger: 'blur' },
				],
			},
		};
	},
	mounted() {
		this.initOptions();
	},
	methods: {
		async initOptions() {
			this.formLoading = true;
			const urls = [
				'/base/core-equipment/page?pageNo=1&pageSize=100&special=true',
			];
			try {
				const [eq] = await Promise.all(urls.map((url) => this.$axios(url)));
				if (eq.code == 0) {
					this.equipmentOptions = eq.data.list.map((item) => ({
						label: item.name,
						value: item.id,
					}));
				}
				this.formLoading = false;
			} catch (err) {
				this.formLoading = false;
				console.error(err);
			}
		},
		init(row) {
			this.visible = true;
			if (row?.id) {
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields();
					this.formLoading = true;
					this.$axios({
						url: '/base/equipment-check-order-det/get?id=' + row.id,
					}).then((res) => {
						Object.keys(this.dataForm).forEach((key) => {
							this.dataForm[key] = res.data[key];
						});
						this.formLoading = false;
					});
				});
			}
		},
		// 表单提交
		async dataFormSubmit() {
			let valid = false;
			try {
				valid = await this.$refs['dataForm'].validate();
			} catch (err) {}
			if (!valid) return;

			const res = await this.$axios({
				url:
					'base/equipment-check-order-det' +
					(this.dataForm.id ? '/update' : '/create'),
				method: this.dataForm.id ? 'put' : 'post',
				data: {
					...this.dataForm,
					orderId: this.orderId,
				},
			});

			if (res.code == 0) {
				this.$modal.msgSuccess(this.dataForm.id ? '修改成功' : '新增成功');
				this.$emit('refreshDataList');
				this.visible = false;
			}
		},
	},
};
</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>
