const _my = require("../../../__antmove/api/index.js")(my);
const wx = _my;
const {
  userInfoInsert,
  getAdminConfig
} = require("../../../utils/api/api");
const {
  getAuthFlagByAdminId,
  adminShowCheck
} = require("../../../utils/api/xiaosha");
const {
  weixinTestMakeUserInfo
} = require("../../../utils/testForWeixin/index");
const {
  throttle
} = require("../../../utils/throttle/throttle");
const face = require("../../../utils/faceCheck/faceCheck");
const app = getApp();
// pages/wx_airport/rlxf_pass/rlxf_pass.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    machineInfo: {},
    timeCount: 0,
    functionList: [{
      img: "../../../static/function-bar/one.png",
      tit: "问题上报",
      url: "/pages/wx_airport/problem_report/index",
      isShow: false
    }, {
      img: "../../../static/function-bar/two.png",
      tit: "上报记录",
      url: "/pages/wx_airport/Reported_list/index",
      isShow: false
    }, {
      img: "../../../static/function-bar/three.png",
      tit: "消杀登记",
      url: "/pages/wx_airport/Xiaosha_registration/scan_code/index",
      isShow: false
    }, {
      img: "../../../static/function-bar/four.png",
      tit: "消杀记录",
      url: "/pages/wx_airport/Xiaosha_record/index?checkAuth=",
      isShow: false
    }],
    authFlag: null,
    //获取导航栏权限
    wxLoginType: 1 //微信小程序登录方式1-身份证 2-卡号认证
  },

  // 功能栏跳转
  clickFun(e) {
    let item = e.currentTarget.dataset.item;
    if (item.url === "/pages/wx_airport/Xiaosha_registration/scan_code/index") {
      wx.requestSubscribeMessage({
        tmplIds: ["dsI63TG3MNY5EdOcCjqMyeHXRDf-ZoG9Cx5PNkOkfY8"],
        success: res => {
          if (app.data.disinfectAreaId) {
            wx.navigateTo({
              url: "/pages/wx_airport/Xiaosha_registration/index?disinfectAreaId=" + app.data.disinfectAreaId
            });
          } else {
            wx.navigateTo({
              url: item.url
            });
          }
        }
      });
    } else {
      if (item.url === "/pages/wx_airport/Xiaosha_record/index?checkAuth=") {
        item.url = "/pages/wx_airport/Xiaosha_record/index?checkAuth=" + this.data.authFlag.checkAuth;
      }
      wx.navigateTo({
        url: item.url
      });
    }
  },
  //切换用户
  back() {
    let isMachine = this.data.isMachine;
    wx.showModal({
      title: "提示",
      content: `确定要${isMachine ? "切换用户" : "重新认证"}吗?`,
      success(res) {
        if (res.confirm) {
          if (isMachine) {
            wx.removeStorageSync("machineInfo");
            wx.reLaunch({
              url: "/pages/temporary_prove/index/index"
            });
          } else {
            let options = wx.getStorageSync("userInfo").options;
            wx.removeStorageSync("userInfo");
            wx.reLaunch({
              url: "/pages/wx_airport/rlxf_index/rlxf_index?" + options + "isJump=1"
            });
          }
        }
      }
    });
  },
  //查看头像
  preview() {
    if(!this.data.userInfo.avatar) {
      return
    }
    wx.previewImage({
      current: this.data.userInfo.avatar,
      urls: [this.data.userInfo.avatar]
    });
  },
  //人脸下发
  doPass: throttle(function () {
    var data = {
      userId: this.data.userInfo.userId,
      adminId: this.data.userInfo.orgId,
      type: app.data.type,
      action: 2 //1-自定义字段更新 2-下发
    };
    /**
     * 开始
     * 测试身份证号，若要修改，请联系负责人
     */
    if (this.data.userInfo.idNumber == "330821********0212") {
      wx.showLoading({
        title: "申报中.."
      });
      setTimeout(() => {
        this.data.timeCount = 30;
        this.countDown();
        wx.hideLoading();
        my.alert({
          content: "恭喜申报成功，您已获得闸机快速通行权限，如有疑问，请就近联系管理人员"
        });
      }, 600);
      return;
    }
    /**
     * 结束
     */
    wx.showLoading({
      title: "申报中.."
    });
    userInfoInsert(data).then(res => {
      if (res.code == 200 && res.data.status) {
        this.data.timeCount = 30;
        this.countDown();
        this.data.userInfo.createTime = res.data.date;
        my.alert({
          content: "门禁-" + this.data.userInfo.org + "恭喜申报成功，您已获得闸机快速通行权限，如有疑问，请就近联系管理人员"
        });
      } else {
        this.data.userInfo.createTime = "";
        my.alert({
          content: res.msg
        });
      }
      wx.setStorageSync("userInfo", this.data.userInfo);
      this.getUserInfo();
    }).catch(err => {}).finally(() => {
      wx.hideLoading();
    });
  }, 30000),
  //申报倒计时
  countDown() {
    let {
      timeCount
    } = this.data;
    if (timeCount > 0) {
      setTimeout(() => {
        timeCount--;
        this.setData({
          timeCount
        });
        this.countDown();
      }, 1000);
    }
  },
  //获取基本信息
  getUserInfo(userInfoTest = null) {
    var userInfo = userInfoTest || wx.getStorageSync("userInfo");
    userInfo.idNumber = userInfo.idNumber.replace(/(\w)/g, function (a, b, c, d) {
      return c > 5 && c < 14 ? "*" : a;
    });
    userInfo.phone = userInfo.phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
    userInfo.avatar = userInfo.avatar;
    this.setData({
      userInfo: userInfo
    });
  },
  // 判断消杀
  adminShowCheck() {
    let {
      userInfo,
      functionList
    } = this.data;
    let data = {
      adminId: userInfo.orgId,
      userId: userInfo.userId
    };
    adminShowCheck(data).then(res => {
      // 是否显示开启问题上报 1:显示 0：不显示
      if (res.data.showProblemReport) {
        functionList[0].isShow = true;
        functionList[1].isShow = true;
      }
      // 是否显示消杀 1-显示 0-不显示
      if (res.data.showDisinfect) {
        functionList[2].isShow = true;
        functionList[3].isShow = true;
      }
      this.setData({
        functionList,
        authFlag: res.data
      });
    });
  },
  // 获取账号的配置信息
  getAdminConfig(adminId) {
    getAdminConfig(adminId).then(res => {
      this.setData({
        wxLoginType: res.data.wxLoginType
        // isNeedRealAuthentication: res.data.isNeedRealAuthentication,
      });
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({
    orgId = null,
    org,
    name,
    idNumber,
    phone,
    showModal,
    isMachine
  }) {
    if (orgId != null) {
      this.orgId = orgId;
      this.getUserInfo();
      this.getAdminConfig(orgId);
    } else {
      /**
       * 开始
       * 测试身份证号，若要修改，请联系负责人
       */
      this.getUserInfo(weixinTestMakeUserInfo(name, phone, idNumber));
      /**
       * 结束
       */
    }

    if (showModal === "true") {
      this.doPass();
    }
    this.adminShowCheck();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton();
    this.adminShowCheck();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});