<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="qxgn">
							<el-select v-model="dataForm.qxgn" placeholder="请选择（问题）分类" clearable :style='{"width":"100%"}' >
								<el-option v-for="item in qxgnOptions" :key="item.id" :label="item.fullName" :value="item.fullName" />
							</el-select>
						</el-form-item>
					</el-col>
					<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="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="filterYlzd1">
							<el-select v-model="filterYlzd1" placeholder="按岗位/职责筛选对接人员" clearable :style='{"width":"100%"}' @change="filterSbwhryOptions">
								<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="clr">
							<el-select v-model="dataForm.clr" placeholder="请选择对接人员（需在设备维护人员中设置处理范围为“系统问题”）" clearable filterable :style='{"width":"100%"}' :loading="sbwhryLoading">
								<el-option v-for="item in clrOptions" :key="item.id" :label="(item.xm || '') + (item.gh ? ' (' + item.gh + ')' : '')" :value="item.xm" />
							</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="fj2">
							<NCC-UploadImg v-model="dataForm.fj2" :fileSize="1" sizeUnit="GB" :limit="9" />
						</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="点击上传" />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="处理结果" prop="cljg">
							<el-select v-model="dataForm.cljg" placeholder="请选择处理结果" clearable :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-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'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				filterYlzd1: '',
				sbwhryOptionsAll: [],
				sbwhryLoading: false,
				qxgnOptions: [],
				// 问题分类字典类型ID（与 migrations 中一致）
				QXGN_DICT_TYPE_ID: '626798200000000001',
				dataForm: {
					id:'',
					title:undefined,
					nr:undefined,
					fj2:[],
					fj1:[],
					clr:undefined,
					fkjg:undefined,
					fbr:undefined,
					fbsj:undefined,
					qxgn:undefined,
					cljg:undefined,
				},
				rules: {},
			}
		},
		computed: {
			clrOptions() {
				if (!this.filterYlzd1) return this.sbwhryOptionsAll
				return this.sbwhryOptionsAll.filter(item => item.ylzd1 === this.filterYlzd1)
			}
		},
		watch: {},
		created() {
			this.getSbwhryOptions()
			this.getQxgnOptions()
		},
		mounted() {},
		methods: {
			async getQxgnOptions() {
				try {
					const res = await getDictionaryDataSelector(this.QXGN_DICT_TYPE_ID)
					this.qxgnOptions = (res.list || []).filter(d => d && d.fullName)
				} catch (e) {
					this.qxgnOptions = []
				}
			},
			getSbwhryOptions() {
				this.sbwhryLoading = true
				request({
					url: '/api/Extend/Sbwhry',
					method: 'GET',
					data: { currentPage: 1, pageSize: 1000, clfw: '系统问题' }
				}).then(res => {
					this.sbwhryOptionsAll = (res.data.list || []).filter(item => item && (item.id || item.xm))
					this.sbwhryLoading = false
				}).catch(() => {
					this.sbwhryOptionsAll = []
					this.sbwhryLoading = false
				})
			},
			filterSbwhryOptions() {
				// 筛选变更时仅影响下拉选项，无需额外逻辑
			},
			goBack() {
				this.$emit('refresh')
			},
			init(id, isDetail) {
				this.dataForm.id = id || 0
				this.visible = true
				this.isDetail = isDetail || false
				this.filterYlzd1 = ''
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields()
					if (this.dataForm.id) {
						request({
							url: '/api/Extend/Xtwtfk/' + 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 = []
						})
					} else {
						this.dataForm.qxgn = ''
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Extend/Xtwtfk`,
                                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/Xtwtfk/' + 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>
