<template>
  <TemplateDetailsHeader :loading="loading" :template="template" @refresh="() => getTemplateData(id)" />
  <el-col>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="属性模板" lazy name="attrModel">
        <DeviceAttrTemplateModel ref="attrModelRef" />
      </el-tab-pane>
    </el-tabs>
  </el-col>
</template>
<script lang="ts" setup>
import * as DeviceTemplateApi from '@/api/pms/devicetemplate'
import TemplateDetailsHeader from './TemplateDetailsHeader.vue'
import DeviceAttrTemplateModel from '@/views/pms/devicetemplate/detail/attrsModel/index.vue'
import { useTagsViewStore } from '@/store/modules/tagsView'
import { useRouter } from 'vue-router'
import {IOT_PROVIDE_KEY} from "@/views/iot/utils/constants";

defineOptions({ name: 'DeviceAttrTemplateDetail' })

const { delView } = useTagsViewStore() // 视图操作
const { currentRoute } = useRouter()

const route = useRoute()
const message = useMessage()
const id = route.params.id // 编号
const loading = ref(true) // 加载中
const template = ref<DeviceTemplateApi.DeviceAttrTemplateVO>({} as DeviceTemplateApi.DeviceAttrTemplateVO) // 模板详情
const activeTab = ref('attrModel') // 默认为 属性列表 标签页

provide(IOT_PROVIDE_KEY.DEVICE_ATTR_TEMPLATE, template) // 提供设备属性模板信息给详情页的子组件

/** 获取 设备属性模板 详情 */
const getTemplateData = async (id: number) => {
  loading.value = true
  try {
    template.value = await DeviceTemplateApi.getDeviceTemplate(id)
  } finally {
    loading.value = false
  }
}

/** 初始化 */
onMounted(async () => {
  if (!id) {
    message.warning('参数错误，设备属性模板不能为空！')
    delView(unref(currentRoute))
    return
  }
  await getTemplateData(id)
})
</script>
