import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { JadwalComponent } from "./jadwal/jadwal.component";
import { BaseCrudComponent } from "src/app/cores/class-templates/base-crud.component";
import {
  Perbaikan,
  PerbaikanService,
} from "src/app/cores/api-services/perbaikan/perbaikan.service";
import { HelpersService } from "src/app/cores/services/helpers.service";
import { LoadingService } from "src/app/cores/services/loading.service";
import { AuthService } from "src/app/cores/services/auth.service";

@Component({
  selector: "app-perbaikan",
  templateUrl: "./perbaikan.component.html",
  styleUrls: ["./perbaikan.component.scss"],
})
export class PerbaikanComponent
  extends BaseCrudComponent<Perbaikan>
  implements OnInit
{
  id: string = "";
  fromDate: any;
  toDate: any;

  constructor(
    private router: Router,
    private auth: AuthService,
    private activatedRoute: ActivatedRoute,
    public override modal: NgbModal,
    public override helpers: HelpersService,
    public override api: PerbaikanService,
    public override loading: LoadingService
  ) {
    super(helpers, loading, api, modal);
    this.searchFields =
      "report.no_ticket,report.user.fullname,report.segmens.segmen.name";
    this.queries = {
      page: 1,
      sort: "-created_at",
      limit: 10,
      embed: "report.user,report.status_report,report.segmens.segmen",
    };
  }

  sortBy: string = "";

  applyFilter(sortBy: string) {
    this.sortBy = sortBy;
    if (sortBy === "terbaru") {
      this.queries.sort = "-created_at";
    } else if (sortBy === "terlama") {
      this.queries.sort = "created_at";
    } else {
      this.queries.sort = "-created_at";
    }
    if (this.auth.getLoggedUser().role.code == "PNGL") {
      this.api.getAll(this.queries).subscribe({
        next: ({ data }) => {
          this.loading.hide();
          this.dataSource = data;
        },
        error: () => {
          this.loading.hide();
          this.helpers.toast("error", `Gagal mengambil data`);
        },
      });
    } else {
      this.getAll();
    }
  }

  goToMap(id: string): void {
    this.router.navigate(["detail", id], { relativeTo: this.activatedRoute });
  }

  onUpdateSchedule(data: Perbaikan, id: string): void {
    if (id == "RJT" || id == "DONE" || id == "FIXED") {
      return alert("Jadwal tidak bisa diubah lagi");
    }
    const modalRef = this.modal.open(JadwalComponent);
    modalRef.componentInstance.action = "update";
    modalRef.componentInstance.id = id;
    modalRef.componentInstance.data = data;
  }

  getFormattedDateRange(): any {
    if (this.fromDate) {
      const fromDateStr = `${this.fromDate.day}/${this.fromDate.month}/${this.fromDate.year}`;
      if (this.toDate) {
        const toDateStr = `${this.toDate.day}/${this.toDate.month}/${this.toDate.year}`;
        return `${fromDateStr} - ${toDateStr}`;
      } else {
        return fromDateStr;
      }
    } else {
      return "";
    }
  }

  isPNGL(): boolean {
    const decodedToken = this.auth.getLoggedUser();
    if (decodedToken) {
      const userRole = decodedToken.role.code;

      return userRole === "SADM";
    } else {
      return false;
    }
  }
}
