import {Component} from '@angular/core';
import {UserService} from "../../api/user.service";
import {MatTableDataSource} from "@angular/material/table";
import {UserDto} from "../../model/userDto";
import {PageableEvent} from "../component/pageable/pageable.component";
import {FiltersFor} from "../component/pageable/filters/filters.component";
import {AddUserComponent} from "../component/dialog/add-user/add-user.component";
import {MatDialog} from "@angular/material/dialog";

@Component({
    selector: 'app-users',
    templateUrl: './users.component.html',
    styleUrls: ['./users.component.css']
})
export class UsersComponent {
    dataSource: MatTableDataSource<UserDto> = new MatTableDataSource<UserDto>();
    columns = [
        {ref: "id", label: "ID", sortable: false},
        {ref: "email", label: "Email", sortable: true},
        {ref: "name", label: "Name", sortable: true},
        {ref: "lastName", label: "Last Name", sortable: false},
        {ref: "scope", label: "Scope", sortable: true},
    ];
    lastEvent?: PageableEvent;

    constructor(private userService: UserService, private dialog: MatDialog) {
    }

    updateDataSource(event: PageableEvent) {
        this.lastEvent = event
        let sorts: string[] = [];
        if (!!event.sort.active && !!event.sort.direction) {
            sorts = [event.sort.active + "," + event.sort.direction]
        }
        this.userService.getUsers(!!event.filterQ ? [event.filterQ] : [], event.scope, event.paginator.pageIndex, event.paginator.pageSize, sorts).subscribe(
            r => {
                this.dataSource.data = r.content!
                event.paginator.length = r.totalElements
            }
        )
    }

    protected readonly FiltersFor = FiltersFor;

    delete($event: any) {
        this.userService.deleteUser($event.id).subscribe(() => {
            if (this.lastEvent) {
                this.updateDataSource(this.lastEvent)
            }
        })
    }

    registerNew() {
        const dialogRef = this.dialog.open(AddUserComponent);
    }
}
