import { SysDataService } from "../../../service/sys-data.service";
import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { ActivatedRoute } from "@angular/router";
import { PremissionData } from "../../../data/PremissionData";

@Component({
  selector: "app-sys004-set-role",
  templateUrl: "./sys004-set-role.component.html"
})
export class Sys004SetRoleComponent implements OnInit {
  premissionList = [];
  errorMessage: String;
  data: any;
  premissionData: PremissionData;
  selectedRole: any;
  roleId: number;

  constructor(
    private sysDataService: SysDataService,
    private router: Router,
    private route: ActivatedRoute
  ) {}

  ngOnInit() {
    this.route.data.subscribe((data: { selectedRole: any }) => {
      this.selectedRole = data.selectedRole;
      this.roleId = this.selectedRole.Id;
      this.premissionData = new PremissionData();
      this.getAllPagePermission();
    });
  }

  getAllPagePermission(): any {
    this.premissionList = [];
    this.sysDataService.getAllPagePermission(this.roleId).subscribe(data => {
      this.premissionList = data;
    }, error => (this.errorMessage = <any>error));
  }

  onCheckEnable(p): any {
    this.sysDataService.putPageRoleStatus(p).subscribe(data => {
       p = data;
    }, error => (this.errorMessage = <any>error));
  }

  backPreviousPage() {
    this.router.navigateByUrl("/sys/sys004");
  }
}
