import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { BaseCrudComponent } from "src/app/cores/class-templates/base-crud.component";
import { HelpersService } from "src/app/cores/services/helpers.service";
import {
  Laporan,
  LaporanService,
} from "src/app/cores/api-services/laporan/laporan.service";
import { LoadingService } from "src/app/cores/services/loading.service";
import { AuthService } from "src/app/cores/services/auth.service";
import { ExportLaporanByIdStatus } from "src/app/cores/api-services/export/export-laporanByStatus.service";
import { ExportLaporanById } from "src/app/cores/api-services/export/export-laporanByid.service";

@Component({
  selector: "app-laporan",
  templateUrl: "./laporan.component.html",
  styleUrls: ["./laporan.component.scss"],
})
export class LaporanComponent
  extends BaseCrudComponent<Laporan>
  implements OnInit
{
  constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private auth: AuthService,
    private exportAll: ExportLaporanByIdStatus,
    public override modal: NgbModal,
    public override helpers: HelpersService,
    public override api: LaporanService,
    public override loading: LoadingService
  ) {
    super(helpers, loading, api, modal);
    this.searchFields = "no_ticket,user.fullname,segmens.segmen.name";
    this.api.endpoint = `report-list`;
    this.queries = {
      page: 1,
      sort: "-created_at",
      limit: 10,
      embed: "status_report,user,segmens,segmens.segmen,segmens.photos",
    };
    if (this.auth.getLoggedUser().role.code == "PNGL") {
      this.api.endpoint = `report-list/pengelola`;
    }
  }

  statusId: string = "";

  field = {
    eq: this.statusId,
  };

  applyFilter(statusId: string) {
    this.statusId = statusId;
    if (this.statusId !== "") {
      this.queries.filter = { status_id: { eq: this.statusId } };
    } else {
      if (this.auth.getLoggedUser().role.code == "SADM") {
        delete this.queries.filter.status_id;
      } else {
        this.queries.filter = { status_id: { ne: `PROG,RJT` } };
      }
    }
    this.getAll();
    return true;
  }

  export() {
    this.loading.show();
    this.exportAll.endpoint = `report-list/export-all/${this.statusId}`;
    this.exportAll
      .getAll()
      .pipe()
      .subscribe({
        next: (response: any) => {
          const data = response.data;
          const rel_path = data.rel_path;
          setTimeout(() => {
            window.open(rel_path, "_blank");
          }, 500);
          window.open(rel_path, "_blank");
          this.loading.hide();
        },
        error: (error) => {},
      });
  }

  goToDetail(id: string) {
    if (this.auth.getLoggedUser().role.code == "PNGL") {
      this.router.navigate(["dashboard", "data-laporan", id]);
    } else {
      this.router.navigate([id], { relativeTo: this.activatedRoute });
    }
  }

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

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

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