// pages/updateInfo/indexShip.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginStatus: wx.getStorageSync('loginStatus') ? wx.getStorageSync('loginStatus') : false,
    showModal: false,
    isSubmitting:false,
    defaultText: '请选择',
    userId:'',
    name:'',
    phone:'',
    nickname: '',
    fileList:[],
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    ///证书图片
    let fileList = []
    if(null != wx.getStorageSync('unloadImg') && "" != wx.getStorageSync('unloadImg')){
      let unloadImg = wx.getStorageSync('unloadImg');
      console.log("unloadImg---" + unloadImg);
      let unloadImgArray = unloadImg.split(',');
      unloadImgArray.forEach(element => {
        fileList.push({
          url     : app.host.BASEIMG_URL + element,
          imgUrl  : element,
          isImage : true
        })
      });
    }

    this.setData({
      loginStatus : wx.getStorageSync('loginStatus') ? wx.getStorageSync('loginStatus') : false,
      userId      : wx.getStorageSync('id'),
      name        : wx.getStorageSync('name'),
      phone       : wx.getStorageSync('phone'),
      nickname    : wx.getStorageSync('nickname'),
      fileList    : fileList,//证书图片
    })
  },

  //图片上传
  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
    })
  },

  //表单提交
  formSubmit: function (e) {
    let formData = e.detail.value;
    let reg = /^1[3-9]\d{9}$/;
    var mobile = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;
    var warn = ""; //弹框时提示的内容
    if (formData.account == '') {
      warn = "请输入账号！";
    } else if (formData.name == '') {
      warn = "请输入船主姓名！";
    } else if (formData.phone == '') {
      warn = "请输入船主手机号！";
    } else if(!reg.test(formData.phone)){
      warn = "请确认手机号格式！";
    } else if (formData.nickname == '') {
      warn = "请输入船舶号！";
    } 

    /** 图片处理 START **/
    let getfileList = this.data.fileList
    if(getfileList.length < 1){
      warn = "请上传船舶证书图片！";
    }
    let fileNameList = []
    let filePathList = []
    let unloadImg = ""

    getfileList.forEach((element,index) => {
      fileNameList.push('图片'+(index+1)+'.'+element.imgUrl.split('.')[1])
      filePathList.push(element.imgUrl)
      if(""!=unloadImg){
        unloadImg = unloadImg + "," + element.imgUrl
      }else{
        unloadImg = element.imgUrl
      }
    });
    formData.fileNameList = fileNameList
    formData.filePathList = filePathList
    /** 图片处理 END **/

    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url: app.API.updateUserInfo,
        params: formData,
        page: this,
        isLoadingTxt: '处理中...',
        isSubmitting: true,
        successFun: true
      }).then(res => {
        let _this = this
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 1500,
          mask:true,
          complete: function () {
            setTimeout( ()=> {
              wx.setStorageSync('name', formData.name); //用户姓名
              wx.setStorageSync('nickname', formData.nickname); //车牌号
              wx.setStorageSync('unloadImg', unloadImg); //证书图片
              wx.navigateBack()
            }, 1500) //延迟时间  
          }
        })
      })


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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})