<template>
  <div class="admin-snippets">
    <h1 class="page-title">代码片段管理</h1>
    
    <div class="toolbar">
      <router-link to="/admin/snippets/create" class="btn btn-primary">
        + 新增代码片段
      </router-link>
    </div>
    
    <div class="table-container">
      <table class="admin-table">
        <thead>
          <tr>
            <th>ID</th>
            <th>标题</th>
            <th>类型</th>
            <th>查看次数</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="snippet in snippets" :key="snippet.id">
            <td>{{ snippet.id }}</td>
            <td>{{ snippet.title }}</td>
            <td>
              <span class="type-badge">{{ snippet.type }}</span>
            </td>
            <td>{{ snippet.viewCount || 0 }}</td>
            <td class="actions">
              <router-link :to="`/admin/snippets/${snippet.id}/edit`" class="btn btn-sm btn-secondary">
                编辑
              </router-link>
              <button @click="deleteSnippet(snippet.id)" class="btn btn-sm btn-danger">
                删除
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    
    <div v-if="snippets.length === 0" class="empty-state">
      <p>暂无代码片段，请点击上方按钮新增</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { getAdminSnippets, deleteSnippet as deleteSnippetApi, Snippet } from '../../services/api'
import { useToast } from '../../composables/useToast'

const toast = useToast()
const snippets = ref<Snippet[]>([])

const fetchSnippets = async () => {
  try {
    snippets.value = await getAdminSnippets()
  } catch (error) {
    console.error('Error fetching snippets:', error)
    toast.error('获取代码片段列表失败')
  }
}

const deleteSnippet = async (id: string) => {
  if (confirm('确定要删除这个代码片段吗？')) {
    try {
      await deleteSnippetApi(id)
      toast.success('代码片段删除成功')
      fetchSnippets()
    } catch (error) {
      console.error('Error deleting snippet:', error)
      toast.error('删除代码片段失败')
    }
  }
}

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

<style scoped>
.admin-snippets {
  max-width: 1200px;
  margin: 0 auto;
}

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

.toolbar {
  margin-bottom: 1rem;
  display: flex;
  justify-content: flex-end;
}

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

.admin-table {
  width: 100%;
  border-collapse: collapse;
  color: white;
}

.admin-table th,
.admin-table td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  font-family: 'Inter', sans-serif;
}

.admin-table th {
  background: rgba(255, 255, 255, 0.08);
  font-weight: 600;
  color: #d4b383;
}

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

.type-badge {
  display: inline-block;
  padding: 0.25rem 0.5rem;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 500;
  background-color: rgba(212, 179, 131, 0.2);
  color: #d4b383;
  border: 1px solid rgba(212, 179, 131, 0.3);
}

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

.btn {
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-family: 'Inter', sans-serif;
  border: 1px solid transparent;
}

.btn-sm {
  padding: 0.25rem 0.5rem;
  font-size: 0.875rem;
}

.btn-primary {
  background: rgba(212, 179, 131, 0.1);
  color: #d4b383;
  border-color: #d4b383;
}

.btn-primary:hover {
  background-color: transparent;
  box-shadow: 0 0 15px rgba(212, 179, 131, 0.3);
}

.btn-secondary {
  background-color: rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.8);
  border-color: rgba(255, 255, 255, 0.2);
}

.btn-secondary:hover {
  background-color: rgba(212, 179, 131, 0.1);
  border-color: #d4b383;
  color: #d4b383;
}

.btn-danger {
  background-color: rgba(239, 68, 68, 0.2);
  color: #ef4444;
  border-color: rgba(239, 68, 68, 0.3);
}

.btn-danger:hover {
  background-color: rgba(239, 68, 68, 0.3);
  border-color: #ef4444;
  box-shadow: 0 0 15px rgba(239, 68, 68, 0.3);
}

.empty-state {
  text-align: center;
  padding: 2rem;
  color: rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.05);
  border-radius: 0.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.1);
  font-family: 'Inter', sans-serif;
}
</style>