const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

//获取应用实例
const app = getApp();
Page({
  data: {
    // text:"这是一个页面"
    videoSource: '',
    videoHidden: true,
    cameraCtx: null,
    src: null,
    videoSrc: null,
    isStartDisable: null,
    isEndDisable: null
  },

  /**
* 生命周期函数--监听页面初次渲染完成
*/
  onReady: function (res) {
    if (wx.createCameraContext()) {
      // this.cameraContext = wx.createCameraContext('myCamera')
      // this.takePhoto();

      this.cameraContext = wx.createCameraContext()
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  takePhoto() {
    var that = this;
    const ctx = wx.createCameraContext()
    console.log(ctx)
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        that.setData({
          src: res.tempImagePath
        })
      },
      fail: (res) => {
        console.log('拍摄失败')
        console.log(res)
      },
      complete: (res) => {
        console.log('接口调用结束的回调函数（调用成功、失败都会执行）')
        console.log(res)
      }
    })
  },
  takeVideo() {
    wx.showLoading({
      title: '视频录制开始...',
    });
    var that = this;
    // const ctx = wx.createCameraContext()
    console.log(that.cameraContext)
    that.cameraContext.startRecord({
      timeoutCallback: (res) => {
        console.log('摄影开始中1')
        console.log(res)
        that.setData({
          isStartDisable: true,
          isEndDisable: false
        })
      },
      success: (res) => {
        console.log('摄影开始成功2')
        console.log(res)
        that.setData({
          isStartDisable: true,
          isEndDisable: false
        })
      },
      fail: (res) => {
        console.log('摄影开始失败')
        console.log(res)
        that.setData({
          isStartDisable: false,
          isEndDisable: false
        })
      },
      complete: (res) => {
        console.log('摄影开始，接口调用结束的回调函数（调用成功、失败都会执行）')
        console.log(res)
      }
    })
  },
  stopVideo: function () {
    wx.hideLoading();
    var that = this;
    that.cameraContext.stopRecord({
      success: (videoRes) => {
        console.log('摄影成功2')
        console.log(videoRes.tempVideoPath)
        that.setData({
          isStartDisable: true,
          isEndDisable: true,
          videoSrc: videoRes.tempVideoPath
        })
        that.getBase64(videoRes.tempVideoPath);
        // util.request(api.CheckLivenessRecognition, {
        //   storeId: wx.getStorageSync('storeId'),
        //   merchId: wx.getStorageSync('merchSn'),
        //   idCard: '',
        //   name: '',
        //   videoBase64: 'base64',
        //   livenessType: 'SILENT'
        // }, 'POST').then(function (urlRes) {
        //   console.log(urlRes);
        //   // wx.showToast({
        //   //   title: '成功',
        //   //   icon: 'success',
        //   //   duration: 2000
        //   // })
        //   wx.showToast({
        //     title: urlRes.msg,
        //     icon: 'success'
        //   })
        // });
      },
      fail: (videoFailRes) => {
        console.log('摄影失败')
        console.log(videoFailRes)
        that.setData({
          isStartDisable: false,
          isEndDisable: false
        })
      },
      complete: (videoComRes) => {
        console.log('接口调用结束的回调函数（调用成功、失败都会执行）')
        console.log(videoComRes)
      }
    });
  },
  getBase64(path) {
    wx.showLoading({
      title: '人脸身份核验中...',
    });
    var that = this;
    wx.getFileSystemManager().readFile({
      filePath: path,
      encoding: "base64",
      complete: res => {
        // var baseRes = wx.getFileSystemManager().readFileSync(videoRes.tempVideoPath, 'base64')
        console.log('base64码');
        // console.log(res);
        var base64 = res.data;
        util.request(api.CheckLivenessRecognition, {
          storeId: wx.getStorageSync('storeId'),
          merchId: wx.getStorageSync('merchSn'),
          idCard: '',
          name: '',
          videoBase64: base64,
          livenessType: 'SILENT'
        }, 'POST').then(function (urlRes) {
          console.log(urlRes);
          if(urlRes.errno!=undefined){
            if(urlRes.errno===0){
              wx.showToast({
                title: '人脸核验成功',
                icon: 'success',
                duration: 2000
              })
            }else{
              wx.showToast({
                title: urlRes.errmsg,
                icon: 'none'
              });
            }
          } else {
            if (urlRes.code != 0) {
              wx.showToast({
                title: urlRes.msg,
                icon: 'none'
              });
              that.setData({
                isStartDisable: false,
                isEndDisable: false
              })
            }
          }
        });
      },
      fail: function (baseFailRes) {
        console.log(baseFailRes);
      }
    });
  },
  error(e) {
    console.log(e.detail)
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})