<template>
  <div>
    <div @click="cardFun"></div>
    <div @click="addFingerprintFun"></div>
    <div @click="writeCard"></div>
  </div>
</template>
<style scoped>
</style>
<script>
  import Payload from "../common/plugin/websocket/payload";
  import Packet from "../common/plugin/websocket/packet";
  import Util from "../common/plugin/websocket/util.js";
  var Payload1 = new Payload();
  export default {
    name: "zw",
    data() {
      return {};
    },
    created() {

    },
    methods: {
    initSubscribe(zl, callback) {
      this.sockets.subscribe("connect", () => {});
      this.sockets.subscribe("message", (data) => {
        const data_recv = new Packet().fromBytes(new Uint8Array(data));
        const response = Payload.response(data_recv);
        if (response.cmd === 0x8013) {
          console.log(response.currRecord);
          if (response.status === 0) {
            switch (response.currRecord) {
              case 1:
                this.$message.success("第一次录入,请抬起手指再次按压");
                // this.alertMessage("第一次录入,请抬起手指再次按压")
                break;
              case 2:
              this.$message.success("第二次录入,请抬起手指再次按压");
                // this.alertMessage("第二次录入,请抬起手指再次按压")
                break;
              case 3:
              this.$message.success("第三次录入,请抬起手指再次按压");
                // this.alertMessage("第三次录入,请抬起手指再次按压")
                break;
              case 4:
              this.$message.success("第四次录入,读取指纹模版");
                // this.alertMessage("第四次录入,读取指纹模版")
                this.readFingerPrint();
                break;
              default:
                break;
            }
          } else {
              this.$message.error("录入失败,错误码：" + response.status);
          }
        } else if (response.cmd === 0x8014) {
          if (response.status === 0) {
            if (
              response.mode.length === 2048 || response.mode.length === 1200
            ) {
              this.$message.success("录入成功");
              let hex = Buffer.from(response.mode).toString("hex");
              callback(hex)
            } else {
              this.$message.error("该指纹有误，请重试");
            }
          } else {
            this.$message.error("读取失败,错误码：" + response.status);
          }
        } else {
            this.$message.error("录入失败" );
        }
      });
    },
    async alertMessage(text){
      var str=await this.$message.success(text);
      str.close()
    },
    readFingerPrint() {
      const payload = Payload.request(0x8014);
      const packet = new Packet();
      this.$vueSocket.send(packet.toBytes(payload));
    },
      // 添加指纹
      addFingerprintFun(zl, callback) {
      const payload = Payload.request(0x8013);
      const packet = new Packet();
      this.$vueSocket.send(packet.toBytes(payload));
      this.$message.success("请在采集器上按压手指");
      this.initSubscribe('',function(data){
        callback(data)
      })
      },
      // 写卡
      writeCard(zl,cardNo,startTime,endTime,useCountLimit,privateKey,keyType, callback){
        var connect = this.$vueSocket.isConnected();
        console.log(connect)
        if (connect) {
          var data={
            keyType:keyType,
            cardNo:cardNo,
            startTime:startTime,
            endTime:endTime,
            useCountLimit:useCountLimit,
            index:"00",
            isLine:"00",
            privateKey: Util.hexStr2Byte(privateKey)
          }
          const payload = Payload.request(zl,data);
          const packet = new Packet();
          this.$vueSocket.send(packet.toBytes(payload));
          this.sockets.subscribe("message", data => {
            const data_recv = new Packet().fromBytes(new Uint8Array(data));
            const response = Payload.response(data_recv);
            callback(response);
            this.sockets.unsubscribe("message");
            this.sockets.unsubscribe("connect");
            this.$vueSocket.disconnect();
          });
        } else {
          var data={
            keyType:keyType,
            cardNo:cardNo,
            startTime:startTime,
            endTime:endTime,
            useCountLimit:useCountLimit,
            index:"00",
            isLine:"00",
            privateKey: Util.hexStr2Byte(privateKey)
          }
          this.$vueSocket.reconnect();
          this.sockets.subscribe("connect", () => {
            const payload = Payload.request(zl,data);
            const packet = new Packet();
            this.$vueSocket.send(packet.toBytes(payload));
            this.sockets.subscribe("message", data => {
              const data_recv = new Packet().fromBytes(new Uint8Array(data));
              const response = Payload.response(data_recv);
              callback(response);
              this.sockets.unsubscribe("message");
              this.sockets.unsubscribe("connect");
              this.$vueSocket.disconnect();
            });
          });
        }
      },
      // 添加门卡
      cardFun(zl, callback) {
        var connect = this.$vueSocket.isConnected();
        if (connect) {
          const payload = Payload.request(zl);
          const packet = new Packet();
          this.$vueSocket.send(packet.toBytes(payload));
          this.sockets.subscribe("message", data => {
            const data_recv = new Packet().fromBytes(new Uint8Array(data));
            const response = Payload.response(data_recv);
            callback(response);
            this.sockets.unsubscribe("message");
            this.sockets.unsubscribe("connect");
            this.$vueSocket.disconnect();
          });
        } else {
          this.$vueSocket.reconnect();
          this.sockets.subscribe("connect", () => {
            const payload = Payload.request(zl);
            const packet = new Packet();
            this.$vueSocket.send(packet.toBytes(payload));
            this.sockets.subscribe("message", data => {
              const data_recv = new Packet().fromBytes(new Uint8Array(data));
              const response = Payload.response(data_recv);
              callback(response);
              this.sockets.unsubscribe("message");
              this.sockets.unsubscribe("connect");
              this.$vueSocket.disconnect();
            });
          });
        }
      }
    },
    components: {}
  };
</script>
