<template>
	<div class="device-ivrCategory-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<div class="mb20">
				<el-button type="primary" @click="onAddCategory" v-waves v-auth="'device:ivrCategory:add'">
					<SvgIcon name="ele-Plus" class="mr5" />新增
				</el-button>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading" row-key="id">
				<el-table-column prop="name" label="分类名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
				<el-table-column prop="creationTime" 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="120" fixed="right" align="center">
					<template #default="{row}">
						<el-button link type="primary" @click="onEditCategory(row)" v-auth="'device:ivrCategory:edit'" title="修改IVR分类">
							修改
						</el-button>
						<el-button link type="danger" @click="onDelCategory(row)" v-auth="'device:ivrCategory:delete'" title="删除IVR分类">
							删除
						</el-button>
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
		</div>
		<category-add ref="CategoryAddRef" @updateList="handleQuery" />
		<category-edit ref="CategoryEditRef" @updateList="handleQuery" />
	</div>
</template>
<script lang="ts" setup name="ivrCategory">
import { defineAsyncComponent, onMounted, ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import { getIvrCategories, deleteIvrCategory } from '/@/api/device/ivr';
import { formatDate } from '/@/utils/formatTime';

// 定义接口来定义对象的类型
interface IvrCategoryState {
	loading: boolean;
	tableData: Array<any>; //列表数据
}

// 引入组件
const CategoryAdd = defineAsyncComponent(() => import('/@/views/device/ivrCategory/component/Category-add.vue'));
const CategoryEdit = defineAsyncComponent(() => import('/@/views/device/ivrCategory/component/Category-edit.vue'));

// 定义变量内容
// import { useRouter } from "vue-router";
const state = reactive<IvrCategoryState>({
	loading: false,
	tableData: [],
});
const CategoryAddRef = ref<RefType>();
const CategoryEditRef = ref<RefType>();

// 新增分类
const onAddCategory = () => {
	CategoryAddRef.value.openDialog();
};
// 编辑分类
const onEditCategory = (row: any) => {
	CategoryEditRef.value.openDialog(row);
};
//删除分类
const onDelCategory = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除分类：【${row.name}】，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			deleteIvrCategory(row.id).then(() => {
				ElMessage.success('删除成功');
				handleQuery();
			});
		})
		.catch(() => {});
};
//查询列表
const handleQuery = () => {
	if (!auth('device:ivrCategory:query')) ElMessage.error('抱歉，您没有权限获取IVR分类管理列表！');
	else {
		state.loading = true;
		getIvrCategories()
			.then((res: any) => {
				state.tableData = res?.result ?? [];
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
};
onMounted(() => {
	handleQuery();
});
</script>
<style scoped lang="scss">
.device-ivrCategory-container {
	.el-table {
		flex: 1;
	}
}
</style>
