<route lang="yaml">
meta:
  enabled: false
</route>

<script lang="ts" setup name="modelObjectDetails">
import ModelObjectAttrListComponent from '../object_attr/index.vue'
import ModelObjectComponent from './components/ModelObject.vue'
import { getObjectById } from '@/api/model/modelObjectApi'
import useSettingsStore from '@/store/modules/settings'

const settingsStore = useSettingsStore()

const router = useRouter()
const tabbar = useTabbar()
const route = useRoute()

const modelId = parseInt(route.params.modelId.toString())
const objectId = parseInt(route.params.objectId.toString())

const state = reactive({
  loading: false,
  curModelId: 0,
  objectData: {
    objectTitle: '',
    objectCode: '',
    objectType: 0,
    comment: '',
  },
  dialogState: 1,
})

const getObjectData = async () => {
  state.loading = true
  const { data } = await getObjectById(modelId, objectId)
  state.objectData.objectTitle = data.objectTitle
  state.objectData.objectType = data.objectType
  state.objectData.objectCode = data.objectCode
  state.objectData.comment = data.comment
  state.loading = false
}

onMounted(() => {
  getObjectData()
  nextTick(() => {

  })
})

const activeTab = ref('first')
const modelObjectRef = ref()

// 编辑对象
const onObjectEdit = (row: { modelId: number; objectId: number }) => {
  modelObjectRef.value.showEditModel(modelId, objectId)
}

// Object Attrs
const modelObjectAttrList = ref()

// 返回列表页
function goBack() {
  if (settingsStore.settings.tabbar.enable && settingsStore.settings.tabbar.mergeTabsBy !== 'activeMenu') {
    tabbar.close({ name: 'model_object_list' })
  }
  else {
    router.push({ name: 'model_object_list' })
  }
}
</script>

<template>
  <div class="absolute-container">
    <page-header :title="state.objectData.objectTitle">
      <el-button size="default" round @click="goBack">
        <template #icon>
          <el-icon>
            <svg-icon name="i-ep:arrow-left" />
          </el-icon>
        </template>
        返回
      </el-button>
    </page-header>

    <page-main>
      <div class="container">
        <el-tabs v-model="activeTab" class="demo-tabs">
          <el-tab-pane label="基本信息" name="first">
            <el-descriptions
              :title="state.objectData.objectTitle"
              :column="1"
              size="small"
              border
            >
              <template #extra>
                <el-button type="primary" @click="onObjectEdit">
                  编辑
                </el-button>
              </template>

              <el-descriptions-item
                label="代码"
                label-class-name="el-description_label"
                width="150px"
                label-align="right"
              >
                {{ state.objectData.objectCode }}
              </el-descriptions-item>

              <el-descriptions-item
                label-align="right"
                label-class-name="el-description_label"
                width="150px"
                label="说明"
              >
                {{ state.objectData.comment }}
              </el-descriptions-item>
            </el-descriptions>
          </el-tab-pane>

          <el-tab-pane label="对象属性" name="second">
            <ModelObjectAttrListComponent ref="modelObjectAttrList" :model-id="modelId" :object-id="objectId" />
          </el-tab-pane>
        </el-tabs>
      </div>
    </page-main>
    <ModelObjectComponent ref="modelObjectRef" @success="getObjectData" />
  </div>
</template>

<style scoped>
.el-descriptions {
  margin: 0 10px;
}

.cell-item {
  display: flex;
  align-items: center;
}
</style>
