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

  /**
   * 页面的初始数据
   */
  data: {
    defaultText: '请选择',
    isSubmitting: false,
    isKeyboard: false, //是否显示车牌输入键盘
    inputOnFocusIndex: '', //当前锁定的车号位置
    licensePlate: '', //车牌号

    isShow: true,
    current_index: 0,

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

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

  //下拉选择
  bindPickerChange: function (e) { //下拉菜单
    let { pickername } = e.target.dataset
    let getIndex = pickername + 'Index'
    app.util.getPickerIndex(this, getIndex, e);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!wx.getStorageSync('loginStatus')) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    } else {
      //获取订单下拉列表、车辆轴数下拉列表
      this.getSelectData()
    }

  },

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

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

  // 选择图片
  afterRead: function (e) {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: res => {
        var base64 = 'data:image/jpeg;base64,' + wx.getFileSystemManager().readFileSync(res.tempFilePaths[0], "base64");
        that.setData({
          originalInvoicePhoto: base64,
          originalInvoicePhotoUrl: res.tempFilePaths[0]
        })
      }
    })
  },
  //删除当前选择的图片
  clearImgFun: function () {
    this.setData({
      originalInvoicePhoto: '',
      originalInvoicePhotoUrl: ''
    })
  },

  //提交事件
  formSubmit: function ({detail:{value}}) {
    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}$/;
    var warn = ""; //弹框时提示的内容
    //value.certificateNoImage = this.data.originalInvoicePhoto
    if (value.licensePlate == '') {
      warn = "车牌不能为空！";
    } else if (value.licensePlate.length < 7) {
      warn = "请选择完整车牌号！";
    } else if (value.licensePlate.length == 7 && !creg.test(value.licensePlate)) {
      warn = "车牌号格式错误！";
    } else if (value.licensePlate.length == 8 && !xreg.test(value.licensePlate)) {
      warn = "车牌号格式错误！";
    } else if (value.carzheId == '') {
      warn = "请选择车辆轴数！";
    }
    //  else if (value.driverName == '') {
    //   warn = "司机姓名不能为空！";
    // } else if (value.driverPhone == '') {
    //   warn = "司机号码不能为空！";
    // } else if (!reg.test(value.driverPhone)) {
    //   warn = "请填写正确的号码！";
    // }
    value.vehicleAxles = value.carzheId
    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.bizvehicleAdd,
        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) //延迟时间  
          }
        })
      })
    }
  }

})