// pages/reservationPipe/add.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    defaultText: '请选择',
    isSubmitting: false,

    orderArray:[], //管桩计划
    orderIndex:null,

    appointmentStatusArray: [{
      dictValue: "",
      dictLabel: "全部状态"
    }],
    appointmentStatusIndex: 0,

    isKeyboard: false, //是否显示车牌输入键盘
    inputOnFocusIndex: '', //当前锁定的车号位置
    licensePlate: '', //用于提交
    inputPlates: '', //用于显示

  },
  //输入框焦点聚焦时隐藏车牌号输入框
  hideKeybord: function () {
    this.setData({
      isKeyboard: false,
      inputOnFocusIndex: ''
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 根据缓存获取司机车牌号
    let getCarNumArr = wx.getStorageSync('nickname')?wx.getStorageSync('nickname').split(''):''
    let inputPlates = {};
    for (let i = 0; i < getCarNumArr.length; i++) {
      inputPlates['index' + i] = getCarNumArr[i]
    }
    // 默认填充司机信息
    this.setData({
      driverId    : wx.getStorageSync('id'),
      driverName  : wx.getStorageSync('name'),
      driverMobile: wx.getStorageSync('phone'),
      inputPlates : inputPlates,
      isNewEnergy : getCarNumArr.length > 7 ? true : false,
      licensePlate: wx.getStorageSync('nickname')
    })
    this.setData({
      orderId: options.orderId
    })
    //获取管桩计划信息
    // this.getorderData()

    app.request.GET({
      url: app.API.bizpipeplanDetail,
      params: {'id' : options.orderId},
      page: this,
      successFun: true
    }).then(res => {
      let getInfo = res.data.data;
      this.setData({
        planNumber      : getInfo.planNumber,
        planName        : getInfo.planName,
        planCount       : getInfo.planCount,
        planAlreadyCount: getInfo.planAlreadyCount,
      })
    })
  },

  //选择管桩计划
  // getorderData:function(){
  //   app.request.GET({
  //     url: app.API.bizpipeplanList,
  //     params: {},
  //     page: this,
  //     successFun: true
  //   }).then(res => {
  //     let orderList = res.data.data
  //     orderList.forEach((element,index) => {
  //       orderList[index].name = element.planNumber
  //     });
  //     this.setData({
  //       orderArray: orderList
  //     })
  //   })
  // },
  

  //获取车牌号
  licensePlate: function (e) {
    this.setData({
      licensePlate: e.detail.carNum
    })
  },

  //下拉选择
  bindPickerChange: function (e) { 
    let { pickername } = e.target.dataset
    let getIndex = pickername + 'Index'
    app.util.getPickerIndex(this, getIndex, e);
    switch (pickername) {
      case 'order': //管桩计划
        this.setData({
          planNumber      : this.data.orderArray[this.data.orderIndex].planNumber,
          planName        : this.data.orderArray[this.data.orderIndex].planName,
          planCount       : this.data.orderArray[this.data.orderIndex].planCount,
          planAlreadyCount: this.data.orderArray[this.data.orderIndex].planAlreadyCount,
        })
        break;
    }
  },

  // 单选
  dangerStatusChange: function (e) {
    this.setData({
      [e.currentTarget.dataset.radiotype]: e.detail.value
    })
  },
  //多选
  checkboxChange: function (e) {
    this.setData({
      [e.currentTarget.dataset.checkboxtype]: e.detail
    })
  },

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

  //表单提交 carTaskAdd
  formSubmit: function ({detail:{value}}) {
    console.info("----driverId---" + value.driverId)
    var warn = ""; //弹框时提示的内容
    let reg = /^1[3-9]\d{9}$/;
    var xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
    var creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
    //value.certificateNoImage = this.data.originalInvoicePhoto
    if (value.orderId == '') {
      warn = "请选择管桩计划订单！";
    } else if (value.licenseNumber == '') {
      warn = "车牌不能为空！";
    } else if (value.licenseNumber.length < 7) {
      warn = "请选择完整车牌号！";
    } else if (value.licenseNumber.length == 7 && !creg.test(value.licenseNumber)) {
      warn = "车牌号格式错误！";
    } else if (value.licenseNumber.length == 8 && !xreg.test(value.licenseNumber)) {
      warn = "车牌号格式错误！";
    } else if(value.driverName == ''){
      warn = "请输入司机姓名！";
    } else if(value.driverMobile == ''){
      warn = "请输入司机电话！";
    } 

    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.appointmentPipeAdd,
        params: value,
        page: this,
        isLoadingTxt: '提交中...',
        isSubmitting: true,
        successFun: true
      }).then(res => {
        wx.showToast({
          title: '新增成功',
          icon: 'success',
          duration: 2000,
          mask:true,
          complete: function () {
            setTimeout(() => {
              wx.navigateBack()
            }, 1500) //延迟时间  
          }
        })
      })

    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  }
})