import { POS_ROUTES } from '@/domains/pos/routes';
import { PosInfoStore } from '@/domains/pos/store';
import { InnerPagesHeaderComponent } from '@/shared/components/innerPagesHeader/inner-pages-header.component';
import { Component, computed, inject, signal } from '@angular/core';
import { Router, RouterLink } from '@angular/router';
import { Button, ButtonDirective } from 'primeng/button';
import { Skeleton } from 'primeng/skeleton';
import { finalize } from 'rxjs';
import images from 'src/assets/images';

import { PosGoodsManagementRoutes } from '../constants';
import { IPosGoodsManagementResponse } from '../models';
import { PosOwnedGoodsService } from '../services/main.service';

@Component({
  selector: 'pos-ownedGoods-list',
  templateUrl: './list.component.html',
  imports: [InnerPagesHeaderComponent, ButtonDirective, Skeleton, Button, RouterLink],
})
export class PosOwnedGoodsListComponent {
  private readonly service = inject(PosOwnedGoodsService);
  private readonly infoStore = inject(PosInfoStore);
  private readonly router = inject(Router);

  goodPlaceholder = images.placeholders.default;

  loading = signal(true);
  items = signal<IPosGoodsManagementResponse[]>([]);
  backRoute = POS_ROUTES.path || '/';

  preparedPageTitle = computed(
    () => `لیست کالاهای ${this.infoStore.entity()?.businessActivity.name}`
  );

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.loading.set(true);
    this.service
      .getAll()
      .pipe(finalize(() => this.loading.set(false)))
      .subscribe((res) => {
        this.items.set(res.data ?? []);
      });
  }

  refresh() {
    this.getData();
  }

  editPageRoute(goodId: string) {
    return PosGoodsManagementRoutes.update.meta.pagePath!(goodId);
  }
}
