<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>

export default {
  props: {
    formData: {
      type: Object,
      default () {
        return {}
      }
    },
    comName: {
      type: String,
      default: 'COM3'
    }
  },
  data () {
    return {
      iccardBase: 'http://127.0.0.1:24010/ZKIDROnline/MFCard?OP-DEV=1&CMD-URL=1&iPort=',
      active: false
    }
  },
  methods: {
    startRead () {
      this.active = true
      this.getDevice()
    },
    getDevice () {
      let url = this.iccardBase + this.comName[3]
      console.log(url)
      this.$http.post(url).then(res => {
        console.log(res.data)
        // let data = typeof res.data === 'string' ? JSON.parse(res.data.replace(/\r|\n|\t/g, '').replace(/\\/g, '\\\\')) : res.data
        // console.log(data)
        switch (res.data.ret) {
          case 0:
            this.$message.success('读取成功！')
            this.$emit('success', res.data.data)
            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>
