<template>
	<div class="tels-blacklist-container layout-pd">
		<el-card shadow="never">
			<!-- 通用搜索 -->
			<el-form :model="state.queryParams" ref="ruleFormRef" :inline="true" @submit.native.prevent class="mt15">
				<el-form-item label="黑名单号码" prop="telePhone">
					<el-input v-model="state.queryParams.telePhone" placeholder="请输入黑名单号码" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="state.queryParams.remark" placeholder="请输入备注" clearable @keyup.enter="handleQuery" />
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="handleQuery" :loading="state.loading" v-waves> <SvgIcon name="ele-Search" class="mr5" />查询 </el-button>
					<el-button @click="resetQuery(ruleFormRef)" v-waves class="default-button" :loading="state.loading">
						<SvgIcon name="ele-Refresh" class="mr5" />重置
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="never">
			<div class="mb20">
				<el-button type="primary" @click="onAddBlacklist" v-auth="'tels:blackList:add'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
				<el-button type="primary" @click="onRemove" v-auth="'tels:blackList:delete'" :disabled="!multipleSelection.length">
					<SvgIcon name="ele-Delete" class="mr5" />解除
				</el-button>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading" row-key="id" ref="multipleTableRef" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" :reserve-selection="true"/>
				<el-table-column prop="telePhone" label="黑名单号码" show-overflow-tooltip></el-table-column>
				<el-table-column label="开始时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.setTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="失效时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.removeTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
				<el-table-column prop="platFromCode" label="平台编码" show-overflow-tooltip></el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
			<!-- 分页 -->
			<pagination
				:total="state.total"
				v-model:page="state.queryParams.pageIndex"
				v-model:limit="state.queryParams.pageSize"
				@pagination="queryList"
			/>
		</el-card>
    <!--  新增黑名单  -->
		<blacklist-add ref="blacklistAddRef" @updateList="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="blacklist">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import type { FormInstance } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { auth } from '/@/utils/authFunction';
import { blacklistPaged, blacklistRemove } from '/@/api/public/wex';
import { throttle } from '/@/utils/tools';

// 引入组件
const BlacklistAdd = defineAsyncComponent(() => import('/@/views/tels/blacklist/component/Blacklist-add.vue')); // 新增黑名单

// 定义变量内容
const state = reactive<any>({
	queryParams: {
		// 查询参数
		pageIndex: 1, // 当前页码
		pageSize: 100, // 每页条数
		telePhone: '', // 电话号码
		remark: '', // 备注
	},
	loading: false, // 加载状态
	total: 0, // 总条数
	tableData: [],  // 表格数据
	multipleSelection: [],  // 多选数据
});
const ruleFormRef = ref<FormInstance>();  // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = throttle(() => {
	state.queryParams.pageIndex = 1;
	queryList();
}, 300);
/** 获取黑名单列表 */
const queryList = () => {
	if (!auth('tels:blackList:query')) ElMessage.error('抱歉，您没有权限获取黑名单列表！');
	else {
		state.loading = true;
		blacklistPaged(state.queryParams)
			.then((response: any) => {
				state.tableData = response.data ?? [];
				state.total = response.count ?? 0;
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
};
/** 重置按钮操作 */
const resetQuery = throttle((formEl: FormInstance | undefined) => {
	if (!formEl) return;
	formEl.resetFields();
	handleQuery();
}, 300);
// 打开新增黑名单弹窗
const blacklistAddRef = ref<RefType>();
const onAddBlacklist = () => {
	blacklistAddRef.value.openDialog();
};
// 表格多选
const multipleTableRef = ref<RefType>();
const multipleSelection = ref<any>([]);
const handleSelectionChange = (val: any[]) => {
	multipleSelection.value = val;
};
// 删除黑名单
const onRemove = () => {
  const ids:string = multipleSelection.value.map((item: any) => item.blackId).join(',');
  const phoneNo:string = multipleSelection.value.map((item: any) => item.telePhone).join(',');
	ElMessageBox.confirm(`此操作将解除：【${phoneNo}】的黑名单，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			blacklistRemove(ids).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
onMounted(() => {
	queryList();
});
</script>
