import React, { useEffect, useState } from "react"; import AuthenticatedLayout from "@/layouts/authenticated-layout"; import { Head, Link } from "@inertiajs/react"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Search, FileText, BadgeCheck } from "lucide-react"; import Select from "react-select"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; // ------------------------------------------------- // Tipe data (opsional, jika Anda pakai TypeScript) interface Perusahaan { PerusahaanId: number; NamaPerusahaan: string; } interface PeriodePelaporan { PeriodePelaporanId: number; Nama: string; // misal: "Triwulan 1", "Semester 1", "Triwulan 2", dsb. } interface Pelaporan { PelaporanId: number; PerusahaanId: number; PeriodePelaporanId: number; Tahun: number; // Contoh field SKL?: number; SPL?: number; SKL_IL?: number; SKL_AL?: number; SKL_LB3?: number; SKL_SB?: number; SKL_BS?: number; SKL_STB?: number; SKL_LP?: number; SKL_KDM?: number; // relasi perusahaan?: Perusahaan; periodePelaporan?: PeriodePelaporan; } interface PelaporanIndexProps { companies: Perusahaan[]; periodes: PeriodePelaporan[]; pelaporan: Pelaporan[]; } // ------------------------------------------------- export default function PelaporanIndex({ companies, periodes, pelaporan, }: PelaporanIndexProps) { // ------------------------------------------------- // 1. Buat options dari data companies & periodes const companyOptions = companies.map((c) => ({ value: c.PerusahaanId.toString(), label: c.NamaPerusahaan, })); const periodeOptions = periodes.map((p) => ({ value: p.PeriodePelaporanId.toString(), label: p.Nama, })); // ------------------------------------------------- // 2. State filter const [year, setYear] = useState("2025"); // Secara default, pilih perusahaan pertama & periode pertama (jika ada) const [company, setCompany] = useState<{ value: string; label: string; } | null>(companyOptions.length > 0 ? companyOptions[0] : null); const [selectedPeriode, setSelectedPeriode] = useState<{ value: string; label: string; } | null>(periodeOptions.length > 0 ? periodeOptions[0] : null); // Data terfilter const [filteredData, setFilteredData] = useState([]); // ------------------------------------------------- // 3. Fungsi bantu: cari label periode, dsb. function getPeriodeLabel(id: number) { const found = periodes.find((p) => p.PeriodePelaporanId === id); return found ? found.Nama : "-"; } // Apakah “Triwulan 1/3” atau “Semester 1/3”? // => cek label mengandung "1" atau "3" function isOneOrThree(label: string) { return label.includes("Triwulan 1") || label.includes("Triwulan 3"); } // ------------------------------------------------- // 4. Fungsi warna & nilai // - Jika data null => merah (belum diisi) // - Jika data 0 => abu-abu // - Jika data > 0 => hijau // - Triwulan 1/3 => kolom IL, SB, BS, STB, KDM => blank function getCellDisplay( val: number | undefined, hidden: boolean ): { text: string; style: string } { if (hidden) { // Kolom disembunyikan (kosong) => merah (belum diisi) return { text: "", style: "bg-red-100 text-red-700" }; } if (val === undefined || val === null) { // Belum ada data => merah return { text: "", style: "bg-red-100 text-red-700" }; } if (val === 0) { // Data 0 => abu-abu return { text: "0.00", style: "bg-gray-100 text-gray-700" }; } // Data > 0 => hijau return { text: val.toFixed(2), style: "bg-green-100 text-green-700" }; } // ------------------------------------------------- // 5. Fungsi Filter const handleFilter = () => { let temp = [...pelaporan]; // Filter Perusahaan if (company) { temp = temp.filter( (item) => item.PerusahaanId.toString() === company.value ); } // Filter Tahun if (year) { temp = temp.filter((item) => item.Tahun.toString() === year); } // Filter Periode if (selectedPeriode) { temp = temp.filter( (item) => item.PeriodePelaporanId.toString() === selectedPeriode.value ); } // Jika tidak ada data tapi filter sudah diisi => buat dummy row if ( temp.length === 0 && company !== null && selectedPeriode !== null && year !== "" ) { const dummy: Pelaporan = { PelaporanId: 0, // menandakan dummy PerusahaanId: parseInt(company.value, 10), PeriodePelaporanId: parseInt(selectedPeriode.value, 10), Tahun: parseInt(year, 10), SKL: 0, SPL: 0, SKL_IL: 0, SKL_AL: 0, SKL_LB3: 0, SKL_SB: 0, SKL_BS: 0, SKL_STB: 0, SKL_LP: 0, SKL_KDM: 0, perusahaan: { PerusahaanId: parseInt(company.value, 10), NamaPerusahaan: company.label, }, periodePelaporan: { PeriodePelaporanId: parseInt(selectedPeriode.value, 10), Nama: selectedPeriode.label, }, }; temp = [dummy]; } setFilteredData(temp); }; // Agar default langsung memfilter React.useEffect(() => { handleFilter(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // ------------------------------------------------- // 6. Fungsi reset filter const handleReset = () => { setYear("2025"); setCompany(companyOptions.length > 0 ? companyOptions[0] : null); setSelectedPeriode( periodeOptions.length > 0 ? periodeOptions[0] : null ); // Re-filter setTimeout(() => handleFilter(), 0); }; // ------------------------------------------------- return (
{/* Filter Section */}
Pelaporan SKL
{/* Select Perusahaan */} setYear(e.target.value) } className="w-full sm:w-24" />