const _my = require("../../__antmove/api/index.js")(my);
const wx = _my;
// pages/regular_visit_details/regular_visit_details.js
const {
  userWhiteHandleVisitList,
  userWhiteGetSnList,
  visitoryDownFaceAgain
} = require("../../utils/api/api");
const app = getApp();
import international from "../../international/appointment_scence/index";
const {
  throttle
} = require("../../utils/throttle/throttle");
import QRCode from "../../utils/util/weapp-qrcode";
import { userCreate } from '/utils/api/zfb_ioTVsp'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    international: international,
    language: app.data.language,
    regularList: null,
    //相关信息
    machineList: [],
    // 通道权限列表--设备列表
    chooseSN: false,
    snListLength: null,
    snList: null,
    isRegular: false,
    // 人员类型列表
    personTypeList: [{
      id: 0,
      name_ch: "访客",
      name_en: "visitors"
    }, {
      id: 1,
      name_ch: "同事",
      name_en: "colleagues"
    }, {
      id: 2,
      name_ch: "亲属",
      name_en: "relatives"
    }],
    personnelType: 0,
    remark: "",
    uniqueId: null,
    //备注
    reviewStatus: 2 //2同意1拒绝
  },

  // 常客获取楼宇的设备列表(sn和name)
  userWhiteGetSnList() {
    let adminInfo = app.data.adminInfo;
    if (adminInfo) {
      userWhiteGetSnList({
        adminId: adminInfo.adminId,
        companyId: adminInfo.companyAdminId
      }).then(res => {
        // res.data.
        this.setData({
          machineList: res.data
        });
      });
    }
  },
  //获取开始时间
  getValue(e) {
    var {
      form
    } = this.data;
    switch (e.currentTarget.dataset.type) {
      case "snList":
        // form.snList = e.detail
        this.setData({
          snList: e.detail,
          snListLength: e.detail.length,
          chooseSN: false
        });
        break;
      case "personnelType":
        this.setData({
          personnelType: e.detail
        });
        break;
      default:
        form[e.currentTarget.dataset.type] = e.detail;
        break;
    }
  },
  // 打开通道权限弹出框
  showSN() {
    this.setData({
      chooseSN: true
    });
  },
  // 选择通过/拒绝
  chooseFun(e) {
    this.setData({
      reviewStatus: e.currentTarget.dataset.type,
      remark: ""
    });
  },
  // 备注
  onInput(e) {
    this.setData({
      remark: e.detail
    });
  },
  handle(e) {
    let {
      snList,
      regularList,
      remark,
      reviewStatus,
      personnelType
    } = this.data;
    wx.requestSubscribeMessage({
      entityIds: ["9d4ddcd78f66444482ad66c5a262ffed"],
      success(res) {
        if(res.behavior === 'cancel') {
          return
        }
        if (snList || reviewStatus == 1) {
          let data = {
            snList: snList || [],
            reviewStatus,
            userVisitorListDetailId: regularList.userVisitorListDetailId,
            personnelType,
            remark
          };
          userWhiteHandleVisitList(data).then(res => {
            if (res.code == 200) {
              my.alert({
                title: "提醒",
                content: "操作成功！",
                success(res) {
                  wx.navigateBack({
                    delta: 1
                  })
                }
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none"
              });
            }
          });
        } else {
          wx.showToast({
            title: "请选择通道权限！",
            icon: "none"
          });
        }
      }
    });
  },
  // 访客重新下发
  visitoryDownFaceAgain: throttle(function () {
    visitoryDownFaceAgain({
      userVisitorListDetailId: this.data.regularList.userVisitorListDetailId
    }).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: "下发成功!",
          icon: "none"
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "none"
        });
      }
    });
  }, 5000),
  // 生成二维码
  creatQrCode(id) {
    // wx.showLoading({
    //     title: '加载中...',
    // })
    let that = this;
    new QRCode("myQrcode", {
      text: `HHFKJ?userId=$123&timestamp=${this.data.dateTime}`,
      width: 176,
      //canvas 画布的宽
      height: 176,
      //canvas 画布的高
      padding: 0,
      // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.L,
      // 二维码可辨识度
      callback: res => {
        //工具回调数据
        // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
        // wx.hideLoading()
        //将图片路劲放入data中，显示在wxml的image标签上
        console.log(res);
        that.setData({
          qrCode: res.path
        });
      }
    });
  },

  // 获取人脸入库的流水号
  getUniqueId() {
    userCreate({
      certType: 'IDENTITY_CARD',
      certNo: app.data.userInfo.idNumber,
      certName: app.data.userInfo.username,
      phone: app.data.userInfo.phone,
      avatar: app.data.userInfo.avatar
    }).then((res) => {
      if(res.data && !res.data.uniqueId) {
        // uniqueId为空，表示已入库
        this.setData({
          uniqueId: null
        })
      } else {
        this.setData({
          uniqueId: res.data.uniqueId
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.creatQrCode()
    if (options && options.isRegular) {
      this.setData({
        isRegular: true
      })
    } else {
      this.getUniqueId()
    }
    this.data.regularList = JSON.parse(options.list)
    this.userWhiteGetSnList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let {
      regularList
    } = this.data;
    this.setData({
      regularList
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      language: app.data.language
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  antmoveAction: function () {
    //执行时动态赋值，请勿删除
  }
});