<template>
	<el-dialog :title="!dataForm.id ? '新建' :  isDetail ? '详情':'编辑'" :close-on-click-modal="false" :visible.sync="visible" class="NCC-dialog NCC-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="" >
				<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="24">
						<el-form-item label="问题设备" prop="title">
							<el-input v-model="dataForm.title" placeholder="请输入" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="问题部件" prop="wtbj">
							<el-input v-model="dataForm.wtbj" placeholder="请输入问题部件" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="问题描述" prop="nr">
							<el-input v-model="dataForm.nr" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize="{minRows:4,maxRows:4}" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="故障类型" prop="gzlx">
							<el-input v-model="dataForm.gzlx" placeholder="请输入故障类型（APP端对客户不显示）" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="服务人员" prop="fwry">
							<el-select v-model="fwryValue" placeholder="请从设备维护人员中选择" clearable filterable :style='{"width":"100%"}' @change="handleFwryChange">
								<el-option v-for="item in sbwhryOptions" :key="item.id" :label="item.xm + (item.dh ? ' - ' + item.dh : '')" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="联系电话" prop="shdh">
							<el-input v-model="dataForm.shdh" placeholder="选择服务人员后自动带出" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="联系邮箱" prop="shyx">
							<el-input v-model="dataForm.shyx" placeholder="选择服务人员后自动带出" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="处理结果" prop="cljg">
							<el-select v-model="dataForm.cljg" placeholder="请选择处理结果" :style='{"width":"100%"}' >
								<el-option label="未处理" value="未处理" />
								<el-option label="进行中" value="进行中" />
								<el-option label="已处理" value="已处理" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="反馈结果" prop="fkjg">
							<el-input v-model="dataForm.fkjg" placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize="{minRows:4,maxRows:4}" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="处理人员" prop="clr">
							<el-select v-model="clrValue" placeholder="请从设备维护人员中选择" clearable filterable :style='{"width":"100%"}' @change="handleClrChange">
								<el-option v-for="item in sbwhryOptions" :key="item.id" :label="item.xm + (item.dh ? ' - ' + item.dh : '')" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="反馈用户" prop="fbr">
							<el-input v-model="dataForm.fbr" placeholder="系统自动生成" readonly />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="反馈时间" prop="fbsj">
							<el-input v-model="dataForm.fbsj" placeholder="系统自动生成" readonly />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="设备名称" prop="sbmc">
							<el-select v-model="sbmcValue" placeholder="请选择设备名称" clearable filterable :style='{"width":"100%"}' @change="handleSbmcChange">
								<el-option v-for="item in zlglOptions" :key="item.id" :label="item.zlm + ' (' + item.sbbh + ')'" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="售后人员" prop="shry">
							<el-select v-model="shryValue" placeholder="请从设备维护人员中选择" clearable filterable :style='{"width":"100%"}' @change="handleShryChange">
								<el-option v-for="item in sbwhryOptions" :key="item.id" :label="item.xm + (item.dh ? ' - ' + item.dh : '')" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="是否已读" prop="sfyd">
							<el-select v-model="dataForm.sfyd" placeholder="请选择" :style='{"width":"100%"}' >
								<el-option label="未读" value="未读" />
								<el-option label="已读" value="已读" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="截图" prop="fj2">
							<NCC-UploadImg v-model="dataForm.fj2" :fileSize="1" sizeUnit="GB" :limit="9" >
							</NCC-UploadImg>
						</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-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: '',
					title: undefined,
					nr: undefined,
					wtbj: undefined,
					gzlx: undefined,
					sbmc: undefined,
					fwry: undefined,
					shdh: undefined,
					shyx: undefined,
					cljg: '未处理',
					sfyd: '未读',
					fkjg: undefined,
					clr: undefined,
					fbr: undefined,
					fbsj: undefined,
					shry: undefined,
					fj2: [],
					fj1: [],
				},
				rules: {},
				zlglOptions: [],
				sbmcValue: '',
				sbwhryOptions: [],
				fwryValue: '',  // 服务人员选中的 id
				clrValue: '',   // 处理人员选中的 id
				shryValue: '',  // 售后人员选中的 id
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getZlglOptions();
			this.getSbwhryOptions();
		},
		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)
				})
			},
			getSbwhryOptions() {
				return request({
					url: '/api/Extend/Sbwhry',
					method: 'GET',
					data: { currentPage: 1, pageSize: 1000 }
				}).then(res => {
					this.sbwhryOptions = (res.data.list || []).filter(item => item && item.id && item.xm);
				}).catch(() => { this.sbwhryOptions = []; });
			},
			handleFwryChange(val) {
				if (val) {
					const p = this.sbwhryOptions.find(item => item.id === val);
					if (p) {
						this.dataForm.fwry = p.xm;
						this.dataForm.shdh = p.dh || '';
						this.dataForm.shyx = p.yx || '';
					}
				} else {
					this.dataForm.fwry = '';
					this.dataForm.shdh = '';
					this.dataForm.shyx = '';
				}
			},
			handleClrChange(val) {
				if (val) {
					const p = this.sbwhryOptions.find(item => item.id === val);
					if (p) this.dataForm.clr = p.xm;
				} else {
					this.dataForm.clr = '';
				}
			},
			handleShryChange(val) {
				if (val) {
					const p = this.sbwhryOptions.find(item => item.id === val);
					if (p) this.dataForm.shry = p.xm;
				} else {
					this.dataForm.shry = '';
				}
			},
			handleSbmcChange(val) {
				if (val) {
					const device = this.zlglOptions.find(item => item.id === val);
					if (device) this.dataForm.sbmc = device.zlm || '';
				} else {
					this.dataForm.sbmc = '';
				}
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				this.dataForm.id = id || 0;
				this.visible = true;
				this.isDetail = isDetail || false;
				this.sbmcValue = '';
				this.fwryValue = '';
				this.clrValue = '';
				this.shryValue = '';
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/Extend/Sbwtfk/' + this.dataForm.id,
							method: 'get'
						}).then(res => {
							this.dataForm = res.data;
							if (!this.dataForm.fj2) this.dataForm.fj2 = [];
							if (!this.dataForm.fj1) this.dataForm.fj1 = [];
							if (this.dataForm.cljg === undefined || this.dataForm.cljg === null) this.dataForm.cljg = '未处理';
							if (this.dataForm.sfyd === undefined || this.dataForm.sfyd === null) this.dataForm.sfyd = '未读';
							const setSelectValues = () => {
								if (this.dataForm.sbmc && this.zlglOptions.length) {
									const device = this.zlglOptions.find(item => item.zlm === this.dataForm.sbmc);
									if (device) this.sbmcValue = device.id;
								}
								if (this.sbwhryOptions.length) {
									if (this.dataForm.fwry) {
										const p = this.sbwhryOptions.find(item => item.xm === this.dataForm.fwry);
										if (p) this.fwryValue = p.id;
									}
									if (this.dataForm.clr) {
										const p = this.sbwhryOptions.find(item => item.xm === this.dataForm.clr);
										if (p) this.clrValue = p.id;
									}
									if (this.dataForm.shry) {
										const p = this.sbwhryOptions.find(item => item.xm === this.dataForm.shry);
										if (p) this.shryValue = p.id;
									}
								}
							};
							if (this.zlglOptions.length && this.sbwhryOptions.length) {
								setSelectValues();
							} else {
								Promise.all([
									this.zlglOptions.length ? Promise.resolve() : this.getZlglOptions(),
									this.sbwhryOptions.length ? Promise.resolve() : this.getSbwhryOptions()
								]).then(setSelectValues);
							}
						});
					} else {
						this.dataForm.cljg = '未处理';
						this.dataForm.sfyd = '未读';
						this.dataForm.sbmc = '';
					}
				});
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Extend/Sbwtfk`,
                                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/Sbwtfk/' + 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>
