<cd-loading-panel *ngIf="loading && !error"
                  i18n>Loading configuration...</cd-loading-panel>
<cd-alert-panel type="error"
                *ngIf="loading && error"
                i18n>The configuration could not be loaded.</cd-alert-panel>

<div class="cd-col-form"
     *ngIf="!loading && !error">
  <form name="mgrModuleForm"
        #frm="ngForm"
        [formGroup]="mgrModuleForm"
        novalidate>
    <div class="card">
      <div class="card-header"
           i18n>Edit Manager module</div>
      <div class="card-body">
        <div class="form-group row"
             *ngFor="let moduleOption of moduleOptions | keyvalue">

          <!-- Field label -->
          <label class="col-form-label col-sm-5"
                 for="{{ moduleOption.value.name }}">
            {{ moduleOption.value.name }}
            <cd-helper *ngIf="moduleOption.value.long_desc || moduleOption.value.desc">
              {{ moduleOption.value.long_desc || moduleOption.value.desc | upperFirst }}
            </cd-helper>
          </label>

          <!-- Field control -->
          <!-- bool -->
          <div class="col-sm-7"
               *ngIf="moduleOption.value.type === 'bool'">
            <div class="custom-control custom-checkbox">
              <input id="{{ moduleOption.value.name }}"
                     type="checkbox"
                     class="custom-control-input"
                     formControlName="{{ moduleOption.value.name }}">
              <label class="custom-control-label"
                     for="{{ moduleOption.value.name }}"></label>
            </div>
          </div>

          <!-- addr|str|uuid -->
          <div class="col-sm-7"
               *ngIf="['addr', 'str', 'uuid'].includes(moduleOption.value.type)">
            <input id="{{ moduleOption.value.name }}"
                   class="form-control"
                   type="text"
                   formControlName="{{ moduleOption.value.name }}"
                   *ngIf="moduleOption.value.enum_allowed.length === 0">
            <select id="{{ moduleOption.value.name }}"
                    class="form-control custom-select"
                    formControlName="{{ moduleOption.value.name }}"
                    *ngIf="moduleOption.value.enum_allowed.length > 0">
              <option *ngFor="let value of moduleOption.value.enum_allowed"
                      [ngValue]="value">
                {{ value }}
              </option>
            </select>
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'invalidUuid')"
                  i18n>The entered value is not a valid UUID, e.g.: 67dcac9f-2c03-4d6c-b7bd-1210b3a259a8</span>
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'pattern')"
                  i18n>The entered value needs to be a valid IP address.</span>
          </div>

          <!-- uint|int|size|secs -->
          <div class="col-sm-7"
               *ngIf="['uint', 'int', 'size', 'secs'].includes(moduleOption.value.type)">
            <input id="{{ moduleOption.value.name }}"
                   class="form-control"
                   type="number"
                   formControlName="{{ moduleOption.value.name }}"
                   min="{{ moduleOption.value.min }}"
                   max="{{ moduleOption.value.max }}">
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'required')"
                  i18n>This field is required.</span>
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'max')"
                  i18n>The entered value is too high! It must be lower or equal to {{ moduleOption.value.max }}.</span>
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'min')"
                  i18n>The entered value is too low! It must be greater or equal to {{ moduleOption.value.min }}.</span>
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'pattern')"
                  i18n>The entered value needs to be a number.</span>
          </div>

          <!-- float -->
          <div class="col-sm-7"
               *ngIf="moduleOption.value.type === 'float'">
            <input id="{{ moduleOption.value.name }}"
                   class="form-control"
                   type="number"
                   formControlName="{{ moduleOption.value.name }}">
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'required')"
                  i18n>This field is required.</span>
            <span class="invalid-feedback"
                  *ngIf="mgrModuleForm.showError(moduleOption.value.name, frm, 'pattern')"
                  i18n>The entered value needs to be a number or decimal.</span>
          </div>

        </div>
      </div>
      <div class="card-footer">
        <div class="button-group text-right">
          <cd-submit-button (submitAction)="onSubmit()"
                            [form]="mgrModuleForm">
            <ng-container i18n>Update</ng-container>
          </cd-submit-button>
          <button type="button"
                  class="btn btn-light"
                  routerLink="/mgr-modules"
                  i18n>Back</button>
        </div>
      </div>
    </div>
  </form>
</div>
