<template>
  <button 
    v-bind:class="[componentClass, {'is-active': isActive }]" 
    :title="getTitle" 
    v-on:click="setValue">
    <slot
      name="content"
      :row="contextRow" 
      :where="where"
    >{{ getLabel }}
    </slot>
  </button>
</template>

<script>
import base from './c-base'
const props = {
  dataFieldIdent: {
    type: String,
    default: null
  },
  dataFieldIdent4Label: {
    type: String,
    default: null
  },
  dataFieldIdent4Title: {
    type: String,
    default: null
  },
  label: {
    type: String,
    default: null
  },

  labelValueToSet: {
    type: String,
    default: null
  },
  labelValueToUnSet: {
    type: String,
    default: null
  },

  dataFieldValueToSet: {
    default: null
  },
  dataFieldValueToUnSet: {
    default: null
  }
}

export default {
  extends: base,
  data: function () {
    return {
      componentClass: 'c-btnsv'
    }
  },
  props,
  computed: {
    getLabel: function () {
      let label = null;
      if(this.labelValueToSet != null && this.labelValueToSet != '' && this.dataFieldValueToUnSet === this.dataFieldValue) {
        label = this.labelValueToSet
      } else if(this.labelValueToUnSet != null && this.labelValueToUnSet != '' && this.dataFieldValueToSet === this.dataFieldValue) {
        label = this.labelValueToUnSet
      }
      return label || this.label || this.getDataFieldValue(this.dataFieldIdent4Label)
    },
    getTitle: function () {
      return this.getDataFieldValue(this.dataFieldIdent4Title)
    },
    isActive: function () {
      return this.dataFieldValueToSet === this.dataFieldValue ? true : false
    }
  },
  methods: {
    setValue: function () {
      if (this.isActive) {
        if (this.dataFieldValueToUnSet != null) {
          this.setDataFieldValue({
            dataFieldIdent: this.dataFieldIdent,
            dataFieldValue: this.dataFieldValueToUnSet
          })
        }
      } else {
        this.setDataFieldValue({
          dataFieldIdent: this.dataFieldIdent,
          dataFieldValue: this.dataFieldValueToSet
        })
      }
    }
  }
}</script>
