let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {},
    getCodeTime: 40, //发送短信的间隔时间
    verifyCodeTime: '获取验证码',
    IMG_NAME: app.IMG_NAME
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    let that = this
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/manage/detail', {
      id: e.id
    }, function (res) {
      that.setData({
        listdetail: res.data,
        'form.userName': wx.getStorageSync('MemberInfo').nickname,
        // 'form.mobile': wx.getStorageSync('MemberInfo').mobile,
        'form.userId': wx.getStorageSync('MemberInfo').userId,
        id: e.id
      })
    })
  },


  /**
   * 执行发送验证码
   */
  verifyCodeEvent: function (e) {
    var that = this;
    //验证手机号
    if (that.data.buttonDisable == true) return false;
    var mobile = that.data.form.mobile;
    //验证手机号 END
    wx.showLoading();
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + app.Getopenid()
      },
      method: 'GET',
      url: app.DOMAIN_NAME + '/manage/send_mobile',
      data: {
        mobile: mobile
      },
      success: res => {
        if (res.data.code == 0) {
          wx.hideLoading()
          // wx.showToast({
          //   title: res.data.msg,
          //   duration: 3000,
          //   icon: 'none'
          // })
          that.Countdown()
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 3000,
            icon: 'none'
          })
        }
      },
      fail: () => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 发送短信倒计时,防止恶意刷短信
   */
  Countdown: function () {
    var that = this;
    that.setData({
      buttonDisable: true
    });
    var getCodeTime = wx.getStorageSync('getCodeTime');
    var intervalId = setInterval(function () {
      getCodeTime--;
      wx.setStorageSync('getCodeTime', getCodeTime);
      that.setData({
        verifyCodeTime: '重新获取' + '(' + getCodeTime + 's' + ')'
      });
      //倒计时完成
      if (getCodeTime == 0) {
        wx.setStorageSync('getCodeTime', that.data.getCodeTime);
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000);
  },
  codeinput(e) {
    this.setData({
      'form.code': e.detail.value
    })
  },
  userNameinput(e) {
    this.setData({
      'form.userName': e.detail.value
    })
  },
  mobileinput(e) {
    this.setData({
      'form.mobile': e.detail.value
    })
  },
  apply() {
    let that = this
    let form = that.data.form
    if (!form.userName) {
      wx.showToast({
        icon: 'none',
        title: '请先填写姓名',
      })
      return false
    }
    if (!form.mobile) {
      wx.showToast({
        icon: 'none',
        title: '请先填写手机号',
      })
      return false
    }
    if (!form.code) {
      wx.showToast({
        icon: 'none',
        title: '请先填写验证码',
      })
      return false
    }
    var condition = form.userId + '&supplierId=' + that.data.listdetail.supplierId + '&serviceId=' + that.data.id + '&code=' + form.code + '&parkId=' + wx.getStorageSync('parkId') + '&mobile=' + form.mobile
    app.AjaxRequest('post', {
        'content-type': 'application/json',
        'Authorization': 'Bearer ' + app.Getopenid()
      }, '/manage/apply?memberId=' + condition, {},
      function (res) {
        if (res.code === 0) {
          wx.showToast({
            title: '申请成功',
            success() {
              wx.reLaunch({
                url: '/pages/enterpriseServices/myServiceList/myServiceList',
              })
            }
          })
        }
      })
  }
})