<template>
    <div class="card rounded-lg border border-gray-200 mb-2">
        <div class="mt-4">
            <LTitle title="查验行为概览（次数 / 机构数 / 天数）" />

            <!-- 查验次数图表 -->
            <div class="h-64 px-2 mb-4">
                <v-chart class="chart-container" :option="countChartOption" autoresize />
            </div>

            <div class="mb-4 mt-4 space-y-4">
                <!-- 查验次数 -->
                <div>
                    <div class="mb-4 border border-gray-200 rounded-lg overflow-hidden mx-4">
                        <!-- 表头 -->
                        <div class="bg-[#5d7eeb] text-white">
                            <div class="grid grid-cols-5 text-sm">
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">周期
                                </div>
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">银行
                                </div>
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">消费金融
                                </div>
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">其他机构
                                </div>
                                <div class="py-3 text-center font-semibold whitespace-nowrap">总次数</div>
                            </div>
                        </div>
                        <!-- 数据行 -->
                        <div class="bg-white">
                            <div v-for="(row, idx) in queryCountRows" :key="row.key"
                                :class="['grid grid-cols-5 text-sm', idx < queryCountRows.length - 1 ? 'border-b border-gray-200' : '']">
                                <div
                                    class="py-3 text-center font-medium text-gray-800 border-r border-gray-200 whitespace-nowrap">
                                    {{ row.label }}</div>
                                <div class="py-3 text-center text-[#333333] border-r border-gray-200 whitespace-nowrap">
                                    {{ row.bank }}</div>
                                <div class="py-3 text-center text-[#333333] border-r border-gray-200 whitespace-nowrap">
                                    {{ row.fin }}</div>
                                <div class="py-3 text-center text-[#333333] border-r border-gray-200 whitespace-nowrap">
                                    {{ row.other }}</div>
                                <div class="py-3 text-center text-[#333333] font-semibold whitespace-nowrap">{{
                                    row.total }}</div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 查验机构数 -->
                <div>
                    <div class="mb-4 border border-gray-200 rounded-lg overflow-hidden mx-4">
                        <!-- 表头 -->
                        <div class="bg-[#5d7eeb] text-white">
                            <div class="grid grid-cols-5 text-sm">
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">周期
                                </div>
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">银行
                                </div>
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">消费金融
                                </div>
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">其他机构
                                </div>
                                <div class="py-3 text-center font-semibold whitespace-nowrap">总机构数</div>
                            </div>
                        </div>
                        <!-- 数据行 -->
                        <div class="bg-white">
                            <div v-for="(row, idx) in queryOrgRows" :key="row.key"
                                :class="['grid grid-cols-5 text-sm', idx < queryOrgRows.length - 1 ? 'border-b border-gray-200' : '']">
                                <div
                                    class="py-3 text-center font-medium text-gray-800 border-r border-gray-200 whitespace-nowrap">
                                    {{ row.label }}</div>
                                <div class="py-3 text-center text-[#333333] border-r border-gray-200 whitespace-nowrap">
                                    {{ row.bank }}</div>
                                <div class="py-3 text-center text-[#333333] border-r border-gray-200 whitespace-nowrap">
                                    {{ row.fin }}</div>
                                <div class="py-3 text-center text-[#333333] border-r border-gray-200 whitespace-nowrap">
                                    {{ row.other }}</div>
                                <div class="py-3 text-center text-[#333333] font-semibold whitespace-nowrap">{{
                                    row.total }}</div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 查验天数 -->
                <div>
                    <div class="mb-4 border border-gray-200 rounded-lg overflow-hidden mx-4">
                        <!-- 表头 -->
                        <div class="bg-[#5d7eeb] text-white">
                            <div class="grid grid-cols-2 text-sm">
                                <div class="py-3 text-center font-semibold border-r border-white whitespace-nowrap">周期
                                </div>
                                <div class="py-3 text-center font-semibold whitespace-nowrap">查验天数</div>
                            </div>
                        </div>
                        <!-- 数据行 -->
                        <div class="bg-white">
                            <div v-for="(row, idx) in queryDayRows" :key="row.key"
                                :class="['grid grid-cols-2 text-sm', idx < queryDayRows.length - 1 ? 'border-b border-gray-200' : '']">
                                <div
                                    class="py-3 text-center font-medium text-gray-800 border-r border-gray-200 whitespace-nowrap">
                                    {{ row.label }}</div>
                                <div class="py-3 text-center text-[#333333] font-semibold whitespace-nowrap">{{ row.days
                                    }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { computed } from 'vue'
import LTitle from '@/components/LTitle.vue'
import VChart from 'vue-echarts'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { BarChart } from 'echarts/charts'
import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
import { PERIODS, normalizeValue } from '../utils/dataParser'

use([CanvasRenderer, BarChart, GridComponent, LegendComponent, TooltipComponent])

const props = defineProps({
    data: {
        type: Object,
        required: true,
        default: () => ({})
    }
})

// 查验次数
const queryCountRows = computed(() => {
    const v = props.data || {}
    return PERIODS.map(p => {
        const prefix = `ppcm_${p.key}`
        return {
            key: p.key,
            label: p.label,
            bank: normalizeValue(v[`${prefix}_bank_qynum`]),
            fin: normalizeValue(v[`${prefix}_nbank_fin_qynum`]),
            other: normalizeValue(v[`${prefix}_nbank_other_qynum`]),
            total: normalizeValue(v[`${prefix}_qynum`])
        }
    })
})

// 查验次数汇总柱状图（总次数）
const countChartOption = computed(() => {
    const labels = PERIODS.map(p => p.label)
    const totals = queryCountRows.value.map(r => r.total)

    return {
        tooltip: {
            trigger: 'axis',
            axisPointer: { type: 'shadow' }
        },
        grid: {
            left: '3%',
            right: '4%',
            top: 20,
            bottom: 0,
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: labels,
            axisLabel: {
                fontSize: 12,
                color: '#6b7280'
            },
            axisLine: {
                lineStyle: {
                    color: '#e5e7eb'
                }
            }
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                fontSize: 12,
                color: '#6b7280',
                formatter: '{value} 次'
            },
            splitLine: {
                lineStyle: {
                    color: '#f3f4f6'
                }
            }
        },
        series: [
            {
                name: '总查验次数',
                type: 'bar',
                data: totals,
                barWidth: '35%',
                barMinHeight: 3,
                itemStyle: {
                    color: '#2B79EE',
                    borderRadius: [4, 4, 0, 0]
                }
            }
        ]
    }
})

// 查验机构数
const queryOrgRows = computed(() => {
    const v = props.data || {}
    return PERIODS.map(p => {
        const prefix = `ppcm_${p.key}`
        return {
            key: p.key,
            label: p.label,
            bank: normalizeValue(v[`${prefix}_bank_qyorg`]),
            fin: normalizeValue(v[`${prefix}_nbank_fin_qyorg`]),
            other: normalizeValue(v[`${prefix}_nbank_other_qyorg`]),
            total: normalizeValue(v[`${prefix}_qyorg`])
        }
    })
})

// 查验天数
const queryDayRows = computed(() => {
    const v = props.data || {}
    const dayKeys = ['m1', 'm3', 'm6', 'm12', 'm24']
    const labelMap = {
        m1: '近1个月',
        m3: '近3个月',
        m6: '近6个月',
        m12: '近1年',
        m24: '近2年'
    }
    return dayKeys.map(key => {
        const prefix = `ppcm_${key}`
        return {
            key,
            label: labelMap[key],
            days: normalizeValue(v[`${prefix}_qyday`])
        }
    })
})
</script>

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