import { Router, ActivatedRoute } from '@angular/router';
import { tap } from 'rxjs/operators';
import { Directive, ViewChild } from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Directive()
export class BaseSubList {
    state: string
    id: number
    constructor(
        public _activeRouter: ActivatedRoute,
    ){
        this._activeRouter.params.pipe(
            tap(x => x.id ? this.state = 'edit' : this.state = 'add'),
            tap(x => x.id ? this.id = x.id : this.id = null)
        ).subscribe()
    }

    @ViewChild(MatSort,{ static: true }) sort: MatSort;
    @ViewChild(MatPaginator,{ static: true }) paginator: MatPaginator;

    protected updateMatTable(res: any){
      if(res instanceof Array){
        res = new MatTableDataSource(res)
        res.paginator = this.paginator
        res.sort = this.sort
        return res
      }
      return res
    }

    getIndex(i){
      return this.paginator.pageIndex * this.paginator.pageSize + i + 1
    }
}
