<template>
  <div class="container">
    <div class="title">地球物理数据入库</div>
    <el-divider />
    <el-tabs v-model="activeName" type="card">
      <el-tab-pane label="数据信息" name="data">
        <FormData
          :data-id="dataId"
          @saved="onSaved"
          @upload="openUploadDialog"
          @cloud-path="openCloudDialog"
        />
      </el-tab-pane>
      <el-tab-pane label="数据资源" name="resource" v-if="dataId">
        <DataResourcePanel ref="resourcePanelRef" :data-id="dataId" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup>
import { ref, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import FormData from '@/views/common/FormData.vue'
import DataResourcePanel from '@/views/common/DataResourcePanel.vue'

const route = useRoute()
const dataId = ref(route.params.id || '')
const activeName = ref('data')
const resourcePanelRef = ref(null)

/**
 * 数据保存成功后更新 ID
 * @param {Object} entity
 */
const onSaved = (entity) => {
  if (entity?.id) {
    dataId.value = String(entity.id)
  }
}

const openUploadDialog = async () => {
  if (!dataId.value) {
    ElMessage.warning('请先保存数据基本信息')
    return
  }
  activeName.value = 'resource'
  await nextTick()
  resourcePanelRef.value?.openUploadDialog?.()
}

const openCloudDialog = async () => {
  if (!dataId.value) {
    ElMessage.warning('请先保存数据基本信息')
    return
  }
  activeName.value = 'resource'
  await nextTick()
  resourcePanelRef.value?.openCloudDialog?.()
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px 8%;
  color: #242424;
  min-height: calc(100vh - 220px);
  background-color: rgb(241, 244, 251);

  .title {
    font-size: 20px;
    font-weight: bold;
    color: #333;
  }
}
</style>
