<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">{{ getDictLabel('sci_review_type', form.reviewType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">项目类型</text>
          <text class="value">{{ getDictLabel('sci_preoject_type', form.projectType) }}</text>
        </view>
        <view class="info-row">
          <text class="label">项目名称</text>
          <text class="value">{{ form.projectName || '-' }}</text>
        </view>
      </view>

      <!-- 申请资料 -->
      <AttachmentList :list="form.fileList" title="申请资料" />

      <!-- 审查信息 -->
      <view class="common-section-card mt20">
        <view class="section-title">审查详情</view>
        <view class="info-row">
          <text class="label">审查方式</text>
          <text class="value">{{ form.reviewMethod === '10' ? '简易审查' : form.reviewMethod === '20' ? '会议审查' : '-' }}</text>
        </view>
        <view class="info-row">
          <text class="label">审查委员</text>
          <text class="value">{{ memberNames || '-' }}</text>
        </view>
        <view class="info-row" v-if="form.formReviewResult === '20'">
          <text class="label">审查说明</text>
          <text class="value">{{ form.remark || '-' }}</text>
        </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 { formatDate } from '@/utils/date';
import { previewFile } from '@/utils/file';
import to from 'await-to-js';

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

const { getDictLabel } = useDict('sci_review_type', 'sci_preoject_type');
const documentApi = useDocumentApi();

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

const memberNames = computed(() => {
  if (!form.value?.memberList?.length) return '';
  return form.value.memberList.map((m: any) => m.memberName || m.nickName).join('、');
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  const [err, res] = await to(documentApi.getReviewByCode(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";
.material-list {
  .material-item {
    background: #f8f9fc;
    border-radius: 12rpx;
    padding: 24rpx;
    margin-bottom: 20rpx;
    .m-header {
      display: flex;
      align-items: center;
      margin-bottom: 16rpx;
      .m-name {
        font-size: 28rpx;
        color: #333;
        font-weight: bold;
      }
      .m-required {
        color: #ff4d4f;
        margin-left: 8rpx;
      }
    }
    .m-meta {
      display: flex;
      flex-wrap: wrap;
      .m-cell {
        width: 50%;
        font-size: 24rpx;
        line-height: 1.6;
        .cl { color: #888; }
        .cv { color: #555; }
      }
    }
    .m-action {
      margin-top: 16rpx;
      text-align: right;
      font-size: 24rpx;
      color: var(--primary-color);
    }
  }
}
</style>
