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

  /**
   * 页面的初始数据
   */
  data: {
    navBar: [
      { name: '活动助手', type: 1, isActive: true },
      { name: '品牌营销', type: 2, isActive: false },
      { name: '产品素材', type: 3, isActive: false },
    ],
    current: 1,
    guidCollege: {
      pageSize: 10,
      pageNum: 1,
      totalPage: 0,
      list: [],
      currentPage: 1,
      noData: false,
    },
    //品牌营销
    brandPromotions: {
      pageSize: 10,
      pageNum: 1,
      totalPage: 0,
      list: [],
      currentPage: 1,
      noData: false,
    },
    brandPromotionsNoData: false,
    //产品素材
    productFiles: {
      pageSize: 10,
      pageNum: 1,
      totalPage: 0,
      list: [],
      currentPage: 1,
      noData: false
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //首次进来先加载导购学院的列表
    this.getGuidCollegeList();
    wx.hideShareMenu();
  },

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

  //点击事件
  //分类点击
  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;
    // tab 切换时 如果发现对应的列表没有加载过，则去加载
    if (this.data.current == 1) {
      if (this.data.guidCollege.list.length == 0) {
        this.getGuidCollegeList();
      }
    }
    else if (this.data.current == 2) {
      if(this.data.brandPromotions.list.length == 0){
        this.getBrandPromotionList();
      }
    }
    else if (this.data.current == 3) {
      if(this.data.productFiles.list.length == 0){
        this.getProductFiles();
      }
    }

    this.setData({
      navBar: this.data.navBar,
      current: this.data.current
    });
  },
  //跳转导购学院详情
  onTapToGuidCollegeInfo(e) {
    let item = e.currentTarget.dataset.item;
    let id = item.id;
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/guidCollegeInfo/guidCollegeInfo?id=' + id,
    });
  },
 

  //去商城
  onTapToMall() {
    wx.redirectTo({
      url: '/pages/userCenter/userCenter',
    })
  },
  //获取导购学院列表
  getGuidCollegeList(refresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let pageSize = this.data.guidCollege.pageSize,
      pageNum = this.data.guidCollege.pageNum;
    wxService.post(`/merchant/guideCollege/getPageInfoListC?pageSize=${pageSize}&pageNum=${pageNum}`, {
      title: '',
      type : 1
    }).then(res => {
      if (res) {
        wx.hideLoading();
        const { result, data } = res.data
        if (result == 0) {
          this.data.guidCollege.list = this.data.guidCollege.list.concat(data.content);
          this.data.guidCollege.totalPage = data.totalPages;
          if (this.data.guidCollege.list.length == 0) {
            this.data.guidCollege.noData = true;
          }

          this.setData({
            guidCollege: this.data.guidCollege
          });

          if (refresh){
            wx.showToast({
              title: '刷新成功',
            });
            wx.stopPullDownRefresh();
          }
        }
      }
    })
  },
  //获取品牌营销列表
  getBrandPromotionList(refresh){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let pageSize = this.data.brandPromotions.pageSize,
      pageNum = this.data.brandPromotions.pageNum;
    wxService.post(`/merchant/guideCollege/getPageInfoListC?pageSize=${pageSize}&pageNum=${pageNum}`, {
      title: '',
      type: 2
    }).then(res => {
      if (res) {
        wx.hideLoading();
        const { result, data } = res.data
        if (result == 0) {
          this.data.brandPromotions.list = this.data.brandPromotions.list.concat(data.content);
          this.data.brandPromotions.totalPage = data.totalPages;
          if (this.data.brandPromotions.list.length == 0) {
            this.data.brandPromotions.noData = true;
          }

          this.setData({
            brandPromotions: this.data.brandPromotions
          });

          if (refresh) {
            wx.showToast({
              title: '刷新成功',
            });
            wx.stopPullDownRefresh();
          }
        }
      }
    });
  },
  //获取产品素材列表
  getProductFiles(refresh){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let pageSize = this.data.productFiles.pageSize,
      pageNum = this.data.productFiles.pageNum;
    wxService.post(`/merchant/guideCollege/getPageInfoListC?pageSize=${pageSize}&pageNum=${pageNum}`, {
      title: '',
      type: 3
    }).then(res => {
      if (res) {
        wx.hideLoading();
        const { result, data } = res.data
        if (result == 0) {
          this.data.productFiles.list = this.data.productFiles.list.concat(data.content);
          this.data.productFiles.totalPage = data.totalPages;
          if (this.data.productFiles.list.length == 0) {
            this.data.productFiles.noData = true;
          }

          this.setData({
            productFiles: this.data.productFiles
          });

          if (refresh) {
            wx.showToast({
              title: '刷新成功',
            });
            wx.stopPullDownRefresh();
          }
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if(this.data.current == 1){
      this.data.guidCollege.pageNum = 1;
      this.data.guidCollege.totalPage = 0;
      this.data.guidCollege.list = [];
      this.getGuidCollegeList(true);
    }
    else if(this.data.current == 2){
      this.data.brandPromotions.pageNum = 1;
      this.data.brandPromotions.totalPage = 0;
      this.data.brandPromotions.list = [];
      this.getBrandPromotionList(true);
    }
    else if(this.data.current == 3){
      this.data.productFiles.pageNum = 1;
      this.data.productFiles.totalPage = 0;
      this.data.productFiles.list = [];
      this.getProductFiles(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current == 1) {
      if(this.data.guidCollege.pageNum < this.data.guidCollege.totalPage){
        this.data.guidCollege.pageNum ++ ;
        this.getGuidCollegeList();
      }
      else{
        if (this.data.guidCollege.pageNum > 1){
          wx.showToast({
            title: '暂无更多数据啦~',
            icon : 'none'
          });
        }
      }
    }
    else if (this.data.current == 2) {
      if (this.data.brandPromotions.pageNum < this.data.brandPromotions.totalPage){
        this.data.brandPromotions.pageNum++;
        this.getBrandPromotionList();
      }
      else{
        if (this.data.brandPromotions.pageNum > 1) {
          wx.showToast({
            title: '暂无更多数据啦~',
            icon: 'none'
          });
        }
      }
    }
    else if (this.data.current == 3) {
      if (this.data.productFiles.pageNum < this.data.productFiles.totalPage) {
        this.data.productFiles.pageNum++;
        this.getProductFiles();
      }
      else {
        if (this.data.getProductFiles.pageNum > 1) {
          wx.showToast({
            title: '暂无更多数据啦~',
            icon: 'none'
          });
        }
      }
    }
  },
})