const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
// pages/ocr/ocr.js
const tip_en = require("../../international/ocr_scence/index");
const app = getApp();
const upload_image = require("../../utils/upload/upload_image");
const {
  getPassportDetail
} = require("../../utils/api/make_appointment");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 中英文
    isChinese: true,
    // 翻译包
    tip: tip_en,
    // 证件类型
    type_list: [{
      name_ch: "护照",
      name_en: "Click here",
      type: 13
    }
    //   {name_ch: '往来港澳通行证', name_en: 'Hong Kong and Macao Travel Permit', type: 22},
    //   {name_ch: '往来台湾通行证', name_en: 'Taiwan Travel Permit', type: 29},
    //   {name_ch: '香港居民身份证', name_en: 'Hong Kong Identity Card', type: 1001},
    ],

    type_index: 0,
    type: 13
  },
  // 选择证件类型
  chooseType(e) {
    this.setData({
      type_index: e.detail.value,
      type: this.data.type_list[e.detail.value].type
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.data.language == "ch") {
      this.setData({
        isChinese: true
      });
    } else {
      this.setData({
        isChinese: false
      });
    }
    // （过审专用，禁止删除）- start
    app.data.fake = false;
    // （过审专用，禁止删除）- end
  },

  // 护照验证
  doOCR() {
    var that = this;
    if (!this.data.type) {
      wx.showToast({
        title: this.data.isChinese ? "请选择证件类型~" : "Please select the id type~",
        icon: "none"
      });
      return;
    }
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        var image = {
          url: res.tempFilePaths[0]
        };
        wx.showLoading({
          title: that.data.isChinese ? "上传中~" : "uploading..."
        });
        upload_image(image).then(img => {
          wx.hideLoading();
          var data = {
            passportUrl: img.url,
            type: that.data.type
          };
          wx.showLoading({
            title: that.data.isChinese ? "识别中..." : "recognizing.."
          });
          getPassportDetail(data).then(result => {
            if (result.code == 200) {
              wx.hideLoading();
              result.passportImage = img.url;
              let userInfo = {
                username: result.data.name,
                avatar: result.data.avatar,
                idNumber: result.data.passportCode,
                phone: app.data.phone
              };
              app.data.temporary_userInfo = userInfo;
              wx.showToast({
                title: that.data.isChinese ? "识别成功" : "recognize success"
              });
              wx.navigateTo({
                url: "/pages/faceCheck/faceCheck"
              });
            } else {
              wx.hideLoading();
              wx.showToast({
                title: res.msg
              });
            }
          }).catch(err => {
            wx.hideLoading();
          });
        }).catch(() => {
          wx.hideLoading();
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});