<template>
  <div class="software-card">
    <div class="card-left">
      <div class="icon-container">
        <img v-if="software.icon_url" :src="software.icon_url" :alt="software.name" class="software-icon" />
        <div v-else class="icon-placeholder" :style="{ backgroundColor: placeholderColor }">
          {{ software.name.charAt(0).toUpperCase() }}
        </div>
      </div>
      <div class="info">
        <div class="title-row">
          <h3 class="name">{{ software.name }}</h3>
          <span class="id-badge">{{ software.id }}</span>
        </div>
        <p class="description" v-if="software.description">{{ software.description }}</p>
        <div class="version-info">
          <span class="version-tag">当前: {{ software.version || '--' }}</span>
          <span class="version-tag available" v-if="software.available_version">
            最新: {{ software.available_version }}
          </span>
        </div>
      </div>
    </div>

    <div class="card-right" v-if="actionLabel || software.status !== 'idle'">
      <div class="action-wrapper">
        <button 
          v-if="software.status === 'idle'" 
          @click="$emit('install', software.id)"
          class="install-btn"
        >
          {{ actionLabel }}
        </button>
        
        <div v-else-if="software.status === 'installing' || software.status === 'pending'" class="progress-status">
          <div class="progress-ring">
            <svg viewBox="0 0 32 32">
              <circle class="bg" cx="16" cy="16" r="14" fill="none" stroke-width="4" />
              <circle class="fg" cx="16" cy="16" r="14" fill="none" stroke-width="4" 
                :style="{ strokeDasharray: 88, strokeDashoffset: 88 - (88 * (software.progress || 0.5)) }" 
              />
            </svg>
          </div>
          <span class="status-text">{{ software.status === 'pending' ? '等待中...' : '安装中...' }}</span>
        </div>

        <div v-else-if="software.status === 'success'" class="status-success">
          <span class="check-icon">✓</span> 已完成
        </div>

        <div v-else-if="software.status === 'error'" class="status-error">
          ❌ 失败
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';

const props = defineProps<{
  software: {
    id: string;
    name: string;
    description?: string;
    version?: string;
    available_version?: string;
    icon_url?: string;
    status: string;
    progress: number;
  },
  actionLabel?: string
}>();

const placeholderColor = computed(() => {
  const colors = ['#FF9500', '#FF3B30', '#34C759', '#007AFF', '#5856D6', '#AF52DE'];
  let hash = 0;
  for (let i = 0; i < props.software.id.length; i++) {
    hash = props.software.id.charCodeAt(i) + ((hash << 5) - hash);
  }
  return colors[Math.abs(hash) % colors.length];
});
</script>

<style scoped>
.software-card {
  background: white;
  border-radius: 20px;
  padding: 16px 24px;
  box-shadow: var(--card-shadow);
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid rgba(0, 0, 0, 0.02);
  margin-bottom: 12px;
}

.software-card:hover {
  transform: scale(1.01);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.06);
}

.card-left {
  display: flex;
  align-items: center;
  gap: 20px;
  flex: 1;
}

.icon-container {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  overflow: hidden;
  flex-shrink: 0;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
}

.software-icon {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.icon-placeholder {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 20px;
  font-weight: 700;
}

.info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.name {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-main);
}

.id-badge {
  font-size: 11px;
  color: var(--text-sec);
  background: var(--bg-light);
  padding: 2px 8px;
  border-radius: 6px;
  font-family: monospace;
}

.description {
  font-size: 13px;
  color: var(--text-sec);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  max-width: 500px;
}

.version-info {
  display: flex;
  gap: 8px;
}

.version-tag {
  font-size: 11px;
  color: var(--text-sec);
  font-weight: 500;
}

.version-tag.available {
  color: var(--primary-color);
  background: rgba(0, 122, 255, 0.08);
  padding: 0 6px;
  border-radius: 4px;
}

.card-right {
  margin-left: 20px;
  min-width: 120px;
  display: flex;
  justify-content: flex-end;
}

.install-btn {
  padding: 8px 24px;
  background-color: var(--bg-light);
  border: none;
  border-radius: 20px;
  color: var(--primary-color);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.install-btn:hover {
  background-color: var(--primary-color);
  color: white;
}

.progress-status {
  display: flex;
  align-items: center;
  gap: 10px;
}

.progress-ring {
  width: 20px;
  height: 20px;
}

.progress-ring svg {
  transform: rotate(-90deg);
}

.progress-ring .bg {
  stroke: var(--border-color);
}

.progress-ring .fg {
  stroke: var(--primary-color);
  stroke-linecap: round;
  transition: stroke-dashoffset 0.3s ease;
}

.status-text {
  font-size: 13px;
  font-weight: 500;
  color: var(--primary-color);
}

.status-success {
  color: #34C759;
  font-weight: 600;
  font-size: 14px;
}

.status-error {
  color: #FF3B30;
  font-weight: 600;
  font-size: 14px;
}
</style>
