var app = getApp();
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sex: ['否', '是'],
    sexindex: 1,
    date: '请选择期望入驻时间',
    type: 1,
    addressList: ['公司迁入', '新设立公司', '子公司'],
    addressIndex: 0,
    typeList: ['注册地址迁入', '新公司注册'],
    typeIndex: 0,
    applyMoveInFileList: [],
    list: {
      promise: '申请人承诺，自《场地服务合同》变更、解除或终止之日起10日内，办结申请人及上述拟成立企业等关联关系单位的工商地址变更、迁出或注销手续。否则，因此产生的责任和后果由我方承担，且贵方有权追究我方违约责任。'
    }
  },

  uploFile() {
    var that = this;
    if (that.data.applyMoveInFileList.length >= 9) return
    wx.showActionSheet({
      itemList: ['上传图片', '打开微信聊天会话选择文件'],
      success(res) {
        if (res.tapIndex == 0) {
          wx.chooseImage({
            count: 1, //能选择文件的数量
            success(res) {
              that.upload(res.tempFiles)
            }
          })
        } else {
          wx.chooseMessageFile({
            count: 1, //能选择文件的数量
            type: 'file',
            success(res) {
              that.upload(res.tempFiles)
            }
          })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },

  upload(data) {
    var that = this
    var applyMoveInFileList = that.data.applyMoveInFileList
    wx.showLoading({
      title: '上传中',
    })
    wx.uploadFile({
      url: app.DOMAIN_NAME + '/dfs/upload?originalFilename=' + data[0].name,
      header: {
        'Authorization': 'Bearer ' + app.Getopenid()
      },
      filePath: data[0].path,
      name: 'file',
      success(res) {
        var deta = JSON.parse(res.data)
        if (deta.code == 401) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '好的',
            content: deta.msg || '身份已过期,需重登录',
            success: res => {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          });
        } else if (deta.code != 0) {
          wx.hideLoading()
          wx.showModal({
            confirmText: '好的',
            content: deta.msg || '服务器开小差去了，请重试',
            showCancel: false
          });
        } else if (deta.code == 0) {
          var first = deta.fileName.lastIndexOf("."); //取到文件名开始到最后一个点的长度
          var namelength = deta.fileName.length; //取到文件名长度
          var filesuffix = deta.fileName.substring(first + 1, namelength); //截取获得后缀名
          var type = ''
          switch (filesuffix) {
            case 'jpg' || 'png':
              type = 'jpg'
              break;
            case 'xls' || 'xlsx':
              type = 'xlsx'
              break;
            case 'doc' || 'docx' || 'dotx':
              type = 'word'
              break;
            case 'ppt' || 'pptx':
              type = 'ppt'
              break;
            default:
              type = '未知文件'
              break;
          }
          applyMoveInFileList.push({
            fileName: deta.originalFilename != "undefined" ? deta.originalFilename : deta.fileName,
            filePath: deta.fileName,
            fileSize: (data[0].size * 0.001).toFixed(2) + 'KB',
            type
          })
          that.setData({
            applyMoveInFileList
          })
          wx.hideLoading()
        }
      }
    })
  },

  delFile(e) {
    var index = e.currentTarget.dataset.index,
      applyMoveInFileList = this.data.applyMoveInFileList
    applyMoveInFileList.splice(index, 1)
    this.setData({
      applyMoveInFileList
    })
  },

  // 与申请人关系
  addressRadio(e) {
    this.setData({
      addressIndex: e.currentTarget.dataset.idx
    })
  },

  // 申请业务
  typeRadio: function (e) {
    this.setData({
      typeIndex: e.currentTarget.dataset.idx
    })
  },

  // 期望入驻开始时间选择
  startDateChange(e) {
    this.setData({
      startDate: e.detail.value
    })
  },

  // 期望入驻结束时间选择
  endDateChange(e) {
    this.setData({
      endDate: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    var time = util.formatDate(new Date())
    this.setData({
      startDate: time,
      endDate: time
    })
    if (e.id) {
      this.setData({
        id: e.id
      })
      this.getList(e.id)
    }
  },

  getList(e) {
    var that = this
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/applyMoveIn/get/' + e, {}, function (res) {
      if (res.code == 0) {
        var applyMoveInFileList = res.data.applyMoveInFileList
        applyMoveInFileList.forEach(item => {
          var first = item.fileName.lastIndexOf("."); //取到文件名开始到最后一个点的长度
          var namelength = item.fileName.length; //取到文件名长度
          var filesuffix = item.fileName.substring(first + 1, namelength); //截取获得后缀名
          switch (filesuffix) {
            case 'jpg' || 'png':
              item.type = 'jpg'
              break;
            case 'xls' || 'xlsx':
              item.type = 'xlsx'
              break;
            case 'doc' || 'docx' || 'dotx':
              item.type = 'word'
              break;
            case 'ppt' || 'pptx':
              item.type = 'ppt'
              break;
            default:
              item.type = '未知文件'
              break;
          }
        })
        that.setData({
          list: res.data,
          applyMoveInFileList,
          addressIndex: res.data.relation,
          typeIndex: res.data.type,
          startDate: res.data.rentStartTime,
          endDate: res.data.rentEndTime
        })
      }
    })
  },

  showToast(e) {
    wx.showToast({
      title: e,
      icon: 'none'
    })
  },

  fromsumb(e) {
    var that = this,
      regMobile = /^[1]([3-9])[0-9]{9}$/,
      deta = e.detail.value
    deta.type = that.data.typeIndex
    deta.relation = that.data.addressIndex
    deta.applyMoveInFileList = that.data.applyMoveInFileList
    deta.rentStartTime = that.data.startDate
    deta.rentEndTime = that.data.endDate
    if (!deta.applyName) {
      that.showToast('请输入申请企业名称')
      return false
    }
    if (!deta.address) {
      that.showToast('请输入承租地址')
      return false
    }
    if (!deta.area) {
      that.showToast('请输入租赁面积')
      return false
    }
    if (!deta.userName) {
      that.showToast('请输入联系人姓名')
      return false
    }
    if (!deta.phone) {
      that.showToast('请输入联系电话')
      return false
    }
    if (!regMobile.test(deta.phone)) {
      that.showToast('联系电话格式错误')
      return false
    }
    if (!deta.contractNo) {
      that.showToast('请输入场地服务合同编号')
      return false
    }
    if (!deta.companyName) {
      that.showToast('请输入拟成立/迁入企业名称')
      return false
    }
    if (!deta.promise) {
      that.showToast('请输入申请人承诺')
      return false
    }
    app.AjaxRequest('POST', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/applyMoveIn/save', deta, function (res) {
      if (res.code == 0) {
        wx.showModal({
          title: '提示',
          content: '已提交成功',
          success(res) {
            wx.reLaunch({
              url: '/pages/my/serviceApply/serviceApply',
            })
          }
        })
      }
    })
  }
})