// packageD/auction/auctioneer-shop/auctioneer_shop/auctioneer_shop.js
var app = getApp();
import utils from '../../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    active: 0,
    page: 1,
    // 商品列表
    current_page: '',
    last_page: '',
    datas: [],
    now: '',
    // 密码弹窗
    showone:false,
    password:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    this.setData({
      now: String(Date.parse(new Date())).substring(0, 10)
    });
    this.getStatus();
  },
  toSearch(){
    console.log(this.data.id);
    wx.navigateTo({
      url: '/packageD/auction/auctioneer-shop/auctioneer_shop_search/auctioneer_shop_search?id='+this.data.id,
    });
  },
  is_password(e) {
    this.setData({
      password: e.detail.value
    });
  },
  isCoverLayer_passwork() {
    wx.showModal({
      title: '提示',
      content: '不输入店铺密码无法查看店铺详情',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定');
          wx.navigateBack({
            delta:1
          });
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  // 店铺密码确认
  confirm(){
    if(app._isTextEmpty(this.data.password)){
      wx.showToast({
        duration:1000,
        title: '请输入密码',
        icon:'none'
      });
      return;
    }
    let json = {
      art:'verifyPassword',
      auction_id:this.data.id,
      passwd:this.data.password
    };
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            showone:false
          });
          that.getStatus();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 收藏
  toCollect() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      art: 'collectionShop',
      auctioneer_id: that.data.id
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (that.data.info.is_collection == 0) {
            wx.showToast({
              title: res.data.collectionShop,
              icon: 'none',
              duration: 1000,
            });
          } else {
            wx.showToast({
              title: '取消收藏成功',
              icon: 'none',
              duration: 1000,
            });
          }
          that.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
  itemClick(e) {
    console.log(e.detail.index);
    this.setData({
      active: e.detail.index
    });
    this.getSearch();
  },
  toMy() {
    wx.navigateTo({
      url: '/packageD/auction/auction-mine/my_index/my_index',
    });
  },
  goxiangqin(e) {
    console.log(e);
    let kid = e.currentTarget.dataset.id.goods_id;
    let member_id = this.data.id;
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_good/auction_good?id=' + kid + "&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) {
      if(this.data.last_page == 1){
        return;
      }
      wx.showToast({
        title: '暂无更多',
        icon: 'none',
        duration: 1000
      });
    } else {
      let ispage = this.data.page + 1;
      this.setData({
        page:ispage
      });
      this.getNextSearch();
    }
  },

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

  },
  getStatus() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      member_id: this.data.id
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var response = resdata.data;
        if (response.result == 1) {
          that.setData({
            status: response.data.getVerify.status,
            is_verify: response.data.getVerify.is_verify
          });
          if (that.data.is_verify == 0 && that.data.status == 1) {
            that.setData({
              showone: true
            });
          } else {
            // this.is_verify=1;
            that.getSearch();
            that.getData();
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
            success() {
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                });
              }, 1000);
            }
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      member_id: this.data.id,
      art: 'shop'
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            info: res.data.shop
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
            success:function(){
              setTimeout(() => {
                wx.navigateBack({
                  delta:1
                });
              }, 500);
            }
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
  getSearch() {
    let that = this;
    var url;
    // let urlStr = app.getNetAddresss('plugin.auction.api.auction.check');
    let json = {
      auctioneer_id: this.data.id,
      page: this.data.page
    };
    if (that.data.active == 0) {
      url = app.getNetAddresss('plugin.auction.api.auction.all');
    } else if (that.data.active == 1) {
      url = app.getNetAddresss("plugin.auction.api.auction.biding");
    } else if (that.data.active == 2) {
      url = app.getNetAddresss("plugin.auction.api.auction.bid");
    } else if (that.data.active == 3) {
      url = app.getNetAddresss("plugin.auction.api.auction.finish");
    }
    app._getNetWork({
      url: url,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (res.data.data.length > 0) {
            for (let i = 0; i < res.data.data.length; i++) {
              res.data.data[i].endTime = utils.formatTimeTwo(res.data.data[i].end_shooting, 'Y-M-D h:m:s');
              res.data.data[i].startTime = utils.formatTimeTwo(res.data.data[i].start_shooting, 'Y-M-D h:m:s');
            }
          }

          that.setData({
            current_page: res.data.current_page,
            last_page: res.data.last_page,
            datas: res.data.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
  getNextSearch() {
    let that = this;
    var url;
    // let urlStr = app.getNetAddresss('plugin.auction.api.auction.check');
    let json = {
      auctioneer_id: this.data.id,
      page: this.data.page
    };
    if (that.data.active == 0) {
      url = app.getNetAddresss('plugin.auction.api.auction.all');
    } else if (that.data.active == 1) {
      url = app.getNetAddresss("plugin.auction.api.auction.biding");
    } else if (that.data.active == 2) {
      url = app.getNetAddresss("plugin.auction.api.auction.bid");
    } else if (that.data.active == 3) {
      url = app.getNetAddresss("plugin.auction.api.auction.finish");
    }
    app._getNetWork({
      url: url,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          if (res.data.data.length > 0) {
            for (let i = 0; i < res.data.data.length; i++) {
              res.data.data[i].endTime = utils.formatTimeTwo(res.data.data[i].end_shooting, 'Y-M-D h:m:s');
              res.data.data[i].startTime = utils.formatTimeTwo(res.data.data[i].start_shooting, 'Y-M-D h:m:s');
            }
          }
          that.setData({
            current_page: res.data.current_page,
            last_page: res.data.last_page,
            datas:that.data.datas.concat(res.data.data)
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
        wx.showToast({
          title: res.msg,
          icon: "none",
          duration: 1000
        });
      }
    });
  },
});