import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { Observable } from 'rxjs';
import { concatMap, filter, tap } from 'rxjs/operators';
import { AppRoleService } from 'src/app/core/service/security/app-role.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';

@Component({
  selector: 'app-set-role',
  templateUrl: './set-role.container.html',
  styleUrls: ['./set-role.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SetRoleContainer implements OnInit {
  roleList$ = new Observable()
  constructor(
    private appRoleSV: AppRoleService,
    private swSV: SweetalertService,
    private cdr: ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    this.roleList$ = this.appRoleSV.getAll()
  }

  search(query){
    if(query) return this.roleList$ = this.appRoleSV.search(`?${query}`)
    if(!query) return this.roleList$ = this.appRoleSV.getAll()
  }

  delete(uid){
    this.swSV.confirm().pipe(
      filter(x => x.isConfirmed),
      concatMap(x => {
        return this.appRoleSV.deleteData(uid).pipe(
          tap(x => this.swSV.removeSuccess()),
          tap(x => this.roleList$ = this.appRoleSV.getAll()),
          tap(x => this.cdr.detectChanges())
        );
      })
    ).subscribe();
  }

}
