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

@Component({
  selector: 'app-edit-organization',
  templateUrl: './edit-organization.component.html',
  styleUrls: ['./edit-organization.component.scss']
})
export class EditOrganizationComponent implements OnInit {
  [x: string]: any;
  title:string = "Editar organización"; 
  listOrganization: any;
  organizationExists:boolean;
  organizationForm: FormGroup;
  organizationId:any;
  role_number: any;

  constructor(private orgService: OrganizationsService, private formBuilder: FormBuilder, private route: ActivatedRoute) {

    this.route.params.subscribe(params => {
      this.organizationId = params['id'];
    });

    this.orgService.getOrganization(this.organizationId).subscribe(res => {
      this.listOrganization = res["data"]["organization"];
      this.organizationExists = true;

      this.organizationForm = this.formBuilder.group({
        name: [this.listOrganization.name],
        address: [this.listOrganization.address],
        city: [this.listOrganization.city],
        country: [this.listOrganization.country],
        contactName: [this.listOrganization.contactName],
        contactNumber: [this.listOrganization.contactNumber],
      });


    }, (err) => {
      Swal.fire({
        type: 'error',
        title: 'Error en el servidor',
        text: "No su pudo obtener la informacion"
      });
    });

  }

  ngOnInit() {
    /*
    this.organizationForm = this.formBuilder.group({
      name: [this.listOrganization.name],
      address: [this.listOrganization.address],
      city: [this.listOrganization.city],
      country: [this.listOrganization.country],
      contactName: [this.listOrganization.contactName],
      contactNumber: [this.listOrganization.contactNumber],
    });

    this.organizationForm = this.formBuilder.group({
      name: [""], //this.listOrganization.name
      address: [""], //this.listOrganization.address
      city: [""], //this.listOrganization.city
      country: [""], //this.listOrganization.country
      contactName: [""], //this.listOrganization.contactName
      contactNumber: [""], //this.listOrganization.contactNumber
    });
    */
  }

  get f() { return this.organizationForm.controls; }

  editOrganization() {
      this.orgService.updateOrganization(this.organizationId, 
        {
          name: this.f.name.value,
          address: this.f.address.value,
          city: this.f.city.value,
          country: this.f.country.value,
          contactName: this.f.contactName.value,
          contactNumber: this.f.contactNumber.value,
          assets: [""],
        }
      )
    .subscribe(success => {
      if (success) {
        Swal.fire({
          allowOutsideClick: false,
          type: 'success',
          text: 'Informacion actualizada con exito'
        });
        //window.location.href="#/dashboard";

      }
      
    });
  }



}
