<template>
	<div class="device-line-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<div class="mb20">
				<el-button type="primary" @click="addLine" v-waves v-auth="'device:line: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="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="{row}">
						<span v-for="(item, index) in 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="{row}">
						<span>{{ row.workCategoryModel.name }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="restCategoryModel" label="休息时间IVR" show-overflow-tooltip width="150">
					<template #default="{row}">
						{{ row.restCategoryModel.name }}
					</template>
				</el-table-column>
				<el-table-column prop="workToGroupModel" label="工作时间直转分机组" show-overflow-tooltip width="160">
					<template #default="{row}">
						{{ row.workToGroupModel.name }}
					</template>
				</el-table-column>
				<el-table-column prop="restToGroupModel" label="休息时间直转分机组" show-overflow-tooltip width="160">
					<template #default="{row}">
						{{ row.restToGroupModel.name }}
					</template>
				</el-table-column>
				<el-table-column label="是否启用" show-overflow-tooltip prop="isEnable">
					<template #default="{row}">
						<el-tag type="success" v-if="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="{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="updateLine(row)" v-auth="'device:line:edit'" title="修改线路"> 修改 </el-button>
						<el-button link type="danger" @click="ivrConfigure(row)" v-auth="'device:line:delete'" title="删除线路"> 删除 </el-button>
					</template>
				</el-table-column>
				<template #empty>
					<Empty />
				</template>
			</el-table>
		</div>
		<line-add ref="LineAddRef" @updateList="queryList" />
		<line-edit ref="DeviceLineEditRefRef" @updateList="queryList" />
	</div>
</template>
<script lang="ts" name="deviceLine" setup>
import { onMounted, reactive, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import { auth } from '/@/utils/authFunction';
// 引入api
import { getList, removeLine } from '/@/api/device/line';

// 引入组件
const LineAdd = defineAsyncComponent(() => import('/@/views/device/line/component/Line-add.vue'));
const LineEdit = defineAsyncComponent(() => import('/@/views/device/line/component/Line-edit.vue'));

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

// 定义变量内容
const state = reactive<TelsState>({
	loading: false,
	tableData: [],
});
const LineAddRef = ref<RefType>();
const DeviceLineEditRefRef = ref<RefType>();
/** 获取线路列表 */
const queryList = () => {
	if (!auth('device:line:query')) ElMessage.error('抱歉，您没有权限获取线路管理列表！');
	else {
		state.loading = true;
		getList()
			.then((response: any) => {
				state.tableData = response.result ?? [];
				state.loading = false;
			})
			.catch(() => {
				state.loading = false;
			});
	}
};
// 新增线路
const addLine = () => {
	LineAddRef.value?.openDialog();
};
// 修改路线
const updateLine = (row: any) => {
	DeviceLineEditRefRef.value?.openDialog(row);
};
// 删除路线
const ivrConfigure = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除线路：【${row.trunkId}】, 是否继续?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			removeLine(row.id).then(() => {
				ElMessage.success('删除成功');
				queryList();
			});
		})
		.catch(() => {});
};
onMounted(() => {
	queryList();
});
</script>

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