<template>
  <footer class="py-8 text-center border-t border-white/5 relative z-10">
    <div class="flex justify-center items-center gap-4 mb-4">
      <a
        v-if="contactEmail"
        :href="`mailto:${contactEmail}`"
        class="text-xs text-art-muted/50 hover:text-art-accent transition-colors"
      >
        {{ contactEmail }}
      </a>
      <a @click="openAdmin" class="text-xs text-art-muted/30 hover:text-art-accent cursor-pointer">管理入口</a>
    </div>
    <p v-if="footerIcp" class="text-art-muted text-xs font-mono tracking-widest opacity-50 mb-2">
      <a
        v-if="icpHref"
        :href="icpHref"
        target="_blank"
        rel="noopener noreferrer"
        class="hover:text-art-accent transition-colors"
      >
        {{ footerIcp }}
      </a>
      <span v-else>{{ footerIcp }}</span>
    </p>
    <p class="text-art-muted text-xs font-mono tracking-widest opacity-50">
      &copy; {{ currentYear }} {{ siteTitle }}. 保留所有权利.
    </p>
  </footer>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { usePublicSettings } from '../composables/usePublicSettings'

const { cache } = usePublicSettings()

const currentYear = new Date().getFullYear()
const siteTitle = computed(() => cache.value?.site_title?.trim() || '年糕崽崽')
const footerIcp = computed(() => cache.value?.footer_icp?.trim() || '')
const contactEmail = computed(() => cache.value?.contact_email?.trim() || '')

const icpHref = computed(() => {
  const icp = footerIcp.value
  if (!icp) return null
  if (/ICP|备案|备\d|icp/i.test(icp) || /\d{8,}/.test(icp)) {
    return 'https://beian.miit.gov.cn/'
  }
  return null
})

const openAdmin = () => {
  window.open('/admin', '_blank')
}
</script>
