<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="Phone">
					<el-input v-model="state.queryParams.Phone" placeholder="请输入黑白名单号码" clearable @keyup.enter="handleQuery" />
				</el-form-item>
				<el-form-item label="类型" prop="SpecialFlag">
					<el-select v-model="state.queryParams.SpecialFlag" placeholder="请选择类型" class="w100">
						<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" 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>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading">
				<el-table-column prop="phone" label="黑白名单号码" show-overflow-tooltip></el-table-column>
        <el-table-column prop="phone" label="类型" show-overflow-tooltip>
          <template #default="{ row }">
            <span>{{ specialFlagList.find(item=>row.specialFlag === item.value).label }}</span>
          </template>
        </el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip width="170">
					<template #default="{ row }">
						<span>{{ formatDate(row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
        <el-table-column label="操作" width="200" fixed="right" align="center">
          <template #default="{ row }">
            <el-button link type="primary" @click="onRemove(row)" v-auth="'system:parameter:edit'" title="删除黑名单"> 删除 </el-button>
          </template>
        </el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
		</el-card>
		<!--  新增黑名单  -->
		<blacklist-add ref="blacklistAddRef" @updateList="handleQuery" :specialFlagList="specialFlagList"/>
	</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 { queryBlacklist, removeBlacklist } 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: {
    Phone: null, // 电话号码
    SpecialFlag: null, // 备注
	},
	loading: false, // 加载状态
	total: 0, // 总条数
	tableData: [], // 表格数据
	multipleSelection: [], // 多选数据
});
const ruleFormRef = ref<FormInstance>(); // 表单ref
/** 搜索按钮操作 节流操作 */
const handleQuery = throttle(() => {
	queryList();
}, 300);
const specialFlagList = ref<EmptyArrayType>([
	{ value: 1, label: '白名单' },
	{ value: 2, label: '呼入黑名单' },
	{ value: 3, label: '呼出黑名单' },
	{ value: 4, label: '呼入呼出黑名单' },
]); // 黑白名单类型
/** 获取黑名单列表 */
const queryList = () => {
	if (!auth('tels:blackList:query')) ElMessage.error('抱歉，您没有权限获取黑名单列表！');
	else {
		state.loading = true;
		queryBlacklist(state.queryParams)
			.then((response: any) => {
				state.tableData = response.result ?? [];
				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 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(() => {});
};
onMounted(() => {
	queryList();
});
</script>
