<template>
	<div class="tels-blacklist-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<el-form :model="state.queryParams" ref="ruleFormRef" inline @submit.native.prevent>
				<el-form-item label="黑白名单号码" prop="Phone">
					<el-input v-model="state.queryParams.Phone" placeholder="请填写黑白名单号码" clearable @keyup.enter="handleQuery" class="keyword-input" />
				</el-form-item>
				<el-form-item label="类型" prop="SpecialFlag">
					<el-select v-model="state.queryParams.SpecialFlag" placeholder="请选择类型">
						<el-option v-for="item in specialFlagList" :key="item.value" :label="item.label" :value="item.value" />
					</el-select>
				</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>
				</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="phone" title="黑白名单号码" min-width="140"></vxe-column>
					<vxe-column field="specialFlag" title="类型" min-width="140">
						<template #default="{ row }">
							<span>{{ specialFlagList.find((item) => row.specialFlag === item.value).label }}</span>
						</template>
					</vxe-column>
					<vxe-column field="creationTime" title="创建时间" width="160">
						<template #default="{ row }">
							{{ formatDate(row.creationTime, '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="primary" @click="onRemove(row)" title="删除黑名单" v-auth="'tels:blackList:delete'"> 删除 </el-button>
						</template>
					</vxe-column>
				</vxe-table>
			</div>
		</div>
		<!--  新增黑名单  -->
		<blacklist-add ref="blacklistAddRef" @updateList="handleQuery" :specialFlagList="specialFlagList" />
	</div>
</template>

<script lang="tsx" 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 { queryBlacklist, removeBlacklist } from '@/api/public/wex';

// 引入组件
const BlacklistAdd = defineAsyncComponent(() => import('@/views/tels/blacklist/components/Blacklist-add.vue')); // 新增黑名单
// 定义变量内容
const state = reactive({
	queryParams: {
		Phone: null, // 电话号码
		SpecialFlag: null, // 备注
	},
	loading: false, // 加载状态
	total: 0, // 总条数
	tableData: [], // 表格数据
	multipleSelection: [], // 多选数据
});
const ruleFormRef = ref<FormInstance>(); // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = () => {
	queryList();
};
const specialFlagList = ref<any>([
	{ value: 1, label: '白名单' },
	{ value: 2, label: '呼入黑名单' },
	{ value: 3, label: '呼出黑名单' },
	{ value: 4, label: '呼入呼出黑名单' },
]); // 黑白名单类型
/** 获取黑名单列表 */
const queryList = () => {
	state.loading = true;
	queryBlacklist(state.queryParams)
		.then((response: any) => {
			state.tableData = response.result ?? [];
			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();
};
// 删除黑名单
const onRemove = (row: any) => {
	ElMessageBox.confirm(`此操作将解除：【${row.phone}】的黑名单，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
		autofocus: false,
	})
		.then(() => {
			removeBlacklist({ ...row }).then(() => {
				ElMessage.success('操作成功');
				queryList();
			});
		})
		.catch(() => {});
};
const toolbarRef = ref<RefType>();
const tableRef = ref<RefType>();
onMounted(() => {
	queryList();
	if (tableRef.value && toolbarRef.value) {
		tableRef.value.connect(toolbarRef.value);
	}
});
</script>
