import { AfterViewInit, Component, ViewChild } from '@angular/core';
import { ModuleTableComponent } from '../../components/tables/module-table/module-table.component';
import { MatTabsModule } from '@angular/material/tabs';
import { UserTableComponent } from '../../components/tables/user-table/user-table.component';
import { GuiSelectedRow } from '@generic-ui/ngx-grid';
import { BasePageComponent } from '../base-page/base-page.component';

@Component({
  standalone: true,
  imports: [ModuleTableComponent, MatTabsModule, UserTableComponent],
  selector: 'app-module',
  templateUrl: './module.component.html',
  styleUrl: './module.component.css'
})
export class ModuleComponent extends BasePageComponent implements AfterViewInit {

  private uModuleId = null;

  initWithoutData = () => { }
  ngAfterViewInit(): void {
    this.buttonVisibilityService.setVisibleOnly()
    this.refreshService.removeAll()
    this.refreshService.add(() => {
      this.moduleTable.fetchData();
      if(this.uModuleId)
        this.userTable.fetchDataByModuleId(this.uModuleId);
    });
  }

  @ViewChild("moduleTable") moduleTable!: ModuleTableComponent;
  @ViewChild("userTable") userTable!: UserTableComponent;

  modulesOnSelectedRows = (rows: GuiSelectedRow[]) => {
    if(rows.length > 0){
      this.uModuleId = rows[0].source.id;
      if(this.uModuleId)
        this.userTable.fetchDataByGroupId(this.uModuleId);
    }
  }
}