<template>
<view id="vipManage">
    <view class="navbar mx-1px-bottom">
        <block>

            <view class="navbar-item" :class="index == activeIndex ? 'activity' : ''" v-for="(item, index) in tabList" :key="index"  @tap="tabClick" :data-index="index">
                <view class="navbar-title">{{item.title}}</view>
            </view>
            <!--<view id="{{index}}" hidden="{{activeIndex == index}}" class="navbar-item">
                <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="vipManagepage">
       <block v-if="activeIndex == 0 && tabList[0].init">
           <block v-if="dataList[0][0].length">
               <view v-for="(items,idx) in dataList[0]" :key="idx">
                   <view class="vipManage-item" v-for="(item, index) in items" :key="index" >
                       <image :src="item.user.avatar"></image>
                       <view class="item-right">
                           <view class="username">
                               <span class="nickname">{{item.user.nick_name}}</span>
                               <!-- <span class="grade">V{{item.grade}}</span>-->
                           </view>
                           <view class="vipdate">
                               <span class="vipnum">会员号：{{item.user_id}}</span>
                               <span>加入日期：{{item.user.created_at}}</span>
                           </view>
                       </view>
                   </view>
               </view>
           </block>
           <view class="isList" v-else>
               暂无会员
           </view>
       </block>
        <block v-if="activeIndex == 1 && tabList[1].init">
            <block v-if="dataList[1][0].length">
                <view v-for="(items, idx) in dataList[1]" :key="idx" >
                    <view class="vipManage-item" v-for="(item, index) in items" :key="index" >
                        <image :src="item.user.avatar"></image>
                        <view class="item-right">
                            <view class="username">
                                <span class="nickname">{{item.user.nick_name}}</span>
                                <!-- <span class="grade">V{{item.grade}}</span>-->
                            </view>
                            <view class="vipdate">
                                <span class="vipnum">会员号：{{item.user_id}}</span>
                                <span>加入日期：{{item.user.created_at}}</span>
                            </view>
                        </view>
                    </view>
                </view>
            </block>
            <block v-else>
                <view class="isList">
                    暂无会员
                </view>
            </block>
        </block>
    </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() {
    var index = this.activeIndex;
    var type = '';

    if (index == 0) {
      type = 'level1';
    } else if (index == 1) {
      type = 'level2';
    }

    var hasMore = this.tabList[this.activeIndex].more;
    var page = this.tabList[this.activeIndex].page + 1;

    if (hasMore) {
      this.queryVipList(page, type);
    } else {
      wx.showToast({
        image: '../../../static/error.png',
        title: '再拉也没有啦'
      });
    }
  },

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

  onLoad() {
    this.queryVipList(1, 'level1');
  },

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

      if (index == 0) {
        type = 'level1';
      } else if (index == 1) {
        type = 'level2';
      }

      if (!this.tabList[index].init) {
        this.queryVipList(1, type);
      }
    },

    //请求会员列表
    queryVipList(page, type) {
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      var token = this.$cookieStorage.get('user_token');
      this.$http.get({
        api: 'api/distribution/agent/members',
        header: {
          Authorization: token
        },
        data: {
          page: page,
          type: type
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            var status = '';

            if (type == 'level1') {
              status = 0;
            } else if (type == 'level2') {
              status = 1;
            }

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

        // this.setData({
        //   show: false
        // });
		this.show=false;
		
        wx.hideLoading();
      }).catch(rej => {
        wx.hideLoading();
        wx.showModal({
          content: rej.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 "vipManage";
</style>