const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
// components/face_verify/face_verify.js
import international from "../../international/appointment_scence/index";
import { foreignAuthentication } from "../../utils/api/make_appointment";
const face = require("../../utils/faceCheck/faceCheck");
const {
  throttle
} = require("/utils/throttle/throttle")
// const getThree = require("/utils/getThree/zfb_getThree")
// const getPhone = require("../../utils/getPhone/wx_getPhone");
var verify = require("../../utils/util/util");
const util = require("../../utils/func/request");
const {
  isCardID,
  isPhone,
  isChineseWord,
  desensitization,
  isCornet
} = require("../../utils/util/util");
const app = getApp();
Component({
  options: {
    observers: true,
    lifetimes: false
  },
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      value: "人员信息"
    },
    language: {
      type: String,
      value: "ch"
    },
    isDisabled: {
      type: Boolean,
      value: false
    },
    userInfo: {
      type: Object
    },
    companyName: {
      value: null
    },
    type: {
      value: null
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    international: international,
    //国际化语言包
    username: "",
    idNumber: "",
    phone: "",
    userId: null,
    avatar: null
  },
  /**
   * 组件的方法列表
   */
  methods: {
    authResult(e) {
      if (app.data.language === "ch") {
        this.setData({
          username: e.detail.username,
          idNumber: e.detail.idNumber,
          phone: e.detail.phone,
          avatar: e.detail.avatar,
          userId: e.detail.userId
        })
      } else {
        this.setData({
          phone: e.detail.phone
        })
      }
      setTimeout(() => {
        this.Certification()
      }, 800)
    },
    goOCR() {
      let {
        username,
        idNumber,
        phone
      } = this.data;
      if (phone) {
        this.data.flag = 2;
        app.data.phone = phone;
        wx.navigateTo({
          url: "/pages/ocr/ocr"
        });
      } else {
        wx.showToast({
          icon: "none",
          title: "Please obtain the phone number first"
        });
      }
    },
    /// 认证
    Certification() {
      let {
        username,
        idNumber,
        phone
      } = this.data;
      if (app.data.language === "en") {
        this.goOCR()
      } else {
        if (username && idNumber && phone) {
          this.submit();
        } else {
          wx.showToast({
            icon: "none",
            title: "请将个人信息填写完整！"
          });
        }
      }
    },
    //实名认证
    submit: throttle(function () {
      this.doZFBFaceCheck()
    }, 5000),
    // 微信验证人脸
    async doFaceCheck() {
      let taskId = wx.getStorageSync("wx_dateCode");
      // taskId = '00-883306131355291200';//本地
      if (!taskId) return;
      let res = await face.getFace_wxrlxf("/api/next/doQueryFaceSuccess", "get", {
        taskId: taskId
      });
      if (res.data.status === true) {
        let userInfo = res.data;
        wx.setStorageSync("userInfo", userInfo);
        app.data.userInfo = userInfo;
        this.triggerEvent("certificationState", true);
      } else {
        this.triggerEvent("certificationState", false);
      }
    },
    //获得姓名
    getInputName(e) {
      this.setData({
          username: e.detail
      })
  },
    //获得身份证号
    getInputIdcard(e) {
      this.setData({
          idNumber: e.detail
      })
    },
    // 输入短号
    getInputPhone(e) {
      this.setData({
          phone: e.detail
      })
    },
    // 支付宝验证人脸
    async doZFBFaceCheck() {
      let {
        username,
        idNumber,
        phone,
        userId
      } = this.data;
      let res = await face.getFace_wxrlxf("/api/next/ZFBConfirmFace", "post", {
        idNumber,
        phone,
        username,
        userId
      });
      if (res.data.status === true) {
        let userInfo = res.data;
        userInfo.avatar = this.data.avatar || null
        app.data.userInfo = userInfo
        // iotVspUniqueId(userInfo.idNumber, userInfo.username, userInfo.phone, userInfo.avatar).then((res) => {
        //   if(res) {
        //     this.setCertificationState(true)
        //   }
        // })
        this.setCertificationState(true)
      } else {
        this.setCertificationState(false)
      }
    },
    // 设置授权状态
    setCertificationState(isSuccess) {
      if(isSuccess) {
        wx.setStorageSync("userInfo", app.data.userInfo)
        this.triggerEvent("certificationState", true)
      } else {
        app.data.userInfo = null
        this.triggerEvent("certificationState", false)
      }
    },
    // 外宾确认或修改
    update() {
      let data = {
        username: this.data.username,
        idNumber: this.data.idNumber,
        phone: this.data.phone || app.data.phone,
        avatar: this.data.userInfo.avatar,
        userId: this.data.userInfo.userId
      };
      if (data.idNumber && data.phone && data.username) {
        foreignAuthentication(data).then(res => {
          if (res.code === 200) {
            wx.setStorageSync("userInfo", res.data);
            app.data.userInfo = res.data;
            wx.setStorageSync("language", "cn");
            this.triggerEvent("certificationState", true);
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg
            });
          }
        });
      } else {
        wx.showToast({
          icon: "none",
          title: "Please complete your personal information!"
        });
      }
    },
    getAuthCode: function () {
      const that = this
      getThree((userInfo) => {
        if (app.data.language === "ch") {
          this.setData({
            username: userInfo.data.username,
            idNumber: userInfo.data.idNumber,
            phone: userInfo.data.phone,
            avatar: userInfo.data.avatar,
            userId: userInfo.data.userId
          })
        } else {
          this.setData({
            phone: userInfo.data.phone
          })
        }
        this.Certification()
      })
    },
    antmoveAction: function () {
      //执行时动态赋值，请勿删除
    }
  },
  observers: {
    userInfo: function (userInfo) {
      if (userInfo) {
        this.setData({
          username: userInfo.username,
          idNumber: userInfo.idNumber,
          phone: userInfo.phone,
          userId: userInfo.userId,
          avatar: userInfo.avatar
        });
      }
    }
  },
  lifetimes: {
    attached: function () {
      let userInfo = this.data.userInfo;
      if (userInfo) {
        this.setData({
          username: userInfo.username,
          idNumber: userInfo.idNumber,
          phone: userInfo.phone,
          userId: userInfo.userId
        });
      }
    }
  },
  pageLifetimes: {
    show() {
      if (this.data.flag === 1) {
        this.data.flag = null;
        // this.doFaceCheck();
      } else if (this.data.flag === 2) {
        this.data.flag = null;
        this.triggerEvent("updateState", app.data.temporary_userInfo ? true : false);
      }
    }
  }
  // rootEvents: {
  //   onReady() {
  //     const that = this
  //     iotVspInit((res) => {
  //       that.setCertificationState(res.success)
  //       my.navigateBack()
  //     })
  //   }
  // }
})