<template>
  <div class="flex flex-col gap-4">
    <!-- 评分卡片 -->
    <ScoreSection
      :score="behavScore"
      :latest-query-text="latestQueryText"
    />

    <!-- 查验次数时间分布 -->
    <QueryTrendSection :data="ppcmData" />

    <!-- 查验次数 / 机构数 / 天数表格概览 -->
    <QueryOverviewSection :data="ppcmData" />

    <!-- 还款成功次数趋势 -->
    <RepaySuccessTrendSection :data="ppcmData" />

    <!-- 还款失败次数趋势 -->
    <RepayFailTrendSection :data="ppcmData" />

    <!-- 余额不足导致还款失败占比 -->
    <InsufficientRatioSection :data="ppcmData" />

    <!-- 还款机构数与等级 -->
    <RepayOrgAndLevelSection :data="ppcmData" />

    <!-- 借款与逾期概览 -->
    <LoanAndOverdueSection :data="ppcmData" />

    <!-- 基础信息与查贷比 -->
    <BasicInfoSection :data="ppcmData" />
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRiskNotifier } from '@/composables/useRiskNotifier'
import {
  extractPpcmData,
  getBehavScore,
  getLatestQueryBucket,
  mapScoreTo100
} from './utils/dataParser'
import ScoreSection from './components/ScoreSection.vue'
import QueryTrendSection from './components/QueryTrendSection.vue'
import QueryOverviewSection from './components/QueryOverviewSection.vue'
import RepaySuccessTrendSection from './components/RepaySuccessTrendSection.vue'
import RepayFailTrendSection from './components/RepayFailTrendSection.vue'
import InsufficientRatioSection from './components/InsufficientRatioSection.vue'
import RepayOrgAndLevelSection from './components/RepayOrgAndLevelSection.vue'
import LoanAndOverdueSection from './components/LoanAndOverdueSection.vue'
import BasicInfoSection from './components/BasicInfoSection.vue'

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

// 原始数据：可能是 { code,data } 或 { data:{code,data} }
const rawData = computed(() => props.data?.data || props.data || {})

// 提取 ppcm 指标对象
const ppcmData = computed(() => extractPpcmData(rawData.value))

// 原始行为评分（300-900）
const behavScore = computed(() => getBehavScore(ppcmData.value))

// 最近一次查验距今天数说明
const latestQueryText = computed(() => getLatestQueryBucket(ppcmData.value))

// 将评分映射到 0-100 分，用于统一风险分
const riskScore = computed(() => {
  return mapScoreTo100(behavScore.value)
})

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

defineExpose({
  riskScore
})
</script>

<style scoped>
.card {
  background: #ffffff;
}
</style>


