<template>
  <div class="btn-group">
    <button
      type="button"
      class="btn btn-default dropdown-toggle"
      aria-haspopup="true"
      aria-expanded="false"
      @click="reverseShow"
    >
      <span class="fa fa-bars" />
    </button>
    <Modal v-model:show="modal">
      <template #header>
        {{ $t("lang.gridColumnDef.gridField") }}
      </template>
      <vuedraggable v-model="gridFields" item-key="id" class="wrapper" draggable=".column-dragable" @end="sortChaned()">
        <template #item="{element}">
          <div class="column-dragable">
            <div class="column">
              <input
                :id="'GridFieldDefItem' + '_' + windowNo + '_' + tabIndex + '_' + element.fieldName + '_' + element.entityFieldIndex"
                v-model="element.visible"
                autocomplete="off"
                class="visible-checkbox"
                type="checkbox"
                :disabled="element.mandatory && element.visible"
                @change="visibleChanged(element)"
              />
              <label
                :for="'GridFieldDefItem' + '_' + windowNo + '_' + tabIndex + '_' + element.fieldName + '_' + element.entityFieldIndex"
                class="column-name"
                :class="{'column-red' : element.mandatory == true}"
              >{{ Language.getDisplayNameTrl($i18n.locale, element) }}</label>
            </div>
          </div>
        </template>
      </vuedraggable>
    </Modal>
  </div>
</template>

<script>

import Common from '../../common/Common.js';

import GridColumnDef from './GridColumnDef.js';
import Language from '../../common/Language.js';

import vuedraggable from 'vuedraggable';


export default {


  components: {
    vuedraggable,
  },
  /**
     * 页签表单字段
     */
  // eslint-disable-next-line
  props: ['windowNo', 'tabIndex', 'tabGridFields'],

  data: function () {
    this.Language = Language;
    return {
      gridFieldItemMap: {},
      gridFields: [],
      modal: false,
    };
  },
  watch: {
    'tabGridFields': function () {
      this.cloneTabGridFields();
    },
  },
  mounted: function () {
    this.cloneTabGridFields();
  },

  methods: {
    /**
         * 克隆页签表单字段
         */
    cloneTabGridFields: function () {
      var _self = this;
      if (_self.tabGridFields == null) {
        return;
      }
      _self.gridFields.splice(0, _self.gridFields.length);
      let tempGridFields = [];
      for (var i = 0, len1 = _self.tabGridFields.length; i < len1; i++) {
        let tempSortNo = _self.tabGridFields[i].sortNo;
        var tabGridFieldClone = {
          'fieldName': _self.tabGridFields[i].fieldName,
          'displayName': _self.tabGridFields[i].displayName,
          'displayNameEng': _self.tabGridFields[i].displayNameEng,
          'entityFieldIndex': _self.tabGridFields[i].entityFieldIndex,
          'mandatory': _self.tabGridFields[i].mandatory,
          'visible': _self.tabGridFields[i].visible,
          'key':('GridFieldDef_' + _self.windowNo + '_'+_self.tabIndex+'_' + _self.tabGridFields[i].fieldName + '_' + _self.tabGridFields[i].entityFieldIndex),
          'sortNo': (tempSortNo == null) ? 100 : tempSortNo,
          'width':_self.tabGridFields[i].width,
        };
        tempGridFields.push(tabGridFieldClone);
      }
      tempGridFields.sort(function(a,b){
        return a.sortNo - b.sortNo;
      });
      tempGridFields.forEach(function(tempGridField){
        _self.gridFields.push(tempGridField);
      });

    },

    /**
         * 表格字段的显示属性发生改变
         * @param { object } gridField 表格字段
         */
    visibleChanged: function (gridField) {
      var _self = this;
      if (gridField.mandatory) {
        gridField.visible = true;
        return;
      }
      GridColumnDef.saveGridFieldDef(_self.windowNo, _self.tabIndex, _self.gridFields).then(successData => {
        _self.$emit('propertyChanged', _self.gridFields);
      }, errorData => {
        Common.processException(errorData);
      });
    },

    /**
         * 显示表单编辑模态框
         */
    reverseShow: function () {
      this.modal = true;
    },

    /**
         * 排序发生改变
         */
    sortChaned:function(){
      console.log('sortChaned');
      var _self = this;
      for (var i = 0, len1 = _self.gridFields.length; i < len1; i++) {
        _self.gridFields[i].sortNo = i * 10;
      }
      GridColumnDef.saveGridFieldDef(_self.windowNo, _self.tabIndex, _self.gridFields).then(successData => {
        _self.$emit('propertyChanged', _self.gridFields);
      }, errorData => {
        console.log(errorData);
      });
    },
  },
};

</script>
<style scoped>
.visible-checkbox {
    width: 17px;
    height: 17px;
    text-align: center;
    margin-right: 15px;
    margin-left: 15px;
    margin-top: 8px;
}
.column-name {
    height: 25px;
    line-height: 25px;
}
.column-red {
    color: red;
}
.m-dropdown-menu {
    display: block;
}
ul label {
    cursor: pointer;
}
</style>