<template>
  <div class="common-layout">
    <el-container style="height: 100vh">
      <el-header>
        <el-menu :default-active="activeIndex" class="el-menu-demo pl-20 bg-blueGray-2 custom-menu" mode="horizontal">
          <el-menu-item index="index">
            <router-link to="/"> 首页 </router-link>
          </el-menu-item>
          <el-menu-item index="protocol">
            <router-link to="/protocol"> 协议 </router-link>
          </el-menu-item>
          <el-menu-item index="communication">
            <router-link to="/communication"> 通信参数 </router-link>
          </el-menu-item>
        </el-menu>
      </el-header>
      <el-button type="primary" @click="restartDevice">重启device</el-button>
      <el-main>
        <!-- <router-view /> -->
        <router-view v-slot="{ Component }">
          <keep-alive>
            <component :is="Component" />
          </keep-alive>
        </router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
const activeIndex = ref('1')
import { DeviceProtocol, ProtocolCmd } from "@/dao/device";
import { GetProtocolVersion, GetCurrentVersion } from '@/dao/protocol';
import { useProtocolInfoStore } from './stores/protocolInfo';
import { useProtocolVersionStore } from '@/stores/allProtocolVersion';
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/plugins/axios/requests'

const store = useProtocolVersionStore()
const protocolStore = useProtocolInfoStore()

onMounted(async () => {
  await GetProtocolVersion()
  .then(res => {
    store.protocolVersions.push(...res)
  })
  await GetCurrentVersion()
  .then(res => {
    store.currentVersions = res    
  })
  await DeviceProtocol()
  .then((res) => {
    protocolStore.deviceProtocol = res.data
  })
  await ProtocolCmd()
  .then((res) => {
    protocolStore.protocolCmd = res.data
  })
})

const restartDevice = () => {
  ElMessageBox.confirm(
    '确定要重启 device 模块吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  .then(() => {
    request.post('/api/restart_device/')
    .then(() => {
      ElMessage({
        type: 'success',
        message: '重启成功',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'error',
        message: '重启失败',
      })
    })
  })
  .catch(() => {
    ElMessage({
      type: 'info',
      message: '取消成功',
    })
  })
}

</script>

<style>
.el-header {
  --el-header-padding: 0 !important;
}
.el-main{
  align-items: stretch;
  background-color: #f4f4f4;
}
.custom-menu {
  display: flex;
  justify-content: center;
  
}
.custom-menu .el-menu-item{
  font-size: 18px;
}

</style>
