import { PlatformUsersService } from '../../../../../api/api-services/platform-users.service'; import { IPlatformUserAccountVm } from '../../../../../api/platform-user-account-vm.model'; import { DialogService } from '../../../../shared/services/dialog.service'; import { SortOrder } from '../../../../../api/sort-order.model'; // import { FASDynamicTableColumnConfig } from 'fas-services'; import { Component, OnInit, inject } from '@angular/core'; import { map, Observable } from 'rxjs'; @Component({ selector: 'lib-users-list', templateUrl: './users-list.component.html', styleUrl: './users-list.component.scss', }) export class UsersListComponent implements OnInit { private _dialogService = inject(DialogService); private _platformUsersService = inject(PlatformUsersService); // public columns: FASDynamicTableColumnConfig[] = [ // { // field: 'guid', // columnHeader: 'ID', // valueFunction: (data: IPlatformUserAccountVm) => data.guid ? `${data.guid.slice(-6)}` : '', // columnClass: 'flex align-items-center', // actions: [ // { // label: '', // buttonClass: 'pi pi-copy cursor-pointer text-center appear transparent-button', // callback: (data: IPlatformUserAccountVm) => this.copyToClipboard(data.guid), // tooltip: 'Kopiuj ID', // }, // ], // }, // { field: 'companyName', columnHeader: 'Firma', }, // { // field: 'displayName', // columnHeader: 'Nazwa użytkownika (Imię i nazwisko)', // columnClass: 'flex justify-content-between align-items-center', // actions: [ // { // label: 'Szczegóły', // buttonClass: 'white-button appear', // callback: (data: IPlatformUserAccountVm) => this.showUserDetails(data), // icoSuffix: 'pi pi-arrow-right', // }, // ], // }, // { field: 'email', columnHeader: 'Adres email' }, // { field: 'companyRole', columnHeader: 'Rola', }, // { field: 'created', columnHeader: 'Data utworzenia', dateField: true }, // { field: 'loginCount', columnHeader: 'Logowania' }, // { field: 'subscriptionPlan', columnHeader: 'Pakiet' }, // { // field: 'subscriptionPlanActiveTo', // columnHeader: 'Status', // valueFunction: (data) => this.getSubscriptionStatus(data.subscriptionPlanActiveTo).status, // styleColorFunction: (data) => this.getSubscriptionStatus(data.subscriptionPlanActiveTo).color, // }, // { // columnHeader: 'Akcje', // No field since it's custom // actions: [ // { // label: '', // buttonClass: 'cursor-pointer text-center appear transparent-button', // callback: (data: IPlatformUserAccountVm) => this.editUser(data), // icoSuffix: 'pi pi-pencil', // tooltip: 'Edytuj użytkownika' // }, // { // label: '', // buttonClass: 'cursor-pointer text-center appear transparent-button', // callback: (data: IPlatformUserAccountVm) => this.deleteUser(data), // icoSuffix: 'pi pi-trash', // tooltip: "Usuń użytkownika" // }, // { // label: '', // buttonClass: 'cursor-pointer text-center appear transparent-button', // callback: (data: IPlatformUserAccountVm) => this.addSubsccription(data), // icoSuffix: 'pi pi-plus', // tooltip: "Przydziel abonament" // }, // ], // } // ]; fetchUsers = (params: { page: number; pageSize: number; sortField?: keyof IPlatformUserAccountVm; sortOrder?: number; search?: string }): Observable<{ data: IPlatformUserAccountVm[]; total: number }> => { const { page, pageSize, sortField, sortOrder, search } = params; const sortOrderValue = sortOrder === 1 ? SortOrder.Ascending : SortOrder.Descending; return this._platformUsersService .get(search, page, pageSize, sortField) .pipe( map((result) => ({ data: result.data ?? [], total: result.allElementsCount, })) ); }; public dialogID_usersDetails = 'usersDetailsDialog'; public userSelected!: any | null; public usersList: IPlatformUserAccountVm[] = []; public currentUser!: IPlatformUserAccountVm public userIndex = 0; ngOnInit(): void { } handleDataLoaded(data: IPlatformUserAccountVm[]): void { this.usersList = data; // Store the loaded data console.log('Data loaded:', this.usersList); } private editUser(data: IPlatformUserAccountVm) { console.log('Edit user mode - work in progress', data); } private deleteUser(data: IPlatformUserAccountVm) { console.log('Delete user mode - work in progress', data); } private addSubsccription(data: IPlatformUserAccountVm) { console.log('Add subscription plan - work in progress', data); } private showUserDetails(data: IPlatformUserAccountVm) { this.currentUser = { ...data }; this.userIndex = this.getCurrentUserIndex(this.currentUser.guid); this.openDialog(this.dialogID_usersDetails); } private openDialog(id: string): void { this._dialogService.openDialog(id); } private getCurrentUserIndex(guid: string): number { return this.usersList.findIndex((user: IPlatformUserAccountVm) => user.guid === guid) } private getSubscriptionStatus(subscriptionDate?: Date): { status: string; color: string } { if (!subscriptionDate) { return { status: '', color: '' }; } const now = new Date(); const subDate = new Date(subscriptionDate); const diffInDays = Math.ceil((subDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24)); if (subDate < now) { return { status: `Nieaktywny od ${this.formatDate(subDate)}`, color: '#9CA3AF' }; } if (diffInDays <= 3) { return { status: `Aktywny do ${this.formatDate(subDate)}`, color: '#F97316' }; } return { status: `Aktywny do ${this.formatDate(subDate)}`, color: '#22C55E' }; } private formatDate(date: Date): string { return date.toLocaleDateString('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric', }); } private copyToClipboard(guid: string): void { if (navigator.clipboard) { navigator.clipboard.writeText(guid).then( () => { console.log('Copied to clipboard:', guid); }, (err) => { console.error('Failed to copy:', err); } ); } else { // Fallback for older browsers const textarea = document.createElement('textarea'); textarea.value = guid; document.body.appendChild(textarea); textarea.select(); try { document.execCommand('copy'); console.log('Copied to clipboard (fallback):', guid); } catch (err) { console.error('Failed to copy (fallback):', err); } document.body.removeChild(textarea); } } }