<template>
	<div class="device-phone-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<div class="mb20">
				<el-button type="primary" @click="asyncDevice" v-waves v-auth="'device:phone:async'">
					<SvgIcon name="ele-Refresh" class="mr5" />同步
				</el-button>
			</div>
			<!-- 表格 -->
			<el-table :data="state.tableData" v-loading="state.loading" row-key="id">
				<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="{row}">
						<span>{{ formatDate(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="phone" setup>
import { onMounted, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { auth } from '/@/utils/authFunction';
import { formatDate } from '/@/utils/formatTime';
// 引入api
import { getTelsList, syncTel } from '/@/api/device/phone';

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

// 定义变量内容
const state = reactive<TelsState>({
	loading: false,
	tableData: [],
});
/** 获取分机列表 */
const queryList = () => {
	if (!auth('device:phone:query')) ElMessage.error('抱歉，您没有获取话机管理列表的权限！');
	else {
		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(() => {
				queryList();
				ElMessage.success('同步成功');
			});
		})
		.catch(() => {});
};
onMounted(() => {
	queryList();
});
</script>

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