{% 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 Event</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">Events</h5>
          </div>
          <div class="col-auto">
            <q-btn flat color="grey" @click="exporteventsCSV"
              >Export to CSV</q-btn
            >
          </div>
        </div>
        <q-table
          dense
          flat
          :data="events"
          row-key="id"
          :columns="eventsTable.columns"
          :pagination.sync="eventsTable.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="link"
                  :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="how_to_reg"
                  :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
                  type="a"
                  :href="'/events/register/' + props.row.id"
                  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="updateformDialog(props.row.id)"
                  icon="edit"
                  color="light-blue"
                ></q-btn>
              </q-td>
              <q-td auto-width>
                <q-btn
                  flat
                  dense
                  size="xs"
                  @click="deleteEvent(props.row.id)"
                  icon="cancel"
                  color="pink"
                ></q-btn>
              </q-td>
            </q-tr>
          </template>
          {% endraw %}
        </q-table>
      </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">Tickets</h5>
          </div>
          <div class="col-auto">
            <q-btn flat color="grey" @click="exportticketsCSV"
              >Export to CSV</q-btn
            >
          </div>
        </div>
        <q-table
          dense
          flat
          :data="tickets"
          row-key="id"
          :columns="ticketsTable.columns"
          :pagination.sync="ticketsTable.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-tr>
          </template>
          <template v-slot:body="props">
            <q-tr :props="props">
              <q-td auto-width>
                <q-btn
                  unelevated
                  dense
                  size="xs"
                  icon="local_activity"
                  :color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
                  type="a"
                  :href="'/events/ticket/' + props.row.id"
                  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="deleteTicket(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}} Events extension
        </h6>
      </q-card-section>
      <q-card-section class="q-pa-none">
        <q-separator></q-separator>
        <q-list> {% include "events/_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="sendEventData" class="q-gutter-md">
        <div class="row">
          <div class="col">
            <q-input
              filled
              dense
              v-model.trim="formDialog.data.name"
              type="name"
              label="Title of event "
            ></q-input>
          </div>
          <div class="col q-pl-sm">
            <q-select
              filled
              dense
              emit-value
              v-model="formDialog.data.wallet"
              :options="g.user.walletOptions"
              label="Wallet *"
            >
            </q-select>
          </div>
        </div>

        <q-input
          filled
          dense
          v-model.trim="formDialog.data.info"
          type="textarea"
          label="Info about the event "
        ></q-input>
        <div class="row">
          <div class="col-4">Ticket closing date</div>
          <div class="col-8">
            <q-input
              filled
              dense
              v-model.trim="formDialog.data.closing_date"
              type="date"
            ></q-input>
          </div>
        </div>

        <div class="row">
          <div class="col-4">Event begins</div>
          <div class="col-8">
            <q-input
              filled
              dense
              v-model.trim="formDialog.data.event_start_date"
              type="date"
            ></q-input>
          </div>
        </div>

        <div class="row">
          <div class="col-4">Event ends</div>
          <div class="col-8">
            <q-input
              filled
              dense
              v-model.trim="formDialog.data.event_end_date"
              type="date"
            ></q-input>
          </div>
        </div>

        <div class="row">
          <div class="col">
            <q-input
              filled
              dense
              v-model.number="formDialog.data.amount_tickets"
              type="number"
              label="Amount of tickets "
            ></q-input>
          </div>
          <div class="col q-pl-sm">
            <q-input
              filled
              dense
              v-model.number="formDialog.data.price_per_ticket"
              type="number"
              label="Price per ticket "
            ></q-input>
          </div>
        </div>

        <div class="row q-mt-lg">
          <q-btn
            v-if="formDialog.data.id"
            unelevated
            color="primary"
            type="submit"
            >Update Event</q-btn
          >
          <q-btn
            v-else
            unelevated
            color="primary"
            :disable="formDialog.data.wallet == null || formDialog.data.name == null || formDialog.data.info == null || formDialog.data.closing_date == null || formDialog.data.event_start_date == null || formDialog.data.event_end_date == null || formDialog.data.amount_tickets == null || formDialog.data.price_per_ticket == null"
            type="submit"
            >Create Event</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 mapEvents = 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 = ['/events/', obj.id].join('')
    return obj
  }

  new Vue({
    el: '#vue',
    mixins: [windowMixin],
    data: function () {
      return {
        events: [],
        tickets: [],
        eventsTable: {
          columns: [
            {name: 'id', align: 'left', label: 'ID', field: 'id'},
            {name: 'name', align: 'left', label: 'Name', field: 'name'},
            {name: 'info', align: 'left', label: 'Info', field: 'info'},
            {
              name: 'event_start_date',
              align: 'left',
              label: 'Start date',
              field: 'event_start_date'
            },
            {
              name: 'event_end_date',
              align: 'left',
              label: 'End date',
              field: 'event_end_date'
            },
            {
              name: 'closing_date',
              align: 'left',
              label: 'Ticket close',
              field: 'closing_date'
            },
            {
              name: 'price_per_ticket',
              align: 'left',
              label: 'Price',
              field: 'price_per_ticket'
            },
            {
              name: 'amount_tickets',
              align: 'left',
              label: 'No tickets',
              field: 'amount_tickets'
            },
            {
              name: 'sold',
              align: 'left',
              label: 'Sold',
              field: 'sold'
            }
          ],
          pagination: {
            rowsPerPage: 10
          }
        },
        ticketsTable: {
          columns: [
            {name: 'id', align: 'left', label: 'ID', field: 'id'},
            {name: 'event', align: 'left', label: 'Event', field: 'event'},
            {name: 'name', align: 'left', label: 'Name', field: 'name'},
            {name: 'email', align: 'left', label: 'Email', field: 'email'},
            {
              name: 'registered',
              align: 'left',
              label: 'Registered',
              field: 'registered'
            }
          ],
          pagination: {
            rowsPerPage: 10
          }
        },
        formDialog: {
          show: false,
          data: {}
        }
      }
    },
    methods: {
      getTickets: function () {
        var self = this
        LNbits.api
          .request(
            'GET',
            '/events/api/v1/tickets?all_wallets=true',
            this.g.user.wallets[0].inkey
          )
          .then(function (response) {
            console.log(response)
            self.tickets = response.data.map(function (obj) {
              console.log(obj)
              return mapEvents(obj)
            })
          })
      },
      deleteTicket: function (ticketId) {
        var self = this
        var tickets = _.findWhere(this.tickets, {id: ticketId})

        LNbits.utils
          .confirmDialog('Are you sure you want to delete this ticket')
          .onOk(function () {
            LNbits.api
              .request(
                'DELETE',
                '/events/api/v1/tickets/' + ticketId,
                _.findWhere(self.g.user.wallets, {id: tickets.wallet}).inkey
              )
              .then(function (response) {
                self.tickets = _.reject(self.tickets, function (obj) {
                  return obj.id == ticketId
                })
              })
              .catch(function (error) {
                LNbits.utils.notifyApiError(error)
              })
          })
      },
      exportticketsCSV: function () {
        LNbits.utils.exportCSV(this.ticketsTable.columns, this.tickets)
      },

      getEvents: function () {
        var self = this

        LNbits.api
          .request(
            'GET',
            '/events/api/v1/events?all_wallets=true',
            this.g.user.wallets[0].inkey
          )
          .then(function (response) {
            self.events = response.data.map(function (obj) {
              return mapEvents(obj)
            })
          })
      },
      sendEventData: function () {
        var wallet = _.findWhere(this.g.user.wallets, {
          id: this.formDialog.data.wallet
        })
        var data = this.formDialog.data

        if (data.id) {
          this.updateEvent(wallet, data)
        } else {
          this.createEvent(wallet, data)
        }
      },

      createEvent: function (wallet, data) {
        var self = this
        LNbits.api
          .request('POST', '/events/api/v1/events', wallet.inkey, data)
          .then(function (response) {
            self.events.push(mapEvents(response.data))
            self.formDialog.show = false
            self.formDialog.data = {}
          })
          .catch(function (error) {
            LNbits.utils.notifyApiError(error)
          })
      },
      updateformDialog: function (formId) {
        var link = _.findWhere(this.events, {id: formId})
        console.log(link.id)
        this.formDialog.data.id = link.id
        this.formDialog.data.wallet = link.wallet
        this.formDialog.data.name = link.name
        this.formDialog.data.info = link.info
        this.formDialog.data.closing_date = link.closing_date
        this.formDialog.data.event_start_date = link.event_start_date
        this.formDialog.data.event_end_date = link.event_end_date
        this.formDialog.data.amount_tickets = link.amount_tickets
        this.formDialog.data.price_per_ticket = link.price_per_ticket
        this.formDialog.show = true
      },
      updateEvent: function (wallet, data) {
        var self = this
        console.log(data)

        LNbits.api
          .request(
            'PUT',
            '/events/api/v1/events/' + data.id,
            wallet.inkey,
            data
          )
          .then(function (response) {
            self.events = _.reject(self.events, function (obj) {
              return obj.id == data.id
            })
            self.events.push(mapEvents(response.data))
            self.formDialog.show = false
            self.formDialog.data = {}
          })
          .catch(function (error) {
            LNbits.utils.notifyApiError(error)
          })
      },
      deleteEvent: function (eventsId) {
        var self = this
        var events = _.findWhere(this.events, {id: eventsId})

        LNbits.utils
          .confirmDialog('Are you sure you want to delete this form link?')
          .onOk(function () {
            LNbits.api
              .request(
                'DELETE',
                '/events/api/v1/events/' + eventsId,
                _.findWhere(self.g.user.wallets, {id: events.wallet}).inkey
              )
              .then(function (response) {
                self.events = _.reject(self.events, function (obj) {
                  return obj.id == eventsId
                })
              })
              .catch(function (error) {
                LNbits.utils.notifyApiError(error)
              })
          })
      },
      exporteventsCSV: function () {
        LNbits.utils.exportCSV(this.eventsTable.columns, this.events)
      }
    },

    created: function () {
      if (this.g.user.wallets.length) {
        this.getTickets()
        this.getEvents()
      }
    }
  })
</script>
{% endblock %}
