<h3>{{ customTitle ? customTitle : labels.Title }}</h3>
<el-form
  ref="form"
  :model="FormData"
  class="WorkflowDesignerWindowForm"
  label-position="top"
  label-width="150px">

  <div v-if="Field.State != undefined" class="SettingsWithPadding">
    <div class="el-form--inline el-form--label-top">
      <el-form-item
        :label="customLabels.Name"
        prop="Name"
        :rules="getFieldRules(Field.Name)"
        class="el-form-item"
        style="flex-grow: 1;">
        <el-input v-model="FormData.Name" :readonly="readonly"></el-input>
      </el-form-item>

      <el-form-item
        :label="customLabels.State"
        prop="State"
        :rules="getFieldRules(Field.State)"
        class="el-form-item"
        style="flex-grow: 1;">
        <el-input v-model="FormData.State" :readonly="readonly"></el-input>
      </el-form-item>
    </div>
  </div>

  <h4 v-if="Field.State != undefined" style="padding-bottom: 1px;border-bottom: 1px solid rgba(34,36,38,.15);">
    {{ customLabels.Heading }}</h4>

  <div :class="Field.State != undefined ?'SettingsWithPadding' : '' " style="margin-bottom: 10px;">
    <el-form-item :label="customLabels.Url" prop="Url" :rules="getFieldRules(Field.Url)">
      <el-input v-model="FormData.Url" :readonly="readonly"></el-input>
    </el-form-item>

    <div class="el-form--inline el-form--label-top">
      <el-form-item
        prop="Post"
        :rules="getFieldRules(Field.Post)">
          <template #label>{{ customLabels.Post }}
            <span :style="parameterFromProcess.Post ? {color:parameterFromProcessColor}: {}"
                  class="WorkflowDesignerParameterFromProcess"
                  @click="togglePost">@
            </span>
          </template>
        <el-checkbox
          v-if="!parameterFromProcess.Post"
          v-model="FormData.Post"
          :disabled="readonly"
          style="width:180px">
        </el-checkbox>
        <el-input v-if="parameterFromProcess.Post" v-model="FormData.Post" :disabled="readonly" style="width:180px"></el-input>
      </el-form-item>
      <el-form-item
        prop="StoreResponse"
        :rules="getFieldRules(Field.StoreResponse)">
          <template #label>{{ customLabels.StoreResponse }}
            <span
              :style="parameterFromProcess.StoreResponse ? {color:parameterFromProcessColor}: {}"
              class="WorkflowDesignerParameterFromProcess"
              @click="toggleStoreResponse">@
            </span>
          </template>
        <el-checkbox
          v-if="!parameterFromProcess.StoreResponse"
          v-model="FormData.StoreResponse"
          :disabled="readonly"
          style="width:180px">
        </el-checkbox>
        <el-input v-if="parameterFromProcess.StoreResponse" v-model="FormData.StoreResponse" :disabled="readonly"
                  style="width:180px"></el-input>
      </el-form-item>
      <el-form-item :label="customLabels.UseAuth">
        <el-checkbox
          v-model="useAuth"
          :disabled="readonly">
        </el-checkbox>
      </el-form-item>
    </div>

    <div v-if="useAuth" class="el-form--inline el-form--label-top">
      <el-form-item
        :label="customLabels.Username"
        prop="Username"
        :rules="getFieldRules(Field.Username)"
        class="el-form-item"
        style="flex-grow: 1;">
        <el-input v-model="FormData.Username" :readonly="readonly"></el-input>
      </el-form-item>
      <el-form-item
        :label="customLabels.Password"
        prop="Password"
        :rules="getFieldRules(Field.Password)"
        class="el-form-item"
        style="flex-grow: 1;">
        <el-input v-model="FormData.Password" :readonly="readonly"></el-input>
      </el-form-item>
    </div>

    <div v-if="FormData.StoreResponse || parameterFromProcess.StoreResponse" class="el-form--inline el-form--label-top">
      <el-form-item
        :label="customLabels.ParameterName"
        prop="ParameterName"
        :rules="getFieldRules(Field.ParameterName)"
        class="el-form-item"
        style="flex-grow: 1;">
        <el-input v-model="FormData.ParameterName" :readonly="readonly"></el-input>
      </el-form-item>
      <el-form-item
        prop="ParameterPurpose"
        :rules="getFieldRules(Field.ParameterPurpose)"
        class="el-form-item"
        style="flex-grow: 1;">
          <template #label>{{ customLabels.ParameterPurpose }}
            <span
              :style="parameterFromProcess.ParameterPurpose ? {color:parameterFromProcessColor}: {}"
              class="WorkflowDesignerParameterFromProcess"
              @click="togglePurpose">@
            </span>
          </template>
        <el-select v-if="!parameterFromProcess.ParameterPurpose" v-model="FormData.ParameterPurpose" :disabled="readonly"
                   style="width:200px">
          <el-option
            v-for="item in Field.ParameterPurpose.DropdownValues"
            :key="item.Value"
            :label="item.Name"
            :value="item.Value">
          </el-option>
        </el-select>
        <el-input v-if="parameterFromProcess.ParameterPurpose" v-model="FormData.ParameterPurpose" :disabled="readonly"
                  style="width:200px"></el-input>
      </el-form-item>
    </div>

    <div v-if="FormData.Post || parameterFromProcess.Post ">
      <el-form-item
        prop="ContentType"
        :rules="getFieldRules(Field.ContentType)">
          <template #label>{{ customLabels.ContentType }}
            <span
              :style="parameterFromProcess.ContentType ? {color:parameterFromProcessColor}: {}"
              class="WorkflowDesignerParameterFromProcess"
              @click="toggleContentType">@
            </span>
          </template>
        <el-select v-if="!parameterFromProcess.ContentType" v-model="FormData.ContentType" :disabled="readonly">
          <el-option
            v-for="item in Field.ContentType.DropdownValues"
            :key="item.Value"
            :label="item.Name"
            :value="item.Value">
          </el-option>
        </el-select>
        <el-input v-if="parameterFromProcess.ContentType" v-model="FormData.ContentType" :disabled="readonly"></el-input>
      </el-form-item>
      <el-form-item
        prop="AddProcessInstanceParameters"
        :rules="getFieldRules(Field.AddProcessInstanceParameters)">
          <template #label>{{ customLabels.AddProcessInstanceParameters }}
            <span
              :style="parameterFromProcess.AddProcessInstanceParameters ? {color:parameterFromProcessColor}: {}"
              class="WorkflowDesignerParameterFromProcess"
              @click="toggleInstanceParameters">@
            </span>
          </template>
        <el-checkbox
          v-if="!parameterFromProcess.AddProcessInstanceParameters"
          v-model="FormData.AddProcessInstanceParameters"
          :disabled="readonly"
          style="width:300px">
        </el-checkbox>
        <el-input v-if="parameterFromProcess.AddProcessInstanceParameters" v-model="FormData.AddProcessInstanceParameters"
                  :disabled="readonly" style="width:300px"></el-input>
      </el-form-item>
      <el-form-item
        :label="customLabels.Parameters"
        prop="Parameters"
        :rules="getFieldRules(Field.Parameters)">
        <el-input v-model="FormData.Parameters" :readonly="readonly" :rows="5" type="textarea"></el-input>
      </el-form-item>
    </div>

    <el-form-item
      :label="customLabels.Headers"
      prop="Headers"
      :rules="getFieldRules(Field.Headers)">
      <el-input v-model="FormData.Headers" :readonly="readonly" :rows="5" type="textarea"></el-input>
    </el-form-item>
  </div>
</el-form>

<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 customforms_basicsplugin_httprequestform_Data(me) {
    return {
      customTitle: undefined,
      readonly: false,
      FormData: {},
      Field: {
        Name: {
          Name: '',
          Title: '',
          Comment: '',
          Type: '',
          IsRequired: false,
          DefaultValue: null
        },
        State: { /* same structure as Name field */},
        Url: { /* same structure as Name field */},
        Post: { /* same structure as Name field */},
        StoreResponse: { /* same structure as Name field */},
        Username: { /* same structure as Name field */},
        Password: { /* same structure as Name field */},
        ParameterName: { /* same structure as Name field */},
        ParameterPurpose: {
          DropdownValues: [
//            {
//              'Value': 'string | number',
            //             'Name': 'string'
//            }
          ]
        },
        ContentType: {
          'DropdownValues': [
            //{
              //'Value': 'string | number',
              //'Name': 'string'
            //}
          ]
        },
        AddProcessInstanceParameters: { /* same structure as Name field */},
        Headers: { /* same structure as Name field */}
      },
      parameterFromProcess: {
        Post: false,
        StoreResponse: false,
        ParameterPurpose: false,
        AddProcessInstanceParameters: false,
        ContentType: false
      },
      useAuth: false
    }

  }

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

    methods.UpdateLanguage = function () {
      Object.assign(data, {
        labels: WorkflowDesignerConstants.EditParametersFormlabel,
        customLabels: WorkflowDesignerConstants.EditParametersFormlabel.CustomForms.HTTPRequest,
        ButtonTextSave: WorkflowDesignerConstants.ButtonTextSave,
        ButtonTextCancel: WorkflowDesignerConstants.ButtonTextCancel,
        parameterFromProcessColor: WorkflowDesignerConstants.ParameterFromProcessColor,
      })
    }

    methods.UpdateLanguage();

    methods.onUpdate = function (value) {
      data.readonly = me.graph.Settings.readonly;

      if (me.options && me.options.customTitle) {
        data.customTitle = me.options.customTitle;
      }

      var fields = undefined;
      var parameterDefinition = me.options.parameterDefinition;

      if (parameterDefinition && parameterDefinition.name) {
        for (var i = 0; i <= parameterDefinition.type.length; i++) {
          fields = me.graph.getActionParameterDefinition(parameterDefinition.name, parameterDefinition.type[i]);
          if (fields && fields.length > 0)
            break;
        }
      } else if (Array.isArray(me.options.parameterDefinition)) {
        fields = me.options.parameterDefinition;
      }

      if (fields && fields.length > 0) {
        fields.forEach(function (field) {
          field.Label = field.Title ? field.Title : field.Name;
          if (field.Comment && field.Comment !== '') {
            field.Label = field.Label + ' (' + field.Comment + ')';
          }

          data.Field[field.Name] = field;
        });

        if (!value && !data.readonly) {
          var tmp = {};

          fields.forEach(function (field) {
            if (field.Type === 'Checkbox') {
              field.DefaultValue = field.DefaultValue !== null && field.DefaultValue !== undefined && field.DefaultValue.toString() === 'true';
            }

            if (typeof field.DefaultValue != 'undefined') {
              tmp[field.Name] = field.DefaultValue;
            } else {
              tmp[field.Name] = null;
            }
          });

          value = WorkflowDesignerCommon.toString(tmp);
        }
      }

      data.FormData = WorkflowDesignerCommon.toJSON(value);

      if (!data.FormData)
        data.FormData = {};

      for (key in data.FormData) {
        if (data.parameterFromProcess[key] != undefined && WorkflowDesignerCommon.containsAt(data.FormData[key]))
          data.parameterFromProcess[key] = true;
        if (key !== '__customtype' && data.FormData.hasOwnProperty(key))
          if (!data.Field.hasOwnProperty(key))
            delete data.FormData[key];
      }

      data.FormData = me.graph.insertDefaultValuesInForm(data.Field, data.FormData);

      data.useAuth = data.FormData.Username != null || data.FormData.Password != null;
    };

    methods.changeParameterInput = function (fieldName) {
      data.parameterFromProcess[fieldName] = !data.parameterFromProcess[fieldName];
    }

    methods.getFieldRules = function (field) {
      try {
        var rules = field.IsRequired ? me.requiredRule() : [{required: false}];

        if (data.parameterFromProcess[field.Name]) {
          rules.push(WorkflowDesignerCommon.containsAtRule);
        }

      } catch (e) {
        console.warn(e);
      }

      return rules;
    };

    methods.onSave = function () {
      if (!data.useAuth) {
        data.FormData.Password = null;
        data.FormData.Username = null;
      }

      var value = undefined;
      value = WorkflowDesignerCommon.toString(data.FormData);

      this.$refs.form.validate(function (valid) {
        if (valid) {
          me.onSuccess(WorkflowDesignerCommon.toCompactJSON(value));
          me.onClose(true);
        }
      });
    };

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

    methods.togglePost = function () {
      data.FormData.Post = data.parameterFromProcess.Post ? false : null;
      methods.changeParameterInput('Post');
    }

    methods.toggleStoreResponse = function () {
      data.FormData.StoreResponse = data.parameterFromProcess.StoreResponse ? false : null;
      methods.changeParameterInput('StoreResponse');
    }

    methods.togglePurpose = function () {
      data.FormData.ParameterPurpose = data.parameterFromProcess.ParameterPurpose ? data.Field.ParameterPurpose.DropdownValues[0].Value : null;
      methods.changeParameterInput('ParameterPurpose');
    }

    methods.toggleContentType = function () {
      data.FormData.ContentType = data.parameterFromProcess.ContentType ? data.Field.ContentType.DropdownValues[0].Value : null;
      methods.changeParameterInput('ContentType');
    }

    methods.toggleInstanceParameters = function () {
      data.FormData.AddProcessInstanceParameters = data.parameterFromProcess.AddProcessInstanceParameters ? false : null;
      methods.changeParameterInput('AddProcessInstanceParameters');
    }
  }
</script>
