<template>
  <div class="flex flex-col gap-4">
    <!-- 统计概览小模块（内部自带维度切换按钮） -->
    <SummaryApplyStats v-model:dimension="activeDimension" :data="variableValue" />

    <!-- 申请次数 -->
    <ApplicationCountSection :data="variableValue" :dimension="activeDimension" />

    <!-- 产品类型申请分布 -->
    <ProductTypeDistributionSection :data="variableValue" :dimension="activeDimension" />

    <!-- 近期集中申请提示 -->
    <RecentIntensiveApplicationSection :data="variableValue" :dimension="activeDimension" />

    <!-- 申请总次数 (银行+非银) -->
    <ApplicationTotalSection :data="variableValue" :dimension="activeDimension" />

    <!-- 申请机构总数 (银行+非银) -->
    <InstitutionTotalSection :data="variableValue" :dimension="activeDimension" />
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRiskNotifier } from '@/composables/useRiskNotifier'
import { extractVariableValue } from './utils/dataParser'
import ApplicationCountSection from './components/ApplicationCountSection.vue'
import ApplicationTotalSection from './components/ApplicationTotalSection.vue'
import InstitutionTotalSection from './components/InstitutionTotalSection.vue'
import SummaryApplyStats from './components/SummaryApplyStats.vue'
import ProductTypeDistributionSection from './components/ProductTypeDistributionSection.vue'
import RecentIntensiveApplicationSection from './components/RecentIntensiveApplicationSection.vue'

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

// 当前维度：id（身份证） / cell（手机号）
const activeDimension = ref('id')

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

// 提取 variableValue
const variableValue = computed(() => extractVariableValue(rawData.value))

// 计算风险评分（0-100分，分数越高越安全）
const riskScore = computed(() => {
  const data = variableValue.value
  const dimKey = activeDimension.value === 'cell' ? 'cell' : 'id'

  if (!data || Object.keys(data).length === 0) {
    return 100 // 无数据视为最安全
  }

  let score = 100 // 初始满分

  // 近7天申请次数评估
  const d7Total =
    parseInt(data[`als_d7_${dimKey}_bank_allnum`] || 0) +
    parseInt(data[`als_d7_${dimKey}_nbank_allnum`] || 0)
  if (d7Total > 5) {
    score -= 20 // 近7天申请过多
  } else if (d7Total > 3) {
    score -= 10
  }

  // 近15天申请次数评估
  const d15Total =
    parseInt(data[`als_d15_${dimKey}_bank_allnum`] || 0) +
    parseInt(data[`als_d15_${dimKey}_nbank_allnum`] || 0)
  if (d15Total > 10) {
    score -= 15
  } else if (d15Total > 5) {
    score -= 8
  }

  // 近1个月申请次数评估
  const m1Total =
    parseInt(data[`als_m1_${dimKey}_bank_allnum`] || 0) +
    parseInt(data[`als_m1_${dimKey}_nbank_allnum`] || 0)
  if (m1Total > 15) {
    score -= 20
  } else if (m1Total > 8) {
    score -= 10
  }

  // 近3个月申请次数评估
  const m3Total =
    parseInt(data[`als_m3_${dimKey}_bank_allnum`] || 0) +
    parseInt(data[`als_m3_${dimKey}_nbank_allnum`] || 0)
  if (m3Total > 20) {
    score -= 15
  } else if (m3Total > 10) {
    score -= 8
  }

  // 近6个月申请次数评估
  const m6Total =
    parseInt(data[`als_m6_${dimKey}_bank_allnum`] || 0) +
    parseInt(data[`als_m6_${dimKey}_nbank_allnum`] || 0)
  if (m6Total > 30) {
    score -= 10
  } else if (m6Total > 15) {
    score -= 5
  }

  // 确保分数在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>
