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

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    orderName: '',
    orderNumber:'',

    showModal: false,
    defaultText: '请选择',
    isSubmitting: false,

    orderArray: [], //订单
    orderIndex: '',

    carzheArray: [], //车辆轴数
    carzheIndex: '',

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

    driverId:'',
    driverName:'',
    driverMobile:'',

    loadPointArray:[],//装卸点位
    loadPointIndex:null,

    loadTimeArray:[], //装卸时间
    loadTimeIndex:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId,
      orderName: options.orderName,
      orderNumber:options.orderNumber,
      driverId    : wx.getStorageSync('id'),
    })
    this.getCarInfo()
    //装卸点位
    this.loadPointData()
  },

  //获取长期车辆信息
  getCarInfo: function () {
    app.request.GET({
      url: app.API.bizvehicleDefault,
      params: {},
      page: this,
      successFun: true
    }).then(res => {
      let getInfo = res.data.data
      console.log(getInfo)
      let inputPlates = {};
      if(getInfo){
        let getCarNumArr = getInfo.licensePlate ? getInfo.licensePlate.split('') : ''
        for (let i = 0; i < getCarNumArr.length; i++) {
          inputPlates['index' + i] = getCarNumArr[i]
        }
        this.setData({
          inputPlates: inputPlates,
          isNewEnergy: getCarNumArr.length > 7 ? true : false,
          licensePlate: getInfo.licensePlate,
          driverId:getInfo.driverId,
          driverName:getInfo.driverName,
          driverMobile:getInfo.driverMobile,
        })
        this.getSelectData(getInfo.overId)
      }else{
        this.getSelectData()
      }
      
      //获取订单下拉列表、车辆轴数下拉列表
      
    })
  },

  //获取订单下拉列表、车辆轴数下拉列表
  getSelectData: function (overId) {
    //获取订单下拉列表
    // app.request.GET({
    //   url: app.API.orderAllList,
    //   params: {},
    //   page: this,
    //   successFun: true
    // }).then(res => {
    //   this.setData({
    //     orderArray:res.data.data
    //   })
    // })

    //获取车辆轴数下拉列表
    app.request.GET({
      url: app.API.getCarzheList,
      params: {},
      page: this,
      successFun: true
    }).then(res => {
      this.setData({
        carzheArray: res.data.data,
        carzheIndex: overId?app.util.getDicIndex(res.data.data, overId, 'id'):null,
      })
    })

  },

  //下拉选择
  bindPickerChange: function (e) { //下拉菜单
    let { pickername } = e.target.dataset
    let getIndex = pickername + 'Index'
    app.util.getPickerIndex(this, getIndex, e);
    switch (pickername) {
      case 'loadPoint': //客户
        this.loadTimeData() //获取销售订单
        break;
    }
  },

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

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

  //装卸点位
  loadPointData:function(){
    app.request.GET({
      url: app.API.loadPointYY,
      params: {
        orderId:this.data.orderId
      },
      page: this,
      successFun: true
    }).then(res => {
      this.setData({
        loadPointArray:res.data.data
      })
    })
  },

  //装卸时间
  loadTimeData:function(){
    app.request.GET({
      url: app.API.loadTimeYY,
      params: {
        pointId:this.data.loadPointArray[this.data.loadPointIndex].loadPointId,
        orderId:this.data.orderId
      },
      page: this,
      successFun: true
    }).then(res => {
      let orderList = res.data.data
      orderList.forEach((element,index) => {
        orderList[index].time = element.beginTime + '~' + element.endTime
      });
      this.setData({
        loadTimeArray:orderList
      })
    })
  },

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

  },

  //表单提交
  formSubmit: function ({detail: {value}}) {
    let formData = value;
    let reg = /^1\d{10}$/;
    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}$/;
    var warn = ""; //弹框时提示的内容
    if (formData.licenseNumber == '') {
      warn = "请选择车牌！";
    } else if (formData.licenseNumber.length < 7) {
      warn = "请选择完整车牌号！";
    } else if (formData.licenseNumber.length == 7 && !creg.test(formData.licenseNumber)) {
      warn = "车牌号格式错误！";
    } else if (formData.licenseNumber.length == 8 && !xreg.test(formData.licenseNumber)) {
      warn = "车牌号格式错误！";
    } else if (typeof formData.carzheId == 'undefined' || formData.carzheId == '') {
      warn = "请选择车辆轴数！";
    } else if (formData.driverName == '') {
      warn = "请填写司机姓名！";
    } else if (formData.driverMobile == '') {
      warn = "请填写司机电话！";
    } else if (!reg.test(formData.driverMobile)) {
      warn = "司机电话格式错误！";
    } else if (typeof formData.loadPointId == 'undefined' || formData.loadPointId == '') {
      warn = "请选择装卸点位！";
    } else if(typeof formData.loadTimeId == 'undefined' || formData.loadTimeId == ''){
      warn = "请选择装卸时间！";
    }

    value.overId = value.carzheId
    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.appointmentAdd,
        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 () {

  }
})