<template>
	<div class="telslog-callRecord-container layout-pd">
		<el-card shadow="never">
			<el-tabs v-model="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="'200101'"></el-tab-pane>
				<el-form :model="queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
					<el-form-item label="呼叫方向" prop="Direction" v-if="activeName === 'missed'">
						<el-select v-model="queryParams.Direction" placeholder="请选择呼叫方向" class="w100">
							<el-option v-for="item in 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="queryParams.CallState" placeholder="请选择接通状态" class="w100">
							<el-option v-for="item in 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="queryParams.PhoneNum" placeholder="请输入被叫号码" clearable @keyup.enter="getList(activeName)" />
					</el-form-item>
					<el-form-item label="主叫号码" prop="ToNum">
						<el-input v-model="queryParams.ToNum" placeholder="请输入主叫号码" clearable @keyup.enter="getList(activeName)" />
					</el-form-item>
					<el-form-item label="坐席" prop="UserName">
						<el-input v-model="queryParams.UserName" placeholder="请输入坐席姓名或工号" clearable @keyup.enter="getList(activeName)" />
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="getList(activeName)" :loading="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>
					<el-button type="primary" :disabled="!state.multipleSelection.length" v-waves @click="onImportTable">
						<SvgIcon name="iconfont icon-daochu" class="mr5" />导出
					</el-button>
				</div>
			</div>
			<!-- 表格 -->
			<el-table :data="tableList" v-loading="loading" row-key="id" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" :reserve-selection="true" />
				<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="activeName === 'incoming'">
					<el-table-column label="工单" show-overflow-tooltip>
						<template #default="scope">
							<el-button text type="primary">
								{{ scope.row.toNo }}
							</el-button>
						</template>
					</el-table-column>

					<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="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></el-table-column>
					<el-table-column label="操作" width="120" fixed="right" align="center">
						<template #default="scope">
							<el-button text type="primary" @click="onPalySoundRecording(scope.row)" v-auth="'200202'" title="播放录音">
								<SvgIcon name="ele-VideoPlay" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button text type="success" @click="onDownload(scope.row)" v-auth="'200202'" title="下载录音">
								<SvgIcon name="ele-Download" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button text type="warning" @click="onRelationWorkOrder(scope.row)" 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="activeName === 'exhale'">
					<el-table-column label="工单" show-overflow-tooltip>
						<template #default="scope">
							<el-button text type="primary">
								{{ scope.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="120" fixed="right" align="center">
						<template #default="scope">
							<el-button text type="primary" @click="onPalySoundRecording(scope.row)" title="播放录音">
								<SvgIcon name="ele-VideoPlay" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button text type="success" @click="onDownload(scope.row)" title="下载录音">
								<SvgIcon name="ele-Download" size="var(--hotline-table-icon-font-size)" />
							</el-button>
							<el-button text type="warning" @click="onRelationWorkOrder(scope.row)" title="关联工单">
								<SvgIcon name="ele-Connection" size="var(--hotline-table-icon-font-size)" />
							</el-button>
						</template>
					</el-table-column>
				</template>
				<!-- 未接 -->
				<template v-else-if="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="total" v-model:page="queryParams.PageIndex" v-model:limit="queryParams.PageSize" @pagination="getList(activeName)" />
		</el-card>
		<PlayRecording ref="playRecordingRef" />
	</div>
</template>

<script lang="ts" setup name="callRecord">
import { ref, reactive, toRefs, onMounted, defineAsyncComponent } from 'vue';
import type { FormInstance } from 'element-plus';
import { ElMessage } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import table2excel from 'js-table2excel';
import { storeToRefs } from 'pinia';
import { useThemeConfig } from '/@/stores/themeConfig';
// 引入节流
import { throttle } from '/@/utils/tools';
import { callPaged, callPagedMissed } from '/@/api/tels/callRecord';

// 引入组件
const PlayRecording = defineAsyncComponent(() => import('/@/views/tels/callRecord/component/PlayRecording.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;
	multipleSelection: Array<any>;
	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,
	multipleSelection: [],
	stateList: [
		{
			label: '全部',
			value: '',
		},
		{
			label: '已接通',
			value: 0,
		},
		{
			label: '未接通',
			value: 1,
		},
	],
	directionList: [
		{
			label: '全部',
			value: '',
		},
		{
			label: '呼入',
			value: 0,
		},
		{
			label: '呼出',
			value: 1,
		},
	],
});
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const { queryParams, tableList, loading, total, activeName, directionList, stateList } = toRefs(state);
const ruleFormRef = ref<FormInstance>();
const playRecordingRef = ref();
/** 获取用户列表 */
const getList = (val: string) => {
	queryParams.value.PageIndex = 1;
	state.loading = true;
	switch (val) {
		case 'incoming':
			callPaged(queryParams.value)
				.then((response: any) => {
					state.tableList = response?.result.items ?? [];
					state.total = response?.result.total;
					state.loading = false;
				})
				.catch(() => {
					state.loading = false;
				});
			break;
		case 'exhale':
			callPaged(queryParams.value)
				.then((response: any) => {
					state.tableList = response?.result.items ?? [];
					state.total = response?.result.total;
					state.loading = false;
				})
				.catch(() => {
					state.loading = false;
				});
			break;
		case 'missed':
			callPagedMissed(queryParams.value)
				.then((response: any) => {
					state.tableList = response?.result.items ?? [];
					state.total = response?.result.total;
					state.loading = false;
				})
				.catch(() => {
					state.loading = false;
				});
			break;

		default:
			break;
	}
};
// 多选表格
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	getList(state.activeName);
}, 1000);
// 切换tab 查询列表
const handleClick = (val: string) => {
	getList(val);
};
// 导出表格
const onImportTable = () => {
	const tabeHeader = [
		{ key: 'userName', colWidth: '', title: '用户名称', type: 'text', isCheck: true },
		{ key: 'fromNo', colWidth: '', title: '主叫号码', type: 'text', isCheck: true },
		{ key: 'toNo', colWidth: '', title: '被叫号码', type: 'text', isCheck: true },
		{ key: 'callTypeText', colWidth: '', title: '通话类型', type: 'text', isCheck: true },
		{ key: 'callStatusText', colWidth: '', title: '通话状态', type: 'text', isCheck: true },
		{ key: 'callDirectionText', colWidth: '', title: '呼叫方向', type: 'text', isCheck: true },
		{ key: 'endByText', colWidth: '', title: '通话结束方', type: 'text', isCheck: true },
		{ key: 'phoneIspText', colWidth: '', title: '运营商', type: 'text', isCheck: true },
		{ key: 'ringOffTypeText', colWidth: '', title: '挂断类型', type: 'text', isCheck: true },
		{ key: 'attribution', colWidth: '', title: '归属地', type: 'text', isCheck: true },
		// { key: 'creationTime', colWidth: '', title: '创建时间', type: 'text', isCheck: true },
		// { key: 'image', colWidth: '', width: '70', height: '40', title: '图片描述', type: 'image', isCheck: true },
	];
	table2excel(tabeHeader, state.multipleSelection, `${themeConfig.value.globalTitle} ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`);
};
// 播放录音
const onPalySoundRecording = (val: any) => {
	playRecordingRef.value.openDialog(val);
};
// 下载录音
const onDownload = (val: any) => {};
// 关联工单
const onRelationWorkOrder = (val: any) => {};
onMounted(() => {
	getList(state.activeName);
});
</script>
<style lang="scss" scoped></style>
