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-plant',
  templateUrl: './plant.component.html',
  styleUrls: ['./plant.component.scss']
})
export class PlantComponent implements OnInit {
  [x: string]: any;
  title:string = "Detalle de la planta"; 
  listPlant: any;
  organizationExists:boolean;
  assetForm: FormGroup;
  assetID:any;
  role_number: any;
  assetExists:boolean;
  distributor = ['CAESS', 'DEL SUR', 'AES-CLESA', 'EEO', 'DEUSEM'];
  
  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() { 
  }

}
