<template>
  <div class="panel-excel-upload-detail">
    <div v-if="loading" class="detail-loading">Yükleniyor...</div>
    <template v-else-if="job">
      <div class="detail-summary">
        <div class="detail-row detail-file-row">
          <span class="detail-label">Dosya:</span>
          <span class="detail-file-name">{{ job.fileName }}</span>
        </div>
        <div class="detail-row">
          <span class="detail-label">Durum:</span>
          <span :class="['detail-status', 'status-' + job.status?.toLowerCase()]">
            {{ statusText(job.status) }}
          </span>
        </div>
        <div class="detail-row">
          <span class="detail-label">İlerleme:</span>
          <span>{{ job.progress }}%</span>
        </div>
        <div class="detail-stats">
          <div class="detail-stat-item">
            <span class="detail-stat-value">{{ job.totalRows }}</span>
            <span class="detail-stat-label">Toplam Satır</span>
          </div>
          <div class="detail-stat-item detail-stat-inserted">
            <span class="detail-stat-value">{{ job.insertedCount ?? 0 }}</span>
            <span class="detail-stat-label">Eklenen</span>
          </div>
          <div class="detail-stat-item detail-stat-skipped">
            <span class="detail-stat-value">{{ job.skippedRows ?? 0 }}</span>
            <span class="detail-stat-label">Atlanan</span>
          </div>
          <div class="detail-stat-item detail-stat-duplicate">
            <span class="detail-stat-value">{{ job.duplicateCount ?? 0 }}</span>
            <span class="detail-stat-label">Duplicate</span>
          </div>
          <div class="detail-stat-item detail-stat-error">
            <span class="detail-stat-value">{{ job.errorCount ?? 0 }}</span>
            <span class="detail-stat-label">Hata</span>
          </div>
        </div> 
        <div v-if="job.errorMessage" class="detail-error-box">
          <span class="detail-error-label">Hata:</span>
          <span>{{ job.errorMessage }}</span>
        </div>
      </div>
      <div class="detail-logs" v-if="logs && logs.length > 0">
        <h4 class="detail-logs-title">İşlem Logları</h4>
        <div class="logs-list">
          <div
            v-for="log in logs"
            :key="log.id"
            :class="['log-item', 'log-' + log.logLevel?.toLowerCase()]">
            <span :class="['log-level', 'log-level-' + log.logLevel?.toLowerCase()]">{{ log.logLevel }}</span>
            <span v-if="log.rowNumber" class="log-row">Satır {{ log.rowNumber }}</span>
            <span class="log-message">{{ log.message }}</span>
            <span v-if="log.details" class="log-details">{{ log.details }}</span>
            <span class="log-time">{{ formatDate(log.createdAt) }}</span>
          </div>
        </div>
      </div>
      <div v-else class="detail-no-logs">Log kaydı bulunamadı.</div>
    </template>
    <div v-else class="detail-not-found">Yükleme detayı bulunamadı.</div>
  </div>
</template>
<script setup lang="ts">
  import { ref, onMounted, watch } from 'vue'
  import { usePiyangoKatilimciService } from '../../service/piyangoKatilimciService'
  import { useDateStore } from '@/stores/dateStore'

  const props = defineProps<{
    jobGuid: string
  }>()

  const piyangoKatilimciService = usePiyangoKatilimciService()
  const dateStore = useDateStore()

  const loading = ref(true)
  const job = ref<Record<string, any> | null>(null)
  const logs = ref<Record<string, any>[]>([])

  const statusText = (status: string) => {
    const map: Record<string, string> = {
      Pending: 'Bekliyor',
      Processing: 'İşleniyor',
      Completed: 'Tamamlandı',
      Failed: 'Başarısız'
    }
    return map[status] || status
  }

  const formatDate = (val: string | Date) => {
    if (!val) return '-'
    return dateStore.dateFormat({ date: new Date(val), pattern: 'dd.mm.yyyy HH:MM' })
  }

  const loadDetail = async () => {
    if (!props.jobGuid) return
    loading.value = true
    const data = await piyangoKatilimciService.GetUploadJobDetail(props.jobGuid)
    if (data) {
      job.value = data.job
      logs.value = data.logs || []
    } else {
      job.value = null
      logs.value = []
    }
    loading.value = false
  }

  onMounted(() => loadDetail())
  watch(() => props.jobGuid, () => loadDetail())
</script>
<style scoped>
  .panel-excel-upload-detail {
    padding: 24px 24px 24px 24px;
  }

  .detail-loading {
    text-align: center;
    padding: 32px;
    color: #5f6368;
    font-size: 14px;
  }

  .detail-summary {
    margin-bottom: 24px;
    padding: 20px;
    background: #fafbfc;
    border: 1px solid #e8eaed;
    border-radius: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  }

  .detail-row {
    display: grid;
    grid-template-columns: 120px 1fr;
    gap: 12px;
    margin-bottom: 10px;
    align-items: center;
  }

  .detail-file-row {
    margin-bottom: 12px;
  }

  .detail-file-name {
    font-weight: 600;
    color: #1a1a2e;
    word-break: break-all;
  }

  .detail-label {
    font-weight: 600;
    color: #5f6368;
    font-size: 13px;
  }

  .detail-status {
    font-size: 12px;
    font-weight: 600;
    padding: 4px 10px;
    border-radius: 6px;
    display: inline-block;
    width: fit-content;
  }

  .detail-status.status-completed {
    color: #0d6832;
    background: #d4edda;
  }

  .detail-status.status-failed {
    color: #721c24;
    background: #f8d7da;
  }

  .detail-status.status-processing {
    color: #004085;
    background: #cce5ff;
  }

  .detail-status.status-pending {
    color: #856404;
    background: #fff3cd;
  }

  .detail-stats {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 12px;
    margin: 16px 0;
    padding: 16px 0;
    border-top: 1px solid #e8eaed;
    border-bottom: 1px solid #e8eaed;
  }

  .detail-stat-item {
    text-align: center;
    padding: 8px;
    background: #f1f3f5;
    border-radius: 6px;
  }

  .detail-stat-value {
    display: block;
    font-size: 18px;
    font-weight: 700;
    color: #1a1a2e;
  }

  .detail-stat-label {
    font-size: 12px;
    color: #5f6368;
  }

  .detail-error-box {
    margin-top: 16px;
    padding: 12px 16px;
    background: #fff5f5;
    border: 1px solid #f8d7da;
    border-radius: 6px;
    color: #721c24;
    font-size: 13px;
    display: flex;
    gap: 8px;
  }

  .detail-error-label {
    font-weight: 600;
    flex-shrink: 0;
  }

  .detail-not-found {
    padding: 24px;
    text-align: center;
    color: #721c24;
    background: #fff5f5;
    border: 1px solid #f8d7da;
    border-radius: 8px;
    font-weight: 500;
  }

  .detail-logs-title {
    margin: 0 0 12px;
    font-size: 15px;
    font-weight: 600;
    color: #1a1a2e;
  }

  .logs-list {
    max-height: 400px;
    overflow-y: auto;
    border: 1px solid #e8eaed;
    border-radius: 8px;
    background: #fafbfc;
  }

  .log-item {
    padding: 10px 14px;
    border-bottom: 1px solid #e8eaed;
    font-size: 13px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: baseline;
  }

  .log-item:last-child {
    border-bottom: none;
  }

  .log-item.log-error {
    background: #fff5f5;
  }

  .log-item.log-warning {
    background: #fffbf0;
  }

  .log-item.log-info {
    background: #f0f9ff;
  }

  .log-level {
    font-weight: 600;
    min-width: 60px;
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 4px;
  }

  .log-level.log-level-error {
    background: #f8d7da;
    color: #721c24;
  }

  .log-level.log-level-warning {
    background: #fff3cd;
    color: #856404;
  }

  .log-level.log-level-info {
    background: #cce5ff;
    color: #004085;
  }

  .log-row {
    color: #5f6368;
    font-family: ui-monospace, monospace;
    font-size: 12px;
  }

  .log-message {
    flex: 1;
    min-width: 0;
  }

  .log-details {
    font-size: 12px;
    color: #5f6368;
    width: 100%;
  }

  .log-time {
    font-size: 11px;
    color: #80868b;
    margin-left: auto;
  }

  .detail-no-logs {
    padding: 24px;
    color: #80868b;
    text-align: center;
    font-style: italic;
    font-size: 14px;
  }
</style>
