<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.lectureName" />
        <CommonInfoRow label="讲座类型" :value="getLecTypeLabel(form.lectureType)" />
        <CommonInfoRow label="讲座日期" :value="formatDate(form.lectureDate)" />
        <CommonInfoRow label="讲座时间" :value="formatDate(form.lectureStartTime) + ' 至 ' + formatDate(form.lectureEndTime)" />
        <CommonInfoRow label="参会申请人" :value="form.applicantName" />
        <CommonInfoRow label="所属科室" :value="form.deptName" />
        <CommonInfoRow label="讲座地点" :value="form.lectureAddress" />
      </CommonSection>

      <!-- 讲座细节 -->
      <CommonSection title="讲座细节">
        <CommonInfoRow label="主讲人职务" :value="form.lectureSpeakerDuties" />
        <CommonInfoRow label="主讲人职称" :value="form.lectureSpeakerTitles" />
        <CommonInfoRow label="主持人" :value="form.lectureHost" />
        <CommonInfoRow label="嘉宾" :value="form.lectureGuests" />
        <CommonInfoRow label="主讲人学术简历" :value="form.lectureSpeakerResume" isColumn />
        <CommonInfoRow label="观点综述" :value="form.lectureOpinion" isColumn />
      </CommonSection>

      <!-- 相关附件 -->
      <AttachmentList :list="attachmentList" title="相关附件" />
    </template>
    <uv-empty v-else mode="data" text="暂无数据"></uv-empty>
  </view>
</template>

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

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

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

const LecTypeList = [
  { 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 attachmentList = computed(() => {
  if (!form.value || !form.value.lectureDocument) return [];
  try {
    const file = JSON.parse(form.value.lectureDocument);
    return [{
      name: file.fileName || '讲座相关附件',
      url: file.lectureDocument || '',
      type: '相关文档'
    }];
  } catch (e) {
    return [{
      name: '附件',
      url: form.value.lectureDocument,
      type: '相关文档'
    }];
  }
});

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