<template>
  <div class="card shadow-sm rounded-xl overflow-hidden flex flex-col gap-4">
    <!-- 申请行为详情 -->
    <ApplyReportSection :data="applyData" />

    <!-- 放款还款详情 -->
    <BehaviorReportSection :data="behaviorData" />

    <!-- 大数据详情 -->
    <BigDataReportSection :data="bigDataData" />
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRiskNotifier } from '@/composables/useRiskNotifier'
import ApplyReportSection from './components/ApplyReportSection.vue'
import BehaviorReportSection from './components/BehaviorReportSection.vue'
import BigDataReportSection from './components/BigDataReportSection.vue'

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

// 获取数据
const rawData = computed(() => props.data?.data || props.data || {})

// 申请行为详情
const applyData = computed(() => rawData.value.apply_report_detail || {})

// 放款还款详情
const behaviorData = computed(() => rawData.value.behavior_report_detail || {})

// 大数据详情
const bigDataData = computed(() => rawData.value.current_report_detail || {})

// 计算风险评分（0-100分，分数越高越安全）
const riskScore = computed(() => {
  const apply = applyData.value
  const behavior = behaviorData.value
  const bigData = bigDataData.value

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

  let score = 100 // 初始满分

  // 申请准入分评估（1-1000，分数越高风险越大）
  const applyScore = parseInt(apply.A22160001) || 0
  if (applyScore > 800) {
    score -= 30 // 高风险
  } else if (applyScore > 600) {
    score -= 20 // 中高风险
  } else if (applyScore > 400) {
    score -= 10 // 中等风险
  }

  // 申请命中机构数评估
  const hitInstitutions = parseInt(apply.A22160003) || 0
  if (hitInstitutions > 20) {
    score -= 20 // 命中机构数过多
  } else if (hitInstitutions > 10) {
    score -= 10 // 命中机构数较多
  }

  // 贷款行为分评估（1-1000，分数越高风险越大）
  const behaviorScore = parseInt(behavior.B22170001) || 0
  if (behaviorScore > 800) {
    score -= 25 // 高风险
  } else if (behaviorScore > 600) {
    score -= 15 // 中高风险
  } else if (behaviorScore > 400) {
    score -= 8 // 中等风险
  }

  // 逾期评估
  const m0Overdue6m = parseInt(behavior.B22170025) || 0
  const m1Overdue6m = parseInt(behavior.B22170028) || 0
  if (m0Overdue6m > 5 || m1Overdue6m > 3) {
    score -= 20 // 逾期笔数过多
  } else if (m0Overdue6m > 2 || m1Overdue6m > 1) {
    score -= 10 // 逾期笔数较多
  }

  // 失败扣款评估
  const failedDeduction6m = parseInt(behavior.B22170037) || 0
  if (failedDeduction6m > 10) {
    score -= 15 // 失败扣款过多
  } else if (failedDeduction6m > 5) {
    score -= 8 // 失败扣款较多
  }

  // 正常还款比例评估
  const repaymentRatio = parseFloat(behavior.B22170034?.replace('%', '')) || 100
  if (repaymentRatio < 50) {
    score -= 25 // 还款比例过低
  } else if (repaymentRatio < 70) {
    score -= 15 // 还款比例较低
  } else if (repaymentRatio < 85) {
    score -= 8 // 还款比例中等
  }

  // 确保分数在10-100范围内
  return Math.max(10, Math.min(100, score))
})

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

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

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

