const Parse = getApp().Parse;
const company = getApp().globalData.company
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    cardList:[],
    name:'',
    cardId:'',
    bankName:'',
    objectId:'',
    activeColor: getApp().globalData.activeColor,
    titleColor: getApp().globalData.titleColor
  },

  onClickShow() {
    this.setData({ show: true });
  },

  onClickHide() {
    this.setData({ show: false });
  },
  delete(e){
    let cardList = this.data.cardList
    cardList.splice(e.currentTarget.dataset.index,1)
    
    let Account = Parse.Object.extend("Account")
    let account = new Account()
    account.id = this.data.objectId
    if((cardList && cardList.length == 0) || !cardList) {
      account.set("bank",null)
    }else {
      account.set("bank",cardList)
    }
    account.save().then(res=>{
      wx.showToast({
        title: '删除银行卡成功',
        icon: 'succee',
        duration: 2000
      })
      this.getCardList()
    })
  },
  submit(){
    const _this = this
    let reg = /[\u4e00-\u9fa5]/
    let name = this.data.name
    let bankName = this.data.bankName
    let cardId = this.data.cardId
    if(!this.data.name || !this.data.cardId || !this.data.bankName){
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(!reg.test(name)){
      wx.showToast({
        title: '请输入您的真实姓名',
        icon: 'none',
        duration: 2000
      })
      return
    }
   
    let regExp = /^([1-9]{1})(\d{15}|\d{16}|\d{18})$/;
    if(!regExp.test(cardId)){
      wx.showToast({
        title: '请输入正确的银行卡号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(!reg.test(bankName)){
      wx.showToast({
        title: '请输入正确的开卡行',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let Account = Parse.Object.extend("Account")
    let account = new Account()
    account.id = this.data.objectId
    account.addUnique("bank",{
      name,
      cardId,
      bankName
    })
    account.save().then(res=>{
      console.log(res)
      wx.showToast({
        title: '添加银行卡成功',
        icon: 'succee',
        duration: 2000
      })
      this.onClickHide()
      this.getCardList()
      this.setData({
        name:null,
        cardId: null,
        bankName:null
      })
    })
  },
  getCardList(){
    let Account = new Parse.Query("Account")
    Account.equalTo("user",Parse.User.current().id)
    Account.equalTo("company",company)
    Account.first().then(res=>{
      let account = res.toJSON()
      this.setData({
        cardList:account.bank ? account.bank : [] ,
        objectId:account.objectId
      })
      
    })
  },
  bindKeyInput(e){
      this.setData({
        [e.currentTarget.dataset.prop]: e.detail.value
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCardList()
  },

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

  },

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

  },

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

  },

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

  },

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

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

  },

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

  }
})