<template>
  <div class="tag-form-container">
    <h1 class="page-title">{{ isEdit ? '编辑标签' : '新建标签' }}</h1>
    
    <div class="form-card">
      <form @submit.prevent="submitForm">
        <!-- 名称字段 -->
        <div class="form-group">
          <label for="name" class="form-label">名称</label>
          <input
            type="text"
            id="name"
            v-model="tagForm.name"
            class="form-input"
            placeholder="请输入标签名称"
            required
          />
        </div>
        
        <!-- Slug 字段 -->
        <div class="form-group">
          <label for="slug" class="form-label">别名 (Slug)</label>
          <input
            type="text"
            id="slug"
            v-model="tagForm.slug"
            class="form-input"
            placeholder="请输入标签别名（用于URL，如：vue-3）"
            required
          />
        </div>
        
        <!-- 表单按钮 -->
        <div class="form-actions">
          <button type="button" class="btn-secondary" @click="router.back()">
            取消
          </button>
          <button type="submit" class="btn-primary">
            {{ isEdit ? '保存修改' : '创建标签' }}
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useToast } from '../../composables/useToast'
import { createTag, updateTag, adminGetTag } from '../../services/api'

const router = useRouter()
const route = useRoute()
const toast = useToast()

// 判断是编辑还是创建
const isEdit = computed(() => !!route.params.id)

// 标签表单数据
const tagForm = ref({
  name: '',
  slug: ''
})

// 获取标签详情
const fetchTagDetail = async () => {
  if (!isEdit.value) return
  
  try {
    const tagId = parseInt(route.params.id as string, 10)
    const tag = await adminGetTag(tagId)
    tagForm.value = {
      name: tag.name,
      slug: tag.slug
    }
  } catch (error: any) {
    console.error('Failed to fetch tag detail:', error)
    toast.error('加载标签数据失败: ' + (error.message || '未知错误'))
  }
}

// 提交表单
const submitForm = async () => {
  try {
    if (isEdit.value) {
      const tagId = parseInt(route.params.id as string, 10)
      await updateTag(tagId, tagForm.value)
      toast.success('标签更新成功')
    } else {
      await createTag(tagForm.value)
      toast.success('标签创建成功')
    }
    router.push('/admin/tags')
  } catch (error: any) {
    console.error('Failed to submit tag form:', error)
    toast.error(error.message || (isEdit.value ? '更新标签失败' : '创建标签失败'))
  }
}

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

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

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

.form-card {
  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);
  padding: 2rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  max-width: 600px;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-label {
  display: block;
  font-size: 0.95rem;
  font-weight: 500;
  color: #d4b383;
  margin-bottom: 0.5rem;
  font-family: 'Inter', sans-serif;
}

.form-input {
  width: 100%;
  padding: 0.75rem 1rem;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 0.375rem;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.95rem;
  font-family: 'Inter', sans-serif;
  transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #d4b383;
  box-shadow: 0 0 0 3px rgba(212, 179, 131, 0.1);
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

textarea.form-input {
  resize: vertical;
  min-height: 100px;
}

.form-actions {
  display: flex;
  gap: 1rem;
  justify-content: flex-end;
  margin-top: 2rem;
}

.btn-primary, .btn-secondary {
  padding: 0.625rem 1.25rem;
  border-radius: 0.375rem;
  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 {
  background: rgba(212, 179, 131, 0.1);
  border: 1px solid #d4b383;
  color: #d4b383;
}

.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-secondary {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 255, 0.8);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.15);
  transform: translateY(-2px);
}
</style>