// packageD/auction/auction/auction_index/auction_index.js
var app = getApp();
import utils from '../../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata();
    this.getNew();
  },
  tolink(e){
    let urls = e.currentTarget.dataset.item.small_url;
    wx.navigateTo({
      url: urls,
    });
  },
  goMy(){
    wx.navigateTo({
      url: '/packageD/auction/auction-mine/my_index/my_index',
    });
  },
  gosearch() {
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_search/auction_search',
    });
  },
  toMore(e) {
    let is_id = e.currentTarget.dataset.is_recommend;
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_more/auction_more?is_recommend=' + is_id,
    });
  },
  toMoreNew(e) {
    let is_id = e.currentTarget.dataset.is_new;
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_more/auction_more?is_new=' + is_id,
    });
  },
  toDetail(e){
    let id = e.currentTarget.dataset.id.goods_id;
    let member_id = e.currentTarget.dataset.id.member_id?e.currentTarget.dataset.id.member_id:e.currentTarget.dataset.id.auctioneer_id;
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_good/auction_good?id='+id+'&member_id='+member_id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

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

  },
  getNew() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      keyword: '',
      status: 1,
      auc_status: '',
      is_new: 1,
      is_recommend: '',
      auctioneer_id: '',
      art: 'getSearch',
      page: this.data.page,
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          for(let i = 0;i<res.data.getSearch.data.length;i++){
            res.data.getSearch.data[i].end_shooting = utils.formatTimeTwo(res.data.getSearch.data[i].end_shooting,'Y-M-D h:m:s');
          }
          that.setData({
            datas: res.data.getSearch.data,
            current_page: res.data.getSearch.current_page,
            last_page: res.data.getSearch.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getdata() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      art: 'index'
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          for(let i = 0;i<res.data.index.recommend.length;i++){
            res.data.index.recommend[i].end_shooting = utils.formatTimeTwo(res.data.index.recommend[i].end_shooting,'Y-M-D h:m:s');
          }
          that.setData({
            rotation: res.data.index.rotation,
            recommend: res.data.index.recommend
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getNewMore() {
    var that = this;
    if (this.data.current_page >= this.data.last_page) {
      wx.showToast({
        title: '没有更多',
        icon: 'none',
        duration: 1000
      });
      return;
    } else {
      let loadpage = that.data.page + 1;
      that.setData({
        page: loadpage
      });
      let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
      let json = {
        keyword: '',
        status: 1,
        auc_status: '',
        is_new: 1,
        is_recommend: '',
        auctioneer_id: '',
        art: 'getSearch',
        page: this.data.page,
      };
      app._postNetWork({
        url: urlStr,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            console.log(res);
            that.setData({
              datas: that.data.datas.concat(res.data.getSearch.data),
              current_page: res.data.getSearch.current_page,
              last_page: res.data.getSearch.last_page
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none",
              duration: 1000
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
});