// import { CatalogRoleTagComponent } from '@/shared/catalog/roles';
import { AbstractList } from '@/shared/abstractClasses/abstract-list';
import {
  ChangePasswordFormDialogComponent,
  IChangePasswordSubmitPayload,
} from '@/shared/components';
import { PageDataListComponent } from '@/shared/components/pageDataList/page-data-list.component';
import { partnerAccountListConfig } from '@/shared/constants/list-configs';
import { Component, inject, signal } from '@angular/core';
import { Router } from '@angular/router';
import { finalize } from 'rxjs';
import { partnerAccountsNamedRoutes } from '../constants';
import { IAccountResponse } from '../models';
import { AccountsService } from '../services/main.service';

@Component({
  selector: 'partner-accounts',
  templateUrl: './list.component.html',
  imports: [PageDataListComponent, ChangePasswordFormDialogComponent],
})
export class PartnerAccountsComponent extends AbstractList<IAccountResponse> {
  private readonly service = inject(AccountsService);
  private readonly router = inject(Router);
  passwordSubmitLoading = signal(false);

  override setColumns(): void {
    this.columns = partnerAccountListConfig.columns;
  }

  override getDataRequest() {
    return this.service.getAll();
  }

  toSinglePage(account: IAccountResponse) {
    this.router.navigateByUrl(partnerAccountsNamedRoutes.account.meta.pagePath!(account.id));
  }

  updatePassword(payload: IChangePasswordSubmitPayload) {
    if (!this.selectedItemForEdit()?.id) return;

    this.passwordSubmitLoading.set(true);
    this.service
      .update(this.selectedItemForEdit()!.id, { password: payload.password })
      .pipe(finalize(() => this.passwordSubmitLoading.set(false)))
      .subscribe(() => {
        this.visibleForm.set(false);
        this.refresh();
      });
  }
}
