<template>
  <main id="main" style="margin-top: -80px; padding-bottom: 50px;">
    <div class="container">
      <div class="form">
        <div class="row">
          <div class="col">
            <div class="section-header">
              <h2>Property User Defined Groups</h2>
            </div>
          </div>
        </div>
        <div v-if="checkAccess" class="row">
          <div class="col">
            <listView
              :items="userDefinedGroups"
              :showNew="true"
              :editable="true"
              :deleteable="true"
              :displayColumns="columns"
              :sortKey="'rank'"
              @onEdit="Edit"
              @onDelete="Delete"
              @onNew="New"
            />
          </div>
        </div>
        <div v-else class="row">
          <div class="col">
            <alert
              :text="'You don\'t have permission to view this page'"
              :type="'ERROR'"
            />
          </div>
        </div>
      </div>
    </div>
  </main>
</template>

<script>
/* eslint-disable */
import { mapState, mapActions } from 'vuex'

import Log from '../../../assets/Log'
import alert from '../../shared/alert'
import listView from '../../shared/listView.vue'

export default {
  name: 'UserDefinedGroup',
  components: {
    listView,
    alert,
  },
  data() {
    return {
      columns: ['description', 'propertyUsageType', 'rank'],
    }
  },
  mounted() {
    this.getUserDefinedGroups()
  },
  computed: {
    ...mapState('propertyAdmin', ['userDefinedGroups']),
    checkAccess() {
      if (Log.getUser().role === 'Super Admin') {
        return true
      } else {
        return false
      }
    },
  },
  methods: {
    ...mapActions('propertyAdmin', [
      'getUserDefinedGroups',
      'deleteUserDefinedGroup',
    ]),
    New() {
      this.$router.push('/userDefinedGroups/userDefinedGroup')
    },
    Edit(item) {
      this.$router.push(`/userDefinedGroups/userDefinedGroup/${item.id}`)
    },
    Delete(item) {
      if (item.id === 0) {
        this.userDefinedGroups.pop(item)
      }
      this.deleteUserDefinedGroup(item.id)
    },
  },
}
</script>
