{% extends "base.html" %} {% from "macros.jinja" import window_vars with context
%} {% block page %}
<div class="row q-col-gutter-md">
  <div class="col-12 col-md-7 q-gutter-y-md">
    <q-card>
      <q-card-section>
        <q-btn unelevated color="deep-purple" @click="formDialog.show = true"
          >New pay link</q-btn
        >
      </q-card-section>
    </q-card>

    <q-card>
      <q-card-section>
        <div class="row items-center no-wrap q-mb-md">
          <div class="col">
            <h5 class="text-subtitle1 q-my-none">Pay links</h5>
          </div>
          <div class="col-auto">
            <q-btn flat color="grey" @click="exportCSV">Export to CSV</q-btn>
          </div>
        </div>
        <q-table
          dense
          flat
          :data="payLinks"
          row-key="id"
          :columns="payLinksTable.columns"
          :pagination.sync="payLinksTable.pagination"
        >
          {% raw %}
          <template v-slot:header="props">
            <q-tr :props="props">
              <q-th auto-width></q-th>
              <q-th v-for="col in props.cols" :key="col.name" :props="props">
                {{ col.label }}
              </q-th>
              <q-th auto-width></q-th>
            </q-tr>
          </template>
          <template v-slot:body="props">
            <q-tr :props="props">
              <q-td auto-width>
                <q-btn
                  unelevated
                  dense
                  size="xs"
                  icon="launch"
                  :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
                  type="a"
                  :href="props.row.pay_url"
                  target="_blank"
                ></q-btn>
                <q-btn
                  unelevated
                  dense
                  size="xs"
                  icon="visibility"
                  :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
                  @click="openQrCodeDialog(props.row.id)"
                ></q-btn>
              </q-td>
              <q-td v-for="col in props.cols" :key="col.name" :props="props">
                {{ col.value }}
              </q-td>
              <q-td auto-width>
                <q-btn
                  flat
                  dense
                  size="xs"
                  @click="openUpdateDialog(props.row.id)"
                  icon="edit"
                  color="light-blue"
                ></q-btn>
                <q-btn
                  flat
                  dense
                  size="xs"
                  @click="deletePayLink(props.row.id)"
                  icon="cancel"
                  color="pink"
                ></q-btn>
              </q-td>
            </q-tr>
          </template>
          {% endraw %}
        </q-table>
      </q-card-section>
    </q-card>
  </div>

  <div class="col-12 col-md-5 q-gutter-y-md">
    <q-card>
      <q-card-section>
        <h6 class="text-subtitle1 q-my-none">LNbits LNURL-pay extension</h6>
      </q-card-section>
      <q-card-section class="q-pa-none">
        <q-separator></q-separator>
        <q-list>
          {% include "lnurlp/_api_docs.html" %}
          <q-separator></q-separator>
          {% include "lnurlp/_lnurl.html" %}
        </q-list>
      </q-card-section>
    </q-card>
  </div>

  <q-dialog v-model="formDialog.show" position="top" @hide="closeFormDialog">
    <q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
      <q-form @submit="sendFormData" class="q-gutter-md">
        <q-select
          filled
          dense
          emit-value
          v-model="formDialog.data.wallet"
          :options="g.user.walletOptions"
          label="Wallet *"
        >
        </q-select>
        <q-input
          filled
          dense
          v-model.trim="formDialog.data.description"
          type="text"
          label="Item description *"
        ></q-input>
        <q-input
          filled
          dense
          v-model.number="formDialog.data.amount"
          type="number"
          label="Amount (sat) *"
        ></q-input>
        <q-input
          filled
          dense
          v-model="formDialog.data.webhook_url"
          type="text"
          label="Webhook URL (optional)"
        ></q-input>
        <div class="row q-mt-lg">
          <q-btn
            v-if="formDialog.data.id"
            unelevated
            color="deep-purple"
            type="submit"
            >Update pay link</q-btn
          >
          <q-btn
            v-else
            unelevated
            color="deep-purple"
            :disable="
                formDialog.data.wallet == null ||
                formDialog.data.description == null ||
                (
                  formDialog.data.amount == null ||
                  formDialog.data.amount < 1
                )
                "
            type="submit"
            >Create pay link</q-btn
          >
          <q-btn v-close-popup flat color="grey" class="q-ml-auto"
            >Cancel</q-btn
          >
        </div>
      </q-form>
    </q-card>
  </q-dialog>

  <q-dialog v-model="qrCodeDialog.show" position="top">
    <q-card v-if="qrCodeDialog.data" class="q-pa-lg lnbits__dialog-card">
      {% raw %}
      <q-responsive :ratio="1" class="q-mx-xl q-mb-md">
        <qrcode
          :value="qrCodeDialog.data.lnurl"
          :options="{width: 800}"
          class="rounded-borders"
        ></qrcode>
      </q-responsive>
      <p style="word-break: break-all">
        <strong>ID:</strong> {{ qrCodeDialog.data.id }}<br />
        <strong>Amount:</strong> {{ qrCodeDialog.data.amount }} sat<br />
        <strong>Webhook:</strong> {{ qrCodeDialog.data.webhook_url }}<br />
      </p>
      {% endraw %}
      <div class="row q-mt-lg q-gutter-sm">
        <q-btn
          outline
          color="grey"
          @click="copyText(qrCodeDialog.data.lnurl, 'LNURL copied to clipboard!')"
          class="q-ml-sm"
          >Copy LNURL</q-btn
        >
        <q-btn
          outline
          color="grey"
          @click="copyText(qrCodeDialog.data.pay_url, 'Link copied to clipboard!')"
          >Shareable link</q-btn
        >
        <q-btn
          v-if="!qrCodeDialog.data.is_unique"
          outline
          color="grey"
          icon="print"
          type="a"
          :href="qrCodeDialog.data.print_url"
          target="_blank"
        ></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 %} {{ window_vars(user) }}
<script>
  Vue.component(VueQrcode.name, VueQrcode)

  var locationPath = [
    window.location.protocol,
    '//',
    window.location.host,
    window.location.pathname
  ].join('')

  var mapPayLink = function (obj) {
    obj._data = _.clone(obj)
    obj.date = Quasar.utils.date.formatDate(
      new Date(obj.time * 1000),
      'YYYY-MM-DD HH:mm'
    )
    obj.amount = new Intl.NumberFormat(LOCALE).format(obj.amount)
    obj.print_url = [locationPath, 'print/', obj.id].join('')
    obj.pay_url = [locationPath, obj.id].join('')
    return obj
  }

  new Vue({
    el: '#vue',
    mixins: [windowMixin],
    data: function () {
      return {
        checker: null,
        payLinks: [],
        payLinksTable: {
          columns: [
            {name: 'id', align: 'left', label: 'ID', field: 'id'},
            {
              name: 'description',
              align: 'left',
              label: 'Description',
              field: 'description'
            },
            {
              name: 'amount',
              align: 'right',
              label: 'Amount (sat)',
              field: 'amount'
            },
            {
              name: 'webhook_url',
              align: 'left',
              label: 'Webhook URL',
              field: 'webhook_url'
            }
          ],
          pagination: {
            rowsPerPage: 10
          }
        },
        formDialog: {
          show: false,
          secondMultiplier: 'seconds',
          secondMultiplierOptions: ['seconds', 'minutes', 'hours'],
          data: {
            is_unique: false
          }
        },
        qrCodeDialog: {
          show: false,
          data: null
        }
      }
    },
    methods: {
      getPayLinks: function () {
        var self = this

        LNbits.api
          .request(
            'GET',
            '/lnurlp/api/v1/links?all_wallets',
            this.g.user.wallets[0].inkey
          )
          .then(function (response) {
            self.payLinks = response.data.map(function (obj) {
              return mapPayLink(obj)
            })
          })
          .catch(function (error) {
            clearInterval(self.checker)
            LNbits.utils.notifyApiError(error)
          })
      },
      closeFormDialog: function () {
        this.formDialog.data = {
          is_unique: false
        }
      },
      openQrCodeDialog: function (linkId) {
        var link = _.findWhere(this.payLinks, {id: linkId})
        this.qrCodeDialog.data = _.clone(link)
        this.qrCodeDialog.show = true
      },
      openUpdateDialog: function (linkId) {
        var link = _.findWhere(this.payLinks, {id: linkId})
        this.formDialog.data = _.clone(link._data)
        this.formDialog.show = true
      },
      sendFormData: function () {
        var wallet = _.findWhere(this.g.user.wallets, {
          id: this.formDialog.data.wallet
        })
        var data = _.omit(this.formDialog.data, 'wallet')

        data.wait_time =
          data.wait_time *
          {
            seconds: 1,
            minutes: 60,
            hours: 3600
          }[this.formDialog.secondMultiplier]

        if (data.id) {
          this.updatePayLink(wallet, data)
        } else {
          this.createPayLink(wallet, data)
        }
      },
      updatePayLink: function (wallet, data) {
        var self = this

        LNbits.api
          .request(
            'PUT',
            '/lnurlp/api/v1/links/' + data.id,
            wallet.adminkey,
            _.pick(data, 'description', 'amount', 'webhook_url')
          )
          .then(function (response) {
            self.payLinks = _.reject(self.payLinks, function (obj) {
              return obj.id === data.id
            })
            self.payLinks.push(mapPayLink(response.data))
            self.formDialog.show = false
          })
          .catch(function (error) {
            LNbits.utils.notifyApiError(error)
          })
      },
      createPayLink: function (wallet, data) {
        var self = this

        LNbits.api
          .request('POST', '/lnurlp/api/v1/links', wallet.adminkey, data)
          .then(function (response) {
            self.payLinks.push(mapPayLink(response.data))
            self.formDialog.show = false
          })
          .catch(function (error) {
            LNbits.utils.notifyApiError(error)
          })
      },
      deletePayLink: function (linkId) {
        var self = this
        var link = _.findWhere(this.payLinks, {id: linkId})

        LNbits.utils
          .confirmDialog('Are you sure you want to delete this pay link?')
          .onOk(function () {
            LNbits.api
              .request(
                'DELETE',
                '/lnurlp/api/v1/links/' + linkId,
                _.findWhere(self.g.user.wallets, {id: link.wallet}).adminkey
              )
              .then(function (response) {
                self.payLinks = _.reject(self.payLinks, function (obj) {
                  return obj.id === linkId
                })
              })
              .catch(function (error) {
                LNbits.utils.notifyApiError(error)
              })
          })
      },
      exportCSV: function () {
        LNbits.utils.exportCSV(this.paywallsTable.columns, this.paywalls)
      }
    },
    created: function () {
      if (this.g.user.wallets.length) {
        var getPayLinks = this.getPayLinks
        getPayLinks()
        this.checker = setInterval(function () {
          getPayLinks()
        }, 20000)
      }
    }
  })
</script>
{% endblock %}
