// pages/admin/exam/u'p.js
const api = require("../../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    exam:0,
    qdimgs: [],
    xcimgs: [],
    upqdimgs:[],
    upxcimgs:[]
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        console.log(e.currentTarget.dataset.type)
        if(e.currentTarget.dataset.type == 'qdimgs'){
          that.setData({
            qdimgs: that.data.qdimgs.concat(res.tempFilePaths)
          });
        }else{
          that.setData({
            xcimgs: that.data.xcimgs.concat(res.tempFilePaths)
          });
        }
        
      }
    })
  },
  previewImage: function (e) {
    if(e.currentTarget.dataset.type=='qdimgs'){
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.qdimgs // 需要预览的图片http链接列表
    })
  }else{
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.xcimgs // 需要预览的图片http链接列表
    })
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.exam = options.id
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  deleteImg: function(e){
    var that = this
    wx.showModal({
      content: '确认删除该图片?',
      success (res) {
        if (res.confirm) {
          if(e.currentTarget.dataset.type=='qdimgs'){
            var qdimgs = that.data.qdimgs
            qdimgs.splice(e.currentTarget.dataset.index, 1);
            that.setData({
              qdimgs:qdimgs
            })
          }else{
            var xcimgs = that.data.xcimgs
            xcimgs.splice(e.currentTarget.dataset.index, 1);
            that.setData({
              xcimgs:xcimgs
            })
          }
        } 
      }
    })
  },
  confirm: function(){
    if(this.data.upqdimgs.length>0 && this.data.upxcimgs.length>0){
      let formdata={
        qdimgs:this.data.upqdimgs,
        xcimgs:this.data.upxcimgs
      }
      let id = this.data.exam
      wx.showLoading({
        title: '正在提交...',
      })
      api.requesta(`/examtest/exam/${id}/upimgs/`,'POST', formdata).then(res=>{
        wx.hideLoading({
          success: (res) => {},
        })
        wx.navigateBack({
          delta: 0,
        })
      })
    }
    else if(this.data.qdimgs.length>0 && this.data.xcimgs.length>0){
      this.upImgx(0, 'qdimgs')
    }
    else{
      wx.showToast({
        title: '图片信息不全',
      })
    }
  },
  upImgx: function(index, type){
    var that = this
    if(type=='qdimgs'){
      let x = index+1
      wx.showLoading({
        title: '正在上传签到图片',
      })
      wx.uploadFile({
        filePath: that.data.qdimgs[index],
        name: 'file',
        url: getApp().globalData.host+'/uploadfile/',
        header:{
          'Authorization': 'JWT ' + getApp().globalData.admintoken
        },
        success (res){
          wx.hideLoading()
          let data = JSON.parse(res.data);
          that.data.upqdimgs.push(data.data.path)
          if(x == that.data.qdimgs.length){
            that.upImgx(0, 'xcimgs')
          }else{
            that.upImgx(index+1, 'qdimgs')
          }
        }
      })
    }else{
      let x = index+1
      wx.showLoading({
        title: '正在上传现场图片',
      })
      wx.uploadFile({
        filePath: that.data.xcimgs[index],
        name: 'file',
        url: getApp().globalData.host+'/uploadfile/',
        header:{
          'Authorization': 'JWT ' + getApp().globalData.admintoken
        },
        success (res){
          wx.hideLoading()
          let data = JSON.parse(res.data);
          that.data.upxcimgs.push(data.data.path)
          if(x == that.data.xcimgs.length){
            that.confirm()
          }else{
            that.upImgx(index+1, 'xcimgs')
          }
        }
      })
    }
    
  }
})