<h3 class="WorkflowDesignerTitleWithCreate">
  {{ parentItem?.Name }} {{ labels.Title }}
  <a v-if="!readonly" @click="add()">{{ ButtonTextCreate }}</a>
</h3>
<div id="WorkflowDesignerCodeActionsParametersList" class="WorkflowDesignerWindowForm">
  <table class="WorkflowDesignerTable">
    <tr>
      <th></th>
      <th>{{ labels.InputParametersParameter }}</th>
      <th>{{ labels.InputParametersName }}</th>
      <th>{{ labels.InputParametersIsRequired }}</th>
      <th>{{ labels.InputParametersDefaultValue }}</th>
    </tr>
    <tr v-for="(item, index) in items" :key="index">
      <td></td>
      <td>
        <el-select
          v-model="item.Parameter.Name"
          :class="validateField('Parameter', item) ? 'WorkflowDesignerInputError' : ''"
          :disabled="readonly"
          :title="validateField('Parameter', item)"
          filterable placeholder=""
          style="width: 100%;"
          >
          <el-option v-for="p in parameters" :key="p.Name" :label="p.Name" :value="p.Name"></el-option>
        </el-select>
      </td>
      <td>
        <el-input
          v-model="item.Name"
          :class="validateField('Name', item) ? 'WorkflowDesignerInputError' : ''"
          :readonly="readonly"
          :title="validateField('Name', item)"
        ></el-input>
      </td>
      <td>
        <el-checkbox
          v-model="item.IsRequired"
          :disabled="readonly"
        ></el-checkbox>
      </td>
      <td>
        <el-input
          v-model="item.DefaultValue"
          :readonly="readonly"
        ></el-input>
      </td>
      <td v-if="!readonly" class="WorkflowDesignerTableEditButtons Triple">
        <el-button-group>
          <el-button :class="'WorkflowDesignerTableCodeActionsButton ' + (editItem == item ? 'is-active' : '')"
                     @click="showjson('DefaultValue', item)"></el-button>
          <el-button icon="IconComment" class="WorkflowDesignerTableCommentButton" v-on:click="showComment(item)"></el-button>
          <el-button class="WorkflowDesignerTableDeleteButton" @click="remove(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 commandParameters_Data(me) {
    return {
      readonly: false,
      editItem: undefined,
      parametersStore: me.graph.data.Parameters,

      items: [],
      parentItem: {
        Name: '',
        Title: ''
      }
    };
  }

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

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

    methods.UpdateLanguage();

    me.VueConfig.watch = {
      // TODO check
      parametersStore: function (val) {
        data.parameters = me.graph.getNonSystemParameters();
      },
    }

    methods.setCurrentItem = function (item) {
      this.currentItem = item;
    };

    methods.typeOnChange = function (item) {
      item.DefaultValue = undefined;
    }

    methods.onUpdate = function (item) {
      data.parameters = me.graph.getNonSystemParameters();
      data.parentItem = item;
      data.items = WorkflowDesignerCommon.clone(item.InputParameters);
      data.readonly = me.graph.Settings.readonly;
    };

    methods.add = function () {
      if (!Array.isArray(data.items))
        data.items = [];

      data.items.push({Parameter: {}, Name: '', IsRequired: false, DefaultValue: ''});
    };

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

    methods.showjson = function (name, item) {
      data.editItem = item;
      me.editItem = item;

      var onSuccess = function (value) {
        if (me.editItem) {
          me.editItem[name] = value;
          data.editItem = undefined;
          delete me.editItem;
        }
      };

      var onClose = function (value) {
        data.editItem = undefined;
      };

      var params = {
        name: item['Rule'],
        type: ['RuleCheck', 'RuleGet']
      };
      data.jsonform = me.showjson(item[name], params, onSuccess, onClose);
    };

    methods.showComment = function (item) {
      const name = 'Comment';
      const title = 'Edit comment';
      const type = WorkflowDesignerConstants.ParameterTypes.TextArea;

      me.editItem = item;

      me.showJsonForm(item[name], {
        customTitle: title,
        parameterDefinition: {type},
        onSuccess: function (value) {
          if (me.editItem) {
            me.editItem[name] = value;
          }
        },
        onClose: function () {
          delete me.editItem;
        }
      })
    };

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

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

      if (name === 'Parameter' && !item?.['Parameter']?.['Name']) {
        return WorkflowDesignerConstants.FieldIsRequired;
      }
    };

    methods.validate = function () {
      var validateFunc = methods.validateField;

      var items = data.items;
      for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (validateFunc('Name', item))
          return false;

        if (Array.isArray(items)) {
          for (var j = 0; j < items.length; j++) {
            if (validateFunc('Name', item))
              return false;
          }
        }
      }
      return true;
    };

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

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

      if (!data.readonly && !WorkflowDesignerCommon.compareArray(
        data.parentItem.InputParameters,
        data.items,
        ['Name', 'IsRequired', 'DefaultValue', 'Comment'])) {

        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>
