<%_
const query = generateEntityQueries(relationships, entityInstance, dto);
const queries = query.queries;
const variables = query.variables;
let hasManyToMany = query.hasManyToMany;
_%>
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
<%_ if ( fieldsContainInstant || fieldsContainZonedDateTime ) { _%>
import * as moment from 'moment';
import { DATE_TIME_FORMAT } from 'app/shared/constants/input.constants';
<%_ } _%>
<%_ if (queries && queries.length > 0 || fieldsContainBlob) { _%>
import { <% if (queries && queries.length > 0) { %>JhiAlertService, <% } %><% if (fieldsContainBlob) { %>JhiDataUtils<% } %> } from 'ng-jhipster';
<%_ } _%>

import { I<%= entityAngularName %> } from 'app/shared/model/<%= entityModelFileName %>.model';
import { <%= entityAngularName %>Service } from './<%= entityFileName %>.service';

<%_ for (idx in relationships) {
        const relationshipType = relationships[idx].relationshipType;
        if (relationshipType === 'many-to-one' || relationshipType === 'one-to-one' || relationshipType === 'many-to-many') {
      _%>
import { <%= relationships[idx].otherEntityAngularName %>SelectDialogService } from '../<%= relationships[idx].otherEntityFileName %>/<%= relationships[idx].otherEntityFileName %>-select-dialog.service';
<%_ } _%>
<%_ } _%>
<%_
let hasRelationshipQuery = false;
Object.keys(differentRelationships).forEach(key => {
    const hasAnyRelationshipQuery = differentRelationships[key].some(rel =>
        (rel.relationshipType === 'one-to-one' && rel.ownerSide === true && rel.otherEntityName !== 'user')
            || rel.relationshipType !== 'one-to-many'
    );
    if (hasAnyRelationshipQuery) {
        hasRelationshipQuery = true;
    }
    if (differentRelationships[key].some(rel => rel.relationshipType !== 'one-to-many')) {
        const uniqueRel = differentRelationships[key][0];
        if (uniqueRel.otherEntityAngularName !== entityAngularName) {
            if (uniqueRel.otherEntityAngularName === 'User') {
_%>
import { I<%= uniqueRel.otherEntityAngularName %>, <%= uniqueRel.otherEntityAngularName%>Service } from 'app/core';
<%_         } else { _%>
import { I<%= uniqueRel.otherEntityAngularName %> } from 'app/shared/model/<%= uniqueRel.otherEntityModelName %>.model';
import { <%= uniqueRel.otherEntityAngularName%>Service } from 'app/entities/<%= uniqueRel.otherEntityPath %>';
<%_         }
        }
    }

}); _%>
@Component({
    selector: '<%= jhiPrefixDashed %>-<%= entityFileName %>-update',
    templateUrl: './<%= entityFileName %>-update.component.html'
})
export class <%= entityAngularName %>UpdateComponent implements OnInit {
  private _<%= entityInstance %>: I<%= entityAngularName %>;
  isSaving: boolean;
  <%_ for ( idx in fields ) {
      const fieldName = fields[idx].fieldName;
      const fieldType = fields[idx].fieldType;
      if ( fieldType === 'LocalDate' ) { _%>
  <%= fieldName %>Dp: any;
  <%_ } else if ( ['Instant', 'ZonedDateTime'].includes(fieldType) ) { _%>
  <%= fieldName %>: string;
    <%_ } _%>
  <%_ } _%>
  constructor(
    <%_ if (fieldsContainBlob) { _%>
      private dataUtils: JhiDataUtils,
      <%_ } _%>
      <%_ if (queries && queries.length > 0) { _%>
      private jhiAlertService: JhiAlertService,
      <%_ } _%>
      private <%= entityInstance %>Service: <%= entityAngularName %>Service,
      <%_ Object.keys(differentRelationships).forEach(key => {
          if (differentRelationships[key].some(rel => rel.relationshipType !== 'one-to-many')) {
              const uniqueRel = differentRelationships[key][0];
              if (uniqueRel.otherEntityAngularName !== entityAngularName) { _%>
      private <%= uniqueRel.otherEntityName %>Service: <%= uniqueRel.otherEntityAngularName %>Service,
      <%_
              }
          }
      }); _%>
      <%_ if (fieldsContainImageBlob) { _%>
      private elementRef: ElementRef,
      <%_ } _%>
      private route: ActivatedRoute,
      <%_ for (idx in relationships) {
        const relationshipType = relationships[idx].relationshipType;
        const relationshipName = relationships[idx].relationshipName;
        if (relationshipType === 'many-to-one' || relationshipType === 'one-to-one' || relationshipType === 'many-to-many') {
      _%>
      private <%= relationshipName %>SelectDialogService: <%= relationships[idx].otherEntityAngularName %>SelectDialogService,
      <%_ } _%>
      <%_ } _%>
  ) {}

  ngOnInit() {
      this.isSaving = false;
      this.route.data.subscribe(({<%= entityInstance %>}) => {
          this.<%= entityInstance %> = <%= entityInstance %>.body ? <%= entityInstance %>.body : <%= entityInstance %>;
      });
  }
  <%_ if (fieldsContainBlob) { _%>
  byteSize(field) {
      return this.dataUtils.byteSize(field);
  }
  openFile(contentType, field) {
      return this.dataUtils.openFile(contentType, field);
  }
  setFileData(event, entity, field, isImage) {
      this.dataUtils.setFileData(event, entity, field, isImage);
  }
  <%_ if (fieldsContainImageBlob) { _%>
  clearInputImage(field: string, fieldContentType: string, idInput: string) {
      this.dataUtils.clearInputImage(this.<%= entityInstance %>, this.elementRef, field, fieldContentType, idInput);
  }
  <%_ } _%>
  <%_ } _%>

  previousState() {
    window.history.back();
  }

  save() {
    this.isSaving = true;
    <%_ for (idx in fields) {
        const fieldName = fields[ idx ].fieldName;
        const fieldType = fields[idx].fieldType;
        if ([ 'Instant', 'ZonedDateTime' ].includes(fieldType)) { _%>
            this.<%= entityInstance %>.<%= fieldName %> = moment(this.<%= fieldName %>, DATE_TIME_FORMAT);
    <%_ }
    } _%>
    if (this.<%= entityInstance %>.id !== undefined) {
        this.subscribeToSaveResponse(
            this.<%= entityInstance %>Service.update(this.<%= entityInstance %>));
    } else {
        this.subscribeToSaveResponse(
            this.<%= entityInstance %>Service.create(this.<%= entityInstance %>));
    }
  }

  private subscribeToSaveResponse(result: Observable<HttpResponse<I<%= entityAngularName %>>>) {
      result.subscribe((res: HttpResponse<I<%= entityAngularName %>>) =>
          this.onSaveSuccess(res.body), (res: HttpErrorResponse) => this.onSaveError());
  }

  private onSaveSuccess(result: I<%= entityAngularName %>) {
      this.isSaving = false;
      this.previousState();
  }

  private onSaveError() {
      this.isSaving = false;
  }

  <%_ if (queries && queries.length > 0) { _%>
  private onError(errorMessage: string) {
      this.jhiAlertService.error(errorMessage, null, null);
  }

  <%_ } _%>
  <%_
  const entitiesSeen = [];
  for (idx in relationships) {
      const otherEntityNameCapitalized = relationships[idx].otherEntityNameCapitalized;
      if (relationships[idx].relationshipType !== 'one-to-many' && !entitiesSeen.includes(otherEntityNameCapitalized)) {
  _%>
  track<%= otherEntityNameCapitalized %>ById(index: number, item: I<%= relationships[idx].otherEntityAngularName %>) {
      return item.id;
  }

  <%_ entitiesSeen.push(otherEntityNameCapitalized); } } _%>
  <%_ if (hasManyToMany) { _%>
  getSelected(selectedVals: Array<any>, option: any) {
      if (selectedVals) {
          for (let i = 0; i < selectedVals.length; i++) {
              if (option.id === selectedVals[i].id) {
                  return selectedVals[i];
              }
          }
      }
      return option;
  }
  <%_ } _%>
  get <%= entityInstance %>() {
      return this._<%= entityInstance %>;
  }

  set <%= entityInstance %>(<%= entityInstance %>: I<%= entityAngularName %>) {
      this._<%= entityInstance %> = <%= entityInstance %>;
  <%_ for (idx in fields) {
      const fieldName = fields[idx].fieldName;
      const fieldType = fields[idx].fieldType;
      if (['Instant', 'ZonedDateTime'].includes(fieldType)) { _%>
      this.<%= fieldName %> = moment(<%= entityInstance %>.<%= fieldName %>).format();
   <%_ } } _%>
  }

  <%_ for (idx in relationships) {
      const relationshipType = relationships[idx].relationshipType;
      const relationshipName = relationships[idx].relationshipName;
      if (relationshipType === 'many-to-one' || relationshipType === 'one-to-one') {
  _%>
  change<%= relationships[idx].otherEntityAngularName %>() {
      this.<%= relationshipName %>SelectDialogService.open((selected: Array<I<%= relationships[idx].otherEntityAngularName %>>) => {
          this.<%= entityInstance %>.<%= relationshipName %> = selected[0];
      }, [this.<%= entityInstance %>.<%= relationshipName %>]);
  }

  <%_ } else if (relationshipType === 'many-to-many') { _%>
  change<%= relationships[idx].otherEntityAngularName %>() {
      this.<%= relationshipName %>SelectDialogService.open((selected: Array<I<%= relationships[idx].otherEntityAngularName %>>) => {
          this.<%= entityInstance %>.<%= relationships[idx].relationshipNamePlural %> = selected;
      }, this.<%= entityInstance %>.<%= relationships[idx].relationshipNamePlural %>, true);
  }

  <%_ } _%>
  <%_ } _%>
}
