<template>
	<div class="device-tels-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="addTrunks" v-waves v-auth="'300502'"> <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="trunkId" label="线路号" show-overflow-tooltip width="100"></el-table-column>
				<el-table-column prop="morningBegin" label="早上开始时间" show-overflow-tooltip width="130"></el-table-column>
				<el-table-column prop="morningEnd" label="早上结束时间" show-overflow-tooltip width="130"></el-table-column>
				<el-table-column prop="afterBegin" label="下午开始时间" show-overflow-tooltip width="130"></el-table-column>
				<el-table-column prop="afterEnd" label="下午结束时间" show-overflow-tooltip width="130"></el-table-column>
				<el-table-column prop="afterBegin" label="工作日" show-overflow-tooltip width="150">
					<template #default="scope">
						<span v-for="(item, index) in scope.row.workDay" :key="index"> <span v-show="index">,</span>{{ item.weekName }} </span>
					</template>
				</el-table-column>
				<el-table-column prop="workCategoryModel" label="工作时间IVR" show-overflow-tooltip width="150">
					<template #default="scope">
						<span>{{ scope.row.workCategoryModel.name }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="restCategoryModel" label="休息时间IVR" show-overflow-tooltip width="150">
					<template #default="scope">
						{{ scope.row.restCategoryModel.name }}
					</template>
				</el-table-column>
				<el-table-column prop="workToGroupModel" label="工作时间直转分机组" show-overflow-tooltip width="160">
					<template #default="scope">
						{{ scope.row.workToGroupModel.name }}
					</template>
				</el-table-column>
				<el-table-column prop="restToGroupModel" label="休息时间直转分机组" show-overflow-tooltip width="160">
					<template #default="scope">
						{{ scope.row.restToGroupModel.name }}
					</template>
				</el-table-column>
				<el-table-column label="是否启用" show-overflow-tooltip prop="isEnable">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.isEnable">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column 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="操作" show-overflow-tooltip width="160" fixed="right" align="center">
					<template #default="scope">
						<el-button text type="primary" @click="updateTrunks(scope.row)" v-auth="'300503'" title="修改线路"> 修改线路 </el-button>
						<el-button text type="success" @click="ivrConfigure(scope.row)" v-auth="'300504'" title="删除线路"> 删除线路 </el-button>
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
		</div>
		<AddTrunks ref="addTrunksRef" @updateList="getListFn" />
		<EditTrunks ref="EditTrunksRef" @updateList="getListFn" />
	</div>
</template>
<script lang="ts" name="trunks" setup>
import { onMounted, reactive, toRefs, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { useThemeConfig } from '/@/stores/themeConfig';
import { storeToRefs } from 'pinia';
import { useRoute } from 'vue-router';
import table2excel from 'js-table2excel';
// 引入api
import { getList, removetrunk } from '/@/api/device/trunks';

// 引入组件
const AddTrunks = defineAsyncComponent(() => import('/@/views/device/trunks/component/trunksAdd.vue'));
const EditTrunks = defineAsyncComponent(() => import('/@/views/device/trunks/component/trunksEdit.vue'));

// 定义接口来定义对象的类型
interface TelsState {
	loading: boolean;
	tableData: Array<any>;
	multipleSelection: Array<any>;
}

// 定义变量内容
const state = reactive<TelsState>({
	loading: false,
	tableData: [],
	multipleSelection: [],
});
const { loading, tableData } = toRefs(state);
const addTrunksRef = ref();
const EditTrunksRef = ref();
const route = useRoute();
const storesThemeConfig = useThemeConfig();
const { themeConfig } = storeToRefs(storesThemeConfig);
/** 获取线路列表 */
const getListFn = () => {
	state.loading = true;
	getList()
		.then((response: any) => {
			state.tableData = response.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 新增线路
const addTrunks = () => {
	addTrunksRef.value?.openDialog();
};
// 修改路线
const updateTrunks = (row: any) => {
	EditTrunksRef.value?.openDialog(row);
};
// 删除路线
const ivrConfigure = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除线路：【${row.trunkId}】, 是否继续?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			removetrunk(row.id).then(() => {
				ElMessage.success('删除成功');
				getListFn();
			});
		})
		.catch(() => {});
};
// 表格多选
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};
// 导出表格
const onImportTable = () => {
	const tabeHeader = [
		{ key: 'trunkId', colWidth: '', title: '线路号', type: 'text', isCheck: true },
		{ key: 'morningBegin', colWidth: '', title: '早上开始时间', type: 'text', isCheck: true },
		{ key: 'morningEnd', colWidth: '', title: '早上结束时间', type: 'text', isCheck: true },
		{ key: 'afterBegin', colWidth: '', title: '下午开始时间', type: 'text', isCheck: true },
		{ key: 'afterEnd', colWidth: '', title: '下午结束时间', type: 'text', isCheck: true },
		{ key: 'telStatusText', colWidth: '', title: '工作日', type: 'text', isCheck: true },
		{ key: 'afterBegin', colWidth: '', title: '工作时间IVR', type: 'text', isCheck: true },
		{ key: 'afterBegin', colWidth: '', title: '休息时间IVR', type: 'text', isCheck: true },
		{ key: 'afterBegin', colWidth: '', title: '工作时间直转分机组', type: 'text', isCheck: true },
		{ key: 'afterBegin', colWidth: '', title: '休息时间直转分机组', type: 'text', isCheck: true },
		{ key: 'afterBegin', colWidth: '', title: '是否启用', type: 'text', isCheck: true },
		{ key: 'creationTime', colWidth: '', title: '创建时间', type: 'text', isCheck: true },
	];
	table2excel(
		tabeHeader,
		state.multipleSelection,
		`${themeConfig.value.globalTitle}-${route.meta.title} ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`
	);
};
onMounted(() => {
	getListFn();
});
</script>

<style lang="scss" scoped>
.device-tels-container {
	.el-table {
		flex: 1;
	}
}
</style>
