const { default: DHttp } = require("../DHttp");

// packageH/credit_value/give/give.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hiddenInputPassPopup: true,
    memberId: null,
    memberInfo: null,
    stock: "0.00",
    giftValue: 0,
    payPassword: {
      has: false,
      turnedOn: false,
      inputValue: "",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    DHttp.request(
      "plugin.credit-inventory.frontend.controllers.give.center"
    ).then(({ stock, has_password, need_password }) => {
      this.setData({
        stock,
        ["payPassword.turnedOn"]: need_password,
        ["payPassword.has"]: has_password,
      });
      this.checkPayPassword();
    });
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  displayInputPassPopup() {
    if (this.checkPayPassword() === false) {
      return;
    }
    if (this.data.payPassword.turnedOn===false) {
      this.confirmGiveGift();
      return;
    }
    if (!this.data.memberId || !this.data.memberInfo) {
      wx.showToast({
        title: "需要先指定会员才能赠送",
        icon: "none",
      });
      return;
    }
    if (this.data.giftValue <= 0) {
      wx.showToast({
        title: "赠送的信用值不能小于0",
        icon: "none",
      });
      return;
    }
    this.setData({
      hiddenInputPassPopup: !this.data.hiddenInputPassPopup,
    });
  },
  inputMemberId({ detail: { value } }) {
    this.setData({
      memberId: value,
      memberInfo: null,
    });
  },
  inputGiftValue({ detail: { value } }) {
    this.setData({
      giftValue: value,
    });
  },
  inputPayPassword({ detail: { value } }) {
    this.setData({
      ["payPassword.inputValue"]: value,
    });
  },
  getMemberInfo() {
    if (!this.data.memberId) {
      wx.showToast({
        title: "请输入会员ID",
        icon: "none",
      });
      return;
    }
    wx.showLoading({
      title: "查询中",
      mask: true,
    });
    DHttp.request("plugin.credit-inventory.frontend.controllers.give.detail", {
      member_id: this.data.memberId,
    })
      .then((res) => {
        wx.hideLoading();
        this.setData({
          memberInfo: res,
        });
      })
      .catch(({ msg }) => {
        wx.hideLoading();
        this.setData({
          memberInfo: null,
        });
        wx.showToast({
          title: msg,
          icon: "none",
        });
      });
  },
  confirmGiveGift() {
    if (this.checkPayPassword() === false) {
      return;
    }
    if (!this.data.memberId || !this.data.memberInfo) {
      wx.showToast({
        title: "需要先指定会员才能赠送",
        icon: "none",
      });
      return;
    }
    if (this.data.giftValue <= 0) {
      wx.showToast({
        title: "赠送的信用值不能小于0",
        icon: "none",
      });
      return;
    }
    wx.showLoading({
      title: "请稍等",
      mask: true,
    });
    const requestParams = {
      recipient_id: this.data.memberId,
      give_amounts: this.data.giftValue,
    };
    if (this.data.payPassword.turnedOn) {
      if (
        !this.data.payPassword.inputValue ||
        this.data.payPassword.inputValue.length < 3
      ) {
        wx.showToast({
          title: "请输入正确的提现密码",
          icon: "none",
        });
        return;
      }
      requestParams["password"] = this.data.payPassword.inputValue;
    }
    DHttp.post(
      "plugin.credit-inventory.frontend.controllers.give.handle",
      requestParams
    )
      .then((res) => {
        wx.hideLoading();
        wx.showToast({
          title: "赠送成功",
          icon: "none",
        });
        this.setData({
          hiddenInputPassPopup: true,
          ["payPassword.inputValue"]: "",
        });
      })
      .catch(({ msg }) => {
        wx.hideLoading();
        wx.showToast({
          title: msg,
          icon: "none",
        });
      });
  },
  checkPayPassword() {
    if (this.data.payPassword.turnedOn && this.data.payPassword.has === false) {
      wx.showModal({
        title: "未设置提现密码",
        content: "您还未设置提现密码，设置提现密码后才可以赠送",
        showCancel: true,
        cancelText: "取消",
        confirmText: "确定",
        success: (result) => {
          if (result.confirm) {
            wx.navigateTo({
              url: "/packageA/member/set_balance_password/set_balance_password",
            });
          }
        },
      });
      return false;
    }
    return true;
  },
});
