import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { CommonFunctionService } from 'src/app/core/common/common-function.service';
import { securityFunctions } from 'src/app/core/common/security';
import { API } from 'src/app/core/services/api.service';

@Component({
  selector: 'app-role-view',
  templateUrl: './role-view.component.html',
})
export class RoleViewComponent implements OnInit {

  cols: any[];
  allStaff;
  role_name = '';
  reporting_role_id = '';
  role_description = '';
  AllRole;
  @ViewChild('dt') myTable;
  constructor(
    private router: Router,
    private ApiServ: API,
    public commonServ: CommonFunctionService
  ) { }

  ngOnInit(): void {
    this.cols = [
      { field: "role_id", header: "Role Id" },
      { field: "role_name", header: "Role Name" },
      { field: "role_type", header: "Role Type" },
      { field: "affiliate_name", header: "Agency Name" },
      { field: "role_description", header: "Role Description" },
      { field: "reporting_role_id", header: "Reporting Role Id" },
      { field: "updated_by", header: "Updated By" },
      { field: "updated_on", header: "Updated On" },
      { field: "status", header: "Status" },
    ];
  }
  roleType = {
    Extranet: 'Extranet',
  }


  Search() {
    let LoginUserData = securityFunctions.getSessionStorage('LoginUserData') ? JSON.parse(securityFunctions.getSessionStorage('LoginUserData')) : null
    let searchObj = {
      role_name: this.role_name,
      role_description: this.role_description,
      reporting_role_id: '',
      extranet_vendor_id: LoginUserData.extranet_vendor_id,
    }
    this.ApiServ.AdminPost('/RoleMaster/GetAllRoleBySearchFilters', searchObj).subscribe(res => {
      if (res) {
        this.AllRole = res;
        this.AllRole.forEach(e => {
          e['role_type'] = this.roleType[e['role_type']]
          e['status'] = e['is_active'] ? 'Active' : 'Deactive'
          e['Custom_updated_on'] = this.commonServ.convertDateWithTimeToShow(e.updated_on);
        });
        this.myTable ? this.myTable.first = 0 : '';
      }
    })
  };

  UpdateStatus(id, status) {
    let obj = {
      Id: id,
      Status: status ? false : true,
    };
    this.ApiServ.AdminPost("/RoleMaster/UpdateStatusRoleMaster", obj).subscribe(
      (data) => {
        if (data) {
          this.commonServ.showMessage(data['resp'], data['respMsg'])
          this.Search();
        }
      }
    );
  }
  Edit(role_id) {
    this.router.navigate(["UserConfiguration/RoleManagement/Edit/" + role_id]);
  }
  Reset() {
    this.role_name = '';
    this.role_description = '';
  }
  btnClick() {
    this.router.navigateByUrl("/UserConfiguration/RoleManagement/Add");
   }

}
