var t = function(t) {
    return t && t.__esModule ? t : {
      default: t
    };
  }(require("../../../../utils/LoadMore.js")),
  e = getApp(),
  a = e.request,
  o = new t.default();
var oo = e.globalData.setting,
  app_d = e.globalData;
var ut = require('../../../../utils/util.js');

Page({
  data: {
    url: e.globalData.setting.imghost,
    resourceUrl: e.globalData.setting.resourceUrl,
    iurl: e.globalData.setting.imghost,
    collects: null,
    currentPage: 1,
    curpage: 1,
    ismore: 0,
    iscollects: 0,
    editEd: 0, //商品编辑按钮是否点击
    isall: false, //是否全选
    sum: 0,
    total_collects: 0,
    is_goods: 0,
    pageSize: 8, //分页数量
    isdelete: 0,

    is_retail_price:0
  },
  onLoad: function() {

    var th=this;

    o.init(this, "", "collects"),
      this.requestCollectList();


    getApp().getConfig2(function(rs){
      //计算等级价相关
      var swithc_list=rs.switch_list;
      var sw_arr=JSON.parse(swithc_list);
      if(sw_arr.is_retail_price){
        th.setData({is_retail_price:1});
      }

    });

  },
  requestCollectList: function() {
    var th = this;
    e = "/api/weshop/goodscollect/list?" + "pageSize=" + th.data.pageSize + "&user_id=" +
      app_d.user_id + "&store_id=" + oo.stoid + "&page=" + th.data.currentPage;
    o.request(e, function(e) {
      th.data.currentPage++;
      if (th.data.total_collects == 0) {
        th.setData({
          iscollects: 1,
          total_collects: e.data.data.total
        })
      }
      if (th.data.total_collects < th.data.pageSize) {

        th.setData({
          is_goods: 1
        })
        var goods_list = th.selectComponent("#goods_recommend"); //组件的id
        if (goods_list)
        {
          goods_list.init();
          setTimeout(function () { goods_list.get_list(); }, 300)  
        }
      }
    });
  },

  //--到底部刷新---
  onReachBottom: function() {
    if ((this.data.currentPage - 1) * this.data.pageSize < this.data.total_collects) {
      this.requestCollectList();
    } else {
      if (this.data.is_goods == 0) this.setData({
        is_goods: 1
      });
      var goods_list = this.selectComponent("#goods_recommend"); //组件的id
      goods_list.get_list();
    }

  },

  //--取消收藏---
  cancelCollect: function(t) {
    var e = t.currentTarget.dataset.id,
      o = this;
    a.delete("/api/weshop/goodscollect/del/" + oo.stoid + "/" + e, {
      success: function(t) {
        o.deleteItemData(e);
      }
    });
  },
  deleteItemData: function(t) {
    for (var e = 0; e < this.data.collects.length; e++)
      if (this.data.collects[e].collect_id == t) {
        this.data.collects.splice(e, 1), this.setData({
          collects: this.data.collects,
        });
        break;
      }
	
	this.setData({
		total_collects: this.data.collects.length,
	});
  },
  //图片失败，默认图片
  bind_bnerr: function(e) {
    var _errImg = e.currentTarget.dataset.errimg;
    var _errObj = {};
    _errObj[_errImg] = this.data.iurl + "/miniapp/images/default_g_img.gif";
    this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;

  },

  requestList: function() {

    var e = this;
    a.get("/api/weshop/goods/page", {
      isShowLoading: 0,
      data: {
        store_id: 1,
        user_id: oo.user_id,
        page: e.data.curpage,
        pageSize: 10
      },
      success: function(t) {
        e.data.curpage++;
        var arr1 = e.data.collects_Recommend;
        var arr2 = t.data.data.pageData;
        var arr3 = [...arr1, ...arr2];

        var ismore = 0;
        if (arr3.length == t.data.data.total) ismore = 1

        e.setData({
          collects_Recommend: arr3,
          total_goods: t.data.data.total,
          ismore_goods: ismore,
          loadName: null
        }), wx.stopPullDownRefresh();
      }
    });
  },


  goto: function(ee) {
    getApp().goto("/pages/index/index/index");
    
  },
  //编辑商品显示
  editcollects: function() {
    var th = this.data;
    var t = this;
    if (th.editEd == 0) {
      t.select_checked(false, null)
      t.setData({
        editEd: 1,
       isall:0
      })
    } else {
      t.select_checked(false, null)
      t.setData({
        editEd: 0
      })
    }
  },
  iconClick: function(e) {
    var th = this.data;
    var index = e.currentTarget.dataset.index;
    var url = e.currentTarget.dataset.url;

    var item=this.data.collects[index];
    if(url){
      console.log("---");
      console.log(item.add_time);
      console.log(ut.gettimestamp());

      if(ut.gettimestamp()-item.add_time<3600*24 && item.guide_id>0)
          url+="&c_guide_id="+item.guide_id;
      getApp().goto(url);
      return false;
    }
    var collect = "collects[" + index + "].checked";
    if (this.data.collects[index].checked == true) {
      this.setData({
        [collect]: false,
        isall: 0,
        sum: --th.sum
      })
    } else {
      var id = th.collects[index].collect_id;
      this.setData({
        [collect]: true,
        sum: ++th.sum
      })
      if (th.sum == th.collects.length) {
        this.setData({
          isall: 1
        })
      }
    }
  },
  //全选为收藏商品checked为true
  allClick: function() {
    var th = this;
    var list = th.data.collects;
    //判断是否全选
    if (th.data.isall) {
      for (var i = 0; i < list.length; i++) {
        list[i].checked = false;
      }
      this.setData({
        sum: 0,
        isall: 0,
        collects: list
      })
    } else {
      for (var i = 0; i < list.length; i++) {
        list[i].checked = true;
      }
      this.setData({
        sum: th.data.collects.length,
        collects: list,
        isall: 1
      })
    }
  },

  //分页删除
  alldelete: function(e) {
   
    var th = this;
    if (th.data.isdelete) return false;
    // 全选的话直接删除
    if (th.data.isall) {
      a.delete("/api/weshop/goodscollect/removeUser/" + oo.stoid + "/" + app_d.user_id, {
        isShowLoading: 0,
        success: function(e) {
          th.setData({
            collects: [],
            total_collects: 0,
            isdelete:0
          })
          getApp().my_warnning("取消成功", 1, th);

        }
      })
    } else { //按照多个id删除收藏商品

      var new_colls = [];
      var collectId = th.data.collects;

      var str = "";
      for (var i = 0; i < th.data.collects.length; i++) {
        if (collectId[i].checked) {
          str += collectId[i].collect_id + ",";
        } else {
          new_colls.push(collectId[i]);
        }
      }
      if(str.length==""){
        getApp().my_warnning("请选择商品", 0, th);
        return false;
      }
      th.setData({
        isdelete: 1
      })
      str = str.substr(0, str.length - 1);
      var url = "/api/weshop/goodscollect/removeByIds/" + oo.stoid + "/" + app_d.user_id + "/" + str;

      a.delete(url, {
        isShowLoading: 0,
        success: function(e) {
          th.setData({
            collects: new_colls,
            sum: 0,
            total_collects: new_colls.length,
            isdelete: 0
          })
          // th.select_checked(false,null);
          getApp().my_warnning("取消收藏成功", 1, th);
        }

      })

    }
  },
  select_checked: function(check, sum) {
    var th = this;
    if (sum == null) {
      for (var i = 0; i < this.data.collects.length; i++) {
        var collect = "collects[" + i + "].checked";
        th.setData({
          [collect]: check,
          sum:0
        })
      }
    } else {
      var collect = "collects[" + sum + "].checked";
      th.setData({
        [collect]: check
      })
    }
  },

  go_to: function(e) {
    var th = this;
    var url = e.currentTarget.dataset.url;
    getApp().goto(url);
  }
});