<template>
  <view class="document-form">
    <uv-loading-icon v-if="loading" mode="circle" text="正在加载成果标准详情..."></uv-loading-icon>
    <template v-else-if="form">
      <!-- 基本信息 -->
      <CommonSection title="基本信息" :isFirst="true">
        <CommonInfoRow label="标准名称" :value="form.standardName" />
        <CommonInfoRow label="标准编号" :value="form.standardCode" />
        <CommonInfoRow label="成果所属单位" :value="form.achievementBelongUnitName" />
        <CommonInfoRow label="提交部门" :value="form.commitDeptName" />
        <CommonInfoRow label="提交日期" :value="formatDate(form.commitDate, 'YYYY-MM-DD')" />
        <CommonInfoRow label="是否已发布" :value="form.isPublished === '10' ? '已发布' : '未发布'" />
        <CommonInfoRow label="发布日期" :value="formatDate(form.publishDate, 'YYYY-MM-DD')" />
        <CommonInfoRow label="单位排名" :value="form.unitRanking" />
        <CommonInfoRow label="所有制定单位" :value="form.belongEnactedUnit" isColumn />
        <CommonInfoRow label="所有参与人员" :value="form.belongEngagePersonal" isColumn />
        <CommonInfoRow label="标准类型" :value="getDictLabel('achievement_standard_type', form.standardType)" />
        <CommonInfoRow label="标准电子版" isColumn>
          <view class="file-links">
            <view v-if="attachment" class="file-item" @click="handlePreview(attachment)">
              <text class="file-link">{{ attachment.fileName || attachment.name || '-' }}</text>
            </view>
            <text v-else>-</text>
          </view>
        </CommonInfoRow>
        <CommonInfoRow label="归属年度" :value="form.belongYear" />
        <CommonInfoRow label="归属团队" :value="form.belongTeam" />
        <CommonInfoRow label="所属平台" isColumn>
          <view class="platform-tags">
            <template v-if="platformList.length > 0">
              <uv-tags v-for="(p, index) in platformList" :key="index" :text="p.platformName === '其他' ? '其他' : (p.platformType ? p.platformName + ' (' + p.platformType + ')' : p.platformName)" type="primary" plain size="mini" class="mr5"></uv-tags>
            </template>
            <text v-else>-</text>
          </view>
        </CommonInfoRow>
        <CommonInfoRow label="备注" :value="form.remark" isColumn />
      </CommonSection>

      <!-- 制定人信息 -->
      <view class="common-section-card mt20" v-if="form.enactedPersonal?.length">
        <view class="section-title">制定人信息</view>
        <view class="achievement-card" v-for="(item, index) in form.enactedPersonal" :key="index">
          <view class="a-header mb20">
            <text class="a-name">{{ item.enactedPersonName }}</text>
            <text class="a-tag">{{ getDictLabel('enacted_person_type', item.enactedPersonType) }}</text>
          </view>
          <view class="a-body">
            <view class="a-row">
              <text class="al">学位：</text>
              <text class="av">{{ getDictLabel('sci_academic_degree', item.enactedPersonDegree) }}</text>
            </view>
            <view class="a-row">
              <text class="al">贡献率：</text>
              <text class="av">{{ item.contributionRate }}%</text>
            </view>
            <view class="a-row">
              <text class="al">工作单位：</text>
              <text class="av">{{ item.workUnitName || '-' }}</text>
            </view>
          </view>
        </view>
      </view>

      <!-- 审批记录 -->
      <CommonSection title="审批记录" v-if="form.id">
        <FlowTable :id="form.id" :businessCode="String(form.id)" defCode="sci_achievement_standard" />
      </CommonSection>
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } from 'vue';
import { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { formatDate } from '@/utils/date';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';
import FlowTable from '@/pages/project/components/detail/FlowTable.vue';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';

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

const { getDictLabel } = useDict('achievement_standard_type', 'enacted_person_type', 'sci_academic_degree');
const documentApi = useDocumentApi();
const form = ref<any>(null);
const loading = ref(false);

const attachment = computed(() => {
  if (form.value?.standardFile) {
    try {
      return JSON.parse(form.value.standardFile);
    } catch (e) {
      return null;
    }
  }
  return null;
});

const platformList = computed(() => {
  if (form.value?.belongPlatform) {
    try {
      const data = JSON.parse(form.value.belongPlatform);
      return Array.isArray(data) ? data : [];
    } catch (e) {
      if (form.value.belongPlatform === '其他') return [{ platformName: '其他' }];
      return [{ platformName: form.value.belongPlatform }];
    }
  }
  return [];
});

const handlePreview = (file: any) => {
  const url = file.fileUrl || file.url;
  const name = file.fileName || file.name;
  if (url) {
    previewFile(url, name);
  }
};

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  // 直接通过 ID 获取以对齐 PC 逻辑
  const [err, res] = await to(documentApi.getAchievementStandardById(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";

.info-row {
  display: flex;
  justify-content: space-between;
  padding: 24rpx 0;
  border-bottom: 2rpx dashed #f5f5f5;
  font-size: 28rpx;

  .label {
    color: #343a3f;
    width: 200rpx;
    flex-shrink: 0;
    margin-right: 20rpx;
  }
}

.file-links {
  .file-item {
    padding: 6rpx 0;
  }

  .file-link {
    color: #2979ff;
    text-decoration: underline;
    word-break: break-all;
  }
}
</style>
