// pages/initiate_refund/initiate_refund.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refundMsg:'',
    yesUrl: '../../assets/yes-copy@2x.png',
    noUrl: '../../assets/class_progress_yellow@3x.png',
    puzzleList: [
      {
        yesUrl: '../../assets/yes-copy@2x.png',
        noUrl: '../../assets/class_progress_yellow@3x.png',
        text: '家长发起退款',
        time: '17:00',
        noSend: ''
      },
      {
        yesUrl: '../../assets/yes-copy@2x.png',
        noUrl: '../../assets/class_progress_yellow@3x.png',
        text: '校长审核订单',
        time: '17:30',
        noSend: '待审核'
      },
      {
        yesUrl: '../../assets/yes-copy@2x.png',
        noUrl: '../../assets/class_progress_yellow@3x.png',
        text: '退款',
        time: '17:55',
        noSend: '未退款'
      },
    ],
    reasonList:[
      {
        txt:'辅导没达到预期效果'
      },
      {
        txt: '老师不适合'
      },
      {
        txt: '学生有其他安排,不上了'
      },
      {
        txt: '学校补课,没时间安排'
      },
      {
        txt: '其他'
      },

    ]
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(options);
  },

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

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
 
  // 退款信息
  drawback:function(){
    const { orderNo } = this.data;
    const postData = { fn: this.drawback, param: { ...arguments } }
    app.post('/api/parents/parents/applyOrderRefund', { orderNo }, 0, postData).then(res => {
      const { courseName, grade, refundPrice, totalCount, finishCount, totalPrice, costMoney, refundReason}=res.data
      this.setData({ courseName, grade, refundPrice, totalCount, finishCount, totalPrice, costMoney, refundReason})
    })
  },
  // 选择退款理由
  selectReason: function (e) {
    const { index, value } = e.currentTarget.dataset;
    this.setData({
      idx: index,
      refundMsg: value
    })
  },
  // 持卡人姓名
  cardName:function(e){
    const { value } = e.detail;
      if (value != '') {
        this.setData({ cardName: value })
      } else {
        wx.showToast({
          title: '姓名不能为空',
          icon: 'none'
        })
        return
      }
    
  },
  // 支行名
  subbranch:function(e){
    const { value } = e.detail;
    if (value != '') {
      this.setData({ subbranch: value })
    } else {
      wx.showToast({
        title: '支行名不能为空',
        icon: 'none'
      })
      return
    }
  },
  // 银行卡号码
  cardNum:function(e){
    // /^([1-9]{1})(\d{14}|\d{18})$/;
    const cardExp = /^\d{15,20}$/
    const { value } = e.detail;
      if (value != '') {
        if (!cardExp.test(value)) {
          wx.showToast({
            title: '银行卡位数不对',
            icon: 'none'
          })
          this.setData({ cardNum: value })
          return
        }else{
          this.setData({cardNum: value })
          return
        }
      }else{
        wx.showToast({
          title: '银行卡号不能为空',
          icon: 'none'
        })
      }
    
  },
  // 开户行信息
  cardBank:function(e){
    const { value } = e.detail;
    if (value != '') {
      this.setData({ cardBank: value })
    } else {
      wx.showToast({
        title: '开户行信息不能为空',
        icon: 'none'
      })
      return
    }
  },
  // 预留手机号码
  bankPhone:function(e){
    const mobileExp = /^1\d{10}$/;
    const { value } = e.detail;
    if (value!= '') {
      if (!mobileExp.test(value)) {
        wx.showToast({
          title: '预留手机号位数不对',
          icon: 'none'
        })
        this.setData({ bankPhone: value })
        return
      }else{
        this.setData({ bankPhone: value })
        return
      }
    }
   
  },
  
  
  // 提交退款信息
  submitRefund:function(){
    const postData = { fn: this.submitRefund, param: { ...arguments } }
    const { refundMsg, orderNo, cardName, cardNum, cardBank, bankPhone,subbranch}=this.data;
    console.log(refundMsg, orderNo, cardName, cardNum, cardBank, bankPhone, subbranch);
    if (!refundMsg) {
      wx.showToast({
        title: '请选择退款理由',
        icon: 'none'
      })
      return
    }
    if (!cardName) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return
    }
    if (!cardNum) {
      wx.showToast({
        title: '请收入银行卡号',
        icon: 'none'
      })
      return
    }
    if (!cardBank) {
      wx.showToast({
        title: '请填写开户行信息',
        icon: 'none'
      })
      return
    } 
    
    if (!subbranch) {
      wx.showToast({
        title: '请填写支行名',
        icon: 'none'
      })
      return
    }
    app.post('/api/parents/parents/confirmOrderRefund', { 
      orderNo,
      cardName,
      cardNum,
      cardBank,
      bankPhone,
      refundMsg,
      subbranch
     }, 0, postData).then(res => {
      
      wx.showToast({
        title: '成功提交退款信息',
        icon:'success',
        duration:1000
      })
      setTimeout((function callback() {
        wx.navigateBack({});
      }).bind(this), 2000);

    })
  }
})