<template>
	<div>
		<!-- 回访满意度 -->

		<el-dialog v-model="props.dialogVisible" title="回 访"  width="30%" :before-close="closeModal" :modal="false" z-index="1000" >
			<span>
				
				<el-form ref="form" :rules="rules" :model="formData" label-width="90px" >
					<el-form-item label="满意度" prop="myd">
						<el-radio-group v-model="formData.myd">
							<el-radio label="非常满意" value="2"/>
							<el-radio label="满意" value="1"/>
							<el-radio label="不满意" value="0"/>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="回访结果" prop="visitresult">
						<el-input v-model="formData.visitresult"  type="textarea" />
					</el-form-item>
				</el-form>

			</span>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="closeModal">取 消</el-button>
					<el-button type="primary" @click="submit">确 认</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup> 
	import { ref, defineEmits, defineProps, getCurrentInstance , watch } from 'vue';
	import { getPhoneFix } from '@/api/main/main'
	import { Send } from '@/utils/telWebsocket'
	import useSocketStore from '@/store/modules/socket' 
    const { proxy } = getCurrentInstance()



	const rules = ref({
		myd: [
			{ required: true, message: '请选择满意度', trigger: 'blur' }, 
		],
		visitresult: [
			{ required: true, message: '请输入回访结果...', trigger: 'blur' }, 
		], 
	})

	const props = defineProps({
		dialogVisible : { 
			type: Boolean,
			default: false
		}, 
		callnum : {
			type : String,
			default: ''
		}
	})

	let form = ref('');

	let formData = ref({
		visitresult : '',
		myd : ''
	})
 
	const emit = defineEmits(['update:dialogVisible', 'submit'])


	function submit (){
		form.value.validate((bool)=>{
			if(bool){
				formData.value['callid'] = useSocketStore().incomingData.CallID
				emit('submit', formData.value);
				closeModal()
			}
		})
	}

	function closeModal (){
		formData.value = {
			visitresult : '',
			myd : ''
		}

		emit('update:dialogVisible' , false);
	}
	 
	watch(()=>props.dialogVisible , ( bool )=>{
		if(bool){
			getPhoneFix(props.callnum).then((data) => {
				if (data.state === 'success') {
					let params = {
						AgentId: useSocketStore().accountNumber,
						AgentExten: useSocketStore().extensionNumber,
						Type: 'MakeCall',
						DestinationNumber: data.data[0]
					}
					Send(params, (data) => { 
						 
						if (data.Type === 'MakeCall') { 
							if (data.Result) {
								proxy.$modal.msgSuccess('外呼成功')
							} else {
								proxy.$modal.msgSuccess('外呼失败')
							}
						}
					})
				}
			})
		}
	})
 

</script>

<style lang="scss" scoped>

</style>