const util = require("../../../utils/util");
const api = require('../../../api/api.js');
const check = require('../../../utils/check.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    operType:1,//1核酸2抗原
    anicResult:'阴性',
    checkDate:'',
    files: [],
    picUrls: null,
    hasPicture: false,
    list: [],
    bbt: '',
    canAttendance:1,
  },
  bindBBT(e) {
    this.setData({
      bbt: e.detail.value
    })
  },
  radioAcceptChange(e){
    this.setData({
      canAttendance:e.detail.value
    });
    },
  radioTypeChange(e){
  this.setData({
    operType:e.detail.value
  });
  },
  checkDateChange(e){
    this.setData({
      checkDate:e.detail.value
    });
  },
  anicResultChange(e){
    this.setData({
      anicResult:e.detail.value
    });
    },
  chooseImage: function (e) {
    if (this.data.bbt == '') {
      util.showErrorToast('请先输入体温')
      return false;
    }
    if(this.data.bbt>42){
      util.showErrorToast('请输入正确体温')
      return false;
    }
    if(this.data.operType==2&&this.data.checkDate==''){
      util.showErrorToast('请选择检测日期')
      return false;
    }
    if(this.data.canAttendance==null){
      util.showErrorToast('请选择是否能接单')
      return false;
    }
    if (this.data.files.length >= 5) {
      util.showErrorToast('只能上传五张图片')
      return false;
    }
   
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        console.info(res.tempFilePaths[0])
        that.upload(res)
      }
    })
  },
  upload: function (res) {
    wx.showLoading({
      title: '识别中',
    })
    var that = this;
    console.log(that.data);
    const uploadTask = wx.uploadFile({
      url: api.NucleicOCR(),
      filePath: res.tempFilePaths[0],
      name: 'file',
      formData: {
        bbt: that.data.bbt,
        operType:that.data.operType,//1核酸2抗原
        anicResult:that.data.operType==1?'':that.data.anicResult,
        checkDate:that.data.operType==1?'':that.data.checkDate,
        canAttendance:that.data.canAttendance

      },
      header: {
        'Content-Type': 'multipart/form-data',
        'X-JZ-Token': wx.getStorageSync('token')
      },
      success: function (res) {
        wx.hideLoading();
        var _res = JSON.parse(res.data);
        console.info(_res)
        if (_res.errno === 0) {
          wx.showToast({
            title: '提交成功',
            duration: 3000
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            });
          }, 3000);
        } else {
          wx.showModal({
            title: '错误',
            content: _res.errmsg,
            showCancel: false
          })
        }
      },
      fail: function (e) {
        wx.hideLoading();
        wx.showModal({
          title: '错误',
          content: '上传失败',
          showCancel: false
        })
      },
    })

    uploadTask.onProgressUpdate((res) => {
      console.log('上传进度', res.progress)
      console.log('已经上传的数据长度', res.totalBytesSent)
      console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
    })

  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  //预览图片
  previewSampleImage: function (e) {
    var current = e.target.dataset.src;
    var imgList = [];
    imgList.push(current);
    wx.previewImage({
      current: current, //当前点击的图片链接
      urls: imgList //图片数组
    })
  },
  bindMobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  getList() {
    let that = this;
    if (that.data.mobile == '') return;

    wx.showLoading({
      title: '获取中',
    });
    util.request(api.ocrList, {
      phone: this.data.mobile
    }).then(function (res) {
      console.info(res)
      wx.hideLoading();
      if (res.errno == 0) {
        that.setData({
          list: res.data
        });
      } else {
        wx.showToast({
          title: res.errmsg,
          duration: 5000
        })
      }
    }).catch(res => {
      wx.hideLoading();
      util.showErrorToast(res)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})