<cd-modal [modalRef]="activeModal">
  <ng-container class="modal-title"
                i18n>OSD Recovery Priority</ng-container>

  <ng-container class="modal-content">
    <form #formDir="ngForm"
          [formGroup]="osdRecvSpeedForm"
          novalidate
          cdFormScope="osd">
      <div class="modal-body">
        <!-- Priority -->
        <div class="form-group row">
          <label class="cd-col-form-label required"
                 for="priority"
                 i18n>Priority</label>
          <div class="cd-col-form-input">
            <select class="form-select"
                    formControlName="priority"
                    id="priority"
                    (change)="onPriorityChange($event.target.value)">
              <option *ngFor="let priority of priorities"
                      [value]="priority.name">
                {{ priority.text }}
              </option>
            </select>
            <span class="invalid-feedback"
                  *ngIf="osdRecvSpeedForm.showError('priority', formDir, 'required')"
                  i18n>This field is required.</span>
          </div>
        </div>

        <!-- Customize priority -->
        <div class="form-group row">
          <div class="cd-col-form-offset">
            <div class="custom-control custom-checkbox">
              <input formControlName="customizePriority"
                     class="custom-control-input"
                     id="customizePriority"
                     name="customizePriority"
                     type="checkbox"
                     (change)="onCustomizePriorityChange()">
              <label class="custom-control-label"
                     for="customizePriority"
                     i18n>Customize priority values</label>
            </div>
          </div>
        </div>

        <!-- Priority values -->
        <div class="form-group row"
             *ngFor="let attr of priorityAttrs | keyvalue">
          <label class="cd-col-form-label"
                 [for]="attr.key">
            <span [ngClass]="{'required': osdRecvSpeedForm.getValue('customizePriority')}">
              {{ attr.value.text }}
            </span>
            <cd-helper *ngIf="attr.value.desc">{{ attr.value.desc }}</cd-helper>
          </label>
          <div class="cd-col-form-input">
            <input class="form-control"
                   type="number"
                   [id]="attr.key"
                   [formControlName]="attr.key"
                   [readonly]="!osdRecvSpeedForm.getValue('customizePriority')">
            <span class="invalid-feedback"
                  *ngIf="osdRecvSpeedForm.getValue('customizePriority') &&
                  osdRecvSpeedForm.showError(attr.key, formDir, 'required')"
                  i18n>This field is required!</span>
            <span class="invalid-feedback"
                  *ngIf="osdRecvSpeedForm.getValue('customizePriority') &&
                  osdRecvSpeedForm.showError(attr.key, formDir, 'pattern')"
                  i18n>{{ attr.value.patternHelpText }}</span>
            <span class="invalid-feedback"
                  *ngIf="osdRecvSpeedForm.getValue('customizePriority') &&
                  osdRecvSpeedForm.showError(attr.key, formDir, 'max')"
                  i18n>The entered value is too high! It must not be greater than {{ attr.value.maxValue }}.</span>
            <span class="invalid-feedback"
                  *ngIf="osdRecvSpeedForm.getValue('customizePriority') &&
                  osdRecvSpeedForm.showError(attr.key, formDir, 'min')"
                  i18n>The entered value is too low! It must not be lower than {{ attr.value.minValue }}.</span>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <cd-form-button-panel (submitActionEvent)="submitAction()"
                              [form]="osdRecvSpeedForm"
                              [submitText]="actionLabels.UPDATE"
                              [showSubmit]="permissions.configOpt.update"></cd-form-button-panel>
      </div>
    </form>
  </ng-container>
</cd-modal>
