// pages/index/replace.js
const app = getApp()
Page({

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

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

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

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

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

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取详情
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    let getInfo = prevPage.data.resData[options.index]

    this.setData({
      info: getInfo 
    })

    this.getSelectData()

  },

  //获取订单下拉列表、车辆轴数下拉列表
  getSelectData:function(){
    //获取订单下拉列表
    app.request.GET({
      url: app.API.stoneAppointSupplierList,
      params: { licenseNumber : this.data.info.licenseNumber },
      page: this,
      successFun: true
    }).then(res => {
      this.setData({
        orderArray: res.data.data,
        orderIndex: app.util.getDicIndex(res.data.data, this.data.info.orderId, 'orderId'),
      })
      // 初始装卸点位
      this.loadPointData(true)
    })

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

  //装卸点位
  loadPointData:function(type){
    let orderIdSelect = type ? this.data.info.orderId : this.data.orderArray[this.data.orderIndex].orderId
    app.request.GET({
      url: app.API.stoneAppointPointList,
      params: {
        orderId: orderIdSelect
      },
      page: this,
      successFun: true
    }).then(res => {
      this.setData({
        loadPointArray : res.data.data,
        loadPointIndex : null,
        loadTimeArray  : []
      })
      //初始化详情
      if(type){
        this.setData({
          loadPointIndex : app.util.getDicIndex(res.data.data, this.data.info.loadPointId, 'loadPointId')
        })
        this.loadTimeData(true)
      }
    })
    
  },

  //装卸时间
  loadTimeData:function(type){
    console.info("------type-------" + type)
    let pointIdSelect = type ? this.data.info.loadPointId : this.data.loadPointArray[this.data.loadPointIndex].loadPointId
    let orderIdSelect = type ? this.data.info.orderId : this.data.orderArray[this.data.orderIndex].orderId
    app.request.GET({
      url: app.API.stoneAppointLoadTimeList,
      params: {
        pointId : pointIdSelect,
        orderId : orderIdSelect
      },
      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,
        loadTimeIndex : null
      })
      //初始化详情
      if(type){
        this.setData({
          loadTimeIndex : app.util.getDicIndex(orderList, this.data.info.loadTimeId, 'loadTimeId')
        })
      }
    })
  },

  //下拉选择
  bindPickerChange: function (e) { //下拉菜单
    let { pickername } = e.target.dataset
    let getIndex = pickername + 'Index'
    app.util.getPickerIndex(this, getIndex, e);
    switch (pickername) {
      case 'order': //订单
        this.loadPointData() //获取装卸点位
        break;
      case 'loadPoint': //装卸点位
        this.loadTimeData(false) //获取装卸时间
        break;
    }
  },

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

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


  formSubmit: function ({detail:{value}}) {
    let formData = value;
    var warn = ""; //弹框时提示的内容
    if (typeof formData.orderId == 'undefined' || formData.orderId == '') {
      warn = "请选择订单！";
    } else if (typeof formData.loadPointId == 'undefined' || formData.loadPointId == '') {
      warn = "请选择装卸点位！";
    } else if (typeof formData.loadTimeId == 'undefined' || formData.loadTimeId == '') {
      warn = "请选择装卸时间！";
    } 

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

  }
})