<template>
  <div class="card rounded-lg border border-gray-200 pb-2 mb-2">
    <div class="flex items-center mb-4 p-4">
      <span class="font-bold text-gray-800">借贷申请统计概览</span>
    </div>
    <div class="grid grid-cols-2 gap-4 px-4 pb-4">
      <div v-for="item in items" :key="item.label"
        class="bg-blue-50 rounded-lg p-3 text-center border border-[#2B79EE1A]">
        <div class="text-xl font-bold text-[#111827]">
          {{ item.value }}
          <span class="text-xs text-gray-500 ml-1">{{ item.unit }}</span>
        </div>
        <div class="text-xs text-gray-600 mt-1">
          {{ item.label }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { getApplicationCounts, getBankOrgDetails, getNBankOrgDetails } from '../utils/dataParser'

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

const items = computed(() => {
  const v = props.data || {}

  // 近12个月申请次数（总/银行/非银）
  const m12 = getApplicationCounts(v, 'm12')

  // 近12个月申请机构总数（银行+非银）
  const bankOrgs = getBankOrgDetails(v, 'm12')
  const nbankOrgs = getNBankOrgDetails(v, 'm12')
  const bankOrgTotal = Object.values(bankOrgs || {}).reduce((sum, val) => sum + (val || 0), 0)
  const nbankOrgTotal = Object.values(nbankOrgs || {}).reduce((sum, val) => sum + (val || 0), 0)
  const orgTotal = bankOrgTotal + nbankOrgTotal

  // 近12个月周末 / 夜间申请次数（银行+非银）
  const weekendTotal =
    Number(v.als_m12_id_bank_week_allnum || 0) + Number(v.als_m12_id_nbank_week_allnum || 0)
  const nightTotal =
    Number(v.als_m12_id_bank_night_allnum || 0) + Number(v.als_m12_id_nbank_night_allnum || 0)

  return [
    { label: '总申请次数', value: m12.total || 0, unit: '次' },
    { label: '总申请机构数', value: orgTotal || 0, unit: '家' },
    { label: '银行申请次数', value: m12.bank || 0, unit: '次' },
    { label: '非银申请次数', value: m12.nbank || 0, unit: '次' },
    { label: '夜间申请次数', value: nightTotal || 0, unit: '次' },
    { label: '周末申请次数', value: weekendTotal || 0, unit: '次' },
  ]
})
</script>

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