import { Component } from "@angular/core";
import {
  User,
  UserService,
} from "src/app/cores/api-services/manage/user.service";
import { BaseCrudComponent } from "src/app/cores/class-templates/base-crud.component";
import { Subject, takeUntil } from "rxjs";
import { HelpersService } from "src/app/cores/services/helpers.service";
import { LoadingService } from "src/app/cores/services/loading.service";
import { NgbModal, NgbPagination } from "@ng-bootstrap/ng-bootstrap";
import { CommonModule } from "@angular/common";
import { ActionPengelola } from "../action/action.component";
import { ReactiveFormsModule } from "@angular/forms";

@Component({
  selector: "app-pengelola",
  standalone: true,
  imports: [CommonModule, ReactiveFormsModule, NgbPagination],
  templateUrl: "./pengelola.component.html",
  styleUrls: ["./pengelola.component.scss"],
})
export class PengelolaComponent extends BaseCrudComponent<User> {
  dataInsertSubs$ = new Subject<void>();
  dataUpdateSubs$ = new Subject<void>();

  constructor(
    public override helpers: HelpersService,
    public override loading: LoadingService,
    public override api: UserService,
    public override modal: NgbModal
  ) {
    super(helpers, loading, api, modal);
    this.searchFields = "fullname,email";

    this.queries = {
      filter: { urole_id: { eq: "1038dcc0-3bb0-4253-87fa-ef0e09a4429d" } },
      limit: 10,
      sort: "-created_at",
    };
  }

  override parseNumber(value: number): number {
    const page = this.queries["page"];
    const limit = this.queries["limit"];

    if (page && limit) {
      return (page - 1) * limit + value;
    }

    return value;
  }

  onCreate(): void {
    const modalRef = this.modal.open(ActionPengelola);
    modalRef.componentInstance.action = "create";
    modalRef.result.then(
      (result) => {
        if (result) {
          this.api
            .insert(result)
            .pipe(takeUntil(this.dataInsertSubs$))
            .subscribe({
              next: () => {
                this.helpers.toast("success", "Data berhasil ditambahkan");
                this.getAll();
              },
              error: () => {
                this.helpers.toast("error", "Data gagal ditambahkan");
              },
            });
        }
      },
      () => {}
    );
  }

  onUpdate(data: User): void {
    const modalRef = this.modal.open(ActionPengelola);
    modalRef.componentInstance.action = "update";
    modalRef.componentInstance.data = data;
    modalRef.result.then(
      (result) => {
        if (result) {
          this.api
            .update(data.id, result)
            .pipe(takeUntil(this.dataUpdateSubs$))
            .subscribe({
              next: () => {
                this.helpers.toast("success", "Data berhasil diubah");
                this.getAll();
              },
              error: () => {
                this.helpers.toast("error", "Data gagal diubah");
              },
            });
        }
      },
      () => {}
    );
  }
}
