<template>
  <div class="tags-container">
    <h1 class="page-title">标签管理</h1>
    
    <!-- Action Buttons -->
    <div class="action-bar">
      <button class="btn-primary" @click="router.push('/admin/tags/create')">
        <span class="btn-icon">➕</span>
        新建标签
      </button>
    </div>

    <AdminTableFilter
      v-model:keyword="keyword"
      keyword-placeholder="搜索标签名称..."
      @search="applyFilter"
      @reset="resetFilters"
    />
    
    <!-- Tags Table -->
    <div class="table-container">
      <table class="admin-table">
        <thead>
          <tr>
            <th>ID</th>
            <th>名称</th>
            <th>描述</th>
            <th>创建时间</th>
            <th>更新时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="tag in filteredTags" :key="tag.id">
            <td class="table-cell">{{ tag.id }}</td>
            <td class="table-cell">{{ tag.name }}</td>
            <td class="table-cell">-</td>
            <td class="table-cell">{{ tag.createdAt }}</td>
            <td class="table-cell">{{ tag.updatedAt }}</td>
            <td class="table-cell actions">
              <button class="btn-edit" @click="router.push(`/admin/tags/${tag.id}/edit`)" title="编辑">
                ✏️
              </button>
              <button class="btn-delete" @click="handleDeleteTag(tag.id)" title="删除">
                🗑️
              </button>
            </td>
          </tr>
        </tbody>
      </table>
      
      <!-- Empty State -->
      <div v-if="filteredTags.length === 0" class="empty-state">
        <p>暂无标签数据</p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { adminGetTags, deleteTag, type Tag } from '../../services/api'
import { useAppliedKeyword, matchKeywordFn } from '../../composables/useLocalTableFilter'
import AdminTableFilter from '../../components/admin/AdminTableFilter.vue'

const router = useRouter()
const tags = ref<Tag[]>([])
const { keyword, appliedKeyword, apply, reset } = useAppliedKeyword()

const filteredTags = computed(() => tags.value.filter(t =>
  matchKeywordFn(t, appliedKeyword.value, item => item.name)
))

const applyFilter = () => apply()
const resetFilters = () => reset()

// 获取标签列表
const fetchTags = async () => {
  try {
    const data = await adminGetTags()
    tags.value = data
  } catch (error) {
    console.error('Failed to fetch tags:', error)
  }
}

// 删除标签
const handleDeleteTag = async (id: number) => {
  if (confirm('确定要删除这个标签吗？')) {
    try {
      await deleteTag(id)
      fetchTags() // 重新获取标签列表
    } catch (error) {
      console.error('Failed to delete tag:', error)
    }
  }
}

onMounted(() => {
  fetchTags()
})
</script>

<style scoped>
.tags-container {
  width: 100%;
}

.page-title {
  font-size: 1.75rem;
  font-weight: 600;
  color: #d4b383;
  margin-bottom: 1.5rem;
  font-family: 'Inter', sans-serif;
}

.action-bar {
  display: flex;
  justify-content: flex-start;
  margin-bottom: 1rem;
}

.btn-primary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 1.25rem;
  background: rgba(212, 179, 131, 0.1);
  border: 1px solid #d4b383;
  color: #d4b383;
  border-radius: 0.5rem;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 500;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  font-family: 'Inter', sans-serif;
}

.btn-primary:hover {
  background: rgba(212, 179, 131, 0.2);
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(212, 179, 131, 0.15);
}

.btn-icon {
  font-size: 1rem;
}

.table-container {
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  overflow: hidden;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-family: 'Inter', sans-serif;
}

.admin-table thead {
  background: rgba(255, 255, 255, 0.08);
}

.admin-table th {
  padding: 1rem;
  text-align: left;
  font-size: 0.875rem;
  font-weight: 600;
  color: #d4b383;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.admin-table td {
  padding: 1rem;
  font-size: 0.95rem;
  color: rgba(255, 255, 255, 0.8);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.admin-table tr:last-child td {
  border-bottom: none;
}

.admin-table tr:hover {
  background: rgba(255, 255, 255, 0.05);
}

.actions {
  display: flex;
  gap: 0.5rem;
}

.btn-edit, .btn-delete {
  padding: 0.5rem;
  border: none;
  border-radius: 0.375rem;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.btn-edit {
  background: rgba(59, 130, 246, 0.1);
  color: rgba(59, 130, 246, 0.8);
}

.btn-edit:hover {
  background: rgba(59, 130, 246, 0.2);
  transform: translateY(-1px);
}

.btn-delete {
  background: rgba(239, 68, 68, 0.1);
  color: rgba(239, 68, 68, 0.8);
}

.btn-delete:hover {
  background: rgba(239, 68, 68, 0.2);
  transform: translateY(-1px);
}

.empty-state {
  padding: 3rem;
  text-align: center;
  color: rgba(255, 255, 255, 0.5);
  font-size: 1rem;
}
</style>