<template>
	<div class="tels-blacklist-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-tabs v-model="logType" @tab-change="queryList">
				<el-tab-pane name="1" label="黑名单" :disabled="state.loading"></el-tab-pane>
				<el-tab-pane name="2" label="白名单" :disabled="state.loading"></el-tab-pane>
			</el-tabs>
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item :label="logType === '1' ? '黑名单号码' : '白名单号码'" prop="Keyword">
					<el-input
						v-model="state.queryParams.Keyword"
						:placeholder="logType === '1' ? '黑名单号码' : '白名单号码'"
						clearable
						@keyup.enter="handleQuery"
						class="keyword-input"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading"> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
			<vxe-toolbar
				ref="toolbarRef"
				:loading="state.loading"
				custom
				:refresh="{
					queryMethod: handleQuery,
				}"
			>
				<template #buttons>
					<el-button type="primary" @click="onAddBlacklist" v-auth="'tels:blackList:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
					<el-button type="primary" @click="onActionLog"> 操作记录 </el-button>
				</template>
			</vxe-toolbar>
			<div style="overflow: hidden; width: 100%; height: 100%; flex: 1">
				<vxe-table
					border
					:loading="state.loading"
					:data="state.tableData"
					:sort-config="{ remote: true }"
					: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="blacklist"
					:custom-config="{ storage: true }"
				>
					<vxe-column field="startPhone" :title="logType === '1' ? '黑名单号码' : '白名单号码'" min-width="140"></vxe-column>
					<vxe-column field="validDateTime" title="有效期" width="160">
						<template #default="{ row }">
							{{ formatDate(row.validDateTime, 'YYYY-mm-dd HH:MM:SS') }}
						</template>
					</vxe-column>
					<vxe-column title="操作" fixed="right" width="90" align="center">
						<template #default="{ row }">
							<el-button link type="danger" @click="onRemove(row)" title="删除黑名单" v-auth="'tels:blackList:delete'"> 删除 </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>
		<!--  新增黑白名单  -->
		<blacklist-add ref="blacklistAddRef" @updateList="handleQuery" />
		<!--  操作记录  -->
		<action-log ref="actionLogRef" @updateList="handleQuery" />
	</div>
</template>

<script lang="tsx" setup name="blacklist">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage, ElNotification } from 'element-plus';
import type { FormInstance } from 'element-plus';
import { formatDate } from '@/utils/formatTime';
import { deleteBlackListXT, deleteWhiteListXT, getBlackListXT, getWhiteListXT, verifyBlackListLogXT } from '@/api/tels/blackList';

// 引入组件
const BlacklistAdd = defineAsyncComponent(() => import('@/views/tels/blacklist/components/Blacklist-add-xt.vue')); // 新增黑白名单
const pagination = defineAsyncComponent(() => import('@/components/ProTable/components/Pagination.vue')); // 分页
const ActionLog = defineAsyncComponent(() => import('@/views/tels/blacklist/components/Action-log.vue')); // 操作记录
// 定义变量内容
const state = reactive<any>({
	queryParams: {
		PageIndex: 1, // 当前页
		PageSize: 20, // 每页条数
		Keyword: null, // 电话号码
	},
	loading: false, // 加载状态
	total: 0, // 总条数
	tableData: [], // 表格数据
	multipleSelection: [], // 多选数据
});
const ruleFormRef = ref<FormInstance>(); // 表单ref
const logType = ref('1'); // 黑白名单类型
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	queryList();
};
/** 获取黑名单列表 */
const queryList = () => {
	state.loading = true;
	if (logType.value === '1') {
		// 黑名单
		getBlackListXT(state.queryParams)
			.then((response: any) => {
				state.tableData = response.result.items ?? [];
				state.total = response.total ?? 0;
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	} else {
		getWhiteListXT(state.queryParams)
			.then((response: any) => {
				state.tableData = response.result.items ?? [];
				state.total = response.total ?? 0;
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
};
/** 重置按钮操作 */
const resetQuery = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
};
// 打开新增黑名单弹窗
const blacklistAddRef = ref<RefType>();
const onAddBlacklist = () => {
	blacklistAddRef.value.openDialog(logType.value);
};
// 操作记录
const actionLogRef = ref<RefType>();
const onActionLog = () => {
	actionLogRef.value.openDialog(logType.value);
};
// 删除黑白名单
const onRemove = (row: any) => {
	ElMessageBox.confirm(`此操作将解除：【${row.startPhone}】的${logType.value === '1' ? '黑名单' : '白名单'}，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			state.loading = true;
			if (logType.value === '1') {
				// 黑名单
				deleteBlackListXT(row.startPhone)
					.then(() => {
						ElMessage.success('删除成功');
						queryList();
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
			} else {
				deleteWhiteListXT(row.startPhone)
					.then(() => {
						ElMessage.success('删除成功');
						queryList();
						state.loading = false;
					})
					.catch(() => {
						state.loading = false;
					});
			}
		})
		.catch(() => {});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
