<template>
	<div class="tels-callRecord-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="state.activeName" @tab-change="handleClick">
				<el-tab-pane label="呼入列表" name="incoming"></el-tab-pane>
				<el-tab-pane label="呼出列表" name="exhale"></el-tab-pane>
				<el-tab-pane label="未接列表" name="missed" v-auth="'tels:callRecord:missed'"></el-tab-pane>
				<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
					<el-form-item label="呼叫方向" prop="Direction" v-if="state.activeName === 'missed'">
						<el-select v-model="state.queryParams.Direction" placeholder="请选择呼叫方向" class="w100">
							<el-option v-for="item in state.directionList" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
					</el-form-item>
					<el-form-item label="接通状态" prop="CallState" v-else>
						<el-select v-model="state.queryParams.CallState" placeholder="请选择接通状态" class="w100">
							<el-option v-for="item in state.stateList" :key="item.value" :label="item.label" :value="item.value" />
						</el-select>
					</el-form-item>
					<el-form-item label="被叫号码" prop="PhoneNum">
						<el-input v-model="state.queryParams.PhoneNum" placeholder="请输入被叫号码" clearable @keyup.enter="queryList" />
					</el-form-item>
					<el-form-item label="主叫号码" prop="ToNum">
						<el-input v-model="state.queryParams.ToNum" placeholder="请输入主叫号码" clearable @keyup.enter="queryList" />
					</el-form-item>
					<el-form-item label="坐席" prop="UserName">
						<el-input v-model="state.queryParams.UserName" placeholder="请输入坐席姓名或工号" clearable @keyup.enter="queryList" />
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="queryList" :loading="state.loading" v-waves> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
						<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button"> <SvgIcon name="ele-Refresh" class="mr5" />重置 </el-button>
					</el-form-item>
				</el-form>
			</el-tabs>
		</el-card>
		<el-card shadow="never">
			<div class="flex-center-between mb20">
				<p class="table-title">信息列表</p>
				<div></div>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableList" v-loading="state.loading" row-key="id">
				<el-table-column type="index" width="60" label="序号" />
				<el-table-column prop="fromNo" label="主叫号码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="toNo" label="被叫号码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="toNo" label="呼叫时间" show-overflow-tooltip></el-table-column>
				<!-- 呼入 -->
				<template v-if="state.activeName === 'incoming'">
					<el-table-column label="工单" show-overflow-tooltip>
						<template #default="{ row }">
							<el-button link type="primary">
								{{ row.toNo }}
							</el-button>
						</template>
					</el-table-column>

					<el-table-column prop="toNo" label="进入IVR时间" show-overflow-tooltip width="170"></el-table-column>
					<el-table-column prop="toNo" label="进入队列时间" show-overflow-tooltip width="170"></el-table-column>
					<el-table-column prop="toNo" label="振铃时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="接听时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="挂断时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="坐席" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="挂断方" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="通话时长" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="等待时长" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="通话保持时长" show-overflow-tooltip width="170"></el-table-column>
					<el-table-column prop="ringOffTypeText" label="挂断原因" show-overflow-tooltip></el-table-column>
					<el-table-column label="操作" width="120" fixed="right" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onPlaySoundRecording(row)" v-auth="'200202'" title="播放录音">
								<SvgIcon name="ele-VideoPlay" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button link type="success" @click="onDownload" v-auth="'200202'" title="下载录音">
								<SvgIcon name="ele-Download" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button link type="warning" @click="onRelationWorkOrder" v-auth="'200202'" title="关联工单">
								<SvgIcon name="ele-Connection" size="var(--hotline-table-icon-font-size)" />
							</el-button>
						</template>
					</el-table-column>
				</template>
				<!-- 呼出 -->
				<template v-else-if="state.activeName === 'exhale'">
					<el-table-column label="工单" show-overflow-tooltip>
						<template #default="{ row }">
							<el-button link type="primary">
								{{ row.toNo }}
							</el-button>
						</template>
					</el-table-column>
					<el-table-column prop="toNo" label="振铃时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="接听时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="挂断时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="挂断方" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="等待时长" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="通话保持时长" show-overflow-tooltip></el-table-column>
					<el-table-column label="操作" width="200" fixed="right" align="center">
						<template #default="{ row }">
							<el-button link type="primary" @click="onPlaySoundRecording(row)" title="播放录音"> 播放录音 </el-button>
							<el-button link type="success" @click="onDownload" title="下载录音"> 下载录音 </el-button>
							<el-button link type="warning" @click="onRelationWorkOrder" title="关联工单"> 关联工单 </el-button>
						</template>
					</el-table-column>
				</template>
				<!-- 未接 -->
				<template v-else-if="state.activeName === 'missed'">
					<el-table-column prop="toNo" label="进入IVR时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="进入队列时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="振铃时间" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="等待时长" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="坐席" show-overflow-tooltip></el-table-column>
					<el-table-column prop="toNo" label="挂断方" show-overflow-tooltip></el-table-column>
					<el-table-column prop="ringOffTypeText" label="未接原因" show-overflow-tooltip fixed="right" align="center"></el-table-column>
				</template>

				<!-- <el-table-column prop="callTypeText" label="通话类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="callStatusText" label="通话状态" show-overflow-tooltip></el-table-column>
				<el-table-column prop="callDirectionText" label="呼叫方向" show-overflow-tooltip></el-table-column>
				<el-table-column prop="endByText" label="通话结束方" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phoneIspText" label="运营商" show-overflow-tooltip></el-table-column>
				<el-table-column prop="ringOffTypeText" label="挂断类型" show-overflow-tooltip></el-table-column>
				<el-table-column prop="attribution" label="归属地" show-overflow-tooltip></el-table-column> -->

				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.PageIndex"
				v-model:limit="state.queryParams.PageSize"
				@pagination="queryList"
			/>
		</el-card>
		<play-record ref="playRecordRef" />
	</div>
</template>

<script lang="ts" setup name="callRecord">
import { ref, reactive, onMounted, defineAsyncComponent } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';
import { auth } from '/@/utils/authFunction';
// 引入节流
import { throttle } from '/@/utils/tools';
import { callPaged, callPagedMissed } from '/@/api/tels/callRecord';

// 引入组件
const PlayRecord = defineAsyncComponent(() => import('/@/views/tels/callRecord/component/Play-record.vue'));

// 定义接口来定义对象的类型
interface QueryState {
	activeName: string;
	queryParams: {
		Direction: string; //呼叫方向
		CallState: string; //接通状态
		PhoneNum?: string; // 被叫号码
		ToNum: string; // 主叫号码
		PageIndex: number;
		PageSize: number;
		UserName: string; //坐席
	};
	tableList: Array<any>;
	loading: boolean;
	total: number;
	directionList: Array<any>;
	stateList: Array<any>;
}

// 定义变量内容
const state = reactive(<QueryState>{
	activeName: 'incoming',
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		PhoneNum: '',
		Direction: '',
		ToNum: '',
		CallState: '',
		UserName: '',
	},
	tableList: [],
	loading: false,
	total: 0,
	stateList: [
		{
			label: '全部',
			value: '',
		},
		{
			label: '已接通',
			value: 0,
		},
		{
			label: '未接通',
			value: 1,
		},
	],
	directionList: [
		{
			label: '全部',
			value: '',
		},
		{
			label: '呼入',
			value: 0,
		},
		{
			label: '呼出',
			value: 1,
		},
	],
});
const ruleFormRef = ref<FormInstance>();
const playRecordRef = ref<RefType>();
/** 通话记录列表 */
const queryList = throttle(() => {
	if (!auth('tels:blackList:query')) ElMessage.error('抱歉，您没有权限查询通话记录！');
	else {
		state.loading = true;
		switch (state.activeName) {
			case 'incoming':
				callPaged(state.queryParams)
					.then((response: any) => {
						state.tableList = response?.result.items ?? [];
						state.total = response?.result.total;
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
				break;
			case 'exhale':
				callPaged(state.queryParams)
					.then((response: any) => {
						state.tableList = response?.result.items ?? [];
						state.total = response?.result.total;
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
				break;
			case 'missed':
				callPagedMissed(state.queryParams)
					.then((response: any) => {
						state.tableList = response?.result.items ?? [];
						state.total = response?.result.total;
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
				break;

			default:
				break;
		}
	}
}, 500);
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	queryList();
}, 500);
// 切换tab 查询列表
const handleClick = () => {
	queryList();
};
// 播放录音
const onPlaySoundRecording = (val: any) => {
  playRecordRef.value.openDialog(val);
};
// 下载录音
const onDownload = () => {};
// 关联工单
const onRelationWorkOrder = () => {};
onMounted(() => {
	queryList();
});
</script>
