// pages/more/goods_collect/goods_collect.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    isEdit: false,// 0编辑 1完成
    selectAll: false,
    showSelection: false,
    hasMore: true,
    collectList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      imgUrlBefore: wx.getStorageSync('imgUrlBefore')
    })
  },
  /**
 * 生命周期函数--监听页面显示
 */
  onShow: function () {
    var that = this;
    that.getCollectList(that);
    that.getCollectCount(that);
  },
  editFinish: function(){
    var isEdit = this.data.isEdit;
    this.setData({
      isEdit: 1 - isEdit,
    })
  },
  //选择商品
  selectItem: function(e){
    var index = e.currentTarget.dataset.index;
    var collectList = this.data.collectList;
    collectList[index].choosed = !collectList[index].choosed;
    if ( !collectList[index].choosed && this.data.selectAll){
      this.setData({
        selectAll: false
      })
    } else if (collectList[index].choosed && !this.data.selectAll){
      this.judgeSelectAll(this, collectList);
    }
    this.setData({
      collectList: collectList
    })
  },
  //选择所有或取消
  selectAllOrNot: function(){
    var collectList = this.data.collectList;
    var selectAll = this.data.selectAll;
    for (var i = 0, length = collectList.length; i< length; i++ ){
      collectList[i].choosed = !selectAll;
    }
    this.setData({
      collectList: collectList,
      selectAll: !selectAll
    })
  },
  //判断是否选择所有商品了
  judgeSelectAll: function (that, collectList){
    var c_length = collectList.length;
    var tempSelectAll = true;
    for (var m = 0; m < c_length ; m++){
      if (!collectList[m].choosed){
        tempSelectAll = false;
        break;
      }
    }
    that.setData({
      selectAll: tempSelectAll
    })
  },
  //获取收藏列表
  getCollectList: function(that){
    if(!that.data.hasMore){return}
    wx.request({
      url: app.globalData.hostname + 'mine_collect',
      data: {user_id: wx.getStorageSync('openid'),page: that.data.page},
      method: 'GET',
      dataType: 'json',
      success: function (collectRes) {
        if (collectRes.data.code == 200 && collectRes.data.data.length > 0 ){
          var collectList = collectRes.data.data;
          for (var k = 0, length = collectList.length ; k < length ; k++){
            collectList[k].choosed = false;
          }
          collectList = that.data.collectList.concat(collectList)
          that.setData({
            collectList: collectList,
            page: ++that.data.page
          })
        }else{
          that.setData({
            hasMore: false,
          })
        }
        // console.log(collectRes,'collectRes');
      },
      fail: function (collectRes) {},
      complete: function (collectRes) {
      },
    })
  },
  loadMore: function(){
    var that = this;
    that.getCollectList(that);
  },
  //取消收藏商品
  cancleCollect: function(){
    var that = this;
    var collectList = that.data.collectList;
    var collect_id = '';
    for (var k = 0, length = collectList.length; k < length; k++) {
      if ( collectList[k].choosed ){
        collect_id += "," + collectList[k].id;
      }
    }
    if (collect_id == ''){
      wx.showModal({
        title: '温馨提示',
        content: '您还没有选择商品',
        showCancel: false
      })
    }else {
      collect_id = collect_id.slice(1, collect_id.length);
      wx.showModal({
        title: '温馨提示',
        content: '是否要移除关注所选商品',
        success: function(modalRes){
          if (modalRes.confirm){
            wx.request({
              url: app.globalData.hostname + "collects_delete",
              data: { collect_id: collect_id },
              method: 'GET',
              dataType: 'json',
              success: function (removeRes) { 
                console.log(removeRes);
                if (removeRes.data.code == 200){
                  wx.showToast({
                    title: '删除成功',
                    duration: 1500,
                    success: function(){
                      setTimeout(function(){
                        that.setData({
                          page: 1,
                          hasMore: true,
                          collectList: [],
                        })
                        that.getCollectList(that);
                        that.getCollectCount(that);
                      },1000)
                    }
                  })
                }
              },
              fail: function (removeRes) { },
              complete: function (removeRes) { },
            })
          }
        }
      })
    }
  },
  //获取收藏数量
  getCollectCount: function(that){
    wx.request({
      url: app.globalData.hostname + "count_collect",
      data: {user_id: wx.getStorageSync("openid")},
      method: 'GET',
      dataType: 'json',
      success: function(countRes) {
        if (countRes.data.code == 200){
          console.log(countRes);
          that.setData({
            collectCount: countRes.data.data
          })
        }
      },
      fail: function (countRes) {},
      complete: function (countRes) {},
    })
  },
  // 查看商品详情
  goGoodsDetail: function(e){
    var id = e.currentTarget.dataset.goods_id;
    wx.redirectTo({
      url: '../../index/goodsdetail/goodsdetail?id='+id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },


})