<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.projectName" />
        <CommonInfoRow label="项目编号" :value="form.projectCode" />
        <CommonInfoRow label="来源单位" :value="form.sourceUnit" />
        <CommonInfoRow label="开标时间" :value="form.bidTime ? formatDate(form.bidTime, 'YYYY-MM-DD HH:mm') : '-'" />
        <CommonInfoRow label="招标平台" :value="form.bidPlatform" />
        <CommonInfoRow label="申请人" :value="form.applyUser" />
        <CommonInfoRow label="所属学院" :value="form.deptName" />
        <CommonInfoRow label="负责人" :value="form.leaderUser" />
        <CommonInfoRow label="负责人电话" :value="form.leaderUserPhone" />
        <CommonInfoRow label="委托人" :value="form.entrustUser" />
        <CommonInfoRow label="备注" :value="form.remark" isColumn />
      </CommonSection>

      <!-- 借款信息 -->
      <CommonSection title="借款信息">
        <CommonInfoRow label="是否借款" :value="form.isLoan === 1 ? '是' : '否'" />
        <template v-if="form.isLoan === 1">
          <CommonInfoRow label="支付方式" :value="form.paymentMode" />
          <CommonInfoRow label="保证金金额" :value="'￥' + (form.guaranteeAmount || 0)" isAmount />
          <CommonInfoRow label="保证金作为中介服务费" :value="form.isAgentAmount === 1 ? '是' : '否'" />
        </template>
      </CommonSection>

      <!-- 附件信息 -->
      <AttachmentList :list="allFiles" 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 { formatDate } from '@/utils/date';
import AttachmentList from './AttachmentList.vue';
import CommonSection from '@/components/ui/CommonSection.vue';
import CommonInfoRow from '@/components/ui/CommonInfoRow.vue';
import { useDocumentApi } from '@/api/document';
import to from 'await-to-js';

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

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

const allFiles = computed(() => {
  if (!form.value) return [];
  const list: any[] = [];
  
  // 主附件
  if (form.value.fileUrl) {
    list.push({
      fileName: form.value.fileName || '主附件',
      fileUrl: form.value.fileUrl,
      fileType: '招标附件'
    });
  }
  
  // 借款附件
  const loanFiles = form.value.loanFileList || form.value.loanFiles || [];
  if (Array.isArray(loanFiles)) {
    loanFiles.forEach((f: any) => {
      if (f.fileUrl) {
        list.push({
          fileName: f.fileName || f.fileType,
          fileUrl: f.fileUrl,
          fileType: '借款附件'
        });
      }
    });
  }
  
  return list;
});

const fetchData = async () => {
  if (!props.code) return;
  loading.value = true;
  try {
    const [err, res] = await to(documentApi.getHorizontalBidByCode(props.code));
    if (!err && res?.data) {
      form.value = res.data;
    }
  } catch (error) {
    console.error('Fetch HorizontalBid error:', error);
  }
  loading.value = false;
};

onMounted(() => {
  fetchData();
});

watch(() => props.code, () => {
  fetchData();
});
</script>

<style lang="scss" scoped>
@import "./common.scss";
</style>
