<template>
  <!-- Drama List Page - Refactored with modern minimalist design -->
  <!-- 短剧列表页面 - 使用现代简约设计重构 -->
  <div class="page-container">
    <div class="content-wrapper animate-fade-in">
      <!-- Page Header / 页面头部 -->
      <PageHeader
        :title="$t('drama.title')"
        :subtitle="$t('drama.totalProjects', { count: total })"
      >
        <template #actions>
          <LanguageSwitcher />
          <ThemeToggle />
          <el-button @click="showAIConfig = true" class="header-btn">
            <el-icon><Setting /></el-icon>
            <span class="btn-text">{{ $t('drama.aiConfig') }}</span>
          </el-button>
          <el-button type="primary" @click="handleCreate" class="header-btn primary">
            <el-icon><Plus /></el-icon>
            <span class="btn-text">{{ $t('drama.createNew') }}</span>
          </el-button>
        </template>
      </PageHeader>

      <!-- Project Grid / 项目网格 -->
      <div v-loading="loading" class="projects-grid" :class="{ 'is-empty': !loading && dramas.length === 0 }">
        <!-- Empty state / 空状态 -->
        <EmptyState
          v-if="!loading && dramas.length === 0"
          :title="$t('drama.empty')"
          :description="$t('drama.emptyHint')"
          :icon="Film"
        >
          <el-button type="primary" @click="handleCreate">
            <el-icon><Plus /></el-icon>
            {{ $t('drama.createNew') }}
          </el-button>
        </EmptyState>

        <!-- Project Cards / 项目卡片列表 -->
        <ProjectCard
          v-for="drama in dramas"
          :key="drama.id"
          :title="drama.title"
          :description="drama.description"
          :cover-image="drama.thumbnail"
          :tag="drama.genre"
          :updated-at="drama.updated_at"
          @click="viewDrama(drama.id)"
        >
          <template #actions>
            <ActionButton 
              :icon="Edit" 
              :tooltip="$t('common.edit')" 
              variant="primary"
              @click="editDrama(drama.id)" 
            />
            <ActionButton 
              :icon="View" 
              :tooltip="$t('common.view')" 
              @click="viewDrama(drama.id)" 
            />
            <el-popconfirm
              :title="$t('drama.deleteConfirm')"
              :confirm-button-text="$t('common.confirm')"
              :cancel-button-text="$t('common.cancel')"
              @confirm="deleteDrama(drama.id)"
            >
              <template #reference>
                <ActionButton 
                  :icon="Delete" 
                  :tooltip="$t('common.delete')" 
                  variant="danger"
                />
              </template>
            </el-popconfirm>
          </template>
        </ProjectCard>
      </div>

      <!-- Edit Dialog / 编辑对话框 -->
      <el-dialog
        v-model="editDialogVisible"
        :title="$t('drama.editProject')"
        width="520px"
        :close-on-click-modal="false"
        class="edit-dialog"
      >
        <el-form 
          :model="editForm" 
          label-position="top"
          v-loading="editLoading"
          class="edit-form"
        >
          <el-form-item :label="$t('drama.projectName')" required>
            <el-input 
              v-model="editForm.title" 
              :placeholder="$t('drama.projectNamePlaceholder')"
              size="large"
            />
          </el-form-item>
          <el-form-item :label="$t('drama.projectDesc')">
            <el-input
              v-model="editForm.description"
              type="textarea"
              :rows="4"
              :placeholder="$t('drama.projectDescPlaceholder')"
              resize="none"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="editDialogVisible = false" size="large">{{ $t('common.cancel') }}</el-button>
            <el-button 
              type="primary" 
              @click="saveEdit" 
              :loading="editLoading"
              size="large"
            >
              {{ $t('common.save') }}
            </el-button>
          </div>
        </template>
      </el-dialog>

      <!-- Create Drama Dialog / 创建短剧弹窗 -->
      <CreateDramaDialog 
        v-model="createDialogVisible" 
        @created="loadDramas" 
      />

      <!-- AI Config Dialog / AI配置弹窗 -->
      <AIConfigDialog v-model="showAIConfig" />
    </div>

    <!-- Sticky Pagination / 吸底分页器 -->
    <div v-if="total > 0" class="pagination-sticky">
      <div class="pagination-inner">
        <div class="pagination-info">
          <span class="pagination-total">{{ $t('drama.totalProjects', { count: total }) }}</span>
        </div>
        <div class="pagination-controls">
          <el-pagination
            v-model:current-page="queryParams.page"
            v-model:page-size="queryParams.page_size"
            :total="total"
            :page-sizes="[12, 24, 36, 48]"
            :pager-count="5"
            layout="prev, pager, next"
            @size-change="loadDramas"
            @current-change="loadDramas"
          />
        </div>
        <div class="pagination-size">
          <span class="size-label">{{ $t('common.perPage') }}</span>
          <el-select 
            v-model="queryParams.page_size" 
            size="small"
            class="size-select"
            @change="loadDramas"
          >
            <el-option :value="12" label="12" />
            <el-option :value="24" label="24" />
            <el-option :value="36" label="36" />
            <el-option :value="48" label="48" />
          </el-select>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { 
  Plus, 
  Film, 
  Setting, 
  Edit, 
  View, 
  Delete,
  InfoFilled 
} from '@element-plus/icons-vue'
import { dramaAPI } from '@/api/drama'
import type { Drama, DramaListQuery } from '@/types/drama'
import { PageHeader, ProjectCard, ThemeToggle, ActionButton, CreateDramaDialog, EmptyState, AIConfigDialog } from '@/components/common'
import LanguageSwitcher from '@/components/LanguageSwitcher.vue'

const router = useRouter()
const loading = ref(false)
const dramas = ref<Drama[]>([])
const total = ref(0)

const queryParams = ref<DramaListQuery>({
  page: 1,
  page_size: 12
})

// Create dialog state / 创建弹窗状态
const createDialogVisible = ref(false)
const showAIConfig = ref(false)

// Load drama list / 加载短剧列表
const loadDramas = async () => {
  loading.value = true
  try {
    const res = await dramaAPI.list(queryParams.value)
    dramas.value = res.items || []
    total.value = res.pagination?.total || 0
  } catch (error: any) {
    ElMessage.error(error.message || '加载失败')
  } finally {
    loading.value = false
  }
}

// Navigation handlers / 导航处理
const handleCreate = () => createDialogVisible.value = true
const viewDrama = (id: string) => router.push(`/dramas/${id}`)

// Edit dialog state / 编辑对话框状态
const editDialogVisible = ref(false)
const editLoading = ref(false)
const editForm = ref({
  id: '',
  title: '',
  description: ''
})

// Open edit dialog / 打开编辑对话框
const editDrama = async (id: string) => {
  editLoading.value = true
  editDialogVisible.value = true
  try {
    const drama = await dramaAPI.get(id)
    editForm.value = {
      id: drama.id,
      title: drama.title,
      description: drama.description || ''
    }
  } catch (error: any) {
    ElMessage.error(error.message || '加载失败')
    editDialogVisible.value = false
  } finally {
    editLoading.value = false
  }
}

// Save edit changes / 保存编辑更改
const saveEdit = async () => {
  if (!editForm.value.title) {
    ElMessage.warning('请输入项目名称')
    return
  }
  
  editLoading.value = true
  try {
    await dramaAPI.update(editForm.value.id, {
      title: editForm.value.title,
      description: editForm.value.description
    })
    ElMessage.success('保存成功')
    editDialogVisible.value = false
    loadDramas()
  } catch (error: any) {
    ElMessage.error(error.message || '保存失败')
  } finally {
    editLoading.value = false
  }
}

// Delete drama / 删除短剧
const deleteDrama = async (id: string) => {
  try {
    await dramaAPI.delete(id)
    ElMessage.success('删除成功')
    loadDramas()
  } catch (error: any) {
    ElMessage.error(error.message || '删除失败')
  }
}

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

<style scoped>
/* ========================================
   Page Layout / 页面布局 - 紧凑边距
   ======================================== */
.page-container {
  min-height: 100vh;
  background: var(--bg-primary);
  padding: var(--space-2) var(--space-3);
  transition: background var(--transition-normal);
}

@media (min-width: 768px) {
  .page-container {
    padding: var(--space-3) var(--space-4);
  }
}

@media (min-width: 1024px) {
  .page-container {
    padding: var(--space-4) var(--space-5);
  }
}

.content-wrapper {
  margin: 0 auto;
  width: 100%;
}

/* ========================================
   Header Buttons / 头部按钮
   ======================================== */
.header-btn {
  border-radius: var(--radius-lg);
  font-weight: 500;
}

.header-btn.primary {
  background: linear-gradient(135deg, var(--accent) 0%, #0284c7 100%);
  border: none;
  box-shadow: 0 4px 14px rgba(14, 165, 233, 0.35);
}

.header-btn.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 20px rgba(14, 165, 233, 0.45);
}

@media (max-width: 640px) {
  .btn-text {
    display: none;
  }
  
  .header-btn {
    padding: 0.5rem 0.75rem;
  }
}

/* ========================================
   Projects Grid / 项目网格 - 紧凑间距
   ======================================== */
.projects-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-2);
  margin-bottom: var(--space-4);
  min-height: 300px;
  padding-bottom: 4rem;
}

@media (min-width: 640px) {
  .projects-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: var(--space-2);
  }
}

@media (min-width: 900px) {
  .projects-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: var(--space-3);
  }
}

@media (min-width: 1200px) {
  .projects-grid {
    grid-template-columns: repeat(5, 1fr);
  }
}

@media (min-width: 1500px) {
  .projects-grid {
    grid-template-columns: repeat(6, 1fr);
  }
}

.projects-grid.is-empty {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ========================================
   Sticky Pagination / 吸底分页器
   ======================================== */
.pagination-sticky {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--border-primary);
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.05);
}

.dark .pagination-sticky {
  background: rgba(10, 15, 26, 0.9);
  border-top: 1px solid var(--border-primary);
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.3);
}

.pagination-inner {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin: 0 auto;
  padding: var(--space-3) var(--space-4);
  gap: var(--space-4);
}

@media (min-width: 768px) {
  .pagination-inner {
    padding: var(--space-3) var(--space-6);
  }
}

.pagination-info {
  display: none;
}

@media (min-width: 768px) {
  .pagination-info {
    display: block;
  }
}

.pagination-total {
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-weight: 500;
}

.pagination-controls {
  display: flex;
}

.pagination-size {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.size-label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  display: none;
}

@media (min-width: 768px) {
  .size-label {
    display: block;
  }
}

.size-select {
  width: 4.5rem;
}

.size-select :deep(.el-input__wrapper) {
  height: 2rem;
  border-radius: var(--radius-md);
  background: var(--bg-card);
}

/* ========================================
   Edit Dialog / 编辑对话框
   ======================================== */
.edit-dialog :deep(.el-dialog) {
  border-radius: var(--radius-xl);
}

.edit-dialog :deep(.el-dialog__header) {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border-primary);
  margin-right: 0;
}

.edit-dialog :deep(.el-dialog__title) {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--text-primary);
}

.edit-dialog :deep(.el-dialog__body) {
  padding: 1.5rem;
}

.edit-form :deep(.el-form-item__label) {
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 0.5rem;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
}
</style>
