Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.0 kB
25
Indexable
Never
import {FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators} from '@angular/forms';
import {Component, forwardRef, Injector, OnInit} from '@angular/core';
import { LayoutConfig } from '@insight/shared-data-access-model-generic';
import {WorkOrderEditBaseComponent} from './work-order-edit.base';
import {Observable} from "rxjs";
import {
  Construct,
  WorkOrder,
  WorkOrderAssignment,
  WorkOrderEffort,
  WorkOrderMaterial
} from "@insight/shared-data-access-model-construct";
import { DialogService, DynamicDialogRef } from 'primeng/dynamicdialog';
import { TemplateExpandoApi, WorkOrderTypeApi } from '@insight/shared-data-access-api-construct';


@Component({
	selector: 'insight-work-order-edit',
	templateUrl: './work-order-edit.component.html',
	styleUrls: ['./work-order-edit.component.scss'],
	providers: [
		{
			provide: NG_VALUE_ACCESSOR,
			useExisting: forwardRef(() => WorkOrderEditComponent),
			multi: true
		}
	]
})
export class WorkOrderEditComponent extends WorkOrderEditBaseComponent implements OnInit{
  workOrderType = [];

  message: string;
  layout = new LayoutConfig();
  efforts: WorkOrderEffort[] = [];
  materials: WorkOrderMaterial[] = [];
  assignments: WorkOrderAssignment[] = [];
  ref: DynamicDialogRef;

  workType = 1;

  editForm: FormGroup;

	constructor(
    private formBuilder: FormBuilder,
    injector: Injector,
    dialogService: DialogService) {
		super(injector);
	};

  ngOnInit(): void {
    this.editForm = this.formBuilder.group({
      name: ['',Validators.required],
      orderTypeId: ['',Validators.required],
      orderDate: ['',Validators.required],
      estStartDate: ['',Validators.required],
      estEndDate:['',Validators.required],
      estFinishDate:['',Validators.required],
      status: ['',Validators.required],
      description: ['',Validators.required]

    })
    this.injector.get(TemplateExpandoApi).searchByContext(this.serviceContext).subscribe((res) => {
      this.workOrderType = res;
      console.log(res);

    })
  }


  entityToForm(entity: WorkOrder) {
    super.entityToForm(entity);

    if (this.entity) {
      this.efforts = entity.efforts;
    } else {
      this.efforts = [];
    }
    if (this.entity) {
      this.assignments = entity.assignments;
    } else {
      this.assignments = [];
    }
    if (this.entity) {
      this.materials = entity.materials;
    } else {
      this.materials = [];
    }
  }

  protected formToEntity() {
    super.formToEntity();

    if (this.efforts) {
      this.entity.efforts = JSON.parse(JSON.stringify(this.efforts));
    } else {
      this.entity.efforts = [];
    }
    if (this.materials) {
      this.entity.materials = JSON.parse(JSON.stringify(this.materials));
    } else {
      this.entity.materials = [];
    }
    if (this.assignments) {
      this.entity.assignments = JSON.parse(JSON.stringify(this.assignments));
    } else {
      this.entity.assignments = [];
    }
  }

  get createObservable(): Observable<WorkOrder> {
    const orderType = this.activatedRoute.snapshot.queryParamMap.get('type');
    const request = this.createEntity();

    request.orderType = {code: orderType};

    return this.cloudService.createByOrderType(request);
  }

  protected executeAdd(model: WorkOrder): Observable<WorkOrder> {
    return this.cloudService.addAssociate(model);
  }

  protected executeUpdate(model: WorkOrder): Observable<WorkOrder> {
    return this.cloudService.updateAssociate(model);
  }

  protected executeDelete(model: WorkOrder): Observable<WorkOrder> {
    return this.cloudService.deleteAssociate(model);
  }


  onWorkTypeChange(event:any){
    this.injector.get(TemplateExpandoApi).searchByContext(this.serviceContext).subscribe((res) => {
      this.workOrderType = res;
      console.log(res);
    })
  }

  // countChangedHandler(event: any) {
  //   this.workOrderType = event;
  // }
}