<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="120px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="24">
						<el-form-item label="人员编号" prop="id" v-if="dataForm.id">
							<el-input v-model="dataForm.id" placeholder="-" disabled :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="姓名" prop="xm">
							<el-input v-model="dataForm.xm" placeholder="请输入" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="工号" prop="gh">
							<el-input v-model="dataForm.gh" placeholder="请输入工号" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="岗位/职责" prop="ylzd1">
							<el-select v-model="dataForm.ylzd1" 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="dh">
							<el-input v-model="dataForm.dh" placeholder="请输入" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="邮箱" prop="yx">
							<el-input v-model="dataForm.yx" placeholder="请输入" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="关联产品" prop="glcp">
							<el-select v-model="glcpValue" placeholder="请选择关联产品" clearable filterable :style='{"width":"100%"}' @change="handleGlcpChange">
								<el-option v-for="item in cpglOptions" :key="item.id" :label="(item.cpmc || '') + ' (' + (item.cpid || item.id) + ')'" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="部门" prop="ylzd2">
							<el-input v-model="dataForm.ylzd2" placeholder="请输入部门" clearable :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="所属人员" prop="ssry">
							<user-select v-model="dataForm.ssry" placeholder="请选择" clearable />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="备注" prop="bz">
							<el-input v-model="dataForm.bz" placeholder="请输入" show-word-limit type="textarea" :autosize='{"minRows":3,"maxRows":6}' :style='{"width":"100%"}' />
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="处理范围" prop="clfw">
							<el-select v-model="clfwValue" placeholder="请选择处理范围" multiple clearable :style='{"width":"100%"}' @change="handleClfwChange">
								<el-option label="设备问题" value="设备问题" />
								<el-option label="系统问题" value="系统问题" />
							</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,
				clfwValue: [], // 处理范围多选，提交时转为逗号分隔
				glcpValue: '', // 关联产品选择值（产品资料ID）
				cpglOptions: [], // 产品管理/产品资料选项
				dataForm: {
					id: '',
					xm: undefined,
					gh: undefined,
					ylzd1: undefined,
					dh: undefined,
					yx: undefined,
					glcp: undefined,
					ylzd2: undefined,
					ssry: undefined,
					bz: undefined,
					clfw: undefined,
				},
				rules: {},
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getCpglOptions()
		},
		mounted() {
        },
		methods: {
			goBack() {
                this.$emit('refresh')
            },
			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 => {
					this.cpglOptions = []
					return Promise.reject(err)
				})
			},
			handleGlcpChange(val) {
				this.dataForm.glcp = val || ''
			},
			handleClfwChange(val) {
				this.dataForm.clfw = val && val.length ? val.join(',') : '';
			},
			init(id, isDetail) {
				this.dataForm.id = id || '';
                this.visible = true;
                this.isDetail = isDetail || false;
				this.clfwValue = [];
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/Extend/Sbwhry/' + this.dataForm.id,
							method: 'get'
						}).then(res => {
							this.dataForm = res.data;
							this.clfwValue = (res.data.clfw && typeof res.data.clfw === 'string')
								? res.data.clfw.split(',').filter(Boolean)
								: [];
							this.glcpValue = this.dataForm.glcp || '';
						})
					} else {
						this.dataForm.gh = undefined;
						this.dataForm.glcp = undefined;
						this.dataForm.clfw = undefined;
						this.glcpValue = '';
					}
				})
			},
			dataFormSubmit() {
				this.dataForm.clfw = this.clfwValue && this.clfwValue.length ? this.clfwValue.join(',') : '';
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/Extend/Sbwhry`,
                                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/Sbwhry/' + 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>
