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

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

    loadappointArray:[], //起卸预约
    loadappointIndex:null,

    fileList:[],

    minHour: 0,
    maxHour: 24,
    minDate: new Date().getTime(),
    maxDate: new Date(9999, 12, 31).getTime(),
    currentDate: new Date().getTime(),
    show: false,
    currentChoose: ''
  },
  openPicker() {
    this.setData({ show: true })
  },
  onConfirm(e) {
    this.setData({ show: false, currentChoose: this.formatDate(new Date(e.detail)) })
  },
  onClose() {
    this.setData({ show: false })
  },
  onCancel() {
    this.setData({ show: false })
  },
  formatDate(date) {
    let taskStartTime
    if (date.getMonth() < 9) {
      taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
    } else {
      taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
    }
    if (date.getDate() < 10) {
      taskStartTime += "0" + date.getDate()
    } else {
      taskStartTime += date.getDate()
    }
    if (date.getHours() < 10) {
      taskStartTime += " " + "0" + date.getHours()
    } else {
      taskStartTime += " " + date.getHours()
    }
    if (date.getMinutes() < 10) {
      taskStartTime += ":" + "0" + date.getMinutes()
    } else {
      taskStartTime += ":" + date.getMinutes()
    }
    this.setData({
      taskStartTime: taskStartTime,
    })
    return taskStartTime;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //预约审核状态
    let treeData = app.globalData.treeArr
    for (const element of treeData) {
      let arrStr;
      switch (element.dictValue) {
        case 'arrive_status':
          arrStr = "arriveStatusArray";
          break;
      }
      this.setData({
        [arrStr]: element.children
      })
    }

    //获取起卸预约信息
    this.getLoadappointData()
  },

  //选择起卸预约
  getLoadappointData:function(){
    app.request.GET({
      url: app.API.bizloadappointList,
      params: {'status' : '2'},
      page: this,
      successFun: true
    }).then(res => {
      let appointList = res.data.data
      appointList.forEach((element,index) => {
        appointList[index].name = element.loadNumber
      });
      this.setData({
        loadappointArray: appointList
      })
    })
  },

  //下拉选择
  bindPickerChange: function (e) { 
    let { pickername } = e.target.dataset
    let getIndex = pickername + 'Index'
    app.util.getPickerIndex(this, getIndex, e);
    switch (pickername) {
      case 'loadappoint': //报港预约
        app.request.GET({
          url: app.API.bizloadappointDetail,
          params: {'id' : this.data.loadappointArray[this.data.loadappointIndex].id},
          page: this,
          successFun: true
        }).then(res => {
          let getInfo = res.data.data;
          this.setData({
            customerName    : getInfo.customerName?getInfo.customerName:"",
            customerContact : getInfo.customerContact?getInfo.customerContact:"",
            customerPhone   : getInfo.customerPhone?getInfo.customerPhone:"",
            goodsName       : getInfo.goodsName?getInfo.goodsName:"",
            goodsCode       : getInfo.goodsCode?getInfo.goodsCode:"",
          })
        })
        break;
    }
  },

  //图片上传
  afterRead:function(event){
    const { file } = event.detail;
    const { type } = event.currentTarget.dataset
    app.request.uploadDIY({
      url: app.API.uploadImgMap,
      page: this,
      filePaths:file.url,
      setfiled:'file',
      params: {},
      //isToken: false,
      isLoading: false,
      successFun: true
    }).then(res => {
        const imgArray = this.data[type];
        imgArray.push({
          ...file,
          url: app.host.BASEIMG_URL + JSON.parse(res.data).data.imageFile,
          imgUrl: JSON.parse(res.data).data.imageFile
        });
        this.setData({
          [type]: imgArray
        });
    })
  },

  // 删除图片
  deleteImg: function (event) {
    const { type } = event.currentTarget.dataset
    let getFileList = this.data[type];
    getFileList.splice(event.detail.index, 1)
    this.setData({
      [type]: getFileList
    })
  },

  // 单选
  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}}) {
    var warn = ""; //弹框时提示的内容
    if (value.appointId == '') {
      warn = "请选择起卸预约订单！";
    } else if(value.shipNo == ''){
      warn = "请输入船号！";
    } else if(value.arriveNumber == ''){
      warn = "请输入报港数量！";
    } else if(value.arriveTime == ''){
      warn = "请选择到港时间！";
    } 

    /** 图片处理 START **/
    let getfileList = this.data.fileList
    if(getfileList.length < 1){
      warn = "请上传发航单！";
    }
    let fileNameList = []
    let filePathList = []
    getfileList.forEach((element,index) => {
      fileNameList.push('图片'+(index+1)+'.'+element.imgUrl.split('.')[1])
      filePathList.push(element.imgUrl)
    });
    value.fileNameList = fileNameList
    value.filePathList = filePathList
    /** 图片处理 END **/

    // 处理时间
    value.arriveTime = value.arriveTime + ":00"

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

  }
})