<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.lectureName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">讲座类型</text>
          <text class="value">{{ getLecTypeLabel(form.lectureType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">讲座日期</text>
          <text class="value">{{ form.lectureDate || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">讲座时间</text>
          <text class="value">{{ form.lectureStartTime || '-' }} 至 {{ form.lectureEndTime || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">参会申请人</text>
          <text class="value">{{ form.applicantName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">所属科室</text>
          <text class="value">{{ form.deptName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">讲座地点</text>
          <text class="value">{{ form.lectureAddress || '-' }}</text>
        </view>
      </view>

      <!-- 讲座细节 -->
      <view class="common-section-card mt20">
        <view class="section-title">讲座细节</view>
        <view class="info-row">
          <text class="label">主讲人职务</text>
          <text class="value">{{ form.lectureSpeakerDuties || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">主讲人职称</text>
          <text class="value">{{ form.lectureSpeakerTitles || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">主持人</text>
          <text class="value">{{ form.lectureHost || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">嘉宾</text>
          <text class="value">{{ form.lectureGuests || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">主讲人学术简历</text>
          <text class="value remark">{{ form.lectureSpeakerResume || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">观点综述</text>
          <text class="value remark">{{ form.lectureOpinion || '-' }}</text>
        </view>
      </view>

      <!-- 相关附件 -->
      <view class="common-section-card mt20" v-if="attachment.url">
        <view class="section-title">相关附件</view>
        <view class="common-file-list">
          <view class="file-item" @click="previewFile(attachment.url, attachment.name)">
            <view class="file-info">
              <text class="f-name">{{ attachment.name }}</text>
              <view class="f-meta">
                <text class="f-type">相关文档</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </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 { previewFile } from '@/utils/file';
import to from 'await-to-js';

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 attachment = computed(() => {
  if (!form.value || !form.value.lectureDocument) return { name: '', url: '' };
  try {
    const file = JSON.parse(form.value.lectureDocument);
    return {
      name: file.fileName || '讲座相关附件',
      url: file.lectureDocument || ''
    };
  } catch (e) {
    console.error('Failed to parse lectureDocument JSON', e);
    return { name: '附件', url: form.value.lectureDocument };
  }
});

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>
