import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { PlantsService } from '@app/services/plants.service';
import { ActivatedRoute } from '@angular/router';
import Swal from 'sweetalert2';

@Component({
  selector: 'app-edit-plant',
  templateUrl: './edit-plant.component.html',
  styleUrls: ['./edit-plant.component.scss']
})
export class EditPlantComponent implements OnInit {
  title:string = "Editar planta"; 

  listPlant: any;
  organizationExists:boolean;
  assetForm: FormGroup;
  assetID:any;
  role_number: any;
  assetExists:boolean;
  distributor = ["EEO","CAESS","DELSUR","CLESA","DEUSEM","ABRUZZO","EDESAL","B&D","DEL SUR"];
  categoria_tarifaria = [
    { key: "PD", value: "PD Pequeña Demanda" },
    { key: "MD", value: "MD Mediana Demanda" },
    { key: "GD", value: "GD Gran Demanda" }
  ];

  codigo_tarifa = [
    { key:"R", value: "R Residencial" },
    { key:"G", value: "G General" },
    { key:"AP", value: "AP Alumbrado Público" },
    { key:"MD CMP - BT" ,value: "MD CMP-BT Mediana Demanda Con Medicion de Potencia Baja Tensión" },
    { key:"MD CMP - MT" ,value: "MD CMP-MT Mediana Demanda Con Medicion de Potencia  Media Tensión" },
    { key:"MD CMH - BT" ,value: "MD CMH-BT Mediana Demanda Con Medicion Horaria Baja Tensión" },
    { key:"MD CMH - MT" ,value: "MD CMH-MT Mediana Demanda Con Medicion Horaria Media Tensión" },
    { key:"GD CMH - BT", value: "GD CMH-BT Gran Demanda Con Medicion Horaria Baja Tensión" },
    { key:"GD CMH - MT", value: "GD CMH-MT Gran Demanda Con Medicion Horaria Media Tensión" }
  ];

  constructor(private plantsService: PlantsService, private formBuilder: FormBuilder, private route: ActivatedRoute) {
    
    this.route.params.subscribe(params => {
      this.assetID = params['id'];
    });

    this.plantsService.getAssetById(this.assetID).subscribe(res => {
      this.listPlant = res["data"]["asset"];
      this.organizationExists = true;
      this.assetForm = this.formBuilder.group({
        name: [this.listPlant.name],
        country: [this.listPlant.country],
        city: [this.listPlant.city],
        address: [this.listPlant.address],
        distribuidora: [this.listPlant.distribuidora],
        categoria_tarifaria: [this.listPlant.categoria_tarifaria],
        cod_tarifa: [this.listPlant.cod_tarifa],
      });
      this.assetExists = true;
    }, (err) => {
      Swal.fire({
        type: 'error',
        title: 'Error en el servidor',
        text: err.message
      });
    });

  }

  ngOnInit() {
  
  }

  get f() { return this.assetForm.controls; }
  editAsset() {
    this.plantsService.updatePlant(this.assetID, 
      {
        name: this.f.name.value,
        country: this.f.country.value,
        city: this.f.city.value,
        address: this.f.address.value,
        distribuidora: this.f.distribuidora.value,
        categoria_tarifaria: this.f.categoria_tarifaria.value,
        cod_tarifa: this.f.cod_tarifa.value,
        meters_installed: this.listPlant.meters_installed
      }
    )
    .subscribe(success => {
      if (success) {
        Swal.fire({
          allowOutsideClick: false,
          type: 'success',
          text: 'Informacion actualizada con exito'
        });
        //window.location.href="#/dashboard";

      }
      
    });
  }


}
