{% extends "public.html" %} {% block page %}
<div class="row q-col-gutter-md justify-center">
  <div class="col-12 col-md-7 col-lg-6 q-gutter-y-md">
    <q-card class="q-pa-lg">
      <q-card-section class="q-pa-none">
        <h3 class="q-my-none">{{ form_name }}</h3>
        <br />
        <h5 class="q-my-none">{{ form_desc }}</h5>
        <br />
        <q-form @submit="Invoice()" class="q-gutter-md">
          <q-input
            filled
            dense
            v-model.trim="formDialog.data.name"
            type="name"
            label="Your name "
          ></q-input>
          <q-input
            filled
            dense
            v-model.trim="formDialog.data.email"
            type="email"
            label="Your email (optional, if you want a reply)"
          ></q-input>
          <q-input
            v-if="flatrate"
            filled
            dense
            v-model.number="formDialog.data.text"
            type="textarea"
            label="{{ form_amount }} sats"
          ></q-input>
          <q-input
            v-else
            filled
            dense
            v-model.number="formDialog.data.text"
            type="textarea"
            label="{{ form_amount }} sats per word"
          ></q-input>
          <p>{% raw %}{{amountWords}}{% endraw %}</p>
          <div class="row q-mt-lg">
            <q-btn
              unelevated
              color="primary"
              :disable="formDialog.data.name == '' || formDialog.data.text == ''"
              type="submit"
              >Submit</q-btn
            >
            <q-btn @click="resetForm" flat color="grey" class="q-ml-auto"
              >Cancel</q-btn
            >
          </div>
        </q-form>
      </q-card-section>
    </q-card>
  </div>

  <q-dialog v-model="receive.show" position="top" @hide="closeReceiveDialog">
    <q-card
      v-if="!receive.paymentReq"
      class="q-pa-lg q-pt-xl lnbits__dialog-card"
    >
    </q-card>
    <q-card v-else class="q-pa-lg q-pt-xl lnbits__dialog-card">
      <div class="text-center q-mb-lg">
        <a :href="'lightning:' + receive.paymentReq">
          <q-responsive :ratio="1" class="q-mx-xl">
            <qrcode
              :value="paymentReq"
              :options="{width: 340}"
              class="rounded-borders"
            ></qrcode>
          </q-responsive>
        </a>
      </div>
      <div class="row q-mt-lg">
        <q-btn outline color="grey" @click="copyText(receive.paymentReq)"
          >Copy invoice</q-btn
        >
        <q-btn v-close-popup flat color="grey" class="q-ml-auto">Close</q-btn>
      </div>
    </q-card>
  </q-dialog>
</div>

{% endblock %} {% block scripts %}
<script>
  //console.log('{{ form_costpword }}')
  Vue.component(VueQrcode.name, VueQrcode)

  new Vue({
    el: '#vue',
    mixins: [windowMixin],
    data: function () {
      return {
        paymentReq: null,
        redirectUrl: null,
        flatrate: parseInt('{{form_flatrate}}'),
        formDialog: {
          show: false,
          data: {
            name: '',
            email: '',
            text: ''
          },
          dismissMsg: null,
          paymentChecker: null
        },
        receive: {
          show: false,
          status: 'pending',
          paymentReq: null
        },
        wallet: {
          inkey: ''
        },
        cancelListener: () => {}
      }
    },
    computed: {
      amountWords() {
        if (this.formDialog.data.text.length == '') {
          return '0 Sats to pay'
        } else {
          var regex = /\s+/gi
          var nwords = this.formDialog.data.text
            .trim()
            .replace(regex, ' ')
            .split(' ').length
          if (this.flatrate) {
            var sats = parseInt('{{ form_amount }}')
          } else {
            var sats = nwords * parseInt('{{ form_amount }}')
          }
          this.formDialog.data.sats = sats
          return sats + ' Sats to pay'
        }
      }
    },
    methods: {
      resetForm: function (e) {
        e.preventDefault()
        this.formDialog.data.name = ''
        this.formDialog.data.email = ''
        this.formDialog.data.text = ''
      },
      Invoice: function () {
        var self = this
        var dialog = this.formDialog
        axios
          .post('/lnticket/api/v1/tickets/{{ form_id }}', {
            form: '{{ form_id }}',
            name: self.formDialog.data.name,
            email: self.formDialog.data.email,
            ltext: self.formDialog.data.text,
            sats: self.formDialog.data.sats
          })
          .then(function (response) {
            self.paymentReq = response.data.payment_request
            self.paymentCheck = response.data.payment_hash

            dialog.dismissMsg = self.$q.notify({
              timeout: 0,
              message: 'Waiting for payment...'
            })
            self.receive = {
              show: true,
              status: 'pending',
              paymentReq: self.paymentReq
            }
            dialog.paymentChecker = setInterval(function () {
              axios
                .get('/lnticket/api/v1/tickets/' + response.data.payment_hash)
                .then(function (res) {
                  if (res.data.paid) {
                    clearInterval(dialog.paymentChecker)
                    dialog.dismissMsg()
                    self.receive.show = false
                    self.formDialog.data.name = ''
                    self.formDialog.data.email = ''
                    self.formDialog.data.text = ''
                    self.$q.notify({
                      type: 'positive',
                      message: 'Sats received, thanks!',
                      icon: 'thumb_up'
                    })
                  }
                })
            }, 3000)
          })
          .catch(function (error) {
            LNbits.utils.notifyApiError(error)
          })
      }
    },
    created() {
      this.wallet.inkey = '{{form_wallet}}'
    }
  })
</script>
{% endblock %}
