import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import {
  Surveyor,
  SurveyorService,
} from "src/app/cores/api-services/surveyor/surveyor.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";

@Component({
  selector: "app-surveyor",
  templateUrl: "./surveyor.component.html",
  styleUrls: ["./surveyor.component.scss"],
})
export class SurveyorComponent
  extends BaseCrudComponent<Surveyor>
  implements OnInit
{
  constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
    public override modal: NgbModal,
    public override helpers: HelpersService,
    public override api: SurveyorService,
    public override loading: LoadingService
  ) {
    super(helpers, loading, api, modal);
    this.searchFields = "no_ticket,user.fullname,segmens.segmen.name";
    this.queries = {
      page: 1,
      sort: "-created_at",
      limit: 10,
      embed: "status_report,user,segmens,segmens.segmen,segmens.photos,survey",
    };
  }

  goToDetail(id: string) {
    this.router.navigate([id], { relativeTo: this.activatedRoute });
  }

  applyFilter(sort: string) {
    if (sort === "terbaru") {
      this.queries.sort = "-created_at";
    } else if (sort === "terbaru") {
      this.queries.sort = "created_at";
    } else {
      this.queries.sort = "-created_at";
    }
    this.getAll();
  }

  parsing(text: string) {
    return JSON.parse(text);
  }
}
