<script setup lang="ts">
import type { AdminTaskDetail } from '@/types/admin'
import { formatRedeemOutcomeLabel, formatRedeemResolutionStatus } from '@/utils/admin-display'
import { formatAdminDateTime } from '@/utils/admin-time'

interface Props {
  detail: AdminTaskDetail
}

defineProps<Props>()
</script>

<template>
  <section class="info-card">
    <h3>兑换重试链路</h3>
    <p class="section-copy">
      这里记录自动兑换时每次使用过的 CDK、判定结果，以及是否切换到了新的同类型凭据。
    </p>

    <div class="redeem-summary-grid">
      <article class="redeem-summary-card">
        <span>处理结果</span>
        <strong>{{ formatRedeemResolutionStatus(detail.redeemResolution!.status) }}</strong>
      </article>
      <article class="redeem-summary-card">
        <span>任务收口状态</span>
        <strong>{{ detail.redeemResolution!.taskStatus || detail.task.status || '-' }}</strong>
      </article>
      <article class="redeem-summary-card">
        <span>更换新码次数</span>
        <strong>{{ detail.redeemResolution!.replacementCount }}</strong>
      </article>
      <article class="redeem-summary-card">
        <span>完成时间</span>
        <strong>{{ formatAdminDateTime(detail.redeemResolution!.finishedAt) }}</strong>
      </article>
    </div>

    <div v-if="detail.redeemResolution!.attempts.length > 0" class="redeem-attempts">
      <article
        v-for="attempt in detail.redeemResolution!.attempts"
        :key="`${attempt.attempt}-${attempt.inventoryItemId || 'na'}`"
        class="redeem-attempt-card"
      >
        <div class="redeem-attempt-head">
          <strong>第 {{ attempt.attempt }} 次尝试</strong>
          <span class="redeem-outcome">{{ formatRedeemOutcomeLabel(attempt.outcome) }}</span>
        </div>
        <div class="redeem-attempt-grid">
          <div>
            <span>库存项</span>
            <strong>{{ attempt.inventoryItemId ? `#${attempt.inventoryItemId}` : '-' }}</strong>
          </div>
          <div>
            <span>凭据</span>
            <strong>{{ attempt.codeMasked || '-' }}</strong>
          </div>
          <div>
            <span>类型</span>
            <strong>{{ attempt.credentialType || '-' }}</strong>
          </div>
          <div>
            <span>结果码</span>
            <strong>{{ attempt.resultCode || '-' }}</strong>
          </div>
        </div>
        <p class="redeem-attempt-copy">
          {{ attempt.resultMessage || '-' }}
        </p>
      </article>
    </div>
  </section>
</template>

<style scoped src="../AdminTaskDetail.css"></style>
