<template>
	<div class="statistics-call-detail-index-call-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: queryList,
				}"
				:tools="[{ toolRender: { name: 'exportCurrent' } }, { toolRender: { name: 'exportAll' } }]"
			>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:column-config="{ resizable: true }"
					:row-config="{ isCurrent: true, isHover: true, height: 30,useKey: true }"
					ref="tableRef"
					height="auto"
					auto-resize
					show-overflow
					:scrollY="{ enabled: true, gt: 100 }"
					id="statisticsCallDetailIndexCall"
					:custom-config="{ storage: true }"
					:params="{ exportMethod: callDetailListDetailExport, exportParams: requestParams }"
					showHeaderOverflow
				>
					<vxe-column field="orderNo" title="工单编码" width="140"></vxe-column>
					<vxe-column field="title" title="工单标题" width="200">
						<template #default="{ row }">
							<order-detail :order="{ ...row, id: row.orderId }" @updateList="queryList">{{ row.orderTitle }}</order-detail>
						</template>
					</vxe-column>
					<vxe-column field="cpn" title="主叫号码" width="120"></vxe-column>
					<vxe-column field="cdpn" title="被叫号码" width="120"></vxe-column>
					<vxe-column field="telNo" title="响应分机" width="120"></vxe-column>
					<vxe-column field="endByText" title="挂断状态" width="120"></vxe-column>
					<vxe-column field="userName" title="话务员" width="120"></vxe-column>
					<vxe-column field="createdTime" title="开始时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.createdTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="answeredTime" title="接通时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.answeredTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="overTime" title="挂断时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.overTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column field="duration" title="通话时间(秒)" width="120"></vxe-column>
					<vxe-column title="操作" fixed="right" width="160" align="center" :show-overflow="false">
						<template #default="{ row }">
							<el-button type="primary" @click="onPlaySoundRecording(row)" title="播放录音" link v-if="row.recordingAbsolutePath">播放录音</el-button>
							<el-button link type="primary" @click="onDownload(row)" title="下载录音" v-if="row.recordingAbsolutePath">下载录音</el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
			<pagination
				@pagination="queryList"
				:total="state.total"
				v-model:current-page="state.queryParams.PageIndex"
				v-model:page-size="state.queryParams.PageSize"
				:disabled="state.loading"
			/>
		</div>
		<!-- 播放录音 -->
		<play-record ref="playRecordRef" />
	</div>
</template>
<script setup lang="tsx" name="statisticsCallDetailIndexCall">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessageBox } from 'element-plus';
import { callDetailListDetail, callDetailListDetailExport } from '@/api/statistics/call';
import { useRoute } from 'vue-router';
import { formatDate } from '@/utils/formatTime';
import { fileDownload } from '@/api/public/file';
import { downloadFileBySrc, downloadFileByStream } from '@/utils/tools';
import { useThemeConfig } from '@/stores/themeConfig';
import { storeToRefs } from 'pinia';
import { exportOrder } from '@/api/business/order';
import { useAppConfig } from '@/stores/appConfig';

const PlayRecord = defineAsyncComponent(() => import('@/components/PlayRecord/index.vue')); // 播放录音
const OrderDetail = defineAsyncComponent(() => import('@/components/OrderDetail/index.vue')); // 工单详情
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const state = reactive<any>({
	queryParams: {
		PageIndex: 1,
		PageSize: 20,
	},
	tableData: [], //表单
	loading: false, // 加载
	total: 0, // 总数
	callForwardingSource: [],
	totalCount: {},
});
/** 获取列表 */
const requestParams = ref<EmptyObjectType>({});
const route = useRoute();
const routeQueryParams = route.query;
const queryList = async () => {
	state.loading = true;
	try {
		requestParams.value = {
			StartTime: routeQueryParams.StartTime,
			EndTime: routeQueryParams.EndTime,
			TypeCode: routeQueryParams.TypeCode,
			pageIndex: state.queryParams.PageIndex,
			pageSize: state.queryParams.PageSize,
		};
		const { result } = await callDetailListDetail(requestParams.value);
		state.tableData = result?.items ?? [];
		state.tableData = state.tableData.map((item: any) => {
			return {
				id: item.orderId,
				...item,
			};
		});
		state.total = result.total ?? 0;
		state.loading = false;
	} catch (e) {
		state.loading = false;
		console.log(e);
	}
};
// 播放录音
const playRecordRef = ref<RefType>();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
const appConfigStore = useAppConfig();
const { AppConfigInfo } = storeToRefs(appConfigStore); // 系统配置信息
const onPlaySoundRecording = (val: any) => {
	playRecordRef.value.playRecord(val.otherAccept);
};
// 下载录音
const onDownload = (row: any) => {
	ElMessageBox.confirm(`您确定要下载此录音吗？`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			switch (themeConfig.value.appScope) {
				case 'YiBin':
					fileDownload({ path: AppConfigInfo.value.recordDownLoadPrefix + row.audioFile })
						.then((res: any) => {
							downloadFileByStream(res, row.audioFile);
						})
						.catch(() => {});
					break;
				case 'ZiGong':
				case 'LuZhou':
					downloadFileBySrc(AppConfigInfo.value.recordPrefix + row.audioFile, row.audioFile);
					// window.open(AppConfigInfo.value.recordPrefix + row.audioFile)
					break;
			}
		})
		.catch(() => {});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
