Form.vue 12 KB
<template>
	<el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'" :close-on-click-modal="false" :visible.sync="visible" class="NCC-dialog NCC-dialog_center" lock-scroll append-to-body width="600px">
		<!-- 调试信息:isDetail = {{ isDetail }}, disabled = {{ isDetail === true }} -->
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :disabled="formDisabled" :rules="rules">
					<el-col :span="24">
						<el-form-item label="设备名称" prop="keyword">
							<el-input v-model="dataForm.keyword" placeholder="自动填充" clearable :disabled="true" :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="故障代码" prop="code">
							<el-input v-model="dataForm.code" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24"> 
						<el-form-item label="报警文本" prop="zwbjwb">
							<el-input v-model="dataForm.zwbjwb" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="故障描述" prop="description">
							<el-input v-model="dataForm.description" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":4}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="发布时间" prop="fbsj">
							<el-date-picker v-model="dataForm.fbsj" placeholder="请选择" clearable :style='{"width":"100%"}' type='date' format="yyyy-MM-dd" value-format="timestamp" >
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="设备id" prop="sssb">
							<el-select v-model="sssbValue" placeholder="请选择设备(设备编号)" clearable filterable :style='{"width":"100%"}' @change="handleSssbChange">
								<el-option v-for="item in zlglOptions" :key="item.id" :label="(item.sbbh || '') + (item.zlm ? ' - ' + item.zlm : '')" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="故障图片" prop="gztp">
							<NCC-UploadImg v-model="dataForm.gztp" :fileSize="1" sizeUnit="GB" :limit="9" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="相关附件" prop="attachements">
							<NCC-UploadFz v-model="dataForm.attachements" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="严重程度" prop="yzdcd">
							<el-select v-model="dataForm.yzdcd" placeholder="请选择严重程度" clearable :style='{"width":"100%"}' >
								<el-option label="轻微" value="轻微" ></el-option>
								<el-option label="一般" value="一般" ></el-option>
								<el-option label="严重" value="严重" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<!-- 隐藏故障核对字段 -->
					<!-- <el-col :span="24">
						<el-form-item label="故障核对" prop="gzhd">
							<el-input v-model="dataForm.gzhd" placeholder="请输入故障核对" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":6}' >
							</el-input>
						</el-form-item>
					</el-col> -->
					<el-col :span="24">
						<el-form-item label="外部简单描述" prop="wbjdm">
							<el-input v-model="dataForm.wbjdm" placeholder="请输入外部简单描述" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":6}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="故障原因" prop="gzyy">
							<el-input v-model="dataForm.gzyy" placeholder="请输入故障原因" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":6}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="解决办法" prop="jjbf">
							<el-input v-model="dataForm.jjbf" placeholder="请输入解决办法" show-word-limit :style='{"width":"100%"}' type='textarea' :autosize='{"minRows":4,"maxRows":6}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="分类" prop="fl">
							<el-select v-model="dataForm.fl" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in flOptions" :key="index" :label="item.label" :value="item.value" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<!-- 隐藏规格字段 -->
					<!-- <el-col :span="24">
						<el-form-item label="规格" prop="gg">
							<el-input v-model="dataForm.gg" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col> -->
					<!-- 隐藏“图片”字段,仅保留“故障图片” -->
					<!-- <el-col :span="24">
						<el-form-item label="图片" prop="fj1">
							<NCC-UploadImg v-model="dataForm.fj1" :fileSize="1" sizeUnit="GB" :limit="9" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col> -->
				</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import request from '@/utils/request'
	import { previewDataInterface } from '@/api/systemData/dataInterface'

	// 故障分类固定选项(与后端一致)
	const GZCX_FL_OPTIONS = [
		'机械故障', '电气故障', '液压故障', '气控故障', '操作不当', '维护不当', '意外事故', '安装相关', '其他原因'
	].map(t => ({ label: t, value: t }))

	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					id:'',
					code:undefined,
					keyword:undefined,
					zwbjwb:undefined,
					description:undefined,
					fbsj:undefined,
					fl:undefined,
					gg:undefined,
					fj1:[],
					attachements:[],
					gztp:[],
					sssb:undefined,
					sbmc:undefined,
					yzdcd:undefined,
					gzhd:undefined,
					wbjdm:undefined,
					gzyy:undefined,
					jjbf:undefined,
				},
				rules: {
				},
				flOptions: GZCX_FL_OPTIONS,
				zlglOptions: [], // 资料管理(设备)选项,用于设备id/设备名
				sssbValue: '', // 设备选择值(zlgl.id)
			}
		},
		computed: {
			formDisabled() {
				// 明确返回布尔值,确保表单禁用状态正确
				const disabled = this.isDetail === true;
				console.log('Gzcx Form computed formDisabled - isDetail:', this.isDetail, 'disabled:', disabled);
				return disabled;
			}
		},
        watch: {
			visible(val) {
				if (val) {
					// 对话框打开时,强制确保 isDetail 为 false(除非是详情模式)
					console.log('Gzcx Form visible changed to true, current isDetail:', this.isDetail);
					if (this.isDetail !== true) {
						this.isDetail = false;
					}
				} else {
					// 对话框关闭时,重置 isDetail 为 false
					this.isDetail = false;
					console.log('Gzcx Form visible changed to false, reset isDetail to false');
				}
			}
		},
        created() {
			this.getZlglOptions();
		},
		mounted() {
        },
		methods: {
			getZlglOptions() {
				return request({
					url: '/api/Extend/Zlgl',
					method: 'GET',
					data: { currentPage: 1, pageSize: 1000 }
				}).then(res => {
					this.zlglOptions = res.data.list || []
					return Promise.resolve()
				}).catch(err => {
					console.error('获取资料管理(设备)列表失败:', err)
					this.zlglOptions = []
					return Promise.reject(err)
				})
			},
			handleSssbChange(val) {
				// 选择设备后,自动填充设备名称(资料名)
				if (val) {
					const zl = this.zlglOptions.find(item => item.id === val)
					if (zl) {
						this.dataForm.sssb = zl.id
						this.dataForm.keyword = zl.zlm || zl.sbbh || ''
						this.dataForm.sbmc = zl.zlm || ''
					}
				} else {
					this.dataForm.sssb = ''
					this.dataForm.keyword = ''
					this.dataForm.sbmc = ''
				}
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				console.log('Gzcx Form init called - id:', id, 'isDetail param:', isDetail);
				// 强制重置 isDetail,确保表单可编辑
				this.isDetail = false;
				// 如果明确传入 isDetail 为 true,才设置为详情模式
				if (isDetail === true) {
					this.isDetail = true;
				}
				this.dataForm.id = id || 0;
				console.log('Gzcx Form init - after setting, isDetail:', this.isDetail, 'type:', typeof this.isDetail);
				// 直接设置 visible,不使用 $nextTick,避免时序问题
				this.visible = true;
				console.log('Gzcx Form visible set to true, isDetail:', this.isDetail);
				// 初始化选择值
				this.sssbValue = '';
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/Extend/Gzcx/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
							if(!this.dataForm.fj1)this.dataForm.fj1=[];
							if(!this.dataForm.attachements)this.dataForm.attachements=[];
							if(!this.dataForm.gztp)this.dataForm.gztp=[];
							// 根据设备(资料管理)ID 回显选择
							if (this.dataForm.sssb) {
								if (this.zlglOptions.length === 0) {
									this.getZlglOptions().then(() => {
										const zl = this.zlglOptions.find(item => item.id === this.dataForm.sssb);
										if (zl) this.sssbValue = zl.id;
									});
								} else {
									const zl = this.zlglOptions.find(item => item.id === this.dataForm.sssb);
									if (zl) this.sssbValue = zl.id;
								}
							}
						})
					} else {
						// 新建时清空新字段
						this.dataForm.sssb = '';
						this.dataForm.sbmc = '';
						this.dataForm.yzdcd = '';
						this.dataForm.gzhd = '';
						this.dataForm.wbjdm = '';
						this.dataForm.zwbjwb = '';
						this.dataForm.gzyy = '';
						this.dataForm.jjbf = '';
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Extend/Gzcx`,
                                method: 'post',
                                data: this.dataForm,
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            })
                        } else {
                            request({
                                url: '/api/Extend/Gzcx/' + this.dataForm.id,
                                method: 'PUT',
                                data: this.dataForm
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refresh', true)
                                    }
                                })
                            })
                        }
                    }
                })
			},
		}
	}
</script>