<template>
  <div>
    <!-- 导航栏 -->
    <div class="nav-bar">
      <div class="back-btn" @click="$router.back()">
        <i class="arrow-left"></i>
      </div>
      <div class="title">兑换记录</div>
    </div>

    <!-- 主容器 -->
    <div class="dhjilu-container">
      <div class="content-wrapper">
        <!-- 记录列表 -->
        <div v-if="exchangeRecords.length > 0" class="record-list">
          <div class="record-card" v-for="item in exchangeRecords" :key="item.id">
            <div class="row main-row">
              <span class="label">商品名称：</span>
              <span class="value name">{{ item.prize_name }}</span>
            </div>
            <div class="row">
              <span class="label">兑换时间：</span>
              <span class="value">{{ formatTime(item.create_time) }}</span>
            </div>
            <div class="row">
              <span class="label">发货状态：</span>
              <span class="value status" :class="'status-' + item.status">{{ getStatusText(item.status) }}</span>
            </div>
            <div class="row">
              <span class="label">快递单号：</span>
              <span class="value">{{ item.tranumber || '-' }}</span>
            </div>
            <div class="row">
              <span class="label">收货人：</span>
              <span class="value">{{ item.name || '-' }}</span>
            </div>
            <div class="row">
              <span class="label">电话：</span>
              <span class="value">{{ item.phone || '-' }}</span>
            </div>
            <div class="row">
              <span class="label">地址：</span>
              <span class="value">{{ item.address || '-' }}</span>
            </div>
          </div>
        </div>
        <div v-else class="empty-state">
          <div class="icon">😕</div>
          <div class="text">暂无记录</div>
        </div>
      </div>
    </div>

    <!-- Toast 提示 -->
    <Toast ref="toast" />
  </div>
</template>

<script>
import Toast from '@/components/Toast.vue';
import { getLotteryRecord } from '@/api/profile';

export default {
  name: 'DhjiluPage',
  components: {
    Toast
  },
  data() {
    return {
      exchangeRecords: []
    }
  },
  mounted() {
    this.loadExchangeRecords();
  },
  methods: {
    // 加载兑换记录
    async loadExchangeRecords() {
      try {
        const res = await getLotteryRecord();
        console.log('res:', res);
        this.exchangeRecords = Array.isArray(res.data) ? res.data : [];
        console.log('records:', this.exchangeRecords);
      } catch (error) {
        this.exchangeRecords = [];
        this.$refs.toast && this.$refs.toast.show('加载记录失败', 'error');
      }
    },
    // 状态文本
    getStatusText(status) {
      const map = {
        '0': '备货中',
        '1': '已发货'
      };
      return map[String(status)] || status || '-';
    },
    // 时间格式化
    formatTime(ts) {
      if (!ts) return '-';
      // 支持时间戳（秒/毫秒）或字符串
      let date;
      if (typeof ts === 'number' || /^\d+$/.test(ts)) {
        if (String(ts).length === 10) date = new Date(ts * 1000);
        else date = new Date(Number(ts));
      } else {
        date = new Date(ts);
      }
      if (isNaN(date.getTime())) return '-';
      const y = date.getFullYear();
      const m = String(date.getMonth() + 1).padStart(2, '0');
      const d = String(date.getDate()).padStart(2, '0');
      const h = String(date.getHours()).padStart(2, '0');
      const min = String(date.getMinutes()).padStart(2, '0');
      const s = String(date.getSeconds()).padStart(2, '0');
      return `${y}-${m}-${d} ${h}:${min}:${s}`;
    }
  }
}
</script>

<style scoped>
/* 导航栏样式 */
.nav-bar {
  height: 44px;
  background-color: #fff;
  color: #000;
  display: flex;
  align-items: center;
  position: relative;
  padding: 0 15px;
  font-weight: bold;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.back-btn {
  width: 24px;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.arrow-left {
  width: 12px;
  height: 12px;
  border-left: 2px solid #000;
  border-bottom: 2px solid #000;
  transform: rotate(45deg);
}

.title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 16px;
}

/* 主容器 */
.dhjilu-container {
  min-height: calc(100vh - 44px);
  background: linear-gradient(135deg, #c94545 0%, #b43a39 100%);
  padding: 20px;
  padding-bottom: 100px;
}

.content-wrapper {
  background: white;
  border-radius: 12px;
  padding: 15px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

/* 记录列表 */
.record-list {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.record-card {
  background: #fff0f0;
  border-radius: 14px;
  padding: 18px 16px;
  box-shadow: 0 2px 8px rgba(180,58,57,0.08);
  margin-bottom: 0;
}

.row {
  display: flex;
  align-items: center;
  margin-bottom: 7px;
  font-size: 15px;
}

.row:last-child {
  margin-bottom: 0;
}

.label {
  color: #b43a39;
  font-weight: 600;
  min-width: 80px;
}

.value {
  color: #333;
  font-weight: 500;
  word-break: break-all;
}

.value.name {
  font-size: 16px;
  font-weight: bold;
}

.value.status {
  font-weight: bold;
}

.status-0 {
  color: #e67c00;
}

.status-1 {
  color: #1bbf1b;
}

.main-row {
  margin-bottom: 10px;
}

/* 响应式优化 */
@media screen and (max-width: 375px) {
  .dhjilu-container {
    padding: 15px;
  }
  
  .record-card {
    padding: 12px 8px;
  }
  
  .label {
    min-width: 60px;
    font-size: 14px;
  }
  
  .value {
    font-size: 14px;
  }
}

@media screen and (max-width: 320px) {
  .label {
    min-width: 80px;
  }
  
  .value {
    font-size: 13px;
  }
}

/* 空状态 */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.empty-state .icon {
  font-size: 48px;
  margin-bottom: 15px;
  color: #ddd;
}

.empty-state .text {
  font-size: 16px;
  line-height: 1.5;
}
</style>