//index.js
//获取应用实例
const app = getApp();
var utils = require('../../utils/util.js');
Page({
  data: {
    background: [],
    indicatorDots: true,
    indicatorColor: 'rgba(255,255,255,0.5)',
    indicatorActiveColor: 'rgba(255,255,255,1)',
    autoplay: true,
    circular: true,
    interval: 3000,
    duration: 1000,
    isshare: 0,
    // 主导航配置
    hoverStayTime: "100",
    mainNav: [{
      navUrl: "../list/list?kid=1067257073894559718&title=珠宝玉石",
        imgUrl: "images/1.png",
        text: "珠宝玉石",
      },
      {
        navUrl: "../list/list?kid=1067257074959912960&title=文玩收藏",
        imgUrl: "images/2.png",
        text: "文玩收藏",
      },
      {
        navUrl: "../list/list?kid=1067257075521949696&title=紫砂陶瓷",
        imgUrl: "images/3.png",
        text: "紫砂陶瓷",
      },
      {
        navUrl: "../list/list?kid=1067257077182894080&title=艺术品",
        imgUrl: "images/4.png",
        text: "艺术品",
      },
      {
        navUrl: "../list/list?kid=1067257078290190338&title=二手车",
        imgUrl: "images/6.png",
        text: "二手车",
      },
      {
        navUrl: "../list/list?kid=1067257078290190340&title=废旧设备",
        imgUrl: "images/7.png",
        text: "废旧设备",
      },
      {
        navUrl: "../list/list?kid=1067257078290190339&title=废旧物资",
        imgUrl: "images/5.png",
        text: "废旧物资",
      },
      {
        navUrl: "../more/more",
        imgUrl: "images/8.png",
        text: "更多分类",
      },
      // {
      //   navUrl: "../activity/luckywheel/luckywheel",
      //   imgUrl: "images/nav-luckdraw.png",
      //   text: "幸运转盘",
      // }
    ],
    // 导航公告
    navNotice: [{
        userName: "思凡",
        amountOfMoney: "45000元",
        goods: '积家MASTER COMPRESSOR系列Q1758470男表 自动机械手表'
      },
      {
        userName: "云得物",
        amountOfMoney: "1400元",
        goods: '碧翠玑珠宝 冰糯种翠绿翡翠手镯 内径:56.8mm DKS52-28-17 53.87克'
      },
      {
        userName: "云得物",
        amountOfMoney: "1800元",
        goods: '和田玉黄玉把件 福在眼前 100.06克 玉上铭品'
      }
    ],
    tabNav: [{
        tabName: "正在热拍",
        event: "getGoodsData",
        select: 0
      },
      {
        tabName: "即将开始",
        event: "getGoodsData",
        select: 1
      },
      {
        tabName: "精彩回顾",
        event: "getGoodsData",
        select: 2
      }
    ],
    // 商品
    goodsOpen: [],
    goodsClose: [],
    goodsNotOpen: [],
    goods: [],
    tabIndex: 0,
    templateName: "goodsOpen",
    prodUrl:app.globalData.prodUrl,
    hasMore: true,//下拉刷新开关
    is_first: false,  //是否绑定手机号
    is_share:0
  },  
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  //banner页面跳转
  BannerNavigator:function(e){
      var index = e.currentTarget.dataset.ind;
      console.log(index, this.data.background.length - 1)
    if (index === 0) {
        wx.navigateTo({
            url: '../payDeposit/pages/networkprotocol/networkprotocol',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    }else {
        wx.navigateTo({
            url: '../ContentDetails/ContentDetails',
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    }
    
  },
  onLoad: function(options) {

  },
  onShow:function(){
    var _this = this;
    var ydw_token = wx.getStorageSync('key').ydw_token;
    if (!app.globalData.requestStatus) {
      var tokenInter = setInterval(function () {
        ydw_token = wx.getStorageSync('key').ydw_token;
        if (ydw_token) {
          clearInterval(tokenInter);
          setTimeout(function () {
            _this.loadListItem(ydw_token);
          }, 1000)
        }
      }, 1000);
    } else {
      this.loadListItem(ydw_token);
    }
    wx.createSelectorQuery().select('.classify-position').boundingClientRect(function (rect) {
      _this.setData({
        pageScrollTop: rect.top
      })
    }).exec();
  },
  //
  //获取首页数据
  loadListItem: function(ydw_token) {
    var _this = this;
    if (app.globalData.is_first) {
      _this.setData({
        is_first:app.globalData.is_first
      });
      app.globalData.is_first = false;
    };
    if (app.globalData.red_pack_msg > 0) {
      _this.setData({
        is_share: app.globalData.red_pack_msg
      });
      app.globalData.red_pack_msg = 0;
    }
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/list_slideshow',
      data: '',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(data) {
        _this.setData({
          background: data.data.data
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    });
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/list_item',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      dataType: 'json',
      success: function (data) {
        if(data.data.code) {
        } else {
          var reg = /^2\d{2}$/;
          if (reg.test(data.statusCode)) {
            _this.setData({
              goodsOpen: data.data.open
            })
          } else {
            wx.showToast({
              title: '数据获取失败',
              icon: 'none'
            })
          }
        }

      }
    }); //list_item 请求商品列表
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let _this = this
    if (_this.data.hasMore) {
      _this.setData({
        hasMore: false
      })
      wx.startPullDownRefresh({
        success: function() {
          wx.request({
            url: app.globalData.prodUrl + 'web-pm/wx/pm/list_item',
            data: '',
            header: {
              'ydw-token': wx.getStorageSync('key').ydw_token
            },
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(data) {
              var reg = /^2\d{2}$/;
              if (reg.test(data.statusCode)) {
                if (_this.data.templateName === 'goodsOpen') {
                  _this.setData({
                    goodsOpen: data.data.open
                  });
                } else if (_this.data.templateName === 'goodsNotOpen') {
                  _this.setData({
                    goodsNotOpen: data.data.not_open
                  });
                } else if (_this.data.templateName === 'goodsClose') {
                  _this.setData({
                    goodsClose: data.data.close
                  });
                }

                wx.showToast({
                  title: '数据加载成功',
                  icon: 'none'
                });
                wx.stopPullDownRefresh({
                  success: function() {}
                });
              } else {
                wx.showToast({
                  title: '数据获取失败',
                  icon: 'none'
                });
              };
              _this.setData({
                hasMore: true
              });
            },
            fail: function(res) {},
            complete: function(res) {},
          });

        }
      });
    }
  },

  // 顶部浮动效果
  // onPageScroll: function(e) {
  //   var _this = this;
  //   var scrollTop = _this.data.pageScrollTop;
  //   if (e.scrollTop > scrollTop) {
  //     this.setData({
  //       classifyPosition: true
  //     })
  //   }
  //   if (e.scrollTop < scrollTop) {
  //     this.setData({
  //       classifyPosition: false
  //     })
  //   }
  // },
  navigateTo: function(e) {
    wx.navigateTo({ 
      url: "../details/details?isshare=" + isshare
    })
  },
  //点击tab加载数据
  onLoadTemplate: function(e) {
    // var newData = [];
    var _this = this;
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index === 0) {
      if (_this.data.goodsOpen.length === 0) {
        _this.getGoodsOpen(_this)
      }
      _this.setData({
        templateName: 'goodsOpen'
      })
    } else if (e.currentTarget.dataset.index === 1) {
      if (this.data.goodsNotOpen.length === 0) {
        _this.getGoodsNotOpen(_this);
      }
      _this.setData({
        templateName: 'goodsNotOpen'
      })
    } else if (e.currentTarget.dataset.index === 2) {
      if (this.data.goodsClose.length === 0) {
        _this.getGoodsClose(_this);
      }
      _this.setData({
        templateName: 'goodsClose'
      })
    }
  },
  //获取已开拍的商品
  getGoodsOpen: function(_this) {
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/list_item',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      dataType: 'json',
      success: function (data) {
        var reg = /^2\d{2}$/;
        if (reg.test(data.statusCode)) {
          _this.setData({
            goodsOpen: data.data.open
          })
        } else {
          wx.showToast({
            title: '数据获取失败',
            icon: 'none'
          })
        }
      }
    })
  },
  //获取已结束的商品
  getGoodsClose: function(_this) {
    //console.log(wx.getStorageSync('key').ydw_token)
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/list_item',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      dataType: 'json',
      success: function (data) {
        //console.log(app.globalData.prodUrl,data)
        var reg = /^2\d{2}$/;      
        if (reg.test(data.statusCode)) {
          _this.setData({
            goodsClose: data.data.close
          })
        } else {
          wx.showToast({
            title: '数据获取失败',
            icon: 'none'
          })
        }
      }
    })
  },
  //获取未开拍数据
  getGoodsNotOpen: function(_this) {
    wx.request({
      url: app.globalData.prodUrl + 'web-pm/wx/pm/list_item',
      header: {
        'ydw-token': wx.getStorageSync('key').ydw_token
      },
      dataType: 'json',
      success: function (data) {
        var reg = /^2\d{2}$/;
        if (reg.test(data.statusCode)) {
          _this.setData({
            goodsNotOpen: data.data.not_open
          })
        } else {
          wx.showToast({
            title: '数据获取失败',
            icon: 'none'
          })
        }
      }
    })
  },
// 点击关闭红包
  closeRedPacket: function (e) {
    this.setData({
      is_first: false,
      is_share: 0
    })
  },
  //领取红包
  getRedPacket: function(e) {
    //console.log(e);
    this.setData({
      is_first: false,
      is_share: 0
    } , ()=>{
      wx.showToast({
        title: '领取成功，已存入我的红包',
        icon: 'none',
        duration: 2000
      })
    })
  },
  //跳转我的红包
  navRedPacket: function (e) {
    wx.navigateTo({
      url: '/pages/mine/wxml/myredpacket/myredpacket'
    })
  }
})