<rb-icon-button icon="add" mode="primary" (click)="newModel = !newModel; oldModelGroup = ''" class="space-below">
  New model
</rb-icon-button>

<form *ngIf="newModel" #modelForm="ngForm">
  <rb-form-input name="group" label="group" appValidate="string" required [(ngModel)]="modelGroup" #groupInput="ngModel"
                 [rbFormInputAutocomplete]="autocomplete.bind(this, groups)"
                 [rbDebounceTime]="0" [rbInitialOpen]="true">
    <ng-template rbFormValidationMessage="failure">{{groupInput.errors.failure}}</ng-template>
    <ng-template rbFormValidationMessage="required">Cannot be empty</ng-template>
  </rb-form-input>
  <rb-form-input name="name" label="name" appValidate="string" required [(ngModel)]="model.name" #nameInput="ngModel">
    <ng-template rbFormValidationMessage="failure">{{nameInput.errors.failure}}</ng-template>
    <ng-template rbFormValidationMessage="required">Cannot be empty</ng-template>
  </rb-form-input>
  <rb-form-input name="url" label="URL" appValidate="url" required [(ngModel)]="model.url" #urlInput="ngModel">
    <ng-template rbFormValidationMessage="failure">{{urlInput.errors.failure}}</ng-template>
    <ng-template rbFormValidationMessage="required">Cannot be empty</ng-template>
  </rb-form-input>

  <rb-icon-button icon="save" mode="primary" type="submit" [disabled]="!modelForm.form.valid" (click)="saveModel()">
    Save model
  </rb-icon-button>
</form>

<rb-table class="space-above space-below">
  <tr>
    <th>Name</th>
    <th>URL</th>
    <th></th>
    <th></th>
  </tr>

  <ng-container *ngFor="let group of d.arr.modelGroups">
    <tr><th>{{group.group}}</th><th></th><th></th><th></th><th></th></tr>
    <tr *ngFor="let modelItem of group.models">
      <td>{{modelItem.name}}</td>
      <td>{{modelItem.url}}</td>
      <td>
        <span class="rb-ic rb-ic-edit clickable"
              (click)="modelGroup = group.group;
                       oldModelGroup = group.group;
                       oldModelName = modelItem.name;
                       model = modelItem;
                       newModel = true;">
        </span>
      </td>
      <td>
        <span class="rb-ic rb-ic-delete clickable"
              (click)="delete(modalDeleteConfirm, modelItem.name, group.group)"></span>
      </td>
    </tr>
  </ng-container>
</rb-table>

<rb-table>
  <tr>
    <th>Model files</th>
    <th></th>
    <th></th>
  </tr>
  <tr *ngFor="let file of d.arr.modelFiles">
    <td>{{file.name}}</td>
    <td>{{file.size | size:'M'}}</td>
    <td><span class="rb-ic rb-ic-delete clickable" (click)="delete(modalDeleteConfirm, file.name)"></span></td>
  </tr>
</rb-table>

<ng-template #modalDeleteConfirm>
  <rb-alert alertTitle="Are you sure?" type="danger" okBtnLabel="Delete model" cancelBtnLabel="Cancel">
    Do you really want to delete?
  </rb-alert>
</ng-template>
