<template>
  <QrcodeStream @decode="onDecode"> </QrcodeStream>
</template>

<script>
import { QrcodeStream } from 'vue-qrcode-reader'

export default {
  components: { QrcodeStream },
  props: {
    prestamoIdPrestamo: { type: Function, required: true },
  },
  data() {
    return { idPrestamo: '' }
  },
  methods: {
    resetear() {
      if (this.idPrestamo) this.idPrestamo = ''
    },
    onDecode(decodedString) {
      let decodedData = {}

      try {
        decodedData = JSON.parse(decodedString)
      } catch (err) {
        this.resetear()
      }
      if (decodedData.idPrestamo) {
        if (typeof decodedData.idPrestamo === 'number')
          this.idPrestamo = decodedData.idPrestamo.toString()
        else this.idPrestamo = decodedData.idPrestamo
      }
      if (this.idPrestamo)
        this.prestamoIdPrestamo(this.idPrestamo, this.resetear)
    },
  },
}
</script>

<style></style>
