<template>
  <div class="card">
    <!-- 风险概览 -->
    <RiskOverview :data="riskData" />

    <!-- 信用评分 -->
    <CreditScores :data="riskData" />

    <!-- 贷款行为分析 -->
    <LoanBehaviorAnalysis :data="riskData" />

    <!-- 机构分析 -->
    <InstitutionAnalysis :data="riskData" />

    <!-- 时间趋势分析 -->
    <TimeTrendAnalysis :data="riskData" />

    <!-- 风险指标详情 -->
    <RiskIndicators :data="riskData" />

    <!-- 专业建议 -->
    <RiskAdvice :data="riskData" />
  </div>
</template>

<script>
import RiskOverview from './components/RiskOverview.vue'
import CreditScores from './components/CreditScores.vue'
import LoanBehaviorAnalysis from './components/LoanBehaviorAnalysis.vue'
import InstitutionAnalysis from './components/InstitutionAnalysis.vue'
import TimeTrendAnalysis from './components/TimeTrendAnalysis.vue'
import RiskIndicators from './components/RiskIndicators.vue'
import RiskAdvice from './components/RiskAdvice.vue'

export default {
  name: 'LoanRiskReport',
  components: {
    RiskOverview,
    CreditScores,
    LoanBehaviorAnalysis,
    InstitutionAnalysis,
    TimeTrendAnalysis,
    RiskIndicators,
    RiskAdvice
  },
  props: {
    data: {
      type: Object,
      default: () => ({})
    },
    apiId: {
      type: String,
      default: '',
    },
    index: {
      type: Number,
      default: 0,
    },
    notifyRiskStatus: {
      type: Function,
      default: () => { },
    },
  },
  computed: {
    riskData() {
      return this.data || {}
    },
    hasRisk() {
      return Object.keys(this.data || {}).length > 0;
    },
    riskScore() {
      const d = this.riskData;
      
      // 检查是否有数据
      if (!d || Object.keys(d).length === 0) {
        return 100; // 无数据视为最安全
      }

      // 根据风险概览数据计算评分
      // 假设数据中有风险评分字段
      const riskLevel = d.riskLevel || d.risk_level || '';
      const riskScore = d.riskScore || d.risk_score || 0;

      // 如果有风险评分，直接使用
      if (riskScore > 0) {
        // 风险评分转换为安全评分（分数越高越安全）
        // 假设风险评分是0-100，分数越高风险越大
        return Math.max(0, 100 - riskScore);
      }

      // 根据风险等级计算
      if (riskLevel === 'high' || riskLevel === 'HIGH' || riskLevel === '高风险') {
        return 20; // 高风险
      }
      if (riskLevel === 'medium' || riskLevel === 'MEDIUM' || riskLevel === '中风险') {
        return 60; // 中等风险
      }
      if (riskLevel === 'low' || riskLevel === 'LOW' || riskLevel === '低风险') {
        return 80; // 低风险
      }

      // 默认中等风险
      return 70;
    }
  },
  watch: {
    riskScore(newValue) {
      if (this.apiId && this.notifyRiskStatus) {
        this.notifyRiskStatus(this.apiId, this.index, newValue);
      }
    }
  },
  mounted() {
    // 立即通知一次
    if (this.apiId && this.notifyRiskStatus) {
      this.notifyRiskStatus(this.apiId, this.index, this.riskScore);
    }
  }
}
</script>

<style scoped></style>
