<template>
    <div class="flex flex-col gap-4">
        <!-- 多头共债子分 -->
        <MultipleDebtScoreSection :data="riskData.multipleDebtScore" />

        <!-- 多头申请 -->
        <MultipleApplicationSection :data="riskData.multipleApplication" :query-days="riskData.queryDays" />

        <!-- 多头逾期 -->
        <MultipleOverdueSection :data="riskData.multipleOverdue" />

        <!-- 圈团风险 -->
        <CircleRiskSection :data="riskData.circleRisk" />

        <!-- 可疑欺诈风险 -->
        <FraudRiskSection :data="riskData.fraudRisk" />

    </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRiskNotifier } from '@/composables/useRiskNotifier';
import MultipleDebtScoreSection from './components/MultipleDebtScoreSection.vue'
import MultipleApplicationSection from './components/MultipleApplicationSection.vue'
import MultipleOverdueSection from './components/MultipleOverdueSection.vue'
import CircleRiskSection from './components/CircleRiskSection.vue'
import FraudRiskSection from './components/FraudRiskSection.vue'
import LRemark from '@/components/LRemark.vue'
import { parseRiskData } from './utils/riskDataParser'

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

// 解析风险数据
const riskData = computed(() => {
    // 尝试多种可能的数据路径
    const riskInfo = props.data?.['riskInfo_report_v3.1'] ||
        props.data?.riskInfo_report_v3_1 ||
        props.data?.riskInfo_report_v3?.['1'] ||
        []
    return parseRiskData(riskInfo)
})

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

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

    let score = 100 // 初始满分

    // 多头共债子分评估（分数越高风险越大，0-100分）
    const debtScores = [
        data.multipleDebtScore?.general || 0,
        data.multipleDebtScore?.shortCycle || 0,
        data.multipleDebtScore?.longCycle || 0,
        data.multipleDebtScore?.nonBank || 0,
        data.multipleDebtScore?.bank || 0,
    ]
    const maxDebtScore = Math.max(...debtScores)
    if (maxDebtScore > 80) {
        score -= 30 // 高风险
    } else if (maxDebtScore > 60) {
        score -= 20 // 中高风险
    } else if (maxDebtScore > 40) {
        score -= 10 // 中等风险
    }

    // 圈团风险评估
    const circleRiskScore = data.circleRisk?.riskScore3 || 0
    const circleRiskLevel = data.circleRisk?.riskLevel2 || 0
    if (circleRiskScore > 80 || circleRiskLevel === 3) {
        score -= 25 // 高风险
    } else if (circleRiskScore > 60 || circleRiskLevel === 2) {
        score -= 15 // 中风险
    } else if (circleRiskScore > 40) {
        score -= 8 // 低风险
    }

    // 可疑欺诈风险评估
    const fraudRiskLevel = data.fraudRisk?.riskLevel1 || 0
    if (fraudRiskLevel === 3) {
        score -= 20 // 高风险
    } else if (fraudRiskLevel === 2) {
        score -= 10 // 中风险
    }

    // 多头申请次数评估
    const totalApplication = data.multipleApplication?.applicationCounts?.['7d']?.total || 0
    if (totalApplication > 20) {
        score -= 15 // 申请次数过多
    } else if (totalApplication > 10) {
        score -= 8 // 申请次数较多
    }

    // 多头逾期评估
    const overduePlatforms = data.multipleOverdue?.overduePlatforms?.['1week'] || 0
    if (overduePlatforms > 5) {
        score -= 20 // 逾期平台数过多
    } else if (overduePlatforms > 2) {
        score -= 10 // 逾期平台数较多
    }

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

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

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