var app = getApp();
Page({
  data: {
    name:'',
    xuehao:'',
    school:'',
    time:'',
    imgs:[],
    imgarr:[],
    index:0
  },
  onLoad: function (options) {

  },
  select:function(e){
    this.setData({index:e.currentTarget.dataset.index})
  },
  name:function(e){
    this.setData({name:e.detail.value})
  },
  xuehao:function(e){
    this.setData({xuehao:e.detail.value})
  },
  school:function(e){
    this.setData({school:e.detail.value})
  },
  time:function(e){
    this.setData({time:e.detail.value})
  },
  submit:function(){
    var that = this;
    console.log(that.data.imgarr)
    var nameReg = /^[\u4E00-\u9FA5]{2,4}$/;
    var reg=/^[\u0391-\uFFE5]+$/;   
    var dateFormat =/^(\d{4})-(\d{2})$/;
    if(!that.data.name){
      wx.showToast({
        title: '请输入姓名',
        icon:'none'
      })
      return;
    }else if(!nameReg.test(that.data.name)){
      wx.showToast({
        title: '请输入正确姓名',
        icon:'none'
      })
    } else if(!that.data.xuehao){
      wx.showToast({
        title: that.data.index==0 ? '请输入学号':'请输入工号',
        icon:'none'
      })
      return;
    }else if(!that.data.school && that.data.index==0){
      wx.showToast({
        title: '请输入学校',
        icon:'none'
      })
      return;
    }else if(!reg.test(that.data.school) && that.data.index==0){
      wx.showToast({
        title: '请输入正确学校名称',
        icon:'none'
      })
      return;
    }else if(!that.data.time && that.data.index==0){
      wx.showToast({
        title: '请输入入学时间',
        icon:'none'
      })
      return;
    }else if(!dateFormat.test(that.data.time) && that.data.index==0){
      wx.showToast({
        title: '入学时间输入有误',
        icon:'none'
      })
      return;
    }else if(that.data.imgarr.length<1){
      wx.showToast({
        title: '上传证件照片',
        icon:'none'
      })
      return;
    }else{
      var data = {
        'name':that.data.name,
        'no':that.data.xuehao,
        'type':that.data.index,
        'year':that.data.time,
        'imgs': that.data.imgarr,
        'school':that.data.school
      }
      console.log(data)
      app.request('/user/student', data, 'POST').then(res => {
        console.log(res)
        if(res.statusCode==200){
          wx.navigateTo({
            url: '../student_success/student_success',
          })
        }
      })
    }
  },
  upload:function(){
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          imgs: tempFilePaths
        })
        that.uploadimg({
          url: app.globalData.url + '/upload/image',
          path: tempFilePaths
        });
      }
    })
  },
  xieyi:function(){
    wx.navigateTo({
      url: '/pages/personal/privacy/privacy',
    })
  },
  uploadimg: function (data) {
    wx.showLoading({
      title: '上传中...',
      mask:true
    })
    var that = this,
      i = data.i ? data.i : 0, //当前上传的哪张图片
      success = data.success ? data.success : 0, //上传成功的个数
      fail = data.fail ? data.fail : 0; //上传失败的个数
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Authorization': wx.getStorageSync('token'),
        'merchant-id': app.globalData.merchant_id
      },
      name: 'file', //这里根据自己的实际情况改
      formData: {
        type: 'auth'
      }, //这里是上传图片时一起上传的数据
      success: (resp) => {
        success++; //图片上传成功，图片上传成功的变量+1
        console.log(resp)
        var data = JSON.parse(resp.data)
        // var photo1 = [];
        // photo1.push(data.id)
        that.setData({
          imgarr: data.id
        })
        wx.hideLoading()
        //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          
          //console.log('执行完毕');
          //console.log('成功：' + success + " 失败：" + fail);
        } else { //若图片还没有传完，则继续调用函数
          //console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.uploadimg(data);
        }
      }
    });
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})