const api = require('../../utils/api');

Page({
  data: {
    balance: 0,
    total: 0,
    logs: [],
    loading: true
  },

  onLoad() {
    this.load();
  },

  async load() {
    this.setData({ loading: true });
    try {
      const [bal, logs] = await Promise.all([
        api.getPoints().catch(() => null),
        api.getPointsLogs({ page: 1, pageSize: 50 }).catch(() => null)
      ]);
      this.setData({
        balance: (bal && bal.data && bal.data.points) || 0,
        total: (bal && bal.data && bal.data.total_points) || 0,
        logs: (logs && logs.data && logs.data.list) || [],
        loading: false
      });
    } catch (e) {
      this.setData({ loading: false });
    }
  },

  typeText(t) {
    const map = {
      sign: '每日签到',
      order_reward: '消费送积分',
      redeem: '积分兑换',
      deduct: '积分抵现',
      recharge: '充值赠送',
      admin: '后台调整',
      refund: '积分返还'
    };
    return map[t] || t || '';
  }
});
