<template>
<view id="point_index">
    <!--point-top开始-->
    <view class="point-top" :style="'background: ' + config.mainColor">
        <view class="point-current">
            <view>当前可用积分</view>
            <view class="point-num">{{point.pointValid}}</view>
			<view>冻结积分 {{point.pointFrozen}}</view>
        </view>
        <view class="point-frozen">
            <view class="point-mall" @tap="jump">积分商城</view>
        </view>
    </view>

    <!--point-top结束-->

    <!--point-content开始-->
    <view class="point-content">
        <view class="navbar mx-1px-bottom">
            <block v-for="(item, index) in tabList" :key="index" >
                <view :id="index" class="navbar-item activity" :hidden="activeIndex != index" :style="'color: ' + config.mainColor" @tap="tabClick">
                    <view class="navbar-title">{{item.title}}</view>
                </view>

                <view :id="index" :hidden="activeIndex == index" class="navbar-item" @tap="tabClick">
                    <view class="navbar-title">{{item.title}}</view>
                </view>
            </block>
            <view class="navbar-slider" :style="'width: ' + width + 'px; transform: translateX(' + sliderOffset + 'px); -webkit-transform: translateX(' + sliderOffset + 'px); background: ' + config.mainColor"></view>
        </view>


        <view class="tab-panel">
            <view class="tab-content" :hidden="activeIndex != 0">
                <view class="point-item mx-1px-top">
                    <view v-for="(item, idx) in dataList[0]" :key="idx">
                        <view class="point-info mx-1px-bottom" v-for="(point, index) in dataList[0][idx]" :key="index" >
                            <view class="info-left">
                                <view class="store-title">{{point.note}}</view>
                                <view class="up-time">{{point.updated_at}}</view>
                            </view>

                            <view class="info-right">
                                <view class="up-money">{{point.value}}</view>
                                <view class="point-status">{{point.status_text}}</view>
                            </view>
                        </view>
                    </view>

                </view>
            </view>

            <view class="tab-content" :hidden="activeIndex != 1">
                <view class="point-item mx-1px-top">
                    <view v-for="(item, idx) in dataList[1]" :key="idx" >
                        <view class="point-info mx-1px-bottom" v-for="(point, index) in dataList[1][idx]" :key="index">
                            <view class="info-left">
                                <view class="store-title">{{point.note}}</view>
                                <view class="up-time">{{point.updated_at}}</view>
                            </view>
                            <view class="slow-money">{{point.value}}</view>
                        </view>
                    </view>

                </view>
            </view>
        </view>
    </view>
    <!--point-content结束-->

</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      activeIndex: 0,
      sliderOffset: 0,
      sliderLeft: 0,
      width: 0,
      tabList: [{
        title: "收入",
        init: false,
        page: 0,
        more: true
      }, {
        title: "支出",
        init: false,
        page: 0,
        more: true
      }],
      dataList: {
        0: [],
        1: []
      },
      point: {},
      config: ''
    };
  },

  onReachBottom(e) {
    var status = this.activeIndex;
    var page = this.tabList[status].page + 1;
    var tabList = `tabList[${status}]`;

    if (this.tabList[status].more) {
      this.queryPointListBalance(status, page);
    } else {
      wx.showToast({
        image: '../../../static/error.png',
        title: '再拉也没有啦'
      });
    }
  },

  onShow(e) {
    wx.getSystemInfo({
      success: res => {
        this.setData({
          width: res.windowWidth / this.tabList.length,
          sliderOffset: res.windowWidth / this.tabList.length * this.activeIndex
        });
      }
    });
  },

  onLoad(e) {
    // 第三方平台配置颜色
    var config = this.$cookieStorage.get('globalConfig') || '';
    this.setData({
      config: config
    });

    if (e.type) {
      this.setData({
        activeIndex: e.type
      });
    }

    this.queryPointListBalance(this.activeIndex);
    this.queryUserPoint('default');
  },

  components: {},
  props: {},
  methods: {
    // 点击切换
    tabClick(e) {
      var status = e.currentTarget.id;
      this.setData({
        sliderOffset: e.currentTarget.offsetLeft,
        activeIndex: status
      });

      if (!this.tabList[status].init) {
        // wx.showLoading({
        //     title: "加载中",
        //     mask: true
        // });
        this.queryPointListBalance(status);
      }
    },

    // 查询积分列表
    queryPointListBalance(status = 0, page = 1) {
      var token = this.$cookieStorage.get('user_token');
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      var balance = status ? 'out' : 'in';
      var params = balance ? {
        balance
      } : {};
      params.page = page;
      params.type = 'default';
      this.$http.get({
        api: 'api/users/point/list',
        header: {
          Authorization: token
        },
        data: params
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            var pages = res.meta.pagination;
            var current_page = pages.current_page;
            var total_pages = pages.total_pages;
            var tabList = `tabList[${status}]`;
            // this.setData({
            //   [`dataList.${status}[${page - 1}]`]: res.data,
            //   [`${tabList}.init`]: true,
            //   [`${tabList}.page`]: current_page,
            //   [`${tabList}.more`]: current_page < total_pages,
            //   [`${tabList}.show`]: false
            // });
			this.dataList[status][page - 1]=res.data;
			this.tabList[status].init=true;
			this.tabList[status].page=current_page;
			this.tabList[status].more=current_page < total_pages;
			this.tabList[status].show=false;
			
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: '请求失败',
            showCancel: false
          });
        }

        wx.hideLoading();
      }).catch(rej => {
        wx.hideLoading();
        wx.showModal({
          content: '请求失败',
          showCancel: false
        });
      });
    },

    // 查询用户积分
    queryUserPoint(type) {
      var token = this.$cookieStorage.get('user_token');
      this.$http.get({
        api: 'api/users/point',
        header: {
          Authorization: token
        },
        data: {
          type: type
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;
          this.setData({
            point: res.data
          });
        } else {
          wx.showModal({
            content: '请求失败',
            showCancel: false
          });
        }
      });
    },

    jump() {
      wx.navigateTo({
        url: '/pages/pointStore/index/index'
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "index";
</style>