{% 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-8 col-lg-7 q-gutter-y-md">
    <q-card>
      <q-card-section>
        <q-btn unelevated color="primary" @click="formDialog.show = true"
          >New LNURLPayout</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">LNURLPayout</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="lnurlpayouts"
          row-key="id"
          :columns="lnurlpayoutsTable.columns"
          :pagination.sync="lnurlpayoutsTable.pagination"
        >
          {% raw %}
          <template v-slot:header="props">
            <q-tr :props="props">
              <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 v-for="col in props.cols" :key="col.name" :props="props">
                <a v-if="col.label == 'LNURLPay'" @click="copyText(col.value)"
                  ><q-tooltip>Click to copy LNURL</q-tooltip>{{
                  col.value.substring(0, 40) }}...</a
                >
                <div v-else-if="col.label == 'Threshold'">
                  {{ col.value }} Sats
                </div>
                <div v-else>{{ col.value.substring(0, 40) }}</div>
              </q-td>
              <q-td auto-width>
                <q-btn
                  flat
                  dense
                  size="xs"
                  @click="deletelnurlpayout(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">
          {{SITE_TITLE}} LNURLPayout extension
        </h6>
      </q-card-section>
      <q-card-section class="q-pa-none">
        <q-separator></q-separator>
        <q-list>
          {% include "lnurlpayout/_api_docs.html" %}
          <q-separator></q-separator>
        </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" style="width: 500px">
      <q-form @submit="createlnurlpayout" class="q-gutter-md">
        <q-input
          filled
          dense
          v-model.trim="formDialog.data.title"
          label="Title"
          placeholder="Title"
          type="text"
        ></q-input>
        <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.lnurlpay"
          label="LNURLPay"
          placeholder="LNURLPay"
          type="text"
        ></q-input>
        <q-input
          filled
          dense
          v-model.trim="formDialog.data.threshold"
          label="Threshold (100k sats max)"
          placeholder="Threshold"
          type="number"
          max="100000"
        ></q-input>
        <div class="row q-mt-lg">
          <q-btn
            unelevated
            color="primary"
            :disable="formDialog.data.threshold == null"
            type="submit"
            >Create LNURLPayout</q-btn
          >
          <q-btn v-close-popup flat color="grey" class="q-ml-auto"
            >Cancel</q-btn
          >
        </div>
      </q-form>
    </q-card>
  </q-dialog>
</div>
{% endblock %} {% block scripts %} {{ window_vars(user) }}
<script>
  var maplnurlpayout = function (obj) {
    obj.date = Quasar.utils.date.formatDate(
      new Date(obj.time * 1000),
      'YYYY-MM-DD HH:mm'
    )
    obj.fsat = new Intl.NumberFormat(LOCALE).format(obj.amount)
    obj.lnurlpayout = ['/lnurlpayout/', obj.id].join('')
    return obj
  }

  new Vue({
    el: '#vue',
    mixins: [windowMixin],
    data: function () {
      return {
        lnurlpayouts: [],
        lnurlpayoutsTable: {
          columns: [
            {name: 'id', align: 'left', label: 'ID', field: 'id'},
            {name: 'title', align: 'left', label: 'Title', field: 'title'},
            {name: 'wallet', align: 'left', label: 'Wallet', field: 'wallet'},
            {
              name: 'lnurlpay',
              align: 'left',
              label: 'LNURLPay',
              field: 'lnurlpay'
            },
            {
              name: 'threshold',
              align: 'left',
              label: 'Threshold',
              field: 'threshold'
            }
          ],
          pagination: {
            rowsPerPage: 10
          }
        },
        formDialog: {
          show: false,
          data: {}
        }
      }
    },
    methods: {
      closeFormDialog: function () {
        this.formDialog.data = {}
      },
      getlnurlpayouts: function () {
        var self = this

        LNbits.api
          .request(
            'GET',
            '/lnurlpayout/api/v1/lnurlpayouts?all_wallets=true',
            this.g.user.wallets[0].inkey
          )
          .then(function (response) {
            self.lnurlpayouts = response.data.map(function (obj) {
              return maplnurlpayout(obj)
            })
          })
      },
      createlnurlpayout: function () {
        var data = {
          title: this.formDialog.data.title,
          lnurlpay: this.formDialog.data.lnurlpay,
          threshold: this.formDialog.data.threshold
        }
        var self = this

        LNbits.api
          .request(
            'POST',
            '/lnurlpayout/api/v1/lnurlpayouts',
            _.findWhere(this.g.user.wallets, {id: this.formDialog.data.wallet})
              .inkey,
            data
          )
          .then(function (response) {
            console.log(data)
            self.lnurlpayouts.push(maplnurlpayout(response.data))
            self.formDialog.show = false
          })
          .catch(function (error) {
            LNbits.utils.notifyApiError(error)
          })
      },
      deletelnurlpayout: function (lnurlpayoutId) {
        var self = this
        var lnurlpayout = _.findWhere(this.lnurlpayouts, {id: lnurlpayoutId})

        LNbits.utils
          .confirmDialog('Are you sure you want to delete this lnurlpayout?')
          .onOk(function () {
            LNbits.api
              .request(
                'DELETE',
                '/lnurlpayout/api/v1/lnurlpayouts/' + lnurlpayoutId,
                _.findWhere(self.g.user.wallets, {id: lnurlpayout.wallet})
                  .adminkey
              )
              .then(function (response) {
                self.lnurlpayouts = _.reject(self.lnurlpayouts, function (obj) {
                  return obj.id == lnurlpayoutId
                })
              })
              .catch(function (error) {
                LNbits.utils.notifyApiError(error)
              })
          })
      },
      exportCSV: function () {
        LNbits.utils.exportCSV(
          this.lnurlpayoutsTable.columns,
          this.lnurlpayouts
        )
      }
    },
    created: function () {
      if (this.g.user.wallets.length) {
        this.getlnurlpayouts()
      }
    }
  })
</script>
{% endblock %}
