<template>
  <div class="knowledge-card" @click="handleClick">
    <div class="card-header">
      <!-- 头像 -->
      <div class="knowledge-avatar">
        <img v-if="knowledge.avatar" :src="FILE_DOWNLOAD_URL + knowledge.avatar" alt="知识库头像" />
        <div v-else class="default-avatar">📚</div>
      </div>
      
      <!-- 标题和状态 -->
      <div class="knowledge-title-section">
        <h3 class="knowledge-title">{{ knowledge.title }}</h3>
        <span 
          class="knowledge-status" 
          :class="knowledge.status === 1 ? 'status-active' : 'status-inactive'"
        >
          {{ knowledge.status === 1 ? '启用' : '禁用' }}
        </span>
      </div>
    </div>

    <!-- 描述 -->
    <div class="knowledge-description">
      {{ knowledge.description || '暂无描述' }}
    </div>

    <!-- 统计信息 -->
    <div class="knowledge-stats">
      <div class="stat-item">
        <span class="stat-label">文档数量</span>
        <span class="stat-value">{{ knowledge.documentCount || 0 }}</span>
      </div>

      <div class="stat-item">
        <span class="stat-label">状态</span>
        <span class="stat-value">
          {{ getStatusText(knowledge.status) }}
        </span>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="knowledge-actions" @click.stop>
      <el-button 
        size="small" 
        @click="handleEdit"
        :disabled="!hasWritePermission"
        :title="hasWritePermission ? '编辑知识库' : '无编辑权限'"
      >
        编辑
      </el-button>
      <el-button 
        size="small" 
        type="danger" 
        @click="handleDelete"
        :disabled="!hasDeletePermission"
        :title="hasDeletePermission ? '删除知识库' : '无删除权限'"
      >
        删除
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { useStore } from 'vuex';
import type { AiKnowledge } from '@/types/ai';
import { FILE_DOWNLOAD_URL } from '@/config';

defineOptions({
  name: 'KnowledgeCard'
});

interface Props {
  knowledge: AiKnowledge;
}

const props = defineProps<Props>();
const emit = defineEmits(['click', 'edit', 'delete', 'sync']);

const store = useStore();
const currentUserId = computed(() => store.getters['auth/user']?.id);

// 判断是否有写权限：是创建者 或 有canWrite权限
const hasWritePermission = computed(() => {
  return props.knowledge.creator === currentUserId.value || props.knowledge.canWrite;
});

// 判断是否有删除权限：是创建者 或 有canDelete权限
const hasDeletePermission = computed(() => {
  return props.knowledge.creator === currentUserId.value || props.knowledge.canDelete;
});

function handleClick() {
  emit('click', props.knowledge);
}

function handleEdit() {
  emit('edit', props.knowledge);
}

function handleDelete() {
  emit('delete', props.knowledge);
}

function getStatusText(status?: number) {
  switch (status) {
    case 0: return '禁用';
    case 1: return '启用';
    case 2: return '处理中';
    default: return '未知';
  }
}
</script>

<style scoped lang="scss">
.knowledge-card {
  background: #FFFFFF;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 14px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    border-color: #E7000B;
    box-shadow: 0 4px 12px rgba(231, 0, 11, 0.12);
    transform: translateY(-2px);
  }
}

.card-header {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 16px;
}

.knowledge-avatar {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  overflow: hidden;
  background: #F3F3F5;
  flex-shrink: 0;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .default-avatar {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
  }
}

.knowledge-title-section {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.knowledge-title {
  font-size: 16px;
  font-weight: 500;
  color: #101828;
  margin: 0;
  letter-spacing: -0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.knowledge-status {
  padding: 2px 8px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;

  &.status-active {
    background: #DCFCE7;
    color: #008236;
  }

  &.status-inactive {
    background: #FEF2F2;
    color: #DC2626;
  }
}

.knowledge-description {
  font-size: 14px;
  color: #6B7280;
  line-height: 1.6;
  margin-bottom: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  letter-spacing: -0.01em;
}

.knowledge-stats {
  display: flex;
  gap: 16px;
  padding: 12px 0;
  border-top: 1px solid #F3F3F5;
  border-bottom: 1px solid #F3F3F5;
  margin-bottom: 16px;

  .stat-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;

    .stat-label {
      font-size: 12px;
      color: #9CA3AF;
      letter-spacing: -0.01em;
    }

    .stat-value {
      font-size: 16px;
      font-weight: 500;
      color: #101828;
      letter-spacing: -0.01em;
    }
  }
}

.knowledge-actions {
  display: flex;
  gap: 8px;

  :deep(.el-button) {
    flex: 1;
    border-radius: 8px;
    font-weight: 500;
    letter-spacing: -0.01em;

    &.el-button--small {
      padding: 5px 12px;
    }

    &.el-button--default {
      background: #F3F3F5;
      border-color: transparent;
      color: #0A0A0A;

      &:hover {
        background: #E5E5E7;
      }
    }

    &.el-button--danger {
      background: #FEF2F2;
      border-color: transparent;
      color: #DC2626;

      &:hover {
        background: #FEE2E2;
      }
    }
  }
}
</style>
