import { ElMessage, ElNotification } from 'element-plus';
import router from '/@/router';
import { storeToRefs } from 'pinia';
import { WebsocketInterface } from './websocket';
import { useTelStatus,TelStates, RestStates } from '/@/stores/telStatus';
import { useAppConfig } from '/@/stores/appConfig';
import { telRestAdd, telRestProcess, telUnrestWex, restFlowDel } from '/@/api/public/wex';
import mittBus from '/@/utils/mitt';
const useTelStatusStore = useTelStatus();
const { telStatusInfo } = storeToRefs(useTelStatusStore);
const appConfigStore = useAppConfig();
const { AppConfigInfo } = storeToRefs(appConfigStore);
//定义当前登录的用户，在jquery的$(function(){})中对下面参数赋值， 全局变量，取值在PhoneScript.js中处理的，
const SystemAttr = {
	CurrentUser: { DepartmentID: '', GongHao: '', FenJi: '', AgentGroupName: ''},
};
// 小休原因
let restReason: string = '';
/**
 * @description 音频接口对象
 */
export const VoiceInterfaceObject: any = {
	// 设置全局变量
	SetSendModel: function (params: any) {
		SystemAttr.CurrentUser.DepartmentID = params.DepartmentID || ''; // 部门ID
		SystemAttr.CurrentUser.GongHao = params.GongHao || ''; // 工号
		SystemAttr.CurrentUser.FenJi = params.FenJi || ''; // 分机
		SystemAttr.CurrentUser.AgentGroupName = params.AgentGroupName || ''; // 坐席组
	},
	/*
	 * @@@@全局变量
	 *
	 * 当前实体对象
	 * 下行JSON格式：{ "Action": "", "GongHao": "", "FenJi": "", "PlatFormCode": "", "Params": "" }
	 * 上行JSON格式：{ "Action": "", "GongHao": "", "FenJi": "", "PlatFormCode": "", "Params": "", "Message": "" }
	 */
	GetSendModel: function (action: string, params?: string) {
		let p_action = action || '';
		let p_params = params || '';
		return {
			Action: p_action,
			GongHao: SystemAttr.CurrentUser.GongHao,
			FenJi: SystemAttr.CurrentUser.FenJi,
			PlatFormCode: SystemAttr.CurrentUser.DepartmentID,
			Params: p_params,
		};
	},
	/*
	 * @@@@事件触发
	 *
	 * 当前实体对象
	 * 下行JSON格式：{ "Action": "", "GongHao": "", "FenJi": "", "PlatFormCode": "", "Params": "" }
	 * 上行JSON格式：{ "Action": "", "GongHao": "", "FenJi": "", "PlatFormCode": "", "Params": "", "Message": "" }
	 */
	//弹屏 回调
	Back_TelPhoneEvent: function (returnVal: { Message: string; Params: any }) {
		let DialArray:EmptyArrayType = returnVal.Message.split(',');
		let DialInfo: any = <EmptyObjectType>{};
		DialInfo.fromTel = DialArray[0]; //来电号码
		DialInfo.telGongHao = DialArray[1]; //来电工号
		DialInfo.telArea = DialArray[3]; //来电区域
		DialInfo.telGuid = DialArray[4]; //来电GUID
		DialInfo.telIVR = DialArray[5]; //来电IVR名称
		DialInfo.transfer = DialArray[6]; //转接来源 中继号码
		DialInfo.telType = returnVal.Params; // 0来电 1外呼 2转接

		console.info(returnVal.Message, DialInfo,DialArray, '来电弹单');

		useTelStatusStore.setCallInfo({
			telGongHao: DialInfo.telGongHao,
			fromTel: DialInfo.fromTel,
			telArea: DialInfo.telArea,
			telGuid: DialInfo.telGuid,
			telIVR: DialInfo.telIVR,
			telType: DialInfo.telType,
			transfer: DialInfo.transfer,
		});
		// 设置电话状态 振铃中
		useTelStatusStore.setPhoneControlState(TelStates.ring);

		if (DialInfo.telType === '0') {
			// 来电才展示弹屏
			// 跳转到录入工单页面
			router.push({
				name: 'orderAccept',
				state: {
					createBy: 'tel',
					fromTel: DialInfo.fromTel,
					telGuid: DialInfo.telGuid,
					transfer: DialInfo.transfer,
					telArea: DialInfo.telArea,
				},
				params:{
					callId: DialInfo.telGuid,
					tagsViewName:'工单受理',
				}
			})
		}
	},
	//弹屏 回调
	// Back_RegNumberState: function (returnVal: any) {
	//     //Tel,GongHao,URL,Area,Guid,IVR,Remark
	//     console.log(returnVal);
	// },
	//心跳 回调
	Back_keeplive: function () {
		// console.log(returnVal);
	},
	//登录
	Login: function () {
		const modelJson = this.GetSendModel('Login');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//登录回调
	Back_Login: function (returnVal: { Params: string }) {
		if (returnVal.Params == '0') {
			WebsocketInterface('KeepAlive');
			// 设置分机号和坐席组
			useTelStatusStore.setCallInfo({ telsNo: SystemAttr.CurrentUser.FenJi, groupName: SystemAttr.CurrentUser.AgentGroupName });
			// 设置签入状态
			useTelStatusStore.setDutyState(true);
			// 设置电话状态
			useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
			this.SetGroupRole(); // 设置组权限
			this.SetRecordRole(); // 设置录音权限
			this.SetCallRole(); // 设置呼叫权限
			this.EndTalkingDeal(); // 结束通话处理
			ElMessage.success('登录语音系统成功！');
		} else {
			ElMessage.error('登录语音系统失败！');
		}
	},
	//设置用户权限
	SetGroupRole: function () {
		const modelJson = this.GetSendModel('SetGroupRole', SystemAttr.CurrentUser.AgentGroupName);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//设置用户权限 回调
	Back_SetGroupRole: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			// ElMessage.success(returnVal.Message)
		} else {
			ElMessage.error(returnVal.Message);
		}
	},

	// 设置录音权限
	SetRecordRole: function () {
		const modelJson = this.GetSendModel('SetRecRole', '0');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	// 设置录音权限 回调
	Back_SetRecRole: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			// ElMessage.success(returnVal.Message)
		} else {
			ElMessage.error(returnVal.Message);
		}
	},
	// 设置呼叫权限
	SetCallRole: function () {
		const modelJson = this.GetSendModel('SetDialRole', '2');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	// 设置呼叫权限 回调
	Back_SetDialRole: function (returnVal: { Params: string; Message: any }) {
		console.log('设置呼叫权限回调', returnVal);
		if (returnVal.Params == '0') {
			// ElMessage.success(returnVal.Message)
		} else {
			ElMessage.error(returnVal.Message);
		}
	},
	//登出 当前座席
	LogOut: function () {
		const modelJson = this.GetSendModel('LogOut');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		ElMessage.success('语音系统退出成功！');
		// 关闭websocket
		WebsocketInterface('Close');
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//强制登出
	F_LogOut: function (gongHao: any) {
		const modelJson = { Action: 'LogOut', GongHao: gongHao, FenJi: '', PlatFormCode: SystemAttr.CurrentUser.DepartmentID, Params: '' };
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//登出回调
	Back_LogOut: function (returnVal: any) {
		console.info(returnVal);
	},
	//退出事件
	Back_NoLogin: function (returnVal: any) {
		ElMessage.error(returnVal.Message);
		console.info(returnVal.Message);
	},
	//示忙当前座席
	SetBusy: function (reason: string) {
		const modelJson = this.GetSendModel('SetBusy');
		restReason = reason || '';
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//强制示忙座席
	F_SetBusy: function (gongHao: any) {
		const modelJson = { Action: 'SetBusy', GongHao: gongHao, FenJi: '', PlatFormCode: SystemAttr.CurrentUser.DepartmentID, Params: '' };
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//示忙回调
	Back_SetBusy: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			if (AppConfigInfo.value.IsRestApproval) {
				// 如果小休需要审核
				telRestProcess()
					.then((res: any) => {
						console.log('小休申请成功', res);
						// 设置电话状态小休中
						useTelStatusStore.setPhoneControlState(TelStates.rest);
						useTelStatusStore.setRest(RestStates.resting);
						ElMessage.success('小休开始！');
					})
					.catch((err: any) => {
						console.log('小休申请失败', err);
						restFlowDel().then(() => {
							// 删除小休流程
						});
						this.SetIdle(); // 调用失败示闲
					});
			} else {
				// ElMessage.success('小休开始！');
				// 设置电话状态小休中
				useTelStatusStore.setPhoneControlState(TelStates.rest);
				useTelStatusStore.setRest(RestStates.resting);
				// 添加小休记录
				telRestAdd({ reason: restReason })
					.then((res: any) => {
						console.log('小休记录添加成功 开始休息', res);
						ElMessage.success('小休开始！');
					})
					.catch((err: any) => {
						console.log('小休记录添加失败 开始休息', err);
						this.SetIdle(); // 调用失败示闲
					});
			}
		} else {
			console.info(returnVal.Message);
			ElMessage.error(returnVal.Message);
		}
	},
	//示闲当前座席
	SetIdle: function () {
		const modelJson = this.GetSendModel('SetIdle');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//强制示闲座席
	F_SetIdle: function (gongHao: any) {
		const modelJson = { Action: 'SetIdle', GongHao: gongHao, FenJi: '', PlatFormCode: SystemAttr.CurrentUser.DepartmentID, Params: '' };
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//示闲回调
	Back_SetIdle: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			// 设置休息状态 设置未正常状态
			useTelStatusStore.setRest(RestStates.unRest);
			// 设置话机状态 结束休息改为签入状态
			useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
			telUnrestWex()
				.then((res: any) => {
					console.log('小休记录修改成功 结束休息', res);
					ElMessage.success('小休结束！');
				})
				.catch((err: any) => {
					console.log('小休记录修改失败 结束休息', err);
				});
		} else {
			console.info(returnVal.Message);
			ElMessage.error(returnVal.Message);
		}
	},
	//保持
	KeepInTouch: function () {
		const modelJson = this.GetSendModel('KeepInTouch');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//保持回调
	Back_KeepInTouch: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			// 设置电话状态
			useTelStatusStore.setHold(true);
			// 设置电话状态
			useTelStatusStore.setPhoneControlState(TelStates.onHold);
			ElMessage('开始保持');
		} else {
			console.info(returnVal.Message);
			ElMessage.error(returnVal.Message);
		}
	},
	//取消保持
	KeepCancelInTouch: function () {
		const modelJson = this.GetSendModel('KeepCancelInTouch');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//取消保持回调
	Back_KeepCancelInTouch: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			// 设置电话状态  取消单个保持为通话中
			useTelStatusStore.setHold(false);
			// 设置电话状态
			useTelStatusStore.setPhoneControlState(TelStates.onCall);
			ElMessage('取消保持');
		} else {
			console.info(returnVal.Message);
			ElMessage.error(returnVal.Message);
		}
	},
	//开启事后
	TalkedDealBegin: function () {
		const modelJson = this.GetSendModel('KeepCancelInTouch');
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//开启事后 回调
	Back_TalkedDealBegin: function () {},
	//结束事后
	TalkedDealEnd: function () {},
	//结束事后 回调
	Back_TalkedDealEnd: function () {},
	//语音呼叫
	DialOut: function (phone: any) {
		const modelJson = this.GetSendModel('DialOut', phone);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
			ElMessage.error(WebsocketInterface('GetError', returnVal));
		}
	},
	// 语音呼叫 回调
	Back_DialOut: function (returnVal: any) {
		if (returnVal.Params == '0') {
			ElMessage.success(returnVal.Message);
		} else {
			ElMessage.error(returnVal.Message);
		}
	},
	// 语音呼叫 回调
	Back_dialOut: function (returnVal: any) {
		console.log(returnVal);
		if (returnVal.Params !== '0') {
			ElMessage.error(returnVal.Message);
		}
	},
	//视频呼叫
	VideoCall: function (phone: any) {
		const modelJson = this.GetSendModel('VideoCall', phone);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//挂断
	HangUp: function (phone?: any) {
		const modelJson = this.GetSendModel('HangUp', phone);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		} else {
			// closeWrVideo('videortmp');
		}
	},
	//挂断 回调
	Back_HangUp: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
			// 设置电话状态
			useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
			// 清空时间
			mittBus.emit('endTalkTime');
		} else {
			console.info(returnVal.Message);
			ElMessage.error(returnVal.Message);
		}
	},
	//强拆 挂断
	Rtmp: function (phone: any) {
		const modelJson = this.GetSendModel('Rtmp', phone);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));

		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},

	//自动外呼
	StartAutoDial: function (phone: any) {
		const modelJson = this.GetSendModel('StartAutoDial', phone);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));

		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},

	//通知公告
	Notice: function (tels: string, content: string) {
		const param = tels + '|' + content;
		const modelJson = this.GetSendModel('Notice', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},

	//外呼回调
	Back_Rtmp: function (returnVal: { Params: string; Rtsp: any; Message: any }) {
		if (returnVal.Params == '0') {
			console.log(returnVal);
			/*const videoid = $('#videortmp').prop('id');
			const suuid = parseInt(String(Math.random() * 100000)) + '' + new Date().getTime();
			registerWrVideo(suuid, videoid, returnVal.Rtsp);*/
			//registerWrVideo(returnVal.CallGuid,videoid,returnVal.Rtsp);
		} else {
			console.info(returnVal.Message);
		}
	},

	//强拆 挂断
	F_HangUp: function (gongHao: any) {
		const modelJson = { Action: 'HangUp', GongHao: gongHao, FenJi: '', PlatFormCode: SystemAttr.CurrentUser.DepartmentID, Params: '' };
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},

	//转接
	TeleSwitch: function (gongHao: any) {
		const modelJson = this.GetSendModel('TeleSwitch', gongHao);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//转接 回调
	Back_TeleSwitch: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message, '转接成功');
			ElMessage.success(returnVal.Message);

		} else {
			console.info(returnVal.Message);
			ElMessage.error(returnVal.Message);
		}
	},
	// 转接挂机通知事件
	Back_TransferTrunkTalkingEnd: function (returnVal: { Params: string; Message: any }) {
		console.log(returnVal, '转接挂断事件');
		// 重置通话信息
		useTelStatusStore.resetCallInfo();
		// 设置电话状态
		useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
		mittBus.emit('endTalkTime');
	},
	// 转接开始通知事件
	Back_BeginTransferTalking: function (returnVal: { Params: string; Message: any }) {
		console.log(returnVal, '转接开始事件');
		// 设置电话状态
		useTelStatusStore.setCallInfo({
			telArea: '', // 电话区号
			telGuid: '', // 电话guid
			telIVR: '', // 电话IVR
			telType: '', //来电 外呼 转接
			fromTel: '', // 来电号码
		});
		// 转接成功可以开启三方通话
		useTelStatusStore.setPhoneControlState(TelStates.onTransferSuccess);

		// mittBus.emit('endTalkTime');
	},
	//代接
	InsteadOfTele: function (gonghao: any) {
		const modelJson = this.GetSendModel('InsteadOfTele', gonghao);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//代接 回调
	Back_InsteadOfTele: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
		} else {
			console.info(returnVal.Message);
		}
	},
	//强插
	CancleChannel: function (gongHao: any) {
		const modelJson = this.GetSendModel('CancleChannel', gongHao);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//强插 回调
	Back_CancleChannel: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
		} else {
			console.info(returnVal.Message);
		}
	},
	//监听
	LinkTele: function (gongHao: any) {
		const modelJson = this.GetSendModel('LinkTele', gongHao);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//开启会议 音频
	StartMeeting: function (meetId: string, tels: string) {
		const param = meetId + '|' + tels;
		const modelJson = this.GetSendModel('StartMeeting', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	// 开启会议 音频 回调
	Back_StartMeeting: function (returnVal: any) {
		console.log(returnVal, '开启会议 音频 回调');
	},
	//开启会议 视频
	Back_OnMeeting: function (returnVal: any) {
		console.log(returnVal, '开启会议 音频 回调1');
	},
	//视频会议
	StartVideoMeeting: function (meetId: string, tels: string) {
		const param = meetId + '|' + tels;
		const modelJson = this.GetSendModel('StartVideoMeeting', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//开启会议 视频
	StartMeetingVideo: function (meetId: string, urls: string) {
		const param = meetId + '|' + urls;
		const modelJson = this.GetSendModel('StartMeetingVideo', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//视频推送
	GetMcuVideo: function (meetId: string, guid: string) {
		const param = meetId + '|' + guid;
		const modelJson = this.GetSendModel('GetMcuVideo', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//视频推送 回调
	Back_GetMcuVideo: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
		} else {
			console.info(returnVal.Message);
		}
	},
	//禁言
	ShutDownTalking: function (meetId: string, tel: string) {
		const param = meetId + '|' + tel;
		const modelJson = this.GetSendModel('ShutDownTalking', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//禁言 回调
	Back_ShutDownTalking: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
		} else {
			console.info(returnVal.Message);
		}
	},
	//踢人
	ConferGetOut: function (meetId: string, tel: string) {
		const param = meetId + '|' + tel;
		const modelJson = this.GetSendModel('ConferGetOut', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			console.info(WebsocketInterface('GetError', returnVal));
		}
	},
	//踢人 回调
	Back_ConferGetOut: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
		} else {
			console.info(returnVal.Message);
		}
	},
	//禁言所有
	ShutDownAll: function (meetId: string) {
		const param = meetId + '|' + SystemAttr.CurrentUser.GongHao;
		const modelJson = this.GetSendModel('ShutDownAll', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			ElMessage(WebsocketInterface('GetError', returnVal));
		}
	},
	//踢人 回调
	Back_ShutDownAll: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
		} else {
			console.info(returnVal.Message);
		}
	},
	//停止会议
	StopMeeting: function (meetId: any) {
		const modelJson = this.GetSendModel('StopMeeting', meetId);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		//if (returnVal != 1) {
		//    console.info(WebsocketInterface("GetError", returnVal));
		//}else{
		//    closeWrVideo("video1");
		//    closeWrVideo("video2");
		//    closeWrVideo("video3");
		//    closeWrVideo("video4");
		//    closeWrVideo("video5");
		//    closeWrVideo("video6");
		//    closeWrVideo("video7");
		//    closeWrVideo("video8");
		//    closeWrVideo("video9");
		//    $("video[name='meetvideo']").removeClass("play");
		//}
	},
	// 停止会议 回调
	Back_StopMeeting: function (returnVal: any) {
		console.log(returnVal);
	},
	//会议结束 回调
	Back_MeetingEnd: function (returnVal: any) {
		ElMessage('会议结束');
		console.log(returnVal);
	},
	//监听 回调
	Back_LinkTele: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
		} else {
			console.info(returnVal.Message);
		}
	},
	//分机注册状态回调
	Back_RegNumberState: function (returnVal: any) {

	},
	//通话挂机事件回调
	Back_TalkingEnd: async function (returnVal: { Params: string; Message: any }) {
		console.log()
		if (AppConfigInfo.value.IsAutoTalkingDeal && telStatusInfo.value.telType === '0') {
			// 事后处理(系统配置需要事后处理) 且是来电
			this.BeginTalkingDeal();
		}

		console.info(returnVal.Message, '结束通话 挂机'); //解析Message参数	有电话号码|电话唯一ID|录音文件名称
		// 重置通话信息
		useTelStatusStore.resetCallInfo();
		// 设置电话状态
		useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
		mittBus.emit('endTalkTime');
	},
	// 开启事后处理
	BeginTalkingDeal: function () {
		const param: string = '';
		const modelJson = this.GetSendModel('BeginTalkingDeal', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			ElMessage(WebsocketInterface('GetError', returnVal));
		}
	},
	// 开启事后处理 回调
	Back_BeginTalkingDeal: async function (returnVal: any) {
		try {
			const time: number = AppConfigInfo.value.TalkingDealTime * 1000; // 事后处理时间
			if (returnVal.Params == 0) {
				ElNotification({
					title: '自动开启事后处理成功',
					message: `${returnVal.Message}${AppConfigInfo.value.TalkingDealTime}秒后自动结束事后处理,或者手动结束事后处理`,
					type: 'success',
					duration: time,
				});
				// 设置事后处理
				useTelStatusStore.setTalkingDeal(true);
				// 设置话机状态 设置为事后处理中
				useTelStatusStore.setPhoneControlState(TelStates.onTalkingDeal);
				setTimeout(() => {
					this.EndTalkingDeal(); //自动结束事后，也可以手动调用该方法提前结束事后
				}, 10000);
			}
		} catch {
			console.log('事后处理失败');
		}
	},
	// 结束事后处理
	EndTalkingDeal: function () {
		const param: string = '';
		const modelJson = this.GetSendModel('EndTalkingDeal', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			ElMessage(WebsocketInterface('GetError', returnVal));
		}
	},
	// 结束事后处理 回调
	Back_EndTalkingDeal: function (returnVal: { Params: string; Message: any }) {
		if (returnVal.Params == '0') {
			console.info(returnVal.Message);
			ElMessage(returnVal.Message);
			// 设置事后处理
			useTelStatusStore.setTalkingDeal(false);
			// 设置话机状态 取消事后处理修改为空闲状态
			useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
		} else {
			console.info(returnVal.Message);
		}
	},
	//振铃结束
	Back_UserRingEnd: function (returnVal: any) {
		const DialArray = returnVal.Message.split('|');
		const telNum = DialArray[0]; //来电号码
		const telGuid = DialArray[1]; //来电GUID
		const telVoiceName = DialArray[2]; // 录音文件名

		console.log(telNum, telGuid, telVoiceName);
		// 重置当前通话信息
		useTelStatusStore.resetCallInfo();
		// 设置电话状态
		useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
		mittBus.emit('endTalkTime');
	},
	//开始通话事件
	Back_BeginTalking: function (returnVal: { Params: string; Message: any }) {
		console.info(returnVal.Message, '开始通话'); //解析Message参数	有电话号码|电话唯一ID|录音文件名称
		mittBus.emit('startTalkTime', returnVal.Message);
		// 设置电话状态 通话中
		useTelStatusStore.setPhoneControlState(TelStates.onCall);
	},
	//获取视频流事件
	Back_RtmpVideoMeeting: function (returnVal: { Params: string; Rtsp: any; Tel: any; CallGuid: any }) {
		console.info('Back_RtmpVideoMeeting');
		console.log(returnVal);
		if (returnVal.Params == '1') {
			return;
		}
		/*const vid = $("video[name='meetvideo']:not('.play')").eq(0);
		const vedioId = vid.prop('id');
		const _td = vid.closest('td');
		//var vedioId=$("#video1").prop("id");
		const uuid = parseInt(String(Math.random() * 100000)) + '' + new Date().getTime();
		registerWrVideo(uuid, vedioId, returnVal.Rtsp);
		//registerWrVideo(returnVal.CallGuid, vedioId, returnVal.Rtsp);
		_td.find("input[name='meet_num']").val(returnVal.Tel);
		_td.find("input[name='meet_guid']").val(returnVal.CallGuid);
		vid.addClass('play');*/
	},
	Back_EndVideoTalking: function (returnVal: { Params: string; Tel: string }) {
		console.log(returnVal);
		if (returnVal.Params == '0') {
			/*const _td = $("input[name='meet_num'][value='" + returnVal.Tel + "']").closest('td');
			_td.find("input[name='meet_num']").val('');
			_td.find("input[name='meet_guid']").val('');
			_td.find('video').removeClass('play');
			closeWrVideo(_td.find('video').attr('id'));*/
		}
	},
	Back_StartVideoMeeting: function (returnVal: any) {
		console.log(returnVal);
	},

	Back_CloseRtmp: function (returnVal: any) {
		console.log(returnVal);
	},

	Back_VideoCall: function (returnVal: any) {
		console.log(returnVal);
	},

	Back_BeginVideoTalking: function (returnVal: any) {
		console.log(returnVal);
	},
	//排队信息推送
	Back_TelQuene: function (returnVal: any) {
		//  Params: "0" 表示正在排队 Params: "1" 表示排队结束
		let telQueen = []; // 排队信息
		telQueen.push(returnVal);
		telQueen = telQueen.filter((item: any) => item.Params === '0');
		console.log(telQueen, '21');
		if (telQueen.length > 0) {
			ElMessage({
				message: `当前排队人数：${telQueen.length}`,
				type: 'info',
			});
		}
	},
	// 获取座席状态
	GetOnUserState: function () {
		const param: string = '';
		const modelJson = this.GetSendModel('GetOnUserState', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			ElMessage(WebsocketInterface('GetError', returnVal));
		}
	},
	// 获取座席状态 回调
	Back_GetOnUserState: function (returnVal: any) {
		const stateArray = returnVal.Message.split('|');
		if(stateArray && stateArray.length){
			stateArray.forEach((item: any) => {
				if(item.includes(SystemAttr.CurrentUser.FenJi)){// 判断当前分机
					const userArray = item.split(',');
					if (userArray && userArray.length > 0) {
						const telStateArray = userArray[1].split(':');
						if (telStateArray && telStateArray.length == 2) {
							const telState = telStateArray[1];// 获取或当前分机状态
							const status = item.includes('Busy') ? '示忙' : '空闲';
							console.log('当前坐席状态:',telState,'空闲状态:',status);
							if(status === '示忙'){
								// 设置电话状态小休中
								useTelStatusStore.setPhoneControlState(TelStates.rest);
								useTelStatusStore.setRest(RestStates.resting);
							}else{
								// 设置话机状态
								switch (telState) {
									case '空闲':
										// 设置签入状态
										useTelStatusStore.setDutyState(true);
										// 设置电话状态
										useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
										break;
									case '等待对方响铃': // 呼出振铃
										// 设置电话状态 振铃中
										useTelStatusStore.setPhoneControlState(TelStates.ring);
										break;
									case '振铃中': // 呼入振铃
										// 设置电话状态 振铃中
										useTelStatusStore.setPhoneControlState(TelStates.ring);
										break;
									case '通话中':
										// 开始计时
										mittBus.emit('startTalkTime', returnVal.Message);
										// 设置电话状态 通话中
										useTelStatusStore.setPhoneControlState(TelStates.onCall);
										break;
									case "呼叫保持":
									case "保持中":
										// 开始计时
										mittBus.emit('startTalkTime', returnVal.Message);
										// 设置电话状态
										useTelStatusStore.setHold(true);
										// 设置电话状态
										useTelStatusStore.setPhoneControlState(TelStates.onHold);
										break;
									case "转接坐席振铃":
										// 设置电话状态 振铃中
										useTelStatusStore.setPhoneControlState(TelStates.ring);
										break;
									case "转接坐席通话":// 可以开启三方会议
										// 转接成功可以开启三方通话
										useTelStatusStore.setPhoneControlState(TelStates.onTransferSuccess);
										break;
									case "被转坐席振铃":
										// 设置电话状态 振铃中
										useTelStatusStore.setPhoneControlState(TelStates.ring);
										break;
									case "被转接坐席通话":// 相当于振铃中
										// 设置电话状态 振铃中
										useTelStatusStore.setPhoneControlState(TelStates.ring);
										break;
									default:
										// 设置签入状态
										useTelStatusStore.setDutyState(true);
										// 设置电话状态
										useTelStatusStore.setPhoneControlState(TelStates.dutyOn);
										break;
								}
							}
						}
					}
				}
			})
		}
	},
	//  通话DTMF 在通话过程中，需要给运营商或者对方方式按键信息，可用该接口
	Dtmf: function (param: string) {
		const modelJson = this.GetSendModel('Dtmf', param);
		const returnVal = WebsocketInterface('Send', JSON.stringify(modelJson));
		if (returnVal != 1) {
			ElMessage(WebsocketInterface('GetError', returnVal));
		}
	},
	// 通话DTMF 回调
	Back_Dtmf: function (returnVal: any) {
		console.log(returnVal,'通话DTMF 回调');
		// 设置三方会议状态
		useTelStatusStore.setMetTing(true);
		// 设置话机状态 设置为会议状态
		useTelStatusStore.setPhoneControlState(TelStates.onConference);
	},
	TransferRingEnd: function (param: string) {

	},
	// 转接响铃挂机事件回调
	Back_TransferRingEnd: function (returnVal: any) {
		// 设置电话状态 振铃中
		useTelStatusStore.setPhoneControlState(TelStates.ring);
	},
	// 转接挂机事件回调
	Back_TransferBHangUp: function (returnVal: any) {

	},
	// 转接通话事件回调
	Back_TransferTalking: function (returnVal: any) {
		// 开始计时
		mittBus.emit('startTalkTime', returnVal.Message);
		// 设置电话状态 通话中
		useTelStatusStore.setPhoneControlState(TelStates.onCall);
	}
};
