// packageB/member/love/donate/DonateIndex.js
const app = getApp();
// import utilt from '../../../../utils/util.js'
import utils from '../../../../utils/util.js';
Page({

  /**
     * 页面的初始数据
     */
  data: {
    mobile: '',
    show: true,
    member: [],
    memberShow: false,
    nickname: '',
    imageUse: '',
    tapOne: false,
    shop_id: '',
    bindStatus: true,
    bindErrorTxt:''
  },
  tapBtn() {
    this.setData({
      show: !this.data.show
    });
  },
  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    this.getData();
    this.checkBind();
    wx.setNavigationBarTitle({
      title: '债权人绑定债务人',
    });
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  init() {
    this.setData({
      tapOne: false
    });
  },
  gohome(){
    wx.navigateTo({
      url: '/packageG/index/index',
    });
  },
  bindinp:utils.debounce(function(e){
    let str = e[0].detail;
    this.setData({
      mobile: str
    });
    this.getUser();
  }),
  getUser() {
    let urlStr = app.getNetAddresss("plugin.micro-debt.frontend.controllers.debtor.query");
    app._getNetWork({
      url: urlStr,
      data: {
        mobile: this.data.mobile
      },
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            memberShow: true,
            member: res.data.data.member
          });
        } else {
          this.setData({
            memberShow: false,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  bindYzUser() {
    if (this.data.tapOne) {
      return;
    }
    if (app._isTextEmpty(this.data.mobile)) {
      wx.showToast({
        title: '请输入债务人手机号',
        duration: 1000,
        icon: 'none'
      });
      return;
    }
    this.setData({
      tapOne: true
    });
    let urlStr = app.getNetAddresss("plugin.micro-debt.frontend.controllers.creditor.check");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          let bind = res.data.data.bind;
          // this.setData({
          //     shop_id: res.data.data.micro_id
          // })
          // false 需要绑定债权人,ture 跳转债权人微店页面
          if (bind) {
            wx.navigateTo({
              url: '/packageC/microshop/micro_shop_share_home/micro_shop_share_home?shop_id=' + res.data.data.micro_id,
            });
          } else {
            this.bindUser();
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  bindUser() {
    let urlStr = app.getNetAddresss("plugin.micro-debt.frontend.controllers.creditor.bind");
    app._getNetWork({
      url: urlStr,
      data: {
        mobile: this.data.mobile
      },
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            show: false,
            bindStatus: true
          });
          setTimeout(() => {
            // 动画走了再跳转
            wx.navigateTo({
              url: '/packageC/microshop/micro_shop_share_home/micro_shop_share_home?shop_id=' + res.data.data.micro_id,
            });
          }, 1500);
        } else {
          this.setData({
            bindStatus: false,
            show: false,
            bindErrorTxt:res.data.msg
          });
          // wx.showToast({
          //   title: res.data.msg,
          //   icon: 'none'
          // });
          this.init();
          return;
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.micro-debt.frontend.controllers.member.info");
    app._getNetWork({
      url: urlStr,
      data: {
        mobile: this.data.mobile
      },
      success: (res) => {
        if (res.data.result === 1) {
          this.setData({
            nickname: res.data.data.member.nickname,
            imageUse: res.data.data.member.avatar_image
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  checkBind() {
    let urlStr = app.getNetAddresss("plugin.micro-debt.frontend.controllers.creditor.check");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result === 1) {
          let bind = res.data.data.bind;
          // false 需要绑定债权人,ture 跳转债权人微店页面
          if (bind) {
            wx.navigateTo({
              url: '/packageC/microshop/micro_shop_share_home/micro_shop_share_home?shop_id=' + res.data.data.micro_id,
            });
          }
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
});