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

  /**
   * 页面的初始数据
   */
  data: {
    select: false,
    is_name: 'Auction',
    keyword: '',
    status: '1',
    page: 1,
    current_page: 1,
    last_page: 1,
    is_new: '',
    is_recommend: '',
    auctioneer_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.is_recommend){
      this.setData({
        is_recommend:options.is_recommend
      });
    }
    if(options.is_new){
      this.setData({
        is_new:options.is_new
      });
    }
    if(options.status){
      this.setData({
        status:options.status
      });
    }
    // this.getSearch();
  },
  goshop(e){
    console.log(e.currentTarget.dataset.id);
    let kid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageD/auction/auctioneer-shop/auctioneer_shop/auctioneer_shop?id='+kid,
    });
  },
  selectTap() {
    this.setData({
      select: !this.data.select
    });
    // console.log(this.data.select)
  },
  nametap(e) {
    this.setData({
      is_name: e.currentTarget.dataset.name,
      select: false,
      status: e.currentTarget.dataset.name == "Auction" ? '2' : '1',
      datas: [],
      page: 1,
      current_page: 1,
      last_page: 1
    });
    this.getSearch();
  },
  goSearch(){
    this.setData({
      page: 1,
      current_page: 1,
      last_page: 1
    });
    this.getSearch;
  },
  is_keyword(e) {
    let key = e.detail.value;
    this.setData({
      keyword: key,
      page: 1,
      current_page: 1,
      last_page: 1
    });
    this.getSearch();
  },
  toGoods(e){
    let id = e.currentTarget.dataset.item.goods_id;
    let member_id = e.currentTarget.dataset.item.member_id?e.currentTarget.dataset.item.member_id:e.currentTarget.dataset.item.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 () {
    if (this.data.current_page >= this.data.last_page) {
      wx.showToast({
        title: '没有更多',
        icon: 'none',
        duration: 1000
      });
    } else {
      let is_page = this.data.page + 1;
      this.setData({
        page: is_page
      });
      this.getSearchMore();
    }
  },

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

  },
  getSearch() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      keyword: that.data.keyword,
      status: this.data.status,
      auc_status: '',
      is_new: '',
      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) {
          if (res.data.getSearch.data.length == 0) {
            if (that.data.status == 1) {
              wx.showToast({
                title: '没有相关拍品',
                icon: 'none',
                duration: 1000
              });
            } else if (that.data.status == 2) {
              wx.showToast({
                title: '没有相关拍卖官',
                icon: 'none',
                duration: 1000
              });
            }
            that.setData({
              datas: []
            });
            return;
          }
          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);
      }
    });
  },
  getSearchMore() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      keyword: that.data.keyword,
      status: this.data.status,
      auc_status: '',
      is_new: '',
      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) {
          if (res.data.getSearch.data.length == 0) {
            if (that.data.status == 1) {
              wx.showToast({
                title: '没有相关拍品',
                icon: 'none',
                duration: 1000
              });
            } else if (that.data.status == 2) {
              wx.showToast({
                title: '没有相关拍卖官',
                icon: 'none',
                duration: 1000
              });
            }
            that.setData({
              datas: []
            });
            return;
          }
          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');
          }
          var listdata = res.data.getSearch.data;
          that.setData({
            datas: that.data.datas.concat(listdata),
            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);
      }
    });
  }
});