import { Component, OnInit } from '@angular/core';
import { NavBarService } from '../../../admin.service';
import { AuthService } from '../../../../services/auth.service';
import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';

@Component({
  selector: 'app-roles-admin',
  templateUrl: './roles-admin.component.html',
  styleUrls: ['./roles-admin.component.scss']
})
export class RolesAdminComponent implements OnInit {

  roles = [];
  idToDelete: number;
  dialogData: any;

  constructor(private AuthService: AuthService,
    private toastr: ToastsManager) {
      this.getAllRole()
     }

  ngOnInit() {
    this.dialogData = { title: 'Delete Role', text: 'Do you really want to delete the Role?'}
  }

  getAllRole(){
    this.AuthService.getAllRoles().then(resp =>{
      console.log(resp,'getAllRoles')
      this.roles = resp.roleList;
    }).catch(err =>{
      console.log(err)
    })
  }

  deleteEventTrig(role){
       document.getElementById("delRolConfModal").click();
    this.idToDelete = role.roleId
  }
  confirmDelete(evnt){
    this.AuthService.getDeleteRole(this.idToDelete).then(resp => {
      if (resp.status == 'error') {
        this.toastr.error(resp.message);
      } else {
        this.toastr.success(resp.successMessage);
        this.getAllRole();
      }
      }).catch(err => {
        console.log(err);
        this.toastr.error('Role Delete pending - Contact Owner  ' +err);
      })
    }
}
