{% 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 paywall</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">Paywalls</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="paywalls"
          row-key="id"
          :columns="paywallsTable.columns"
          :pagination.sync="paywallsTable.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.displayUrl"
                  target="_blank"
                ></q-btn>
                <q-btn
                  unelevated
                  dense
                  size="xs"
                  icon="link"
                  :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
                  type="a"
                  :href="props.row.url"
                  target="_blank"
                ></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="deletePaywall(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-4 col-lg-5 q-gutter-y-md">
    <q-card>
      <q-card-section>
        <h6 class="text-subtitle1 q-my-none">
          {{SITE_TITLE}} paywall extension
        </h6>
      </q-card-section>
      <q-card-section class="q-pa-none">
        <q-separator></q-separator>
        <q-list> {% include "paywall/_api_docs.html" %} </q-list>
      </q-card-section>
    </q-card>
  </div>

  <q-dialog v-model="formDialog.show" position="top">
    <q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
      <q-form @submit="createPaywall" 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.url"
          type="url"
          label="Redirect URL *"
        ></q-input>
        <q-input
          filled
          dense
          v-model.trim="formDialog.data.memo"
          label="Title *"
          placeholder="LNbits paywall"
        ></q-input>
        <q-input
          filled
          dense
          autogrow
          v-model.trim="formDialog.data.description"
          label="Description"
        ></q-input>
        <q-input
          filled
          dense
          v-model.number="formDialog.data.amount"
          type="number"
          label="Amount (sat) *"
          hint="This is the minimum amount users can pay/donate."
        ></q-input>
        <q-list>
          <q-item tag="label" class="rounded-borders">
            <q-item-section avatar>
              <q-checkbox
                v-model="formDialog.data.remembers"
                color="primary"
              ></q-checkbox>
            </q-item-section>
            <q-item-section>
              <q-item-label>Remember payments</q-item-label>
              <q-item-label caption
                >A succesful payment will be registered in the browser's
                storage, so the user doesn't need to pay again to access the
                URL.</q-item-label
              >
            </q-item-section>
          </q-item>
        </q-list>
        <div class="row q-mt-lg">
          <q-btn
            unelevated
            color="primary"
            :disable="formDialog.data.amount == null || formDialog.data.amount < 0 || formDialog.data.url == null || formDialog.data.memo == null"
            type="submit"
            >Create paywall</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 mapPaywall = 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.displayUrl = ['/paywall/', obj.id].join('')
    return obj
  }

  new Vue({
    el: '#vue',
    mixins: [windowMixin],
    data: function () {
      return {
        paywalls: [],
        paywallsTable: {
          columns: [
            {name: 'id', align: 'left', label: 'ID', field: 'id'},
            {name: 'memo', align: 'left', label: 'Memo', field: 'memo'},
            {
              name: 'amount',
              align: 'right',
              label: 'Amount (sat)',
              field: 'fsat',
              sortable: true,
              sort: function (a, b, rowA, rowB) {
                return rowA.amount - rowB.amount
              }
            },
            {
              name: 'remembers',
              align: 'left',
              label: 'Remember',
              field: 'remembers'
            },
            {
              name: 'date',
              align: 'left',
              label: 'Date',
              field: 'date',
              sortable: true
            }
          ],
          pagination: {
            rowsPerPage: 10
          }
        },
        formDialog: {
          show: false,
          data: {
            remembers: false
          }
        }
      }
    },
    methods: {
      getPaywalls: function () {
        var self = this

        LNbits.api
          .request(
            'GET',
            '/paywall/api/v1/paywalls?all_wallets=true',
            this.g.user.wallets[0].inkey
          )
          .then(function (response) {
            self.paywalls = response.data.map(function (obj) {
              return mapPaywall(obj)
            })
          })
      },
      createPaywall: function () {
        var data = {
          url: this.formDialog.data.url,
          memo: this.formDialog.data.memo,
          amount: this.formDialog.data.amount,
          description: this.formDialog.data.description,
          remembers: this.formDialog.data.remembers
        }
        var self = this

        LNbits.api
          .request(
            'POST',
            '/paywall/api/v1/paywalls',
            _.findWhere(this.g.user.wallets, {id: this.formDialog.data.wallet})
              .inkey,
            data
          )
          .then(function (response) {
            self.paywalls.push(mapPaywall(response.data))
            self.formDialog.show = false
            self.formDialog.data = {
              remembers: false
            }
          })
          .catch(function (error) {
            LNbits.utils.notifyApiError(error)
          })
      },
      deletePaywall: function (paywallId) {
        var self = this
        var paywall = _.findWhere(this.paywalls, {id: paywallId})

        LNbits.utils
          .confirmDialog('Are you sure you want to delete this paywall link?')
          .onOk(function () {
            LNbits.api
              .request(
                'DELETE',
                '/paywall/api/v1/paywalls/' + paywallId,
                _.findWhere(self.g.user.wallets, {id: paywall.wallet}).inkey
              )
              .then(function (response) {
                self.paywalls = _.reject(self.paywalls, function (obj) {
                  return obj.id == paywallId
                })
              })
              .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) {
        this.getPaywalls()
      }
    }
  })
</script>
{% endblock %}
