var t = getApp(),
  a = t.request,
  o = t.globalData.setting,
  os = o,
  i = require("../../../../../utils/util.js"),
  ut = i,
  s = require("../../../../../utils/common.js");
var regeneratorRuntime = require('../../../../../utils/runtime.js');
const util = require("../../../../../utils/util.js");
var oo = t.globalData.setting;


Page({
  /**
   * 页面的初始数据
   */
  data: {
    usernames: null,
    val: null, //提现金额内容
    current: 1, //单选框判断的参数
    user_money: 0, //可提现余额
    // money:0,//提现金额
    account_bank: "", //银行卡号
    bank_name: "", //银行名字
    button: 0, //按钮样式控制器、
    submit: null, //点击控制器
    weixin: 0,
    cardInfo: '',
    user_infor: {}, //获取初始对象
    needmon:0,
    distribut_min:0,
  },
  // 银行卡失去焦点事件
  blurInputEvent: function(e) {
    var inputvalue = e.detail.value.length;
    if (inputvalue > 14) {
      var value = e.detail.value;
      var account_bank = value.replace(/\s+/g, "");
      this.yhk(account_bank);

    }
   
  },
  /** 获取提现输入框的值 bindinput='genre' */
  money: function(e) {
    this.setData({
      money: Number(e.detail.value)
    })
  },
  // 单选框控制器
  onClickItem(e) {
    console.log(" 单选框控制器", e.currentTarget.dataset.idx) //获取自定义的值
    let idx = e.currentTarget.dataset.idx;
    this.setData({
      current: idx
    })

  },
  /*点击确定提现后表现的形式*/
  Gettime: function(e) {
    var th=this;
    var user_info=getApp().globalData.userInfo;
    //提现是零钱的时候，同时是发红包的时候
    if(this.data.current==2 &&  parseInt(this.data.tx_wx_type)==2){
        if(!user_info.openid){
           ut.m_toast("未绑定公众号微会员,无法提现！");
           return false
        }
    }

    var user_money = this.data.user_money;
    var money = e.detail.value.moneys; //typeof判断数据类型
    var current = this.data.current;
    var money = Number(money);

    if (user_money<this.data.needmon) return false;

    if (money == 0 || money == "" || money == null) {
      getApp().showWarning("请输入提现金额");
      return false;
    }

    if (this.data.distribut_min > money){
      getApp().showWarning("提现需大于"+this.data.distribut_min+"元");
      return false;

    }
    if (Number(user_money) < Number(money)) {
      console.log(money + "有qq吗" + user_money + typeof this.data.money);
      getApp().showWarning("提现金额大于余额");
        return false;

    } else if (money > 0 || user_money >= money && user_money > 99) {
      if (current == 1) {
        console.log(user_money + "有进来吗eeee" + money);
        // 获取输入框中的值
        var moneys = e.detail.value.moneys; //钱
        var cardname = this.data.bank_name; //卡名
        var userName = e.detail.value.userName; //名字
        var card = e.detail.value.card; //卡号

       
        if (card.length < 15) {
          getApp().showWarning("卡号错误");
          this.setData({
            bank_name: "",
            usernames: ""
          });
          return false;
        }
        console.log("钱", moneys, "卡", cardname, "名字", userName, "卡的长度", card.length);
        var len = userName.length; //名字的长度
        if (userName.indexOf(" ") >= 0) {
          getApp().showWarning("名字不能有空");
          return false;

        }
        

        console.log("字符串的长度", len);
        if (this.data.current == 1) {
          if (cardname == null || cardname == "") {
            getApp().showWarning("银行卡不存在");
            return false;

          }
          if (userName == "" || userName == null) {
            getApp().showWarning("名字不能为空");
            return false;
          }
          if (len < 2) {
            getApp().showWarning("名字错误");
            return false;
          }
          if (!/^[\u4e00-\u9fa5]+$/i.test(userName)) {
            getApp().showWarning("您输入不是中文");
            return false;
          }

        }
        var that = this;
        //控制器参数
        var user_id = getApp().globalData.user_id;
        var store_id = os.stoid;

        var current = that.data.current;
        var userName = e.detail.value.userName; //名字
        console.log('微信余额名字', userName, "控制器", current);
        this.data.user_infor.money = moneys;
        this.data.user_infor.user_id = user_id;
        this.data.user_infor.store_id = store_id;
        this.data.user_infor.account_name = userName;
        this.data.user_infor.account_bank = card;
        this.data.user_infor.bank_name = cardname;
        this.data.user_infor.bank_type = this.data.current;
        var user_infor = this.data.user_infor;
        this.Cashwithdrawal(user_infor)
      } else {
        var userName = e.detail.value.userName; //名字
        var len = userName.length; //名字的长度
        if (userName.indexOf(" ") >= 0) {
          getApp().showWarning("名字不能有空");
          return false;

        }
        if (userName == "" || userName == null) {
          getApp().showWarning("名字不能为空");
          return false;
        }
        if (len < 2) {
          getApp().showWarning("名字错误");
          return false;
        }
        if (!/^[\u4e00-\u9fa5]+$/i.test(userName)) {
          getApp().showWarning("您输入不是中文");
          return false;
        }
        var moneys = e.detail.value.moneys; //钱
        var user_id = getApp().globalData.user_id;
        var store_id = os.stoid;
        var len = userName.length;
        this.data.user_infor.money = moneys;
        this.data.user_infor.user_id = user_id;
        this.data.user_infor.store_id = store_id;
        this.data.user_infor.account_name = userName;
        this.data.user_infor.bank_type = this.data.current;
        var user_infor = this.data.user_infor;

        console.log("控制器2", user_infor);
        this.Cashwithdrawal(user_infor);
      }
    } else {
     console.log( "提现金额小于",this.data.distribut_min);
      getApp().showWarning("提现金额小于"+this.data.distribut_min);

     
    }
  },

  // 提现
  Cashwithdrawal: function(user_infor) {


    if(this.data.tx_ing) return false;
    this.data.tx_ing=1;

    var th=this;

    delete user_infor.rmon;
    delete user_infor.isck;
    delete user_infor.needmon;
    delete user_infor.liudong_money;
    delete user_infor.distribut_min;
    delete user_infor.type;

    user_infor.source_type=1;
    //var str = JSON.stringify(user_infor);
    // wx.showLoading({ title: "加载中" });
    getApp().request.promisePost("/api/weshop/users/saveWithdrawals", {
      data: user_infor,is_json:1,is_normal:1
    }).then(res => {

        th.data.tx_ing=0;
        if (res.data.code != -1) {
          wx.navigateTo({
            url: 'msg/msg',
          })
        } else {
          util.m_toast(res.data.msg);
        }

    }).catch(function(err) {
      getApp().showWarning("网络繁忙，请稍后重试");
      th.data.tx_ing=0;
    })

  },

  // 初始化判断微信余额
  async weixin() {
    var user_id = getApp().globalData.user_id;
    var store_id = os.stoid;
    await getApp().request.promiseGet("/api/weshop/users/getWithdrawals", {
      data: {
        user_id: user_id,
        store_id: store_id
      }
    }).then(res => {
      var a = res.data;
      var user_infor = a.data;
      var user_money = user_infor.liudong_money;
     var distribut_min=user_infor.distribut_min;
      console.log( user_money,distribut_min,user_infor, "初始化出来的数据", a.data.account_bank);
      if (user_money > distribut_min) {
        this.setData({
          submit: "submit",
          button: user_money,
          user_money: user_money,
          distribut_min: distribut_min
        });

      } else {
        this.setData({
          submit: " ",
          button: user_money,
          user_money:user_money,
          distribut_min: distribut_min
        })
      }
      console.log(typeof user_money, "ooo" + user_money);
      if (a.code != -1) {
        this.setData({
          user_infor: user_infor,
         
        });
        if (a.data.account_bank != null) {
          var account_bank = a.data.account_bank.replace(/\s+/g, "");
          console.log("卡号", account_bank, "初始化", a.data.isck);
          if (a.data.isck == 1) {
            this.setData({

              bank_name: a.data.bank_name,
              account_bank: account_bank,
              usernames: a.data.account_name,
              distribut_min: a.data.distribut_min,
              user_money: user_money,
              needmon:a.data.needmon,

            });
          } else {
            this.setData({

              bank_name: a.data.bank_name,
              account_bank: account_bank,
              usernames: a.data.account_name,
              distribut_min: a.data.distribut_min,
              user_money: user_money,
              needmon:a.data.needmon,
            });
          }
        }
      }
    })

  },

  //银行卡
  async yhk(account_bank) {
    console.log("有进来吗银行卡" + account_bank);
    var e = this,
      bank_name = "";
    await getApp().request.promiseGet("/api/weshop/users/getBankName", {
      data: {
        bankno: account_bank,
      }
    }).then(res => {
      var a = res.data;
      if (a.data == null) {
        console.log("来了吗");
        getApp().showWarning("银行卡不存在");
        this.setData({
          bank_name: " "
        });
        return false;
      }
      console.log(a);
      bank_name = a.data.bank_name;
      console.log("s1w" + bank_name);

    })
    e.setData({
      bank_name: bank_name
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
      getApp().check_can_share();
    var th=this;
    this.weixin();
    this.setData({
      button: 0
    });
    var user_info = getApp().globalData.userInfo;
    if (user_info == null || user_info.mobile == undefined || user_info.mobile == " " || user_info.mobile == null) {
      wx.navigateTo({
        url: '/packageE/pages/togoin/togoin',
      })
      return false;
    }
    this.setData({
      val: null
    })
    wx.setNavigationBarTitle({
      title: "流动资金提现",
    });

    //优惠券要实时更新
    getApp().getConfig2(function(e) {
      var json_d = JSON.parse(e.switch_list);
	  var is_closetxbank=json_d.is_closetxbank;
	  var is_tx_wx=json_d.is_tx_wx;
	  var tx_wx_type=json_d.tx_wx_type;
	  var data={
		is_closetxbank:is_closetxbank,is_tx_wx:is_tx_wx,tx_wx_type:tx_wx_type
	  };
	  if(is_closetxbank) data.current=2;	  
      th.setData(data);
	  
    },1)
  },
  //提取金额的输入框事件
  defocus: function (event){
    var money = Number(event.detail.value);
    var val =this.data.distribut_min;
    console.log(Number(val).length, "sss", val, "提取金额的输入框事件", money.length, typeof Number(event.detail.value), money);
    console.log("keyCode", event.detail.keyCode);
    if (event.detail.keyCode ==8){
      return false};
   
    if (money<this.data.distribut_min){
      var val = this.data.distribut_min;
      console.log("提取金额的输入框事件");
   
      
      console.log(val,"提取金额的输入框事件");
        getApp().showWarning("提现需大于"+val+"元");
       
   
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function (event){
      
  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
      getApp().globalData.no_clear=1;
  }

})