<template>
  <div class="card" :class="{ 'edit-mode': editMode }">
    <div class="card-header">
      <div class="card-title">{{ t(title) }}</div>
      <div v-if="editMode" class="card-actions">
        <button class="action-btn" @click="$emit('edit')" :title="t('Edit')">
          <Icon icon="tabler:edit" />
        </button>
        <button class="action-btn drag-handle" :title="t('Drag')">
          <Icon icon="tabler:grip-vertical" />
        </button>
        <n-dropdown trigger="click" :options="menuOptions" @select="handleMenuSelect">
          <button class="action-btn" :title="t('More')">
            <Icon icon="tabler:dots" />
          </button>
        </n-dropdown>
      </div>
    </div>
    <div class="card-body">
      <div class="card-links">
        <a v-for="link in links" :key="link.label" class="link" @click.prevent="openLink(link)">
          <span class="link-text">{{ t(link.label) }}</span>
          <Icon class="link-icon" icon="tabler:arrow-up-right" />
        </a>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, h } from 'vue'
import { Icon } from '@iconify/vue'
import { NDropdown } from 'naive-ui'
import { useRouter } from 'vue-router'
import { t } from '@/shared/i18n'
import { pageTypeToSlug } from '@/shared/utils/slug'

const props = defineProps<{
  title: string
  links: Array<{ label: string; link_type: string; link_to: string }>
  editMode: boolean
}>()

const emit = defineEmits<{
  'edit': []
  'delete': []
}>()

const router = useRouter()

const menuOptions = computed(() => [
  { label: t('Delete'), key: 'delete', icon: () => h(Icon, { icon: 'tabler:trash', size: 16 }) }
])

function openLink(link: { link_type: string; link_to: string }) {
  if (!link) return
  const to = (link.link_type || '').toLowerCase()
  if (to === 'pagetype' || to === 'report') {
    const slug = pageTypeToSlug(link.link_to)
    router.push(`/app/${slug}`)
  } else if (to === 'page') {
    router.push(`/${link.link_to}`)
  }
}

function handleMenuSelect(key: string) {
  if (key === 'delete') {
    emit('delete')
  }
}
</script>

<style scoped>
.card {
  background: #fff;
  border: 1px solid #eef2f7;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(15,23,42,0.04);
  position: relative;
  transition: all 0.2s;
}

.edit-mode .card {
  border: 2px dashed transparent;
}

.card-header {
  padding: 14px 16px;
  border-bottom: 1px solid #f1f5f9;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title {
  font-weight: 600;
  color: #0f172a;
}

.card-body {
  padding: 12px 16px;
  color: #334155;
}

.card-links {
  display: grid;
  gap: 8px;
}

.link {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: #0f172a;
  text-decoration: none;
  cursor: pointer;
}

.link:hover {
  color: #18a058;
}

.link-text {
  flex: 1;
}

.link-icon {
  color: #94a3b8;
  font-size: 13px;
  transition: all 0.15s ease;
  margin-left: 2px;
}

.link:hover .link-icon {
  color: #18a058;
  transform: translate(1px, -1px);
}

.card-actions {
  display: flex;
  gap: 4px;
  align-items: center;
  opacity: 0;
  transition: opacity 0.2s;
}

.edit-mode:hover .card-actions {
  opacity: 1;
}

.action-btn {
  background: transparent;
  border: none;
  color: #64748b;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  font-size: 16px;
}

.action-btn:hover {
  background: #f1f5f9;
  color: #0f172a;
}

.drag-handle {
  cursor: grab;
}

.drag-handle:active {
  cursor: grabbing;
}
</style>
