<template lang="pug">
  #banlist
    h3 Bans ({{pagination.total}})
    .message.error(v-if='error') {{ error }}
    .entry(v-else)
      pagination(v-bind="pagination" v-on:page="getBans")
      .list.bans
        ban(v-for='ban in bans' v-bind="ban")
</template>

<script type="text/javascript">
  import Pagination from './Pagination.vue'
  import Ban from './Ban.vue'
 
  export default {
    data: function () {
      return {
        pagination: {
          offset: 0,
          page: 1,
          pages: 1,
          perPage: 6,
          total: 0
        },
        error: '',
        bans: []
      }
    },
    components: {
      Pagination, Ban
    },
    methods: {
      getBans: function (page) {
        this.error = ''
        this.pagination.total = 0
        this.$http.get('/admin/api/bans?page=' + page).then(data => {
          if (data.body && data.body.error) {
            this.error = data.body.error
            return
          }

          this.pagination = data.body.page
          this.bans = data.body.bans
        })
      },
      pardon: function (id) {
        this.$http.post('/admin/api/ban/pardon/' + id).then(data => {
          this.getBans(1)
        })
      }
    },
    mounted: function () {
      this.getBans(1)

      this.$root.$on('reload_bans', () => {
        this.getBans(1)
      })

      this.$on('pardon', (id) => {
        this.pardon(id)
      })
    }
  }
</script>
