<template>
	<div class="telslog-balckList-container layout-pd">
		<el-card shadow="never">
			<!-- 通用搜索 -->
			<el-form :model="queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
				<el-form-item label="时间" prop="time">
					<el-date-picker
						v-model="queryParams.time"
						type="daterange"
						range-separator="至"
						start-placeholder="开始时间"
						end-placeholder="结束时间"
						value-format="YYYY-MM-DD[T]HH:mm:ss.sss[Z]"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :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-card>
		<el-card shadow="never">
			<div class="flex-center-between mb20">
				<p class="table-title">信息列表</p>
				<div>
					<el-button type="primary" v-waves @click="onImportTable" :disabled="!state.multipleSelection.length">
						<SvgIcon name="iconfont icon-daochu" class="mr5" />导出
					</el-button>
				</div>
			</div>
			<!-- 表格 -->
			<el-table :data="tableData" 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="phoneNo" label="电话号码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phoneNo" label="坐席" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phoneNo" label="累计签入(小时)" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phoneNo" label="累计小休(小时)" show-overflow-tooltip></el-table-column>
				<el-table-column prop="phoneNo" label="累计通话时常(小时)" show-overflow-tooltip></el-table-column>
				<el-table-column label="明细" width="100" fixed="right" align="center">
					<template #default="scope">
						<el-button text type="primary" @click="onDetail(scope.row)" v-auth="'200202'" title="查看明细">
							<SvgIcon name="ele-View" size="var(--hotline-table-icon-font-size)" />
						</el-button>
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination :total="total" v-model:page="queryParams.PageIndex" v-model:limit="queryParams.PageSize" @pagination="getList" />
		</el-card>

		<OperationRecord ref="operationRecordRef" />
	</div>
</template>

<script lang="ts" setup name="telsLog">
import { defineAsyncComponent, ref, reactive, toRefs, onMounted } from 'vue';
import type { FormInstance } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { blacklistPaged } from '/@/api/tels/blacklist';
import { throttle } from '/@/utils/tools';
import table2excel from 'js-table2excel';
// 引入组件
const OperationRecord = defineAsyncComponent(() => import('/@/views/tels/telsLog/component/operationRecord.vue'));

// 定义接口来定义对象的类型
interface QueryState {
	queryParams: {
		PageIndex: number;
		PageSize: number;
		time: Array<any>;
	};
	loading: boolean;
	total: number;
	tableData: Array<any>;
	multipleSelection: Array<any>;
}

// 定义变量内容
const state = reactive<QueryState>({
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		time: [],
	},
	loading: false,
	total: 0,
	tableData: [],
	multipleSelection: [],
});
const ruleFormRef = ref<FormInstance>();
const operationRecordRef = ref();
const { queryParams, loading, total, tableData } = toRefs(state);
/** 搜索按钮操作 节流操作 */
const handleQuery = throttle(() => {
	state.queryParams.PageIndex = 1;
	getList();
}, 1000);
/** 获取用户列表 */
const getList = () => {
	state.loading = true;
	blacklistPaged(state.queryParams)
		.then((response: any) => {
			state.tableData = response?.result.items;
			state.total = response?.result.total;
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 表格多选
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
}, 1000);
// 导出表格
const onImportTable = () => {
	const tabeHeader = [
		{ key: 'phoneNo', colWidth: '', title: '电话号码', type: 'text', isCheck: true },
		{ key: 'duration', colWidth: '', title: '黑名单时长(秒)', type: 'text', isCheck: true },
		{ key: 'creationTime', colWidth: '', title: '创建时间', type: 'text', isCheck: true },
	];
	table2excel(tabeHeader, state.multipleSelection, `${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`);
};
// 查看坐席话务明细
const onDetail = (row: any) => {
	operationRecordRef.value.openDialog(row);
};
onMounted(() => {
	getList();
});
</script>
