<template>
  <view class="recent-scan-list">
    <view v-if="records.length === 0" class="recent-scan-list__empty">
      暂无扫码记录
    </view>

    <view
      v-for="(record, index) in records"
      :key="record.id || index"
      class="scan-card"
      :class="{ 'scan-card--latest': latestIndex === index }"
    >
      <view class="scan-card__header">
        <text class="scan-card__name">{{ record.productName || '-' }}</text>
        <text class="scan-card__delete" @tap="$emit('delete', index)">删除</text>
      </view>

      <view class="scan-card__meta">
        <text>条码：{{ record.productBarCode || '-' }}</text>
        <text>规格：{{ record.productSpec || '-' }}</text>
      </view>
      <view class="scan-card__meta">
        <text>仓库：{{ record.warehouseName || '-' }}</text>
        <text>单位：{{ record.productUnit || '-' }}</text>
      </view>
      <view class="scan-card__meta">
        <text>库位：{{ record.locationCode || '-' }}</text>
        <text>批次：{{ record.batchNo || '-' }}</text>
      </view>
      <view class="scan-card__footer">
        <text class="scan-card__time">{{ formatTime(record.scanTime) }}</text>
        <text class="scan-card__count">{{ record.scanCount }}</text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import type { ScanRecord } from '../index.vue'

defineProps<{
  records: ScanRecord[]
  latestIndex?: number
}>()

defineEmits<{
  (event: 'delete', index: number): void
  (event: 'clear'): void
}>()

function formatTime(time?: string) {
  if (!time) return '-'
  const date = new Date(time)
  return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}:${date.getSeconds().toString().padStart(2, '0')}`
}
</script>

<style lang="scss" scoped>
.recent-scan-list {
  &__empty {
    padding: 48rpx;
    border-radius: 16rpx;
    background: #fff;
    color: #86909c;
    text-align: center;
    font-size: 26rpx;
  }
}

.scan-card {
  margin-bottom: 16rpx;
  padding: 24rpx;
  border: 2rpx solid transparent;
  border-radius: 16rpx;
  background: #fff;

  &--latest {
    border-color: #1677ff;
    box-shadow: 0 8rpx 20rpx rgba(22, 119, 255, 0.12);
  }

  &__header,
  &__meta,
  &__footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16rpx;
  }

  &__header {
    margin-bottom: 10rpx;
  }

  &__name {
    color: #1f2329;
    font-size: 28rpx;
    font-weight: 600;
  }

  &__delete {
    flex-shrink: 0;
    color: #f53f3f;
    font-size: 24rpx;
  }

  &__meta {
    margin-top: 6rpx;
    color: #4e5969;
    font-size: 22rpx;
    flex-wrap: wrap;
  }

  &__footer {
    margin-top: 10rpx;
    padding-top: 10rpx;
    border-top: 2rpx solid #f2f3f5;
  }

  &__time {
    color: #86909c;
    font-size: 22rpx;
  }

  &__count {
    color: #1677ff;
    font-size: 28rpx;
    font-weight: 700;
  }
}
</style>
