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

  /**
   * 页面的初始数据
   */
  data: {
    sex: ['否', '是'],
    sexindex: 1,
    addressList: ['项目团队孵化', '成立新公司'],
    addressIndex: 0,
    radiolist: ['未注册', '已注册'],
    currentIndex: 0,
    isComplianceList: ['否', '是'],
    isComplianceIndex: 1,
    applyParkFileList: []
  },

  uploFile() {
    var that = this;
    if (that.data.id) return
    if (that.data.applyParkFileList.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) {
    console.log(data)
    var that = this
    var applyParkFileList = that.data.applyParkFileList
    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;
          }
          applyParkFileList.push({
            fileName: deta.originalFilename != "undefined" ? deta.originalFilename : deta.fileName,
            filePath: deta.fileName,
            fileSize: (data[0].size * 0.001).toFixed(2) + 'KB',
            type
          })
          that.setData({
            applyParkFileList
          })
          wx.hideLoading()
        }
      },
      fail() {
        wx.hideLoading()
      }
    })
  },

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

  // 项目入园方式
  addressRadio(e) {
    this.setData({
      addressIndex: e.currentTarget.dataset.idx,
      type: e.currentTarget.dataset.idx
    })
  },

  isComplianceClick(e) {
    this.setData({
      isComplianceIndex: e.currentTarget.dataset.idx,
      isCompliance: e.currentTarget.dataset.idx
    })
  },

  // 工商注册情况
  radioClick(e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.idx,
      isRegister: 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({
      id: e.id,
      time,
      startDate: time,
      endDate: time
    })
    if (e.id) {
      this.getList(e.id)
    }
  },

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

  getList(e) {
    var that = this
    app.AjaxRequest('get', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/applyPark/get/' + e, {}, function (res) {
      if (res.code == 0) {
        var applyParkFileList = res.data.applyParkFileList
        applyParkFileList.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,
          applyParkFileList,
          startDate: res.data.startDate,
          endDate: res.data.endDate,
          isComplianceIndex: res.data.isCompliance,
          addressIndex: res.data.type,
          currentIndex: res.data.isRegister
        })
      }
    })
  },

  fromsumb(e) {
    var that = this,
      regMobile = /^[1]([3-9])[0-9]{9}$/,
      deta = e.detail.value
    deta.isCompliance = that.data.isComplianceIndex
    deta.type = that.data.addressIndex
    deta.isRegister = that.data.currentIndex
    deta.applyParkFileList = that.data.applyParkFileList
    deta.startDate = that.data.startDate
    deta.endDate = that.data.endDate
    console.log(deta)
    if (!deta.name) {
      that.showToast('请输入拟入孵项目名称')
      return false
    }
    if (!deta.operName) {
      that.showToast('请输入拟定公司法人代表')
      return false
    }
    if (!deta.registCapi) {
      that.showToast('请输入拟定注册资本')
      return false
    }
    /*
    if (!deta.econKind) {
      that.showToast('请输入企业性质')
      return false
    }
    if (!deta.nation) {
      that.showToast('请输入国家')
      return false
    }
    */
    if (!deta.userName) {
      that.showToast('请输入联系人姓名')
      return false
    }
    if (!deta.position) {
      that.showToast('请输入联系人职务')
      return false
    }
    if (!deta.phone) {
      that.showToast('请输入联系电话')
      return false
    }
    if (!regMobile.test(deta.phone)) {
      that.showToast('联系电话格式错误')
      return false
    }
    /*
    if (!deta.qq) {
      that.showToast('请输入qq')
      return false
    }
    */
    if (!deta.email) {
      that.showToast('请输入邮箱')
      return false
    }
    /*
    if (!deta.site) {
      that.showToast('请输入网址')
      return false
    }
    if (!deta.persons) {
      that.showToast('请输入公司人数')
      return false
    }
    */
    if (!deta.preYearValue) {
      that.showToast('请输入预估年产值')
      return false
    }
    if (that.data.date == '请选择期望入驻时间') {
      wx.showToast({
        title: '请选择期望入驻时间',
        icon: 'none'
      })
      return false
    }
    if (!deta.applyArea) {
      that.showToast('请输入申请办公面积')
      return false
    }
    if (!deta.profile) {
      that.showToast('请输入项目简介')
      return false
    }
    if (!deta.scope) {
      that.showToast('请输入主营产品')
      return false
    }
    if (!deta.advantage) {
      that.showToast('请输入产品/企业优势')
      return false
    }
      if (!deta.reason) {
      that.showToast('请输入入驻原因')
      return false
    } 
    app.AjaxRequest('POST', {
      'content-type': 'application/json',
      'Authorization': 'Bearer ' + app.Getopenid()
    }, '/applyPark/save', deta, function (res) {
      if (res.code == 0) {
        wx.showModal({
          title: '提示',
          content: '已提交成功',
          success(res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/my/serviceApply/serviceApply',
              })
            }
          }
        })
      }
    })
  }
})