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

  /**
   * 页面的初始数据
   */
  data: {
    keyWord: "",
    active: 0,
    // 债务人2，债权人1，vip3
    member_type: '',
    listData: [],
    // 债权人拥有商品次数
    owner_goods_count: '',
    // vip拥有商品次数
    vip_goods_count: '',
    // 充值数量
    goodsTotal: 1,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '会员管理',
    })
    if (options.type) {
      this.setData({
        member_type: options.type
      })
      this.init();
      this.getData();
    }
    this.getUser();
    this.setData({
      plugin_setting:wx.getStorageSync('yz_basic_info').plugin_setting.debt_shop || {}
    })
  },
  stepperTap(e) {
    let stepp = e.detail;
    this.setData({
      goodsTotal: stepp
    })
  },
  blackGo(){
    wx.navigateBack({
      delta: 0,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

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

  },

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

  },

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

  },

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

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

  },
  fieldChange(e) {
    console.log(this.data.keyWord)
    this.init();
    this.getData();
  },
  changeTap(e) {
    let ind = e.detail.index;
    this.setData({
      active: ind,
      keyWord: ''
    })
    this.init();
    this.getData();
  },
  renewTap(item) {
    let items = item.currentTarget.dataset.item;
    let renewUserId = items.has_one_debtor.id
    this.setData({
      RenewShow: true,
      renewUserId
    })
  },
  init() {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      listData: []
    })
  },
  getData() {
    let urlStr = ''
    if (this.data.active == 0) {
      urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debtor.ownerList');
    } else {
      urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debtor.vipList');
    }
    console.log(this.data.keyWord, 'aaaaaa')
    app._postNetWork({
      url: urlStr,
      data: {
        member_kwd: this.data.keyWord
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          res.data.data.forEach(element => {
            element.common_credit = Number(element.common_credit);
            element.frozen_credit = Number(element.frozen_credit);
          });
          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() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      if (this.data.active == 0) {
        urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debtor.ownerList');
      } else {
        urlStr = app.getNetAddresss('plugin.debt-shop.frontend.debtor.vipList');
      }
      app._getNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          member_kwd: this.data.keyWord
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            res.data.data.forEach(element => {
              element.common_credit = Number(element.common_credit);
              element.frozen_credit = Number(element.frozen_credit);
            });
            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);
        }
      });
    }
  },
  getUser() {
    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.debtor_goods_count,
            owner_goods_id: res.data.debtor_goods_id,
            vip_goods_id: res.data.vip_goods_id,
            vip_goods_count: res.data.vip_goods_count
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 续约按钮接口
  renewPost() {
    if (this.data.goodsTotal > (this.data.active == 0 ? this.data.owner_goods_count : this.data.vip_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 () {
              this.init();
              this.getData();
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  toDetail() {
    let id = ''
    if (this.data.active == 0) {
      id = this.data.owner_goods_id;
    } else {
      id = this.data.vip_goods_id
    }
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id,
    })
  }
})