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-organization',
  templateUrl: './organization.component.html',
  styleUrls: ['./organization.component.scss']
})
export class OrganizationComponent implements OnInit {
  [x: string]: any;
  title:string = "Detalle de la 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: err.message
      });
    });

  }

  ngOnInit() { 
  }

  delete_organization(id: string){
    Swal.fire({
      allowOutsideClick: false,
      type: 'warning',
      title: '¿Esta seguro?',
      text: 'No podrá revertir esta acción',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Borrar registro',
    })
    .then((result) => {
        if (result.value) {
          this.delete_organization2(id);
        }
    });
  }

  delete_organization2(id: string){
    this.orgService.deleteOrganization(id)
    .subscribe(
      data => {
        if (data["success"] == true) {
          //document.getElementById(id).remove();
          Swal.fire({
            allowOutsideClick: false,
            type: 'success',
            text: 'Registro eliminado con exito'
          }).then((result) => {
            if (result.value) {
              this.reloadComponent();
            }
          });
          //window.location.href="#/organizations";
        }
        else {
          Swal.fire({
            allowOutsideClick: false,
            type: 'error',
            text: "No tienes permiso para eliminar el registro"
          });
        }  
      },
      err => {
        Swal.fire({
          type: 'error',
          title: 'Error en el servidor',
          text: err.message
        });
      });
  }

  reloadComponent(){
    this.router.routeReuseStrategy.shouldReuseRoute = () => false;
    this.router.onSameUrlNavigation = 'reload';
    this.router.navigate(['/organizations']);
  }

  allowedUser(){
    let is_allowed: boolean;
    if(+this.authService.getUserLevel() < 3){
      is_allowed = false;
    }
    else {
      is_allowed = true;
    }
    return is_allowed
  }

}
