<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="level">
					<el-select v-model="queryParams.level" placeholder="请选择呼叫方向" class="w100">
						<el-option v-for="item in levelList" :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="handleQuery" />
				</el-form-item>
				<el-form-item label="状态" prop="status">
					<el-select v-model="queryParams.status" placeholder="请选择呼叫方向" class="w100">
						<el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
					</el-select>
				</el-form-item>
				<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" @click="onAddBacklist" v-waves v-auth="'200201'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
					<el-button type="primary" @click="onRemove" v-waves v-auth="'200202'" :disabled="!state.multipleSelection.length">
						<SvgIcon name="ele-Delete" class="mr5" />解除
					</el-button>
					<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 label="级别" show-overflow-tooltip width="170">
					<template #header>
						<p class="flex-center-align">
							级别
							<el-popover placement="top-start" :width="500" trigger="hover">
								<template #reference>
									<SvgIcon name="ele-QuestionFilled" color="var(--el-color-info)" class="ml5" />
								</template>
								<div>
									<p>呼入限制：接通后直接挂断</p>
									<p>坐席限制：可进入IVR，可使用语音导航功能以及智能机器人（如有），不支持转人工服务，语音导航中屏蔽转人工选项</p>
									<p>友情提示：接通后，播放礼貌性提示语后挂断（提示语在呼叫中心配置），提示语内容由业务设定</p>
								</div>
							</el-popover>
						</p>
					</template>
				</el-table-column>
				<el-table-column label="开始时间" show-overflow-tooltip width="170">
					<template #default="scope">
						<span>{{ formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="结束时间" show-overflow-tooltip width="170">
					<template #default="scope">
						<span>{{ formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</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="duration" label="黑名单时长(秒)" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip width="170" fixed="right" align="center">
					<template #default="scope">
						<span>{{ formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100" fixed="right" align="center">
					<template #default="scope">
						<el-button text type="primary" @click="onViewRecord(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>

		<AddBlacklist ref="addBlacklistRef" @updateList="handleQuery" />
		<OperationRecord ref="operationRecordRef" />
	</div>
</template>

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

// 定义接口来定义对象的类型
interface QueryState {
	queryParams: {
		PageIndex: number;
		PageSize: number;
		PhoneNum?: string;
		level: string; //级别
		status: string; //状态
		time: Array<any>;
	};
	loading: boolean;
	total: number;
	tableData: Array<any>;
	multipleSelection: Array<any>;
	levelList: Array<any>;
	statusList: Array<any>;
}

// 定义变量内容
const state = reactive<QueryState>({
	queryParams: {
		PageIndex: 1,
		PageSize: 10,
		PhoneNum: '',
		level: '',
		status: '',
		time: [],
	},
	loading: false,
	total: 0,
	tableData: [],
	multipleSelection: [],
	levelList: [
		{
			label: '全部',
			value: '',
		},
		{
			label: '呼入限制',
			value: 0,
		},
		{
			label: '坐席限制',
			value: 1,
		},
		{
			label: '友情提示',
			value: 1,
		},
	],
	statusList: [
		{
			label: '全部',
			value: '',
		},
		{
			label: '正常',
			value: 0,
		},
		{
			label: '解除',
			value: 1,
		},
	],
});
const ruleFormRef = ref<FormInstance>();
const addBlacklistRef = ref();
const operationRecordRef = ref();
const { queryParams, loading, total, tableData, levelList, statusList } = 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 onAddBacklist = () => {
	addBlacklistRef.value.openDialog();
};
// 导出表格
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 onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除：“${row.phoneNo}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			balcklistDelete(row.phoneNo).then(() => {
				ElMessage.success('删除成功');
				handleQuery();
			});
		})
		.catch(() => {});
};
// 解除黑名单
const onRemove = () => {
	ElMessageBox.confirm(`此操作将解除：“”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			// balcklistDelete(row.phoneNo).then(() => {
			// 	ElMessage.success('解除成功');
			// 	handleQuery();
			// });
		})
		.catch(() => {});
};
// 查看操作记录
const onViewRecord = (row: any) => {
	operationRecordRef.value.openDialog(row);
};
onMounted(() => {
	getList();
});
</script>
