// pages/ucenter/car/bindCar.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      carNum: '',
      carId: '',
      updateFlag: false,
      isDef: false,
    },
    inputPlateNumber(e){
      console.info(e.detail.value)
      console.info(e)
      this.setData({
        carNum: e.detail.value
      });
    },
    saveCarNum(){
      let carNum = this.data.carNum;
      if (!this.checkCarNum(carNum)) {
        wx.showModal({
          title: '提示',
          showCancel:false,
          content: '车牌号输入有误，请重新输入',
          confirmColor:'#00AADD',
        });
        return;
      } else {
        this.setData({
          carNum: carNum,
          errorMsg: ''
        });
      }
      this.bindUpdateCarNum(carNum);
    },
    checkCarNum(carNum){
      if (carNum){
        // if (!(/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(this.data.carNum))) {
        // if (!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领].*$/.test(carNum)) {
        //   return false;
        // }
        if(carNum.length<1 || carNum.length>10){
            return false;
        }
      }else{
        return false;
      }
      return true;
    },
    bindUpdateCarNum(carNum) {
      let that = this;
      let userInfo = wx.getStorageSync('userInfo');
      // loginName: userInfo.loginName
      
      // 判断是更新车牌还是新增车牌
      console.info(this.data.updateFlag)
      if(this.data.updateFlag){
        console.info("更新车牌号:" + carNum + ",用户:" + userInfo.loginName + ",车牌号:" + this.data.carNum )
  
        wx.request({
          url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carAlter?loginName=' + userInfo.loginName,
          data: {
            carId: that.data.carId,
            plateNumber: carNum,
            defaultType: that.data.isDef?"1":"0"
          },
          method: 'POST',
          success: function (res) {
            console.info(res)
            wx.navigateBack({
              delta: 1
            });
          },
          fail(e) {
          console.log("更新车牌号失败");
          log.info('[更新车牌号]','[fail]', e);
          }
        });
  
  
      }else{
        console.info("新增车牌号:" + carNum + ",用户:" + userInfo.loginName + "")
  
        wx.request({
          url: getApp().globalData.postHeadAgreement + '/restapi/wechat/saveCar?loginName=' + userInfo.loginName + '&plateNumber=' + carNum + '&defaultType=' + (that.data.isDef?"1":"0"),
          // data: {
          //   defaultType: 1,
          //   carId: idx.carId,
          //   plateNumber: idx.plateNumber
          // },
          method: 'POST',
          success: function (res) {
            console.info(res)
            wx.setStorageSync("chooseCar_plateNumber", {plateNumber:carNum});
            wx.navigateBack({
              delta: 2
            });
          },
          fail(e) {
          console.log("新增车牌号失败",e);
          //log.info('[新增车牌号]','[fail]', e);
          }
        });


  
      }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      let {carId,plateNumber,defaultType} = options;
      let updateFlag = false;
      if(carId){
        updateFlag = true;
      }else{
        if(carId==0){
          updateFlag = true
        }else{
          updateFlag = false
        }
      }
      console.info(updateFlag?'更新模式':'新增模式')
      console.info(plateNumber)
      this.setData({
        carId,
        updateFlag,
        carNum:plateNumber,
        isDef:defaultType==1
      })
    },
    radioChange: function(e) {
      let that = this;
      this.setData({
        isDef:!that.data.isDef
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let data = wx.getStorageSync("chooseCar_plateNumber");
        console.info(data)
        if(data){
            this.setData({
                carNum:data.carNum
            })
        }

    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
  
    }
  })