<script>
const props = {
  appWindowIdent: {
    type: String,
    default: null,
    required: true
  },
  dataSetIdent: {
    type: String,
    default: null,
    required: false
  },
  row: null,
  pivotCol: null,
  pivotRow: null,
  id: {
    type: String,
    default: null
  },
  sourceKind: {
    type: String,
    default: "rows"
  }
}

import { mapGetters, mapActions } from 'vuex'
import { deviceDetection } from '../mixins/c-device-detection'
import { utilsMixin } from '../mixins/c-utils-mixin'

export default {
  data: function() {
    return {
      componentId: null,
      componentClass: 'c-base'
    };
  },
  mixins: [deviceDetection, utilsMixin],
  props,
  computed: {
    ...mapGetters('csAppWindowsUtils', [
      'getDataSet',
      'getDataSetRows',
      'getDataSetFieldValue',
      'getDataSetRowsCount',
      'getDataSetPivotRowsCount',
      'getDataSetPivotColsCount',
      'getDataSetCurrentRow',
      'getDataSetWhere',
      'getDataSetBusy',
      'getDataSetRowsIsEmpty',
      'getDataSetPivotIsEmpty',
      'getDataSetState',
      'getDataSetRowFields',
      'getDataSetErrorFields',
      'getDataSetInfoFields',
      'getDataSetWarningFields',
      'getDataSetColFields',
      'getDataSetAggrFields',
      'getDataSetAggrFieldsMaxWidth',
      'getDataSetRowFieldsCount',
      'getDataSetColFieldsCount',
      'getDataSetRowFieldsSumWidth',
      'getDataSetRowFieldsLastHeight',
      'getDataSetColFieldsLastWidth',
      'getDataSetPageNo',
      'getDataSetPivotRowsPageNo',
      'getDataSetPivotColsPageNo',
      'getDataSetPagesCount',
      'getDataSetPivotRowsPagesCount',
      'getDataSetPivotColsPagesCount',
      'getDataSetFieldsIdents',
      'getDataSetKeyFieldsIdents',
      'getDataSetWhereFieldsIdentsDataField',
      'getDataSetFieldsIdentsDataField',
      'getPivotRowKey',
      'getDataSetSelectedRowsCount',
      'getDataSetVisibleColFields', 
      'getDataSetVisibleColFieldsSorted',
      'getDataSetColFieldsSorted',
      'getDataSetLayoutIdent',
      'getDataSetLayout',
      'getAppWindowParams'
    ]),
    ...mapGetters('csServerSettings', [
      'getAPIImageServerURL',
      'getSkin'
    ]),
    ...mapGetters('csDeviceDetection', [
      'device',
      'deviceType'
    ]),
    payload: function () {
      return {
        appWindowIdent: this.appWindowIdent,
        dataSetIdent: this.dataSetIdent,
        dataFieldIdent: this.dataFieldIdent
      }
    },
    getId: function () {
      if (this.id == null) {
        return 'id' + this._uid
      }
      else {
        return this.id
      }
    },
    skin: function () {
      return this.getSkin()
    },
    pivotRowKey: function () {
      return this.getPivotRowKey(this.pivotCol, this.pivotRow)
    },
    dataSet: function () {
      return this.getDataSet(this.appWindowIdent, this.dataSetIdent)
    },
    layoutIdent: function () {
      return this.getDataSetLayoutIdent(this.dataSet)
    },
    layout: function () {
      return this.getDataSetLayout(this.dataSet, this.layoutIdent)
    },
    dataFieldValue: function () {
      return this.getDataFieldValue(this.dataFieldIdent)
    },
    whereFieldsIdentsDataField: function () {
      return this.getDataSetWhereFieldsIdentsDataField(this.dataSet, this.dataFieldIdent)
    },
    fieldsIdentsDataField: function () {
      return this.getDataSetFieldsIdentsDataField(this.dataSet, this.dataFieldIdent)
    },
    rows: function () {
      return this.getDataSetRows(this.dataSet)
    },
    currentRow: function () {
      return this.getDataSetCurrentRow(this.dataSet)
    },
    contextRow: function() {
      return this.row || this.currentRow
    },
    state: function () {
      return this.getDataSetState(this.appWindowIdent, this.dataSetIdent)
    },
    busy: function () {
      return this.getDataSetBusy(this.appWindowIdent, this.dataSetIdent)
    },
    rowsIsEmpty: function () {
      return this.getDataSetRowsIsEmpty(this.dataSet)
    },
    pivotIsEmpty: function () {
      return this.getDataSetPivotIsEmpty(this.dataSet)
    },
    rowsCount: function () {
      return this.getDataSetRowsCount(this.dataSet)
    },
    pivotRowsCount: function () {
      return this.getDataSetPivotRowsCount(this.dataSet)
    },
    pivotColsCount: function () {
      return this.getDataSetPivotColsCount(this.dataSet)
    },
    pageNo: function () {
      return this.getDataSetPageNo(this.dataSet)
    },
    pivotRowsPageNo: function () {
      return this.getDataSetPivotRowsPageNo(this.dataSet)
    },
    pivotColsPageNo: function () {
      return this.getDataSetPivotColsPageNo(this.dataSet)
    },
    pagesCount: function () {
      return this.getDataSetPagesCount(this.dataSet)
    },
    pivotRowsPagesCount: function () {
      return this.getDataSetPivotRowsPagesCount(this.dataSet)
    },
    pivotColsPagesCount: function () {
      return this.getDataSetPivotColsPagesCount(this.dataSet)
    },
    rowsSumWidth: function () {
      return this.getDataSetRowFieldsSumWidth(this.dataSet)
    },
    rowsLastHeight: function () {
      return this.getDataSetRowFieldsLastHeight(this.dataSet)
    },
    rowsLastWidth: function () {
      return this.getDataSetColFieldsLastWidth(this.dataSet)
    },
    fieldsIdents: function () {
      return this.getDataSetFieldsIdents(this.dataSet)
    },
    keyFieldsIdents: function () {
      return this.getDataSetKeyFieldsIdents(this.dataSet)
    },
    colFields: function () {
      return this.getDataSetColFields(this.dataSet)
    },
    colFieldsSorted: function () {
      return this.getDataSetColFieldsSorted(this.dataSet)
    },
    visibleColFields: function(){
      return this.getDataSetVisibleColFields(this.dataSet)
    },
    visibleColFieldsSorted:  function(){
      return this.getDataSetVisibleColFieldsSorted(this.dataSet)
    },
    rowFields: function () {
      return this.getDataSetRowFields(this.dataSet)
    },
    aggrFields: function () {
      return this.getDataSetAggrFields(this.dataSet)
    },
    aggrsMaxWidth: function () {
      return this.getDataSetAggrFieldsMaxWidth(this.dataSet)
    },
    rowFieldsCount: function () {
      return this.getDataSetRowFieldsCount(this.dataSet)
    },
    where: function () {
      return this.getDataSetWhere(this.dataSet)
    },
    selectedRowsCount: function () {
      return this.getDataSetSelectedRowsCount(this.dataSet)
    },
    errorFields: function () {
      return this.getDataSetErrorFields(this.dataSet, this.contextRow)
    },
    infoFields: function () {
      return this.getDataSetInfoFields(this.dataSet, this.contextRow)
    },
    warningFields: function () {
      return this.getDataSetWarningFields(this.dataSet, this.contextRow)
    },
    infoMessageText: function () {
      return this.infoFields && this.infoFields[this.dataFieldIdent] ? this.infoFields[this.dataFieldIdent].messageText : null
    },
    infoMessageAutoCloseTimeout: function () {
      return this.infoFields && this.infoFields[this.dataFieldIdent] ? this.infoFields[this.dataFieldIdent].autoCloseTimeout : null
    },
    warningMessageText: function () {
      return this.warningFields && this.warningFields[this.dataFieldIdent] ? this.warningFields[this.dataFieldIdent].messageText : null
    },
    warningMessageAutoCloseTimeout: function () {
      return this.warningFields && this.warningFields[this.dataFieldIdent] ? this.warningFields[this.dataFieldIdent].autoCloseTimeout : null
    },
    errorMessageText: function () {
      return this.errorFields && this.errorFields[this.dataFieldIdent] ? this.errorFields[this.dataFieldIdent].messageText : null
    },
    errorMessageAutoCloseTimeout: function () {
      return this.errorFields && this.errorFields[this.dataFieldIdent] ? this.errorFields[this.dataFieldIdent].autoCloseTimeout : null
    },
    componentInfoClass: function () {
      return this.infoMessageText ? 'c-info' : null
    },
    componentWarningClass: function () {
      return this.warningMessageText ? 'c-warning' : null
    },
    componentErrorClass: function () {
      return this.errorMessageText ? 'c-error' : null
    },
    showColsAggrs: function(){
      return this.aggrFields.find(aggrField => aggrField.calc4Cols === 1) ? true : false
    },
    showRowsAggrs: function(){
      return this.aggrFields.find(aggrField => aggrField.calc4Rows === 1) ? true : false
    },
    aggrFields4Cols: function(){
      return this.aggrFields.filter(aggrField => aggrField.calc4Cols === 1)
    },
    aggrFields4Rows: function(){
      return this.aggrFields.filter(aggrField => aggrField.calc4Rows === 1)
    }
  },
  methods: {
    ...mapActions('csAppWindowsUtils', [
      'setDataSetFieldValue',
      'setDataSetCurrentRow',
      'refreshDataSet',
      'clearDataSet'
    ]),
    getDataFieldValue: function (dataFieldIdent) {
      return this.getDataSetFieldValue(this.sourceKind, this.dataSet, dataFieldIdent, this.contextRow, this.pivotRowKey)
    },
    setDataFieldValue: function (dataFieldIdent, dataFieldValue, oncfTimeout, refreshLock) {
      let payload = {
        sourceKind: this.sourceKind,
        appWindowIdent: this.appWindowIdent, 
        dataSet: this.dataSet,
        row: this.contextRow,
        pivotCol: this.pivotCol, 
        pivotRow: this.pivotRow, 
        pivotRowKey: this.pivotRowKey, 
        dataFieldIdent: dataFieldIdent,
        dataFieldValue: dataFieldValue,
        oncfTimeout: oncfTimeout,
        refreshLock: refreshLock
      }
      this.setDataSetFieldValue(payload)
    },
    refresh: function () {
      this.refreshDataSet(Object.assign({refreshAggregates: 1}, this.payload))
    },
    clear: function () {
      this.clearDataSet(Object.assign({refreshAggregates: 1}, this.payload))
    }
  },
  mounted () {
    this.componentId = this._uid
  }
}
</script>