var app = getApp();
var a = require("../../utils/public");
var status = require('../../utils/index.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartNum: 0,
    rushCategoryData: {
      tabs: [],
      activeIndex: 0
    },
    rushList: [],
    categoryScrollBarTop: 0,
    resetScrollBarTop: 50,
    loadMore: true,
    loadText: "加载中...",
    scrollViewHeight: 0,
    isFirstCategory: true,
    isLastCategory: false,
    loadingMaskVisible: false,
    pageEmpty: false,
    active_sub_index: 0
  },
  $data: {
    options: {},
    rushCategoryId: "",
    pageNum: 1,
    actIds: [],
    loading: true,
    isScrollTop: true,
    isScrollBottom: false,
    scrollInfo: null,
    isSetCategoryScrollBarTop: true,
    touchStartPos: {
      pageX: 0,
      pageY: 0
    }
  },
  isFirst: 0,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading();
    wx.hideTabBar();
    status.setNavBgColor();
    util.check_login() ? this.setData({
      is_login: true
    }) : this.setData({
      is_login: false
    });
    let systemInfo = app.globalData.systemInfo;
    let isIpx = app.globalData.isIpx;
    isIpx && this.setData({
      isIpx: true
    });

    this.getScrollViewHeight();
    this.setData({
      subCateHeight: this.getPx(44)
    })

    this.gid = app.globalData.typeCateId || 0;
    app.globalData.typeCateId = 0;
    this.get_cate_list();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      tabbarRefresh: true
    })
    let that = this;
    (0, status.cartNum)('', true).then((res) => {
      res.code == 0 && that.setData({
        cartNum: res.data
      })
    });

    if (that.isFirst >= 1) {
      let goodsListCarCount = app.globalData.goodsListCarCount; //[{ actId: 84, num: 2}]
      let rushList = this.data.rushList;
      if (goodsListCarCount.length > 0 && rushList.length > 0) {
        let newRushList = [];
        let changeCarCount = false;
        goodsListCarCount.forEach(function(item) {
          let k = rushList.findIndex((n) => n.actId == item.actId);
          if (k != -1 && rushList[k].skuList.length === 0) {
            let newNum = item.num * 1;
            rushList[k].car_count = newNum >= 0 ? newNum : 0;
            changeCarCount = true;
          }
        })
        this.setData({
          rushList,
          changeCarCount
        })
      }

      console.log(app.globalData.typeCateId)
      this.$data.rushCategoryId = app.globalData.typeCateId || 0;
      app.globalData.typeCateId = 0;
      if (this.$data.rushCategoryId) {
        let rushCategoryData = this.data.rushCategoryData;
        let tabs = rushCategoryData.tabs;
        let active_cate_id = this.$data.rushCategoryId;
        rushCategoryData.activeIndex = tabs.findIndex((p) => { return p.id == active_cate_id }) || 0;
        this.setData({
          rushCategoryData
        }, ()=>{
          that.setCategory(rushCategoryData.activeIndex);
        })
      }
    }
    that.isFirst++;
  },

  /**
   * 授权成功回调
   */
  authSuccess: function() {
    wx.reLaunch({
      url: '/lionfish_comshop/pages/type/index',
    })
  },

  onPullDownRefresh: function() {
    this.initPageData();
  },

  /**
   * 初始化数据
   */
  initPageData: function() {
    var t = this;
    this.setData({
      isFirstCategory: true,
      isLastCategory: false,
      rushList: [],
      resetScrollBarTop: 50
    }, function() {
      t.getHotList('init');
      
    });
  },

  onReachBottom: function() {
    var t = this,
      e = this.$data,
      loading = e.loading,
      canNext = this.data.canNext;
      console.log('滚到底了')
    if (!loading) {
      console.log('gungun')
      if (canNext) {
        this.$data.loading = true;
        this.getHotList();
      } else {
        this.getHotList();
      }
    }
  },

  touchstart: function(t) {
    if (t.changedTouches && t.changedTouches[0]) {
      var a = t.changedTouches[0],
        e = a.pageX,
        o = a.pageY;
      this.$data.touchStartPos = {
        pageX: e,
        pageY: o
      };
    }
  },

  touchend: function(t) {
    var a = this;
    if (t.changedTouches && t.changedTouches[0]) {
      var e = t.changedTouches[0],
        o = e.pageX,
        i = e.pageY,
        r = this.$data.touchStartPos,
        s = r.pageX,
        n = r.pageY,
        l = this.$data,
        g = l.isScrollTop,
        u = l.isScrollBottom,
        d = l.scrollInfo,
        c = (this.data.rushCategoryData, o - s),
        h = i - n;
      if (Math.abs(h) > Math.abs(c)) {
        if (h > 0) {
          if (!g) return;
          if (this.setData({
              resetScrollBarTop: 50
            }), h > 50) {
            var m = this.data.rushCategoryData.activeIndex - 1;
            if (m < 0) return;
            
            // this.setData({
            //   resetScrollBarTop: 50
            // }, function() {
            //   a.setCategory(m);
            // });
          }
        } else {
          if (!u || !this.data.canNext) return;
          if (h < -50) {
            var y = this.data.rushCategoryData,
              p = y.activeIndex + 1;
            if (p >= y.tabs.length || !this.$data.scrollInfo) return;
            // this.setData({
            //   resetScrollBarTop: d.scrollTop
            // }, function() {
            //   a.setCategory(p);
            // });
          }
        }
      } else {
        d && d.scrollTop < 50 && this.setData({
          resetScrollBarTop: 50
        });
      }
    }
  },

  scroll: function(t) {
    var a = t.detail,
      e = a.scrollTop,
      o = a.scrollHeight,
      i = this.data,
      r = i.scrollViewHeight,
      s = i.loadMore;
    this.$data.scrollInfo = a, this.$data.isScrollTop = e <= 49, this.$data.isScrollBottom = !s && o - e - r <= 10;
  },

  /**
   * 搜索栏高度
   */
  getScrollViewHeight: function() {
    var that = this;
    wx.createSelectorQuery().select(".search-bar").boundingClientRect(function(res) {
      res.height && that.setData({
        scrollViewHeight: wx.getSystemInfoSync().windowHeight - res.height
      });
    }).exec();
  },

  changeCategory: function(t) {
    var idx = t.currentTarget.dataset.index;
    console.log(idx)
    idx !== this.data.rushCategoryData.activeIndex && this.setCategory(idx);
  },

  setCategory: function(t) {
    var that = this,
      rushCategoryData = this.data.rushCategoryData,
      tabs = rushCategoryData.tabs[t] || {},
      scrollViewHeight = this.data.scrollViewHeight;

    this.$data.rushCategoryId = tabs.id || null, this.$data.pageNum = 1, this.$data.isSetCategoryScrollBarTop = false; 
    let isFirstCategory = !t;
    let isLastCategory = (t == rushCategoryData.tabs.length - 1);
    this.setData({
      "rushCategoryData.activeIndex": t,
      resetScrollBarTop: 50,
      categoryScrollBarTop: 50 * t - (scrollViewHeight - 50) / 2,
      rushList: [],
      canNext: false,
      isFirstCategory,
      isLastCategory,
      active_sub_index: 0,
      showDrop: false
    }, function() {
      that.getHotList();
    });
  },

  getHotList: function(type) {
    var t = this,
      e = this.$data,
      rushCategoryId = e.rushCategoryId;
    this.$data.loading = true;
    this.setData({
      loadingMaskVisible: true
    });

    this.reqPromise().then(function() {
      wx.stopPullDownRefresh();
      if(type === 'init'){
        t.setData({ resetScrollBarTop: 50 });
      }
    }).catch(function() {
      var a = {
        loadingMaskVisible: !1
      };
      rushCategoryId || (a.pageEmpty = true), t.$data.loading = false, t.setData(a), wx.stopPullDownRefresh();
    });
  },

  reqPromise: function() {
    let that = this;
    return new Promise(function(resolve, reject) {
      let token = wx.getStorageSync('token');
      let cur_community = wx.getStorageSync('community');
      let rushCategoryId = that.$data.rushCategoryId;
      app.util.request({
        url: 'entry/wxapp/index',
        data: {
          controller: 'index.load_gps_goodslist',
          token: token,
          pageNum: that.$data.pageNum,
          head_id: cur_community.communityId,
          gid: rushCategoryId
        },
        dataType: 'json',
        success: function(res) {
          if (res.data.code == 0) {
            let rushList = that.data.rushList.concat(res.data.list);
            let reduction = {
              full_money: res.data.full_money,
              full_reducemoney: res.data.full_reducemoney,
              is_open_fullreduction: res.data.is_open_fullreduction
            }

            var h = {
              rushList: rushList,
              pageEmpty: false,
              cur_time: res.data.cur_time,
              reduction,
              rushCategoryData: that.data.rushCategoryData
            };
            h.loadText = that.data.loadMore ? "加载中..." : "没有更多商品了~";
            h.loadingMaskVisible = false;
            that.$data.isSetCategoryScrollBarTop && (h.categoryScrollBarTop = 50 * h.rushCategoryData.activeIndex - (that.data.scrollViewHeight - 50) / 2);
            that.setData(h, function() {
              that.$data.loading = false, 
              that.$data.pageNum += 1, 
              !rushCategoryId && h.rushCategoryData.tabs && h.rushCategoryData.tabs[0] && (that.$data.rushCategoryId = h.rushCategoryData.tabs[0].id);
              // that.setData({ resetScrollBarTop: 50 });
            })
            if(res.data.list.length < 10){

              let s = {
                loadMore: false,
                loadingMaskVisible: false,
                canNext: true,
                // resetScrollBarTop: 50
              }
              that.setData(s);
            }
          } else if (res.data.code == 1) {
            let s = {
              loadMore: false,
              loadingMaskVisible: false,
              canNext: true,
              // resetScrollBarTop: 50
            }
            if (that.$data.pageNum == 1) {
              console.log('无数据');
              s.pageEmpty = true;
            }
            that.setData(s);
            //无数据
          } else if (res.data.code == 2) {
            //no login
            that.setData({
              is_login: false
            })
          }
          resolve(res);
        }
      })
    });
  },

  getPx: function(t) {
    return Math.round(app.globalData.systemInfo.windowWidth / 375 * t);
  },

  goResult: function(e) {
    let keyword = e.detail.value.replace(/\s+/g, '');
    if (!keyword) {
      wx.showToast({
        title: '请输入关键词',
        icon: 'none'
      })
      return;
    }
    wx.navigateTo({
      url: '/lionfish_comshop/pages/type/result?keyword=' + keyword,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      tabbarRefresh: false,
      changeCarCount: false
    })
  },

  showDrop: function() {
    this.setData({
      showDrop: !this.data.showDrop
    })
  },

  get_cate_list: function() {
    let that = this;
    app.util.request({
      'url': 'entry/wxapp/index',
      'data': {
        controller: 'goods.get_category_list'
      },
      dataType: 'json',
      success: function(res) {
        if (res.data.code == 0) {
          let leftList = res.data.data || [];
          let rushCategoryId = that.$data.rushCategoryId || leftList && leftList[0] && leftList[0].id || 0;
          let active_index = 0;
          if (that.$data.rushCategoryId) {
            active_index = leftList.findIndex((p) => {
              return p.id == that.$data.rushCategoryId
            });
          }
          that.$data.rushCategoryId = rushCategoryId;
          let rushCategoryData = {
            tabs: leftList,
            activeIndex: active_index
          }
          let isFirstCategory = !rushCategoryData.activeIndex;
          let isLastCategory = rushCategoryData.activeIndex == rushCategoryData.tabs.length - 1;

          that.setData({
            rushCategoryData,
            isFirstCategory,
            isLastCategory
          }, () => {
            that.initPageData();
            wx.hideLoading();
          })
        } else {
          // 分类不存在 todo
        }
      }
    })
  },

  // 切换子栏目
  change_sub_cate: function(e) {
    let rushCategoryData = this.data.rushCategoryData;
    let tabs = rushCategoryData.tabs;
    let active_index = rushCategoryData.activeIndex; //当前大栏目选中索引
    let active_sub_index = e.currentTarget.dataset.idx; //当前子栏目选中索引
    let active_cate_id = tabs[active_index].id; //当前大栏目选中id
    let subCate = tabs[active_index].sub; //子栏目
    let active_subcate_id = e.currentTarget.dataset.id || active_cate_id;
    let scrollLeft = this.getPx(64) * active_sub_index;
    console.log(scrollLeft);
    let that = this;
    that.$data.pageNum = 1;
    that.$data.rushCategoryId = active_subcate_id;
    that.setData({ resetScrollBarTop: 50 });
    that.setData({
      showDrop: false,
      active_cate_id: active_subcate_id,
      active_sub_index: active_sub_index,
      rushList: [],
      scrollLeft: scrollLeft,
      showEmpty: false,
      loadMore: true,
      loadText: '加载中'
    }, () => {
      that.getHotList();
    })
  },

  show_search: function() {
    wx.navigateTo({
      url: '/lionfish_comshop/pages/type/search',
    })
  },

  /**
   * 加入购物车
   */
  openSku: function(t) {
    var that = this,
      e = t.detail;
    var goods_id = e.actId;
    var options = e.skuList;
    that.setData({
      addCar_goodsid: goods_id
    })

    let list = options.list || [];
    let arr = [];
    if (list.length > 0) {
      for (let i = 0; i < list.length; i++) {
        let sku = list[i]['option_value'][0];
        let temp = {
          name: sku['name'],
          id: sku['option_value_id'],
          index: i,
          idx: 0
        };
        arr.push(temp);
      }
      //把单价剔除出来begin

      var id = '';
      for (let i = 0; i < arr.length; i++) {
        if (i == arr.length - 1) {
          id = id + arr[i]['id'];
        } else {
          id = id + arr[i]['id'] + "_";
        }
      }
      var cur_sku_arr = options.sku_mu_list[id];

      that.setData({
        sku: arr,
        sku_val: 1,
        cur_sku_arr: cur_sku_arr,
        skuList: e.skuList,
        visible: true,
        showSku: true
      });
    } else {
      let goodsInfo = e.allData;
      that.setData({
        sku: [],
        sku_val: 1,
        skuList: [],
        cur_sku_arr: goodsInfo
      })
      let formIds = {
        detail: {
          formId: ""
        }
      };
      formIds.detail.formId = "the formId is a mock one";
      that.gocarfrom(formIds);
    }
  },

  /**
   * 确认加入购物车
   */
  gocarfrom: function(e) {
    var that = this;
    var is_just_addcar = 1;
    wx.showLoading();
    a.collectFormIds(e.detail.formId);
    that.goOrder();
  },

  goOrder: function() {
    var that = this;
    if (that.data.can_car) {
      that.data.can_car = false;
    }
    var token = wx.getStorageSync('token');
    var community = wx.getStorageSync('community');

    var goods_id = that.data.addCar_goodsid;
    var community_id = community.communityId;
    var quantity = that.data.sku_val;

    var cur_sku_arr = that.data.cur_sku_arr;

    var sku_str = '';
    var is_just_addcar = 1;

    if (cur_sku_arr && cur_sku_arr.option_item_ids) {
      sku_str = cur_sku_arr.option_item_ids;
    }

    //addCar_goodsid: goods_id
    app.util.request({
      'url': 'entry/wxapp/user',
      'data': {
        controller: 'car.add',
        'token': token,
        "goods_id": goods_id,
        "community_id": community_id,
        "quantity": quantity,
        "sku_str": sku_str,
        "buy_type": 'dan',
        "pin_id": 0,
        "is_just_addcar": is_just_addcar
      },
      dataType: 'json',
      method: 'POST',
      success: function(res) {
        //console.log(res);
        if (res.data.code == 3) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == 4) {
          wx.showToast({
            title: '您未登录',
            duration: 2000,
            success: () => {
              that.setData({
                is_login: false
              })
            }
          })
        } else if (res.data.code == 6) {
          var msg = res.data.msg;
          let max_quantity = res.data.max_quantity || '';
          (max_quantity > 0) && that.setData({
            sku_val: max_quantity
          })
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        } else {
          if (is_just_addcar == 1) {
            that.closeSku();
            (0, status.cartNum)(res.data.total);
            that.setData({
              cartNum: res.data.total
            })
            wx.showToast({
              title: "已加入购物车",
              image: "../../images/addShopCart.png"
            })
          } else {
            var pages_all = getCurrentPages();
            if (pages_all.length > 3) {
              wx.redirectTo({
                url: '/lionfish_comshop/pages/buy/index?type=' + that.data.order.buy_type
              })
            } else {
              wx.navigateTo({
                url: '/lionfish_comshop/pages/buy/index?type=' + that.data.order.buy_type
              })
            }
          }
        }
      }
    })

  },

  // 选择规格
  selectSku: function(event) {
    var that = this;
    let str = event.currentTarget.dataset.type;
    let obj = str.split("_");
    let arr = that.data.sku;
    let temp = {
      name: obj[3],
      id: obj[2],
      index: obj[0],
      idx: obj[1]
    };
    arr.splice(obj[0], 1, temp);
    that.setData({
      sku: arr
    })
    var id = '';
    for (let i = 0; i < arr.length; i++) {
      if (i == arr.length - 1) {
        id = id + arr[i]['id'];
      } else {
        id = id + arr[i]['id'] + "_";
      }
    }
    var options = this.data.skuList;
    var cur_sku_arr = options.sku_mu_list[id];

    that.setData({
      cur_sku_arr: cur_sku_arr
    });

    console.log(id);
  },

  /**
   * 数量加减
   */
  setNum: function(event) {
    let types = event.currentTarget.dataset.type;
    var that = this;
    var num = 1;
    let sku_val = this.data.sku_val * 1;
    if (types == 'add') {
      num = sku_val + 1;
    } else if (types == 'decrease') {
      if (ku_val > 1) {
        num = sku_val - 1;
      }
    }

    let arr = that.data.sku;
    var options = this.data.skuList;

    if (arr.length > 0) {
      var id = '';
      for (let i = 0; i < arr.length; i++) {
        if (i == arr.length - 1) {
          id = id + arr[i]['id'];
        } else {
          id = id + arr[i]['id'] + "_";
        }
      }
    }

    if (options.length > 0) {
      let cur_sku_arr = options.sku_mu_list[id];
      if (num > cur_sku_arr['canBuyNum']) {
        num = num - 1;
      }
    } else {
      let cur_sku_arr = this.data.cur_sku_arr;
      if (num > cur_sku_arr['canBuyNum']) {
        num = num - 1;
      }
    }

    this.setData({
      sku_val: num
    })
  },

  /**
   * 关闭购物车选项卡
   */
  closeSku: function() {
    this.setData({
      visible: 0,
      stopClick: false
    });
  },

  changeCartNum: function(t) {
    let that = this;
    let e = t.detail;
    (0, status.cartNum)(that.setData({
      cartNum: e
    }));
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})