<app-common-dialog [dialogContent]="dialogContent" [dialogRef]="dialogRef">
  <ng-template #dialogContent>
    <form appFormSubmit (ngSubmit)="addIpToInstance()" [formGroup]="addIpForm">
      <app-form-errors #formErrors [formGroup]="addIpForm"></app-form-errors>
      <div layoutDirection="row">
        <mat-form-field appFlex="100">
          <mat-label i18n>Port</mat-label>
          <mat-select formControlName="port" placeholder="Port" i18n-placeholder (selectionChange)="changeSubnets()">
            <mat-option *ngFor="let port of ports" [value]="port.id">
              {{port.name ? port.name + ' - ' + port.id : port.id}}
            </mat-option>
          </mat-select>
          <mat-error i18n>This field is required</mat-error>
        </mat-form-field>
      </div>
      <div layoutDirection="row" *ngIf="addIpForm.controls.port.value">
        <mat-form-field appFlex="100">
          <mat-label i18n>Subnet</mat-label>
          <mat-select formControlName="subnet" placeholder="Subnet" i18n-placeholder>
            <mat-option *ngFor="let subnet of subnets" [value]="subnet.id">
              {{subnet.name}} ({{subnet.cidr}})
            </mat-option>
          </mat-select>
          <mat-error i18n>This field is required</mat-error>
        </mat-form-field>
      </div>
      <div formArrayName="fixed_ips" layoutDirection="row"
      *ngFor="let fixedIp of addIpForm.get('fixed_ips')['controls']; let i = index;">
        <div [formGroupName]="i" layoutDirection="row">
          <mat-form-field>
            <mat-label i18n>IP address</mat-label>
            <input matInput formControlName="ip_address" placeholder="IP address" i18n-placeholder
                   type="text" required>
            <mat-error i18n>{{fixedIp.controls["ip_address"].errors | errorFormatter}}</mat-error>
            <button mat-icon-button matSuffix (click)="removeIp(i)" [attr.fl-tooltip]="removeIpTooltip">
              <mat-icon>delete</mat-icon>
            </button>
          </mat-form-field>
        </div>
      </div>
      <button mat-mini-fab class="add-item-button fl-margin-top-small fl-margin-left-small fl-margin-bottom-medium"
              [attr.fl-tooltip]="addIpTooltip" fl-tooltip-direction="right"
              (click)="addIp()">
        <mat-icon>add</mat-icon>
      </button>
    </form>
  </ng-template>
</app-common-dialog>
