import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { OrganizationsService } from '@app/services/organizations.service';
import Swal from 'sweetalert2';
import * as CryptoJS from 'crypto-js';

@Component({
  selector: 'app-new-organization',
  templateUrl: './new-organization.component.html',
  styleUrls: ['./new-organization.component.scss']
})
export class NewOrganizationComponent implements OnInit {
  title:string = "Nueva organización"; 
  organizationForm: FormGroup;
  submitted: boolean = false;
  role_number: any;


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

  }

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

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

  createOrganization() {
    this.submitted = true;

    // stop here if form is invalid
    if (this.organizationForm.invalid) {
      return;
    }

    this.orgService.createOrganization(
      {
        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',
          showCancelButton: false,
          title: 'Exito',
          confirmButtonText: 'El registro ha sido guardado'
        }).then((result) => {
          if (result.value) {
            window.location.href="#/organizations";
          }
        });
      }
      
    });
    


  }


}
