var util = require('../../utils/util.js');
var api = require('../../config/api.js');
const app = getApp();

Page({
  data: {
    navList: [],
    goodsList: [],
    id: 0,
    navIndex:0,
    attribute:'',
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    limit: 10,
    totalPages: 1,
    addressInfo:{}
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    console.log(options);
    var that = this;
    if (options.id) {
      that.setData({
        id: parseInt(options.id)
      });
    }
    if(options.attribute){
      that.setData({
        attribute: parseInt(options.attribute)
      });
    }

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });

    
    var addressInfo=wx.getStorageSync('addressInfo');
    if(addressInfo){
      this.setData({
        addressInfo:addressInfo
      })
    }

    this.getCategoryInfo();

  },
  getCategoryInfo: function () {
    let that = this;
    util.request(api.GoodsCategory, {
        id: this.data.attribute?'-1':this.data.id,
        attribute:this.data.attribute,
        shopId:this.data.addressInfo.shopId
      })
      .then( (res)=>{

        if (res.errno == 0) {
          that.setData({
            navList: res.data.brotherCategory,
            currentCategory: res.data.currentCategory
          });
          // 当id是L1分类id时，这里需要重新设置成L1分类的一个子分类的id
          // 周期服务包特殊处理
          if(this.data.attribute=='3'||this.data.attribute=='2'){
                 wx.setNavigationBarTitle({
            title: res.data.currentCategory.name
          })
        
              that.setData({
                id: res.data.currentCategory.id
              });
            
          }else{
            wx.setNavigationBarTitle({
              title: res.data.parentCategory.name
            })
              if (res.data.parentCategory.id == that.data.id) {
                that.setData({
                  id: res.data.currentCategory.id
                });
              }
          }
        

          //nav位置
          let currentIndex = 0;
          let navListCount = that.data.navList.length;
          for (let i = 0; i < navListCount; i++) {
            currentIndex += 1;
            if (that.data.navList[i].id == that.data.id) {
              break;
            }
          }
          if (currentIndex > navListCount / 2 && navListCount > 5) {
            that.setData({
              scrollLeft: currentIndex * 60
            });
          }
          that.getGoodsList();

        } else {
          //显示错误信息
        }

      });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
   
  },
  onHide: function () {
    // 页面隐藏
  },
  getGoodsList: function () {
    var that = this; 
    util.request(api.GoodsList, {
        categoryId: this.data.attribute=='3'?'-1':that.data.id,
        attribute:this.data.attribute=='3'?'3':'',
        page: that.data.page,
        limit: that.data.limit,
        city:that.data.addressInfo.city,
        shopId:that.data.addressInfo.shopId
      })
      .then(function (res) {
        console.info(res);
        console.info(that.data.page);
        if (that.data.page == 1) {
          that.setData({
            goodsList: res.data.list,
            totalPages: res.data.pages
          });
        } else
          that.setData({
            goodsList: that.data.goodsList.concat(res.data.list),
            totalPages: res.data.pages
          });

          console.info(that.data.goodsList);
      });
  },
  onUnload: function () {
    // 页面关闭
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getGoodsList();
    } else {
      wx.showToast({
        title: '没有更多商品了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  switchCate: function (event) { 
    if (this.data.id == event.currentTarget.dataset.id) {
      return false;
    }
    var that = this;
    var clientX = event.detail.x;
    var currentTarget = event.currentTarget;
    if (clientX < 60) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft - 60
      });
    } else if (clientX > 330) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft
      });
    }
    this.setData({
      id: event.currentTarget.dataset.id,
      navIndex: event.currentTarget.dataset.index,
      page:1
    });
    this.getGoodsList();
    // this.getCategoryInfo();
  }
})