<template>
  <view class="points-detail">
    <!-- 顶部导航栏 -->
    <view class="header">
      <view class="back-icon" @click="handleBack"></view>
      <text class="header-title">积分明细</text>
      <view class="header-right"></view>
    </view>

    <!-- 积分余额卡片 -->
    <view class="balance-card">
      <view class="balance-label">当前积分</view>
      <view class="balance-value">{{ balance }}</view>
    </view>

    <!-- 明细列表 -->
    <view class="records-section">
      <view class="section-title">积分记录</view>
      
      <view v-if="records.length === 0 && !loading" class="empty-tip">
        暂无积分记录
      </view>
      
      <view class="record-list">
        <view class="record-item" v-for="(item, index) in records" :key="index">
          <view class="record-left">
            <view class="record-icon" :class="item.points > 0 ? 'income' : 'expense'">
              {{ item.points > 0 ? '+' : '-' }}
            </view>
            <view class="record-info">
              <view class="record-reason">{{ item.reason }}</view>
              <view class="record-time">{{ formatTime(item.createTime) }}</view>
            </view>
          </view>
          <view class="record-points" :class="item.points > 0 ? 'income' : 'expense'">
            {{ item.points > 0 ? '+' : '' }}{{ item.points }}
          </view>
        </view>
      </view>
      
      <!-- 加载更多 -->
      <view class="load-more" v-if="hasMore" @click="loadMore">
        {{ loading ? '加载中...' : '加载更多' }}
      </view>
    </view>
  </view>
</template>

<script>
import api from '@/utils/api.js'

export default {
  name: 'points-detail',
  data() {
    return {
      balance: 0,
      records: [],
      makerId: null,
      pageNum: 1,
      pageSize: 20,
      total: 0,
      loading: false
    }
  },
  computed: {
    hasMore() {
      return this.records.length < this.total
    }
  },
  onLoad() {
    this.initData()
  },
  onShow() {
    // 每次显示页面时刷新积分数据，确保数据同步
    if (this.makerId) {
      this.pageNum = 1
      this.loadRecords()
    }
  },
  methods: {
    async initData() {
      const userInfo = uni.getStorageSync('userInfo')
      if (userInfo && userInfo.matchmakerId) {
        this.makerId = userInfo.matchmakerId
      } else if (userInfo && userInfo.userId) {
        // 如果没有matchmakerId，通过API获取
        try {
          const res = await api.matchmaker.getByUserId(userInfo.userId)
          let matchmaker = res
          if (res && res.data) {
            matchmaker = res.data
          }
          if (matchmaker && (matchmaker.matchmakerId || matchmaker.matchmaker_id)) {
            this.makerId = matchmaker.matchmakerId || matchmaker.matchmaker_id
            // 保存到userInfo中
            userInfo.matchmakerId = this.makerId
            uni.setStorageSync('userInfo', userInfo)
          }
        } catch (e) {
    
        }
      }
      
      await this.loadRecords()
    },
    
    async loadRecords() {
      if (!this.makerId || this.loading) return
      
      this.loading = true
      try {
        const res = await api.pointsMall.getRecords(this.makerId, this.pageNum, this.pageSize)
        this.balance = res.balance || 0
        this.total = res.total || 0
        
        if (this.pageNum === 1) {
          this.records = res.list || []
        } else {
          this.records = [...this.records, ...(res.list || [])]
        }
      } catch (e) {
        
        uni.showToast({ title: '获取积分明细失败', icon: 'none' })
      } finally {
        this.loading = false
      }
    },
    
    loadMore() {
      if (this.hasMore && !this.loading) {
        this.pageNum++
        this.loadRecords()
      }
    },
    
    handleBack() {
      uni.navigateBack()
    },
    
    formatTime(timeStr) {
      if (!timeStr) return ''
      const date = new Date(timeStr)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hour = String(date.getHours()).padStart(2, '0')
      const minute = String(date.getMinutes()).padStart(2, '0')
      return `${year}-${month}-${day} ${hour}:${minute}`
    }
  }
}
</script>

<style lang="scss" scoped>
.points-detail {
  min-height: 100vh;
  background: #F5F5F5;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 25rpx 30rpx;
  padding-top: calc(25rpx + env(safe-area-inset-top));
  background: #FFFFFF;
  
  .back-icon {
    width: 44rpx;
    height: 44rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32rpx;
    color: #333;
    &::before { content: '‹'; }
  }
  
  .header-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
  }
  
  .header-right {
    width: 44rpx;
  }
}

.balance-card {
  background: linear-gradient(135deg, #9C27B0 0%, #E91E63 100%);
  margin: 30rpx;
  padding: 40rpx;
  border-radius: 20rpx;
  text-align: center;
  
  .balance-label {
    font-size: 28rpx;
    color: rgba(255, 255, 255, 0.8);
    margin-bottom: 15rpx;
  }
  
  .balance-value {
    font-size: 72rpx;
    font-weight: bold;
    color: #FFFFFF;
  }
}

.records-section {
  background: #FFFFFF;
  margin: 0 30rpx;
  border-radius: 20rpx;
  padding: 30rpx;
  
  .section-title {
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 30rpx;
  }
  
  .empty-tip {
    text-align: center;
    color: #999;
    font-size: 28rpx;
    padding: 60rpx 0;
  }
}

.record-list {
  .record-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 25rpx 0;
    border-bottom: 1rpx solid #F0F0F0;
    
    &:last-child {
      border-bottom: none;
    }
    
    .record-left {
      display: flex;
      align-items: center;
      
      .record-icon {
        width: 60rpx;
        height: 60rpx;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 32rpx;
        font-weight: bold;
        margin-right: 20rpx;
        
        &.income {
          background: #E8F5E9;
          color: #4CAF50;
        }
        
        &.expense {
          background: #FFEBEE;
          color: #F44336;
        }
      }
      
      .record-info {
        .record-reason {
          font-size: 28rpx;
          color: #333;
          margin-bottom: 8rpx;
        }
        
        .record-time {
          font-size: 24rpx;
          color: #999;
        }
      }
    }
    
    .record-points {
      font-size: 32rpx;
      font-weight: bold;
      
      &.income {
        color: #4CAF50;
      }
      
      &.expense {
        color: #F44336;
      }
    }
  }
}

.load-more {
  text-align: center;
  padding: 30rpx;
  color: #9C27B0;
  font-size: 26rpx;
}
</style>
