// packageF/debt/creditor/creditorindex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    RenewShow: false,
    listData: [],
    renewUserId: '',
    // 债权人拥有商品次数
    owner_goods_count: '',
    // 债权人拥有商品id
    owner_goods_id: '',
    goodsTotal: 1,

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '债权人',
    })
    this.setData({
      plugin_setting:wx.getStorageSync('yz_basic_info').plugin_setting.debt_shop || {},
      RenewShow:false
    })
  },
  tapclose() {
    this.setData({
      RenewShow: false
    })
  },
  toShop(e) {
    let ownerId = e.currentTarget.dataset.id;
    // 1债权人 2债务人 3VIP
    wx.navigateTo({
      url: '/packageF/debt/debtor/debtorShop/debtorShop?kid=' + ownerId + '&member_type=1',
    })
  },
  toApply() {
    wx.navigateTo({
      url: '/packageF/debt/debtapply/debtapply?type=1',
    })
  },
  renewTap(item) {
    let items = item.currentTarget.dataset.item;
    let renewUserId = items.has_one_owner.id
    this.setData({
      RenewShow: true,
      renewUserId
    })
    this.getApplyData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getdebtListInit();
    this.getData();
    this.getdebtList();
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  getdebtListInit() {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      listData:[]
    })
  },
  stepperTap(e) {
    let stepp = e.detail;
    this.setData({
      goodsTotal: stepp
    })
  },
  renewPost() {
    let that = this;
    if (this.data.goodsTotal > this.data.owner_goods_count) {
      wx.showToast({
        icon: 'none',
        title: '拥有商品数量不足',
        duration: 1500
      });
      return
    };
    let json = {
      id: this.data.renewUserId,
      count: this.data.goodsTotal
    }
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.rechargeDebtMemberTime');
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            RenewShow: false
          })
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1000,
            success: function () {
              that.getdebtListInit();
              that.getData();
              that.getdebtList();
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getApplyData() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.applyData');
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            owner_goods_count: res.data.owner_goods_count,
            owner_goods_id: res.data.owner_goods_id,
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  goGoods(){
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + this.data.owner_goods_id,
    })
  },
  getData() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debt-member.getDebtData');
    app._getNetWork({
      url: urlStr,
      data: {
        member_type: 1
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            userInfo: res.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getdebtList() {
    let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.owner.debtorList');
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res)
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            listData: res.data.data
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss('plugin.debt-shop.frontend.owner.debtorList');
      app._getNetWork({
        url: urlStr,
        data: {
          page: this.data.page
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listData: res.data.data
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
})