<template>
  <div class="project-card">
    <div class="card-content" @click="handleClick">
      <div class="card-header">
        <h3 class="card-title">{{ project.title }}</h3>
        <span v-if="project.category" class="card-category">{{ project.category }}</span>
      </div>
      <p v-if="project.description" class="card-description">{{ project.description }}</p>
      <div v-if="project.tags && project.tags.length > 0" class="card-tags">
        <span v-for="tag in project.tags" :key="tag" class="tag">{{ tag }}</span>
      </div>
      <div class="card-footer">
        <span class="card-date">{{ formatDate(project.created_at) }}</span>
      </div>
    </div>
    <div v-if="canEdit" class="card-actions" @click.stop>
      <button @click="handleEdit" class="action-btn edit-btn">编辑</button>
      <button @click="handleDelete" class="action-btn delete-btn">删除</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/store';
import { Project, projectApi } from '@/api/project';

const props = defineProps<{
  project: Project;
}>();

const emit = defineEmits<{
  deleted: [projectId: number];
}>();

const router = useRouter();
const userStore = useUserStore();

const canEdit = computed(() => {
  return userStore.isAuthenticated && 
    (userStore.user?.id === props.project.user_id || userStore.isSuperAdmin);
});

const handleClick = () => {
  router.push(`/project/${props.project.id}`);
};

const handleEdit = () => {
  router.push(`/upload?edit=${props.project.id}`);
};

const handleDelete = async () => {
  if (!confirm('确定要删除这个项目吗？')) {
    return;
  }
  
  try {
    await projectApi.delete(props.project.id);
    // 触发父组件刷新列表
    emit('deleted', props.project.id);
  } catch (error: any) {
    alert(error.response?.data?.error?.message || '删除失败');
  }
};

const formatDate = (dateString: string) => {
  const date = new Date(dateString);
  return date.toLocaleDateString('zh-CN');
};
</script>

<style scoped>
.project-card {
  background: white;
  border-radius: 12px;
  padding: 1.5rem;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: relative;
}

.project-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.card-content {
  cursor: pointer;
}

.card-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid #eee;
}

.action-btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  transition: all 0.2s;
}

.edit-btn {
  background: #667eea;
  color: white;
}

.edit-btn:hover {
  background: #5568d3;
}

.delete-btn {
  background: #e74c3c;
  color: white;
}

.delete-btn:hover {
  background: #c0392b;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  margin-bottom: 0.75rem;
}

.card-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: #333;
  margin: 0;
  flex: 1;
}

.card-category {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 500;
}

.card-description {
  color: #666;
  font-size: 0.9rem;
  margin: 0.75rem 0;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.75rem 0;
}

.tag {
  background: #f0f0f0;
  color: #666;
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
  font-size: 0.75rem;
}

.card-footer {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-date {
  color: #999;
  font-size: 0.8rem;
}
</style>

