<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.codTitle" />
        <CommonInfoRow label="会议类型" :value="getLecTypeLabel(form.codType)" />
        <CommonInfoRow label="主办单位" :value="form.codOrganizer" />
        <CommonInfoRow label="会议日期" :value="formatDate(form.codStartTime) + ' 至 ' + formatDate(form.codEndTime)" />
        <CommonInfoRow label="主讲人" :value="form.applicantName" />
        <CommonInfoRow label="会议地址" :value="form.codAddress" />
        <CommonInfoRow label="所属平台">
          <view class="platform-tags">
            <template v-if="platformList.length > 0">
              <uv-tags v-for="(item, index) in platformList" :key="index" :text="item.platformName === '其他' ? '其他' : (item.platformType ? item.platformName + ' (' + item.platformType + ')' : item.platformName)" type="primary" plain size="mini" class="mr5"></uv-tags>
            </template>
            <text v-else>-</text>
          </view>
        </CommonInfoRow>
        <CommonInfoRow label="所属团队" :value="form.belongTeam || '-'" />
        <CommonInfoRow label="会议简介" :value="form.codBriefInfo" isColumn />
      </CommonSection>

      <!-- 学术贡献 -->
      <CommonSection title="学术贡献">
        <CommonInfoRow label="论文题目" :value="form.paperName" />
        <CommonInfoRow label="第一作者" :value="form.firstName" />
        <CommonInfoRow label="通讯作者" :value="form.phoneName" />
        <CommonInfoRow label="交流方式" :value="getDictLabel('sci_communicate_type', form.communicateType)" />
        <CommonInfoRow label="论文摘要" :value="form.paperSummary" isColumn />
      </CommonSection>

      <!-- 相关附件 -->
      <AttachmentList :list="uploadList" title="相关附件" />

      <!-- 审批记录 -->
      <CommonSection title="审批记录" v-if="form.id">
        <FlowTable :id="form.id" :businessCode="String(form.id)" defCode="sci_academic_conferences" />
      </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 AttachmentList from './AttachmentList.vue';
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('sci_communicate_type');
const documentApi = useDocumentApi();

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

const LecTypeList = [
  { value: '50', label: '院内会议' },
  { value: '10', label: '学校会议' },
  { value: '20', label: '省级会议' },
  { value: '30', label: '国内学术会议' },
  { value: '40', label: '国际学术会议' }
];

const getLecTypeLabel = (value: string | number) => {
  const item = LecTypeList.find(i => i.value === String(value));
  return item ? item.label : (value || '-');
};

const platformList = computed(() => {
  if (form.value?.belongPlatform) {
    try {
      const data = JSON.parse(form.value.belongPlatform);
      if (Array.isArray(data) && data.length > 0) {
        return data;
      }
    } catch (e) {
      return [];
    }
  }
  return [];
});

const uploadList = computed(() => {
  if (!form.value) return [];
  const list = [];
  try {
    if (form.value.codNotice) {
      const notice = JSON.parse(form.value.codNotice);
      if (notice && notice.url) {
        list.push({ ...notice, type: '会议通知' });
      }
    }
    if (form.value.codDocument) {
      const doc = JSON.parse(form.value.codDocument);
      if (doc && doc.url) {
        list.push({ ...doc, type: '相关文档' });
      }
    }
  } catch (e) {
    console.error('Failed to parse attachment JSON', e);
  }
  return list;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getConferenceById(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>
