<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载论文认领详情..."></uv-loading-icon>
    <template v-else-if="form">
      <view class="common-section-card">
        <view class="section-title">基本信息</view>
        <view class="info-row"><text class="label">名称</text><text class="value">{{ form.paperName || '-' }}</text></view>
        <view class="info-row"><text class="label">DOI</text><text class="value">{{ form.doi || '-' }}</text></view>
        <view class="info-row"><text class="label">来源</text><text class="value">{{ form.source || '-' }}</text></view>
        <view class="info-row"><text class="label">期刊</text><text class="value">{{ form.publicationName || '-' }}</text></view>
        <view class="info-row"><text class="label">公开日期</text><text class="value">{{ form.publishDate ? formatDate(form.publishDate, 'YYYY-MM-DD') : '-' }}</text></view>
        <view class="info-row"><text class="label">归属年度</text><text class="value">{{ form.belongYear || '-' }}</text></view>
        <view class="info-row column"><text class="label">第一署名单位</text><text class="value remark">{{ form.firstSignUnits || '-' }}</text></view>
        <view class="info-row column"><text class="label">第二署名单位</text><text class="value remark">{{ form.secondSignUnits || '-' }}</text></view>
        <view class="info-row column"><text class="label">第一作者</text><text class="value remark">{{ form.firstAuthors || '-' }}</text></view>
        <view class="info-row column"><text class="label">关键词</text><text class="value remark">{{ form.keywords || '-' }}</text></view>
        <view class="info-row column">
          <text class="label">摘要</text>
          <text class="value remark">{{ form.abstract || '-' }}</text>
        </view>
      </view>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { useDocumentApi } from '@/api/document';
import { formatDate } from '@/utils/date';
import to from 'await-to-js';

const props = defineProps<{
  code: string;
}>();

const documentApi = useDocumentApi();
const form = ref<any>(null);
const loading = ref(false);

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getPaperPushById(props.code));
  if (!err && res?.data) {
    form.value = res.data;
  }
  loading.value = false;
};

onMounted(() => {
  fetchData();
});

watch(() => props.code, () => {
  fetchData();
});
</script>

<style lang="scss" scoped>
@import "./common.scss";
</style>
