edit-plant.component.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  3. import { PlantsService } from '@app/services/plants.service';
  4. import { ActivatedRoute } from '@angular/router';
  5. import Swal from 'sweetalert2';
  6. @Component({
  7. selector: 'app-edit-plant',
  8. templateUrl: './edit-plant.component.html',
  9. styleUrls: ['./edit-plant.component.scss']
  10. })
  11. export class EditPlantComponent implements OnInit {
  12. title:string = "Editar planta";
  13. listPlant: any;
  14. organizationExists:boolean;
  15. assetForm: FormGroup;
  16. assetID:any;
  17. role_number: any;
  18. assetExists:boolean;
  19. distributor = ["EEO","CAESS","DELSUR","CLESA","DEUSEM","ABRUZZO","EDESAL","B&D","DEL SUR"];
  20. categoria_tarifaria = ["PD", "MD", "GD"];
  21. codigo_tarifa = ["R","G","AP","MD CMP - BT","MD CMP - MT","MD CMH - BT","MD CMH - MT","GD CMH - BT", "GD CMH - MT"];
  22. constructor(private plantsService: PlantsService, private formBuilder: FormBuilder, private route: ActivatedRoute) {
  23. this.route.params.subscribe(params => {
  24. this.assetID = params['id'];
  25. });
  26. this.plantsService.getAssetById(this.assetID).subscribe(res => {
  27. this.listPlant = res["data"]["asset"];
  28. this.organizationExists = true;
  29. this.assetForm = this.formBuilder.group({
  30. name: [this.listPlant.name],
  31. country: [this.listPlant.country],
  32. city: [this.listPlant.city],
  33. address: [this.listPlant.address],
  34. distribuidora: [this.listPlant.distribuidora],
  35. categoria_tarifaria: [this.listPlant.categoria_tarifaria],
  36. cod_tarifa: [this.listPlant.cod_tarifa],
  37. });
  38. this.assetExists = true;
  39. }, (err) => {
  40. Swal.fire({
  41. type: 'error',
  42. title: 'Error en el servidor',
  43. text: err.message
  44. });
  45. });
  46. }
  47. ngOnInit() {
  48. }
  49. get f() { return this.assetForm.controls; }
  50. editAsset() {
  51. this.plantsService.updatePlant(this.assetID,
  52. {
  53. name: this.f.name.value,
  54. country: this.f.country.value,
  55. city: this.f.city.value,
  56. address: this.f.address.value,
  57. distribuidora: this.f.distribuidora.value,
  58. categoria_tarifaria: this.f.categoria_tarifaria.value,
  59. cod_tarifa: this.f.cod_tarifa.value,
  60. meters_installed: this.listPlant.meters_installed
  61. }
  62. )
  63. .subscribe(success => {
  64. if (success) {
  65. Swal.fire({
  66. allowOutsideClick: false,
  67. type: 'success',
  68. text: 'Informacion actualizada con exito'
  69. });
  70. //window.location.href="#/dashboard";
  71. }
  72. });
  73. }
  74. }