<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="80%">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :disabled="isDetail === true" :rules="rules">
					<el-col :span="24">
						<el-form-item label="备件名称" prop="mc">
							<el-input v-model="dataForm.mc" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="关联产品" prop="sssb">
							<el-select v-model="sssbValue" placeholder="请选择关联产品" clearable filterable :style='{"width":"100%"}' @change="handleSssbChange">
								<el-option v-for="item in cpglOptions" :key="item.id" :label="item.cpmc + ' (' + (item.cpid || item.id) + ')'" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="物料编码" prop="bh">
							<el-input v-model="dataForm.bh" placeholder="请输入物料编码" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备件位置" prop="bjwz">
							<el-input v-model="dataForm.bjwz" placeholder="请输入备件位置" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备件介绍" prop="bjjj">
							<el-input v-model="dataForm.bjjj" 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="ghzy">
							<el-input v-model="dataForm.ghzy" 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="dhzq">
							<el-input v-model="dataForm.dhzq" placeholder="请输入订货周期，如：30天、3个月" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="随机备件" prop="sjbj">
							<NCC-UploadFz v-model="dataForm.sjbj" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="一年备件" prop="ynbj">
							<NCC-UploadFz v-model="dataForm.ynbj" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="两年备件" prop="lnbj">
							<NCC-UploadFz v-model="dataForm.lnbj" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col v-if="false" :span="24">
						<el-form-item label="附件1(随机备件)" prop="sjbj">
							<NCC-UploadFz v-model="dataForm.sjbj" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col v-if="false" :span="24">
						<el-form-item label="附件2(一年备件)" prop="ynbj">
							<NCC-UploadFz v-model="dataForm.ynbj" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col v-if="false" :span="24">
						<el-form-item label="附件3(两年备件)" prop="lnbj">
							<NCC-UploadFz v-model="dataForm.lnbj" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="位置" prop="wz">
							<el-input v-model="dataForm.wz" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="示意图" prop="fj1">
							<NCC-UploadFz v-model="dataForm.fj1" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="附件" prop="fj2">
							<NCC-UploadFz v-model="dataForm.fj2" :fileSize="1" sizeUnit="GB" :limit="9" buttonText="点击上传" >
							</NCC-UploadFz>
						</el-form-item>
					</el-col>
					<el-col v-if="false" :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 v-if="false" :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 v-if="false" :span="24">
						<el-form-item label="设备出厂编号" prop="sbccbh">
							<el-input v-model="dataForm.sbccbh" placeholder="请输入" clearable :style='{"width":"100%"}' >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col v-if="false" :span="24">
						<el-form-item label="设备出厂日期" prop="sbccrq">
							<el-date-picker v-model="dataForm.sbccrq" placeholder="请选择" clearable :style='{"width":"100%"}' type='date' format="yyyy-MM-dd" value-format="timestamp" >
							</el-date-picker>
						</el-form-item>
					</el-col>
					<el-col v-if="false" :span="24">
						<el-form-item label="设备介绍" prop="sbjs">
							<NCC-Quill v-model="dataForm.sbjs" placeholder="请输入内容..." >
							</NCC-Quill>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备件分类" prop="bjfl">
							<el-select v-model="dataForm.bjfl" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in bjflOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="宣传图片" prop="xctp">
							<NCC-UploadImg v-model="dataForm.xctp" :fileSize="1" sizeUnit="GB" :limit="9" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备件情况" prop="bjqk">
							<el-select v-model="dataForm.bjqk" placeholder="请选择备件情况" clearable :style='{"width":"100%"}' >
								<el-option label="有" value="有"></el-option>
								<el-option label="无" value="无"></el-option>
							</el-select>
						</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 { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					id:'',
					wz:undefined,
					mc:undefined,
					bh:undefined,
					ghzy:undefined,
					fj1:[],
					fj2:[],
					fbsj:undefined,
					gg:undefined,
					sbccbh:undefined,
					sbccrq:undefined,
					sbjs:undefined,
					bjfl:undefined,
					xctp:[],
					bjwz:undefined,
					sssb:undefined,
					bjqk:undefined,
					bjjj:undefined,
					dhzq:undefined,
					sjbj:[],
					ynbj:[],
					lnbj:[],
				},
				rules: {
				},
				bjflOptions : [],
				cpglOptions: [], // 产品管理选项
				sssbValue: '', // 关联产品选择值
			}
		},
		computed: {},
        watch: {
			visible(val) {
				if (!val) {
					// 对话框关闭时，重置 isDetail 为 false
					this.isDetail = false
				}
			}
		},
        created() {
			this.getbjflOptions();
			this.getCpglOptions();
		},
		mounted() {
        },
		methods: {
			getbjflOptions(){
				getDictionaryDataSelector('625969064434468101').then(res => {
					this.bjflOptions = res.data.list
				});
			},
			getCpglOptions() {
				return request({
					url: '/api/Extend/Cpgl',
					method: 'GET',
					data: { currentPage: 1, pageSize: 1000 }
				}).then(res => {
					this.cpglOptions = res.data.list || []
					return Promise.resolve()
				}).catch(err => {
					console.error('获取产品管理列表失败:', err)
					this.cpglOptions = []
					return Promise.reject(err)
				})
			},
			handleSssbChange(val) {
				// 选择产品后，设置关联产品ID
				this.dataForm.sssb = val || '';
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				// 强制重置 isDetail，确保表单可编辑
				this.isDetail = false;
				// 如果明确传入 isDetail 为 true，才设置为详情模式
				if (isDetail === true) {
					this.isDetail = true;
				}
				this.dataForm.id = id || 0;
				console.log('Bjxx Form init - id:', id, 'isDetail:', this.isDetail);
				this.visible = true;
				// 初始化选择值
				this.sssbValue = '';
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/Extend/Bjxx/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
							if(!this.dataForm.fj1)this.dataForm.fj1=[];
							if(!this.dataForm.fj2)this.dataForm.fj2=[];
							if(!this.dataForm.xctp)this.dataForm.xctp=[];
							if(!this.dataForm.sjbj)this.dataForm.sjbj=[];
							if(!this.dataForm.ynbj)this.dataForm.ynbj=[];
							if(!this.dataForm.lnbj)this.dataForm.lnbj=[];
							// 设置所属设备选择值
							if (this.dataForm.sssb) {
								this.sssbValue = this.dataForm.sssb;
							}
						})
					} else {
						// 新建时清空新字段
						this.dataForm.bjwz = '';
						this.dataForm.sssb = '';
						this.dataForm.bjqk = '';
						this.dataForm.bjjj = '';
						this.dataForm.dhzq = '';
						this.dataForm.sjbj = [];
						this.dataForm.ynbj = [];
						this.dataForm.lnbj = [];
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Extend/Bjxx`,
                                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/Bjxx/' + 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>
