<template>
  <div class="card shadow-sm rounded-xl overflow-hidden flex flex-col gap-4">
    <!-- 信贷表现概览 -->
    <ProbeOverview :data="data" :result-code="data.result_code" />
    <!-- 逾期信息 -->
    <OverdueInfoSection :max-overdue-amt="data.max_overdue_amt" :max-overdue-days="data.max_overdue_days"
      :latest-overdue-time="data.latest_overdue_time" :currently-overdue="data.currently_overdue" />

    <!-- 履约信息 -->
    <PerformanceInfoSection :max-performance-amt="data.max_performance_amt"
      :latest-performance-time="data.latest_performance_time" :count-performance="data.count_performance"
      :currently-performance="data.currently_performance" />

    <!-- 机构状态 -->
    <InstitutionStatusSection :acc-exc="data.acc_exc" :acc-sleep="data.acc_sleep" />

  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRiskNotifier } from '@/composables/useRiskNotifier';
import ProbeOverview from './components/ProbeOverview.vue'
import OverdueInfoSection from './components/OverdueInfoSection.vue'
import PerformanceInfoSection from './components/PerformanceInfoSection.vue'
import InstitutionStatusSection from './components/InstitutionStatusSection.vue'

const props = defineProps({
  data: {
    type: Object,
    required: true,
    default: () => ({})
  },
  apiId: {
    type: String,
    default: '',
  },
  index: {
    type: Number,
    default: 0,
  },
  notifyRiskStatus: {
    type: Function,
    default: () => { },
  },
})

// 确保data是响应式的
const data = computed(() => props.data || {})

// 计算风险评分（0-100分，分数越高越安全）
const riskScore = computed(() => {
  const d = data.value;

  // 检查是否有数据
  if (!d || Object.keys(d).length === 0) {
    return 100; // 无数据视为最安全
  }

  // 当前是否逾期
  if (d.currently_overdue === '1') {
    // 当前逾期，根据逾期金额和天数计算风险
    const overdueDays = parseInt(d.max_overdue_days) || 0;
    const overdueAmt = parseFloat(d.max_overdue_amt) || 0;

    // 逾期天数超过90天或逾期金额超过10万，视为高风险
    if (overdueDays > 90 || overdueAmt > 100000) {
      return 10; // 极高风险
    }

    // 逾期天数超过30天或逾期金额超过5万，视为高风险
    if (overdueDays > 30 || overdueAmt > 50000) {
      return 20; // 高风险
    }

    // 逾期天数超过7天或逾期金额超过1万，视为中等风险
    if (overdueDays > 7 || overdueAmt > 10000) {
      return 40; // 中等风险
    }

    // 轻微逾期
    return 60; // 低风险
  }

  // 历史逾期记录
  if (d.max_overdue_days && parseInt(d.max_overdue_days) > 0) {
    const overdueDays = parseInt(d.max_overdue_days);

    // 历史逾期天数超过90天
    if (overdueDays > 90) {
      return 30; // 高风险
    }

    // 历史逾期天数超过30天
    if (overdueDays > 30) {
      return 50; // 中等风险
    }

    // 历史逾期天数超过7天
    if (overdueDays > 7) {
      return 70; // 低风险
    }

    // 轻微历史逾期
    return 85; // 较低风险
  }

  // 账户异常或睡眠
  if (d.acc_exc === '1' || d.acc_sleep === '1') {
    return 40; // 中等风险
  }

  // 有履约记录，且无逾期记录
  if (d.count_performance && parseInt(d.count_performance) > 0) {
    return 90; // 良好
  }

  // 无逾期记录，无履约记录
  return 100; // 最安全
});

// 使用 composable 通知父组件风险评分
useRiskNotifier(props, riskScore);

// 暴露给父组件
defineExpose({
  riskScore
});
</script>

<style scoped>
.card {
  background: #ffffff;
  border: 1px solid #e5e7eb;
}
</style>
