// shoppingGuid/page/pages/selectUsers/selectUsers.js
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar: [{ name: '自动标签', type: 1, isActive: true }, { name: '手动标签', type: 2, isActive: false }],
    current: 1,
    isOpen : false,
    groups : [],
    tags : [],
    noTag : false,
    noMoreData : true,
    pagination : {
      pageSize : 10,
      pageNum : 1,
      totalPage : 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 一进来先获取一下自动标签的数据
    this.getMemberTagGroups();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //点击事件
  onTapExpandOrInpand(e){
    this.setData({
      isOpen: !this.data.isOpen
    })
  },
  //跳转到标签下用户列表
  onTapToTagUserList(e){
    let item = e.currentTarget.dataset.item;
    let tagId = item.id , groupId = item.groupId , type = item.type;
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/userList/userList?id=' + Integer.digit(tagId, 10, 64) + '&type=' + type + 
      '&groupId=' + Integer.digit(groupId, 10, 64) + '&name=' + item.name,
    });
  },
  //tab 切换
  onTopBarTap(e){
    let index = e.currentTarget.dataset.index,
      item = e.currentTarget.dataset.item;
    this.data.navBar.forEach(nav => {
      nav.isActive = false;
    });

    this.data.navBar[index].isActive = true;
    this.data.current = this.data.navBar[index].type;
    this.setData({
      navBar: this.data.navBar,
      current: this.data.current
    });

    this.getMemberTagGroups();
  },
  //获取标签分组
  getMemberTagGroups(){
    this.data.groups = [];
    wxService.post(`/marketing/memberTagGroup/findPage?pageSize=10000&pageNo=1&type=${this.data.current}`).then(res => {
      let groups = res.data.data.content ? res.data.data.content : [];
      groups.forEach(item => {
        item.isActive = false;
      });

      this.data.groups = groups;
      if (this.data.groups.length > 0){
        //选中第一个
        this.data.groups[0].isActive = true;
        //获取分组的标签
        this.getTagsList();
      }
      this.setData({
        groups: this.data.groups
      });
    });
  },
  //获取分组下的标签列表
  getTagsList(){
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    let groupId = this.data.groups.filter(item => item.isActive)[0].id;
    this.data.tags = [];
    let pageSize = this.data.pagination.pageSize,
      pageNum = this.data.pagination.pageNum;
    wxService.post(`/marketing/shoppingguide/findPage?pageSize=${pageSize}&pageNo=${pageNum}`,{
      groupId: groupId,
      type : this.data.current
    }).then(res => {
      let list = res.data.data.content ? res.data.data.content : [];
      this.data.tags = this.data.tags.concat(list);
      this.data.pagination.totalPage = res.data.data.totalPages;
      if (this.data.tags.length == 0){
        this.data.noTag = true;
      }
      
      this.setData({
        tags: this.data.tags,
        noTag: this.data.noTag,
        pagination: this.data.pagination,
        noMoreData: true
      },() => {
        wx.hideLoading();
      });
    });
  },
  //选择group
  onTapSelectGroup(e){
    let index = e.currentTarget.dataset.index , 
      item = e.currentTarget.dataset.item;
    this.data.groups.forEach(item => {
      item.isActive = false;
    });
  
    this.data.groups[index].isActive = true;
    this.setData({
      groups: this.data.groups,
      isOpen : false,
    });
    this.getTagsList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.pagination.pageNum < this.data.pagination.totalPage){
      this.data.pageNum ++;
      this.getTagsList();
      this.setData({
        noMoreData: true
      });
    }
    else{
      this.setData({
        noMoreData : false
      });
    }
  },
});