// pages/member/myOrder/truckList/truckList.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    order_id: '',
    truck_list: [],
    value_list: [],
    //加载更多
    isLoading: false,
    isLoadMore: true,
    page: 1,
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.order_id) {
      this.setData({
        order_id: options.order_id
      });
    }
  },

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

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this._initData();
    this._getTuck();
    this._getOrder_list();
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  _initData() {
    this.setData({
      truck_list: [],
      value_list: [],
      //加载更多
      isLoading: false,
      isLoadMore: true,
      page: 1,
    });
  },
  _getTuck() {
    if (!this.data.isLoadMore || this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.virtual-card.frontend.order.virtualCardList");
    urlStr += '&order_id=' + this.data.order_id;
    urlStr += '&page=' + this.data.page;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoading: false
          });
          // 解构出总页数和所有数据
          const {
            data
          } = res.data;
          let resData = [];
          that.setData({
            truck_list: [...that.data.truck_list, ...data]
          });
          that.data.truck_list.forEach(value => {
            resData.push(value);
          });
          that.setData({
            value_list: resData
          });
        }
        if (res.data.total == that.data.page) {
          that.setData({
            isLoadMore: false
          });
        }
        that.setData({
          page: that.data.page + 1
        });
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  _getOrder_list() {
    let that = this;
    let urlStr = app.getNetAddresss('order.detail');
    urlStr += '&order_id=' + this.data.order_id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            order_list: res.data
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  copybtn(e) {
    let truckval = e.currentTarget.dataset.truckval;
    wx.setClipboardData({
      data: truckval,
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              icon: 'none',
              title: '复制成功',
              duration: 1500
            });
          },
          fail(res) {
            wx.showToast({
              icon: 'none',
              title: '复制失败',
              duration: 1500
            });
          }
        });
      }
    });
  }
});
