<template>
  <button v-if="kind === 'next'"
    v-bind:class="[componentClass, {'c-inactive':currentRowIndex >= rowsCountThis - 1}, btnClass]" 
    v-on:click="setRowNext">
  </button>
  <button v-else-if="kind === 'prev'"
    v-bind:class="[componentClass, {'c-inactive':currentRowIndex <= 0}, btnClass]"
    v-on:click="setRowPrev">
  </button>
</template>

<script>
import base from './c-base'
import { mapGetters, mapActions } from 'vuex'

const props = {
  kind: {
    type: String,
    default: 'next'
  },
  sourceKind: {
    type: String,
    default: 'rows'
  }
}

export default {
  extends: base,
  props,
  data: function () {
    return {
      componentClass: 'c-btn-row-change'
    }
  },
  computed: {
    ...mapGetters("csAppWindowsUtils", [
      "getDataSetCurrentRowIndex"
    ]),
    btnClass: function() {
      switch (this.kind) { 
        case 'next': return 'c-next'
        case 'prev': return 'c-prev'
        default: return null
      }
    },
    rowsCountThis:function() {
      switch (this.sourceKind) { 
        case 'rows': return this.dataSet.rows.length //FINAL FORM-c-btn-row-change changes page when there are more pages than one case 'rows': return (this.rowsCount > 0 ? this.rowsCount : this.dataSet.rows.length)
        default: return this.rowsCount
      }
    },
    currentRowIndex: function() {
      switch (this.sourceKind) { 
        case 'rows': return this.getDataSetCurrentRowIndex(this.dataSet)
        default: return this.getDataSetCurrentRowIndex(this.dataSet)
      }
    },
    payloadThis: function() {
      let newRowIndex = -1
      if(this.kind == 'next') {
        newRowIndex = (this.currentRowIndex < (this.rowsCountThis - 1)) ? this.currentRowIndex + 1 : this.currentRowIndex
      } else if (this.kind == 'prev') {
        newRowIndex = (this.currentRowIndex > 0) ? this.currentRowIndex - 1 : 0
      }
      return {
        appWindowIdent: this.appWindowIdent,
        dataSet: this.dataSet,
        currentRowIndex: newRowIndex
      }
    }
  },
  methods: {
    ...mapActions('csAppWindowsUtils', [
      'setDataSetCurrentRowIndex'
    ]),
    setRowNext: function() {
      if(this.currentRowIndex >= (this.rowsCountThis - 1)) return
      switch (this.sourceKind) { 
        case 'rows': this.setDataSetCurrentRowIndex(this.payloadThis); break
        default: this.setDataSetCurrentRowIndex(this.payloadThis); break
      }
    },
    setRowPrev: function() {
      if(this.currentRowIndex <= 0) return
      switch (this.sourceKind) { 
        case 'rows': this.setDataSetCurrentRowIndex(this.payloadThis); break
        default: this.setDataSetCurrentRowIndex(this.payloadThis); break
      }
    }
  }
}</script>