<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.id_prestamo) {
        if (typeof decodedData.id_prestamo === 'number')
          this.idPrestamo = decodedData.id_prestamo.toString()
        else this.idPrestamo = decodedData.id_prestamo
      }
      if (this.idPrestamo)
        this.prestamoIdPrestamo(this.idPrestamo, this.resetear)
    },
  },
}
</script>

<style></style>
