<template>
	<div class="device-ivrCategroy-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<div class="flex-center-between mb20">
				<p class="table-title">信息列表</p>
				<div>
					<el-button type="primary" @click="onAddCategory" v-waves v-auth="'300302'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
					<el-button type="primary" v-waves @click="onImportTable" :disabled="!state.multipleSelection.length">
						<SvgIcon name="iconfont icon-daochu" class="mr5" />导出
					</el-button>
				</div>
			</div>
			<!-- 表格 -->
			<el-table :data="tableData" v-loading="loading" row-key="id" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" :reserve-selection="true" />
				<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="scope">
						<span>{{ formatDate(scope.row.creationTime, 'YYYY-mm-dd HH:MM:SS') }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100" fixed="right" align="center">
					<template #default="scope">
						<el-button text type="primary" @click="onEditCategroy(scope.row)" v-auth="'300303'" title="修改">
							<SvgIcon name="ele-EditPen" size="var(--hotline-table-icon-font-size)" />
						</el-button>
						<el-button text type="danger" @click="onDelCategroy(scope.row)" v-auth="'300304'" title="删除">
							<SvgIcon name="ele-Delete" size="var(--hotline-table-icon-font-size)" />
						</el-button>
						<!-- <el-button text type="success" @click="configIvr(scope.row)">配置ivr</el-button> -->
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
		</div>

		<AddCategroy ref="addCategroyRef" @updateList="handleQuery" />
		<EditCategroy ref="editCategroyRef" @updateList="handleQuery" />
	</div>
</template>
<script lang="ts" setup name="ivrCategroy">
import { defineAsyncComponent, onMounted, ref, reactive, toRefs } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getIvrCategories, deleteIvrCategroies } from '/@/api/device/ivr';
import { formatDate } from '/@/utils/formatTime';
import table2excel from 'js-table2excel';

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

// 引入组件
const AddCategroy = defineAsyncComponent(() => import('/@/views/device/ivrCategroy/component/addCategroy.vue'));
const EditCategroy = defineAsyncComponent(() => import('/@/views/device/ivrCategroy/component/editCatehroy.vue'));

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

const addCategroyRef = ref();
const editCategroyRef = ref();

// 新增分类
const onAddCategory = () => {
	addCategroyRef.value.openDialog();
};
// 编辑分类
const onEditCategroy = (row: any) => {
	editCategroyRef.value.openDialog(row);
};
//删除分类
const onDelCategroy = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除分类：【${row.name}】，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			deleteIvrCategroies(row.id).then(() => {
				ElMessage.success('删除成功');
				handleQuery();
			});
		})
		.catch(() => {});
};
//查询列表
const handleQuery = () => {
	state.loading = true;
	getIvrCategories().then((res: any) => {
		state.tableData = res?.result ?? [];
		state.loading = false;
	});
};
// 配置ivr
// const configIvr = (row:any)=>{
//     router.push({
//         // path:"/ivrConfig/details",
//         name:"ivrConfigDetails",
//         params:{
//             tagsViewName:row.name+'ivr配置',
//             id: row.id,
//         }
//     })
// }
// 表格多选
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};
// 导出表格
const onImportTable = () => {
	const tabeHeader = [
		{ key: 'name', colWidth: '', title: '分类名称', type: 'text', isCheck: true },
		{ key: 'remark', colWidth: '', title: '备注', type: 'text', isCheck: true },
		{ key: 'creationTime', colWidth: '', title: '创建时间', type: 'text', isCheck: true },
	];
	table2excel(tabeHeader, state.multipleSelection, `ivr分类管理 ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`);
};
onMounted(() => {
	handleQuery();
});
</script>
<style scoped lang="scss">
.device-ivrCategroy-container {
	.el-table {
		flex: 1;
	}
}
</style>
