<template>
  <button 
    v-bind:class="[
      componentClass,
      {
        'c-disabled': isDisabled
      }
    ]" 
    :title="getTitle"
    :disabled="isDisabled"
    v-on:click="refreshData">
    <slot
      name="content"
      :row="contextRow" 
      :where="where"
    >{{ getLabel }}
    </slot>
  </button>
</template>

<script>

import { mapActions } from 'vuex'
//
import base from './c-base'
//
import dataSetUtils from '../classes/dataSetUtils'
////

const props = {
  dataFieldIdent: {
    type: String,
    default: null
  },
  dataFieldIdent4Label: {
    type: String,
    default: null
  },
  dataFieldIdent4Title: {
    type: String,
    default: null
  },
  label: {
    type: String,
    default: null
  },
  disabled:{
    type: Boolean,
    default: false
  }
}

export default {
  extends: base,
  data: function () {
    return {
      componentClass: 'c-btnref'
    }
  },
  props,
  computed: {
    getLabel: function () {
      return this.label || this.getDataFieldValue(this.dataFieldIdent4Label)
    },
    getTitle: function () {
      return this.getDataFieldValue(this.dataFieldIdent4Title)
    },
    isDisabled: function() {
      if (this.disabled === true)
        return true
      if (this.state === 0 || this.state === 2)
        return true
      if (this.oprState == dataSetUtils.oprState.inProgress)
        return true
      //
      return false
    }
  },
  methods: {
    ...mapActions('csAppWindowsUtils', [
      'refreshDataSetDependence'
    ]),

    refreshData: function () {
      if ((this.sourceKind == "where")) this.refresh()
      else {
        this.refreshDataSetDependence({
          appWindowIdent: this.appWindowIdent,
          parentDataSetIdent: this.dataSetIdent,
          refreshAggregates: 1
        })
      }
    }
  }
}</script>
