<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="asyncDevice" v-waves v-auth="'300101'"> <SvgIcon name="ele-Refresh" 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="no" label="分机编号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="groupNames" label="所属分机组" show-overflow-tooltip></el-table-column>
				<el-table-column prop="registerIP" label="注册IP" show-overflow-tooltip></el-table-column>
				<el-table-column label="分机状态" show-overflow-tooltip prop="telStatusText"></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>
				<template #empty>
					<Empty />
				</template>
			</el-table>
		</div>
	</div>
</template>

<script lang="ts" name="tels" setup>
import { onMounted, reactive, toRefs } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { formatDate } from '/@/utils/formatTime';
import table2excel from 'js-table2excel';
// 引入api
import { getTelsList, syncTel } from '/@/api/device/tels';

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

// 定义变量内容
const state = reactive<TelsState>({
	loading: false,
	tableData: [],
	multipleSelection: [],
});
const { loading, tableData } = toRefs(state);
/** 获取分机列表 */
const getList = () => {
	state.loading = true;
	getTelsList()
		.then((response: any) => {
			state.tableData = response.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 同步
const asyncDevice = () => {
	ElMessageBox.confirm(`确定要将设备信息同步到数据库，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
		draggable: true,
		cancelButtonClass: 'default-button',
	})
		.then(() => {
			syncTel()
				.then(() => {
					getList();
					ElMessage.success('同步成功');
				})
				.catch((err: any) => {
					console.log(err, '同步失败');
				});
		})
		.catch(() => {});
};
// 表格多选
const handleSelectionChange = (val: any) => {
	state.multipleSelection = val;
};
// 导出表格
const onImportTable = () => {
	const tabeHeader = [
		{ key: 'no', colWidth: '', title: '分机编号', type: 'text', isCheck: true },
		{ key: 'groupNames', colWidth: '', title: '所属分机组', type: 'text', isCheck: true },
		{ key: 'registerIP', colWidth: '', title: '注册IP', type: 'text', isCheck: true },
		{ key: '分机状态', colWidth: '', title: '分机状态', type: 'text', isCheck: true },
		{ key: '创建时间', colWidth: '', title: '创建时间', type: 'text', isCheck: true },
	];
	table2excel(tabeHeader, state.multipleSelection, `话机管理 ${formatDate(new Date(), 'YYYY-mm-dd HH-MM')}`);
};
onMounted(() => {
	getList();
});
</script>

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