<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.codTitle || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">会议类型</text>
          <text class="value">{{ getLecTypeLabel(form.codType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">主办单位</text>
          <text class="value">{{ form.codOrganizer || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">会议日期</text>
          <text class="value">{{ form.codStartTime || '-' }} 至 {{ form.codEndTime || '-' }}</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.codAddress || '-' }}</text>
        </view>
        <view class="info-row column">
          <text class="label">会议简介</text>
          <text class="value remark">{{ form.codBriefInfo || '-' }}</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.paperName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">第一作者</text>
          <text class="value">{{ form.firstName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">通讯作者</text>
          <text class="value">{{ form.phoneName || '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">交流方式</text>
          <text class="value">{{ getDictLabel('sci_communicate_type', form.communicateType) }}</text>
        </view>
        <view class="info-row column">
          <text class="label">论文摘要</text>
          <text class="value remark">{{ form.paperSummary || '-' }}</text>
        </view>
      </view>

      <!-- 相关附件 -->
      <view class="common-section-card mt20" v-if="uploadList.length">
        <view class="section-title">相关附件</view>
        <view class="common-file-list">
          <view class="file-item" v-for="(file, index) in uploadList" :key="index" @click="previewFile(file.url, file.name)">
            <view class="file-info" v-if="file && file.url">
              <text class="f-name">{{ file.name }}</text>
              <view class="f-meta">
                <text class="f-type">{{ file.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 { useDict } from '@/hooks/useDict';
import { useDocumentApi } from '@/api/document';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';

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 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>
