<template>
  <div
    class="readCard"
    :class="{active:active}"
    @click="startRead"
  >
    <div class="inner">
      <span class="wave"></span>
      <!-- <span class="wave wave2"></span> -->

      <svg-icon icon-class="idCard" /> 读取
    </div>
  </div>
</template>

<script>

let readerApi = {
  info: 'http://127.0.0.1:24010/ZKIDROnline/info',
  readId: 'http://127.0.0.1:24010/ZKIDROnline/ScanReadIdCardInfo?OP-DEV=1&CMD-URL=4&REPEAT=1&READTYPE=1'
}

export default {
  props: {
    formData: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      active: false
    }
  },
  methods: {
    startRead () {
      this.active = true
      this.getDevice()
    },
    getDevice () {
      this.$http.post(readerApi.readId).then(res => {
        let data = typeof res.data === 'string' ? JSON.parse(res.data.replace(/\r|\n|\t/g, '').replace(/\\/g, '\\\\')) : res.data
        console.log(data)
        switch (data.ret) {
          case 0:
            this.$message.success('读取成功！')
            this.$emit('success', data.Certificate)
            break
          case 1:
            this.$message.error('阅读器连接失败，请检查设备后重试。')
            break
          case 11:
            this.$message.error('读卡失败。')
        }
        this.active = false
      }).catch(e => {
        e && console.log(e)
        this.$message.error('出错了，请检查设备和驱动')
        this.active = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
