<template>
    <div class="card shadow-sm rounded-xl overflow-hidden">
        <!-- 标题 -->
        <div class="border border-[#EEEEEE]  rounded-xl">
            <div class="flex items-center mb-3 p-4">
                <div class="w-8 h-8 flex items-center justify-center mr-2">
                    <img src="@/assets/images/report/swfx.png" alt="税务风险" class="w-8 h-8 object-contain" />
                </div>
                <span class="font-bold text-gray-800">税务风险</span>
            </div>
            <!-- 统计总结头部 -->
            <div class="">
                <!-- 欠税公告统计 -->
                <div class="mb-4">
                    <LTitle title="欠税公告统计" type="red" class="mb-4" />
                    <div class="grid grid-cols-2 gap-3 p-4">
                        <div class="bg-[#EB3C3C1A] rounded-lg p-4 border border-[#EB3C3C8F] text-center">
                            <div class="text-xl font-bold text-[#EB3C3C] mb-1">{{ ownTaxStats.companyCount }}<span
                                    class=" font-normal text-[#EB3C3C] ml-1">家</span></div>
                            <div class="text-sm text-gray-600">涉及企业</div>
                        </div>
                        <div class="bg-[#EB3C3C1A] rounded-lg p-4 border border-[#EB3C3C8F] text-center">
                            <div class="text-xl font-bold text-[#EB3C3C] mb-1">{{ ownTaxStats.recordCount }}<span
                                    class="font-normal text-[#EB3C3C] ml-1">条</span></div>
                            <div class="text-sm text-gray-600">欠税记录</div>
                        </div>
                        <div class="col-span-1 bg-[#EB3C3C1A] rounded-lg p-4 border border-[#EB3C3C8F] text-center">
                            <div class="text-xl font-bold text-[#EB3C3C] mb-1">{{ ownTaxStats.totalAmount }}<span
                                    class=" font-normal text-[#EB3C3C] ml-1"></span></div>
                            <div class="text-sm text-gray-600">欠税总额</div>
                        </div>
                    </div>
                </div>

                <!-- 税收违法统计 -->
                <div>
                    <LTitle title="税收违法统计" type="red" class="mb-2" />
                    <div class="grid grid-cols-2 gap-3 p-4">
                        <div class="bg-[#EB3C3C1A] rounded-lg p-4 border border-[#EB3C3C8F] text-center">
                            <div class="text-xl font-bold text-[#EB3C3C] mb-1">{{ taxContraventionStats.companyCount
                                }}<span class="font-normal text-[#EB3C3C] ml-1">家</span></div>
                            <div class="text-sm text-gray-600">涉及企业</div>
                        </div>
                        <div class="bg-[#EB3C3C1A] rounded-lg p-4 border border-[#EB3C3C8F] text-center">
                            <div class="text-xl font-bold text-[#EB3C3C] mb-1">{{ taxContraventionStats.recordCount
                                }}<span class="font-normal text-[#EB3C3C] ml-1">条</span></div>
                            <div class="text-sm text-gray-600">违法记录</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 欠税公告 -->
        <OwnTax :data="ownTaxData" class="mt-4" />

        <!-- 税收违法 -->
        <TaxContravention class="mt-4" :data="taxContraventionData" />

        <!-- 温馨提示 -->
    </div>
    <LRemark
        content="税务风险展示申请人相关企业的税务相关风险信息，包括欠税公告和税收违法记录。欠税公告数据来源于国家税务总局和各地方税务局的欠税公告，税收违法数据来源于国家税务总局和各地方税务局的违法案件公示。税务风险记录可能对企业信用评级、经营资质和融资能力产生负面影响。建议关注欠税金额、违法性质和整改情况，及时了解企业的税务合规状况。" />

</template>

<script setup>
import { computed } from 'vue'
import OwnTax from './OwnTax.vue'
import TaxContravention from './TaxContravention.vue'
import LTitle from '@/components/LTitle.vue'
import LRemark from '@/components/LRemark.vue'
import { useRiskNotifier } from '@/composables/useRiskNotifier'

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

// 获取欠税公告数据
const ownTaxData = computed(() => {
    const ownTaxCompanies = props.data?.items?.filter((item) => {
        const ownTax = item?.own_tax || {};
        return ownTax.total > 0 && ownTax.items && ownTax.items.length > 0;
    }) || [];

    // 提取所有欠税公告记录
    const allOwnTax = []
    ownTaxCompanies.forEach(company => {
        if (company.own_tax?.items) {
            company.own_tax.items.forEach(tax => {
                allOwnTax.push({
                    ...tax,
                    companyName: company.orgName,
                    companyInfo: {
                        orgName: company.orgName,
                        relationship: company.relationship,
                        basicInfo: company.basicInfo
                    }
                })
            })
        }
    })

    return { items: allOwnTax, total: allOwnTax.length }
})

// 获取税收违法数据
const taxContraventionData = computed(() => {
    const taxContraventionCompanies = props.data?.items?.filter((item) => {
        const taxContravention = item?.tax_contravention || {};
        return taxContravention.total > 0 && taxContravention.items && taxContravention.items.length > 0;
    }) || [];

    // 提取所有税收违法记录
    const allTaxContravention = []
    taxContraventionCompanies.forEach(company => {
        if (company.tax_contravention?.items) {
            company.tax_contravention.items.forEach(contravention => {
                allTaxContravention.push({
                    ...contravention,
                    companyName: company.orgName,
                    companyInfo: {
                        orgName: company.orgName,
                        relationship: company.relationship,
                        basicInfo: company.basicInfo
                    }
                })
            })
        }
    })

    return { items: allTaxContravention, total: allTaxContravention.length }
})

// 欠税公告统计
const ownTaxStats = computed(() => {
    const ownTaxCompanies = props.data?.items?.filter((item) => {
        const ownTax = item?.own_tax || {};
        return ownTax.total > 0 && ownTax.items && ownTax.items.length > 0;
    }) || [];

    let totalAmount = 0;
    let recordCount = 0;

    ownTaxCompanies.forEach(company => {
        if (company.own_tax?.items) {
            company.own_tax.items.forEach(tax => {
                recordCount++;
                // 尝试解析欠税金额
                const amount = parseFloat(tax.ownTaxAmount) || 0;
                totalAmount += amount;
            });
        }
    });

    return {
        companyCount: ownTaxCompanies.length,
        recordCount,
        totalAmount: totalAmount > 0 ? `${totalAmount.toFixed(2)} 元` : '—'
    };
});

// 税收违法统计
const taxContraventionStats = computed(() => {
    const taxContraventionCompanies = props.data?.items?.filter((item) => {
        const taxContravention = item?.tax_contravention || {};
        return taxContravention.total > 0 && taxContravention.items && taxContravention.items.length > 0;
    }) || [];

    let recordCount = 0;

    taxContraventionCompanies.forEach(company => {
        if (company.tax_contravention?.items) {
            company.tax_contravention.items.forEach(contravention => {
                recordCount++;
            });
        }
    });

    return {
        companyCount: taxContraventionCompanies.length,
        recordCount
    };
});

// 计算风险评分（0-100分，分数越高越安全）
const riskScore = computed(() => {
    const ownTaxList = ownTaxData.value || [];
    const taxContraventionList = taxContraventionData.value || [];

    // 没有税务风险 -> 无风险 -> 100分
    if (ownTaxList.length === 0 && taxContraventionList.length === 0) {
        return 100;
    }

    // 有税务风险 -> 高风险 -> 40分
    return 40;
});

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

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

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

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

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