//src\components\LateStartReportGen\LateStartReportGen.tsx
"use client";
import React, { useMemo, useState } from "react";
// import SearchBox, { Criterion } from "../ReportSearchBoxe1";
import { useTranslation } from "react-i18next";
import { FinancialStatus, TeamCombo } from "@/app/api/reporte1";
import SearchBox, { Criterion } from "@/components/SearchBox";
import { FinancialStatusReportFilter } from "@/app/api/reports";
import { fetchAllClientSubsidiaryProjects } from "@/app/api/clientprojects/actions";
import { fetchProjectsFinancialStatusReport } from "@/app/api/reporte1/action";
import { downloadFile } from "@/app/utils/commonUtil";
import { SessionStaff } from "@/config/authConfig";
import FinancialStatusReportGenLoading from "./FinancialStatusReportGenLoading";
//import { DownloadReportButton } from './DownloadReportButton';
interface Props {
  projects: FinancialStatus[];
  teamCombo : TeamCombo[];
  userStaff: SessionStaff;
}


interface SubComponents {
  Loading: typeof FinancialStatusReportGenLoading;
}

type SearchQuery = Partial<Omit<TeamCombo, "id">>;
type SearchParamNames = keyof SearchQuery;

const GenFinancialStatusReport: React.FC<Props> & SubComponents = ({ projects, teamCombo, userStaff }) => {
  const { t } = useTranslation("projects");

  const combo = teamCombo.map(combo => {return `${combo.code} - ${combo.name}`})

  const searchCriteria: Criterion<SearchParamNames>[] = useMemo(
    () => [
      { label: t("Team"), paramName: "code", type: "select", options: combo, needAll: !Boolean(userStaff?.isTeamLead) },
      // {
      //   label: "Status",
      //   label2: "Remained Date To",
      //   paramName: "targetEndDate",
      //   type: "dateRange",
      // },
    ],
    [t],
  );

  const [loading, setLoading] = useState(false);

  return (
    <>
      {loading && <GenFinancialStatusReport.Loading />}
      {!loading &&
      <SearchBox
        criteria={searchCriteria}
        onSearch={async (query) => {
          setLoading(true)
          if (query.code.length > 0 && query.code.toLocaleLowerCase() !== "all") {
            const projectIndex = teamCombo.findIndex((project) => `${project.code} - ${project.name}` === query.code)
            console.log(teamCombo[projectIndex].id)
            const response = await fetchProjectsFinancialStatusReport({ teamLeadId: teamCombo[projectIndex].id })
            if (response) {
                downloadFile(new Uint8Array(response.blobValue), response.filename!!)
            }
          }else{
            console.log(query.code)
            const response = await fetchProjectsFinancialStatusReport({ teamLeadId: -1 })
            if (response) {
                downloadFile(new Uint8Array(response.blobValue), response.filename!!)
            }
          } 
          setLoading(false)
        }}
        formType="download"
      />}
      {/* <DownloadReportButton /> */}
    </>
  );
};

GenFinancialStatusReport.Loading = FinancialStatusReportGenLoading

export default GenFinancialStatusReport;
