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

@Component({
  selector: "app-surveyor",
  standalone: true,
  imports: [CommonModule, ReactiveFormsModule, NgbPagination],
  templateUrl: "./surveyor.component.html",
  styleUrls: ["./surveyor.component.scss"],
})
export class DataSurveyorComponent 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: "1b4701ab-da41-405b-9fe3-039a4daf0094" } },
      limit: 10,
      sort: "-created_at",
    };
  }

  onCreate(): void {
    const modalRef = this.modal.open(ActionSurveyor);
    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(ActionSurveyor);
    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");
              },
            });
        }
      },
      () => {}
    );
  }

  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;
  }
}
