<script setup>
import Investment from "./components/Investment.vue";
import { useRiskNotifier } from '@/composables/useRiskNotifier';
import SeniorExecutive from "./components/SeniorExecutive.vue";
import Lawsuit from "./components/Lawsuit.vue";
import TaxRisk from "./components/TaxRisk/index.vue";

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

// 投资类关系
const investRelations = ["sh", "his_sh", "lp", "his_lp"];

// 高管类关系
const managerRelations = ["tm", "his_tm"];

const totalCompanies = computed(() => {
    return data?.items?.length || 0;
});

// 获取投资企业记录（股东、历史股东、法人、历史法人）
const investCompanies = computed(() => {
    const items = data?.items || [];
    return items.filter((item) => {
        const relationships = item?.relationship || [];
        return relationships.some((r) => investRelations.includes(r));
    });
});

// 获取高管任职记录（高管、历史高管）
const managerPositions = computed(() => {
    const items = data?.items || [];
    return items.filter((item) => {
        const relationships = item?.relationship || [];
        return relationships.some((r) => managerRelations.includes(r));
    });
});

// 获取有涉诉风险的企业
const lawsuitCompanies = computed(() => {
    const items = data?.items || [];
    return items.filter((item) => {
        const lawsuit = item?.lawsuitInfo || {};
        return (
            (lawsuit.entout && lawsuit.entout.data && Object.keys(lawsuit.entout.data).length > 0) ||
            (lawsuit.sxbzxr && lawsuit.sxbzxr.data && lawsuit.sxbzxr.data.sxbzxr && lawsuit.sxbzxr.data.sxbzxr.length > 0) ||
            (lawsuit.xgbzxr && lawsuit.xgbzxr.data && lawsuit.xgbzxr.data.xgbzxr && lawsuit.xgbzxr.data.xgbzxr.length > 0)
        );
    });
});

// 获取有税务风险的企业（欠税公告或税收违法）
const taxRiskCompanies = computed(() => {
    const items = data?.items || [];
    return items.filter((item) => {
        const ownTax = item?.own_tax || {};
        const taxContravention = item?.tax_contravention || {};
        return (ownTax.total > 0 && ownTax.items && ownTax.items.length > 0) ||
            (taxContravention.total > 0 && taxContravention.items && taxContravention.items.length > 0);
    });
});

// 判断是否有风险（人企关系有数据就算有风险）
const hasRisk = computed(() => {
    return Object.keys(props.data || {}).length > 0;
});

// 计算风险评分（0-100分，分数越高越安全）
const riskScore = computed(() => {
    // 检查是否有数据
    if (!props.data || Object.keys(props.data).length === 0) {
        return 100; // 无数据视为最安全
    }

    // 获取各项风险数据
    const lawsuitCount = lawsuitCompanies.value.length;
    const taxRiskCount = taxRiskCompanies.value.length;
    const investCount = investCompanies.value.length;
    const managerCount = managerPositions.value.length;

    // 计算风险评分
    let score = 100; // 初始分数

    // 涉诉风险：每个涉诉企业扣15分
    if (lawsuitCount > 0) {
        score -= Math.min(lawsuitCount * 15, 60);
    }

    // 税务风险：每个税务风险企业扣10分
    if (taxRiskCount > 0) {
        score -= Math.min(taxRiskCount * 10, 40);
    }

    // 投资企业：每个投资企业扣2分（投资本身不算高风险）
    if (investCount > 0) {
        score -= Math.min(investCount * 2, 20);
    }

    // 高管任职：每个高管任职扣3分（高管本身不算高风险）
    if (managerCount > 0) {
        score -= Math.min(managerCount * 3, 20);
    }

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

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

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

<template>
    <div class="pb-8">
        <!-- 投资企业记录 -->
        <Investment :data="investCompanies" />

        <!-- 高管任职记录 -->
        <SeniorExecutive :data="managerPositions" />

        <!-- 涉诉风险 -->
        <Lawsuit :data="{ lawsuitCompanies: lawsuitCompanies }" />

        <!-- 税务风险 -->
        <TaxRisk :data="taxRiskCompanies" />
    </div>
</template>

<style scoped>
/* 添加过渡效果 */
.company-wrapper {
    overflow: hidden;
}

/* 添加阴影悬停效果 */
.company-wrapper>div:first-child {
    transform-origin: top;
}

/* 确保展开有足够的空间 */
.max-h-\[2000px\] {
    max-height: 2000px;
}
</style>
