// packageE/others/application_market/application_market.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: "plugin.product-market.api.order.index",
    PopupShow: false,
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0,

    // domianShow:false,
    serviceDetailsShow:false,

    erviceNetword: false,
    current_domain: "",
    remaining_time: 0,

    service_allLoad: false,
    service_isLoadMore:true,
    service_page: 1,
    service_total_page: 0,
    service_listData: [],
    service_show:0,
    serviceContentScroll:null


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.marketSub == 1) {
      this.setData({
        url: "plugin.market-sub.api.order.index",
      });
    }
    this.getdata();
  },
  tapPopupShow(e) {
    console.log(e);
    let that = this;
    let item = e.currentTarget.dataset.item;
    let good = e.currentTarget.dataset.good;
    console.log(good);
    for (let i = 0; i < item.has_market_product.widget.length; i++) {
      console.log(good.goods_id, item.has_market_product.widget[i].goods_id);
      if (good.goods_id === item.has_market_product.widget[i].goods_id) {
        that.setData({
          showList: item.has_market_product.widget[i].widget.plugin,
        });
      }
    }
    this.setData({
      PopupShow: true,
    });
  },
  closs_tap() {
    this.setData({
      PopupShow: false,
    });
  },

  setServiceDetailsShow(){
    this.setData({
      serviceDetailsShow:!this.data.serviceDetailsShow
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 加载更多
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getdata() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss(this.data.url);
    app._getNetWork({
      url: urlStr,
      data: {
        page: that.data.page,
      },
      success: function (resdata) {
        wx.hideLoading();
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            domain: res.data.domain,
            order: res.data.order.data,
            page: res.data.order.current_page,
            total_page: res.data.order.last_page,
            current_domain:res.data.current_domain,
            remaining_time:res.data.remaining_time,
            service_show:res.data.service_show
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        wx.hideLoading({
          complete: (res) => {},
        });
      },
    });
  },
  _getMoreData() {
    let that = this;
    let urlStr = app.getNetAddresss(this.data.url);
    if (this.data.page >= this.data.total_page) {
      that.setData({
        isLoadMore: false,
      });
    } else {
      that.setData({
        page: this.data.page + 1,
      });
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              order: that.data.order.concat(res.data.order.data),
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          wx.hideLoading();
        },
      });
    }
  },

  getServiceDetails() {
    if (this.data.serviceNetword) {
      this.setData({serviceDetailsShow:true});
      return;
    }
    this.data.serviceNetword = true;
    let urlStr = app.getNetAddresss("plugin.product-market.api.order.index.check-record");
    if (this.options.marketSub == 1) {
      urlStr = app.getNetAddresss("plugin.market-sub.api.order.index.check-record");
    }
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.service_page,
      },
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            service_listData: res.data.data,
            service_total_page: res.data.last_page,
            serviceDetailsShow:true
          });
        } else {
          app.tips(res.msg);
        }
      }
    });
  },

  getMoreServiceDetails() {
    if(!this.data.service_isLoadMore) return;
    if (!this.data.service_allLoad && this.data.service_page >= this.data.service_total_page) {
      this.setData({service_allLoad:true,service_isLoadMore:false});
      return;
    } 
    this.data.service_isLoadMore = false;
    let urlStr = app.getNetAddresss("plugin.market-sub.api.order.index.check-record");
    this.data.service_page++;
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.service_page,
      },
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          let nextData = response.data.data;
          this.setData({
            service_isLoadMore: true,
            service_listData: this.data.service_listData.concat(nextData),
          });
        } else {
          this.data.service_isLoadMore = false;
          app.tips(res.msg);
        }
      }
    });
  },



});
