<h3 class="WorkflowDesignerTitleWithCreate">
  {{ (name || labels.Dropdown) + " " + labels.Values }}
  <a v-if="!readonly" @click="addDropdownValues()">{{ ButtonTextCreate }}</a>
</h3>
<div id="WorkflowDesignerDropdownValues" class="WorkflowDesignerWindowForm">
  <table class="WorkflowDesignerTable">
    <tr>
      <th></th>
      <th>{{ labels.Name }}</th>
      <th>{{ labels.Value }}</th>
    </tr>
    <tr v-for="(item, index) in items" :key="index"
        :class="dragOverIndex == index && dragOverBlock  == item ? 'dragOver' : ''"
        @dragend="dragend($event)" @dragover="dragover(item, index, $event)"
        @dragstart="dragstart(index, $event)">
      <td :draggable="!readonly" class='WorkflowDesignerTableMoveCol' style="cursor:grab;">
        <div v-if="!readonly" class='WorkflowDesignerTableMoveButton'></div>
      </td>
      <td>
        <el-input
          v-model="item.Name"
          :class="validateFieldDD('Name', item) ? 'WorkflowDesignerInputError' : ''"
          :readonly="readonly"
          :title="validateFieldDD('Name', item)">
        </el-input>
      </td>
      <td>
        <el-input v-model="item.Value"
                  :class="validateFieldDD('Name', item) ? 'WorkflowDesignerInputError' : ''"
                  :readonly="readonly"
                  :title="validateFieldDD('Name', item)">
        </el-input>
      </td>

      <td v-if="!readonly" class="WorkflowDesignerTableEditButtons Double">
        <el-button-group>
          <el-button class="WorkflowDesignerTableDeleteButton" @click="removeDropdownValues(item, index)"></el-button>
        </el-button-group>
      </td>
    </tr>
  </table>
</div>
<div class="WorkflowDesignerButtons">
  <el-button v-if="!readonly" type="primary" @click="onSave">{{ ButtonTextSave }}</el-button>
  <el-button @click="onClose">{{ ButtonTextCancel }}</el-button>
</div>
<script type="application/javascript">
  function dropdownValues_Data(me) {
    return {
      readonly: false,
      name: undefined,
      items: []
    }
  }

  function dropdownValues_Init(me) {
    const {VueConfig} = me;
    const {state: data, methods} = VueConfig;

    methods.UpdateLanguage = function () {
      Object.assign(data, {
        labels: WorkflowDesignerConstants.DropdownValues,
        ButtonTextCreate: WorkflowDesignerConstants.ButtonTextCreate,
        ButtonTextDelete: WorkflowDesignerConstants.ButtonTextDelete,
        ButtonTextSave: WorkflowDesignerConstants.ButtonTextSave,
        ButtonTextCancel: WorkflowDesignerConstants.ButtonTextCancel,
      });
    }

    methods.UpdateLanguage();

    methods.onUpdate = function (parentItem) {
      me.parentItem = parentItem;

      data.items = WorkflowDesignerCommon.clone(parentItem.DropdownValues);
      data.name = parentItem.Name;
      data.readonly = me.graph.Settings.readonly;
    };

    methods.addDropdownValues = function () {
      data.items.push({value: '', name: ''});
    };

    methods.removeDropdownValues = function (item, index) {
      data.items.splice(index, 1);
    };

    methods.validateFieldDD = function (name, item) {
      if (name !== 'Name' && name !== 'Value')
        return;

      if (!item[name]) {
        return WorkflowDesignerConstants.FieldIsRequired;
      }
    };

    methods.validate = function () {
      var validateFunc = methods.validateFieldDD;
      var items = data.items;
      for (var i = 0; i < items.length; i++) {
        var item = items[i];

        if (validateFunc('Name', item) || validateFunc('Value', item))
          return false;
      }

      return true;
    };

    methods.onSave = function () {
      if (methods.validate && methods.validate()) {
        me.graph.UpdateData(me.parentItem.DropdownValues, data.items);
        me.onClose(true);
      }
    };

    methods.onClose = function () {
      if (data.readonly) {
        me.onClose(true);
        return;
      }

      if (!data.readonly && !WorkflowDesignerCommon.compareArray(
        me.parentItem.DropdownValues,
        data.items,
        ['Name', 'Value'])) {

        me.showConfirm();
        return false;
      } else {
        me.onClose(true);
      }
    };

    methods.onCloseSave = function () {
      me.onClose(true);
    };

    me.showConfirm = function () {
      methods.showConfirm({
        title: WorkflowDesignerConstants.DialogConfirmText,
        message: WorkflowDesignerConstants.CloseWithoutSaving,
        onSuccess: function () {
          methods.onCloseSave();
        }
      });
    }
  }
</script>
