<template>
<view id="myReceive">
    <view class="myRecivepage">
        <view class="navbar mx-1px-top">
            <block v-for="(item, index) in tabList" :key="index" >
                <view :id="index" class="navbar-item" :class="activeIndex == index ? 'activity' : ''" @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);'"></view>
        </view>
        <view class="tab-panel">
            <view class="tab-content" :hidden="activeIndex != 0" v-if="dataList && dataList[0] && dataList[0][0] && tabList && tabList[0]">
		
                <view class="no-list" v-if="!dataList[0][0].length && tabList[0].init">
                    <view>暂无累计收益!!!</view>
                </view>
                <view v-for="(items, idx) in dataList[0]" :key="idx" >
                   <view class="myReceive-item" v-for="(item, index) in dataList[0][idx]" :key="index">
                       <view class="orderNumber">
                           <span class="ordertop">分销订单{{item.agent_order_no}}</span>
                           <span class="orderbottom">{{item.created_at}}</span>
                       </view>
                       <view class="orderPrice add">￥{{item.commission}}</view>

                   </view>
            </view>
        </view>
            <view class="tab-content" :hidden="activeIndex != 1"  v-if="dataList && dataList[1] && dataList[1][0] && tabList && tabList[1]">
                <view class="no-list" v-if="!dataList[1][0].length && tabList[1].init">
                    <view>暂无累计收益!!!</view>
                </view>
                <view v-for="(items, idx) in dataList[1]" :key="idx">
                    <view class="myReceive-item" v-for="(item, index) in dataList[1][idx]" :key="index">
                        <view class="orderNumber">
                            <span class="ordertop">分销订单{{item.agent_order_no}}</span>
                            <span class="orderbottom">{{item.created_at}}</span>
                        </view>
                        <view class="orderPrice reduce">￥{{item.commission}}</view>

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

export default {
  data() {
    return {
      activeIndex: 0,
      sliderOffset: 0,
      width: 0,
      tabList: [{
        title: '累计收益',
        init: false,
        page: 0,
        more: true
      }, {
        title: "未到账收益",
        init: false,
        page: 0,
        more: true
      }],
      dataList: {
        0: [],
        1: []
      }
    };
  },

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

    if (this.tabList[status].more) {
      this.queryReceiveList(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) {
    if (e.type) {
      this.setData({
        activeIndex: e.type
      });
    }

    this.queryReceiveList();
  },

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

      if (!this.tabList[status].init) {
        this.queryReceiveList(status);
      }
    },

    queryReceiveList(type = 0, page = 1) {
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      var token = this.$cookieStorage.get('user_token') || '';

      if (type == 0) {
        this.$http.get({
          api: 'api/distribution/earnings',
          header: {
            Authorization: token
          },
          data: {
            page,
            type
          }
        }).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[${type}]`;
     //          this.setData({
     //            [`dataList.${type}[${page - 1}]`]: res.data,
     //            [`${tabList}.init`]: true,
     //            [`${tabList}.page`]: current_page,
     //            [`${tabList}.more`]: current_page < total_pages
     //          });
			  // 
			  
			 this.$set(this.dataList[type], page -1, res.data);
			 this.tabList[type].init=true;
			 this.tabList[type].page=pages;
			 this.tabList[type].more=pages < total_pages;
			  
            } else {
              wx.showModal({
                content: res.message || '请求失败',
                showCancel: false
              });
            }
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }

          wx.hideLoading();
        }).catch(rej => {
          wx.hideLoading();
          wx.showModal({
            content: rej.message || '请求失败',
            showCancel: false
          });
        });
      } else {
        this.$http.get({
          api: 'api/distribution/unearnings',
          header: {
            Authorization: token
          },
          data: {
            page,
            type
          }
        }).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[${type}]`;
              // this.setData({
              //   [`dataList.${type}[${page - 1}]`]: res.data,
              //   [`${tabList}.init`]: true,
              //   [`${tabList}.page`]: current_page,
              //   [`${tabList}.more`]: current_page < total_pages
              // });
			  
			  
			  this.$set(this.dataList[type], page -1, res.data);
			  this.tabList[type].init=true;
			  this.tabList[type].page=pages;
			  this.tabList[type].more=pages < total_pages;
			  
			  
			  
            } else {
              wx.showModal({
                content: res.message || '请求失败',
                showCancel: false
              });
            }
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }

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

    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 "myReceive";
</style>