'use client'; import { useEffect, useState } from 'react'; import dynamic from 'next/dynamic'; import { ApexOptions } from 'apexcharts'; import { useTheme } from 'next-themes'; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { ExternalLink } from "lucide-react"; import Link from "next/link"; const Chart = dynamic(() => import('react-apexcharts'), { ssr: false }); interface JenisPendaftaranData { tahun_angkatan: number; jenis_pendaftaran: string; jumlah: number; } interface JenisPendaftaranChartProps { height?: string; showDetailButton?: boolean; } export default function JenisPendaftaranChart({ height = "h-[300px] sm:h-[350px] md:h-[300px]", showDetailButton = true }: JenisPendaftaranChartProps) { const { theme } = useTheme(); const [mounted, setMounted] = useState(false); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [data, setData] = useState([]); const [series, setSeries] = useState([]); const [options, setOptions] = useState({ chart: { type: 'bar', stacked: false, toolbar: { show: true, }, background: theme === 'dark' ? '#0F172B' : '#fff', }, plotOptions: { bar: { horizontal: false, columnWidth: '55%', }, }, dataLabels: { enabled: true, formatter: function (val: number) { return val.toString(); }, style: { fontSize: '12px', colors: [theme === 'dark' ? '#fff' : '#000'] } }, stroke: { show: true, width: 2, colors: ['transparent'], }, xaxis: { categories: [], title: { text: 'Tahun Angkatan', style: { fontSize: '14px', fontWeight: 'bold', color: theme === 'dark' ? '#fff' : '#000' } }, labels: { style: { fontSize: '12px', colors: theme === 'dark' ? '#fff' : '#000' } }, }, yaxis: { title: { text: 'Jumlah Mahasiswa', style: { fontSize: '14px', fontWeight: 'bold', color: theme === 'dark' ? '#fff' : '#000' } }, labels: { style: { fontSize: '12px', colors: theme === 'dark' ? '#fff' : '#000' } }, min:0, tickAmount: 5 }, fill: { opacity: 1, }, legend: { position: 'top', fontSize: '14px', markers: { size: 12, }, itemMargin: { horizontal: 10, }, labels: { colors: theme === 'dark' ? '#fff' : '#000' } }, colors: ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6', '#EC4899', '#06B6D4', '#F97316'], tooltip: { theme: theme === 'dark' ? 'dark' : 'light', y: { formatter: function (val: number) { return val + " mahasiswa"; } } } }); // Update theme when it changes useEffect(() => { const currentTheme = theme; setOptions(prev => ({ ...prev, chart: { ...prev.chart, background: currentTheme === 'dark' ? '#0F172B' : '#fff', }, dataLabels: { ...prev.dataLabels, style: { ...prev.dataLabels?.style, colors: [currentTheme === 'dark' ? '#fff' : '#000'] } }, xaxis: { ...prev.xaxis, title: { ...prev.xaxis?.title, style: { ...prev.xaxis?.title?.style, color: currentTheme === 'dark' ? '#fff' : '#000' } }, labels: { ...prev.xaxis?.labels, style: { ...prev.xaxis?.labels?.style, colors: currentTheme === 'dark' ? '#fff' : '#000' } } }, yaxis: { ...prev.yaxis, title: { ...(Array.isArray(prev.yaxis) ? prev.yaxis[0]?.title : prev.yaxis?.title), style: { ...(Array.isArray(prev.yaxis) ? prev.yaxis[0]?.title?.style : prev.yaxis?.title?.style), color: currentTheme === 'dark' ? '#fff' : '#000' } }, labels: { ...(Array.isArray(prev.yaxis) ? prev.yaxis[0]?.labels : prev.yaxis?.labels), style: { ...(Array.isArray(prev.yaxis) ? prev.yaxis[0]?.labels?.style : prev.yaxis?.labels?.style), colors: currentTheme === 'dark' ? '#fff' : '#000' } } }, legend: { ...prev.legend, labels: { ...prev.legend?.labels, colors: currentTheme === 'dark' ? '#fff' : '#000' } }, tooltip: { ...prev.tooltip, theme: currentTheme === 'dark' ? 'dark' : 'light' } })); }, [theme]); useEffect(() => { setMounted(true); }, []); useEffect(() => { const fetchData = async () => { try { setLoading(true); setError(null); const response = await fetch('/api/mahasiswa/jenis-pendaftaran'); if (!response.ok) { throw new Error(`Failed to fetch data: ${response.status} ${response.statusText}`); } const result = await response.json(); if (!Array.isArray(result)) { throw new Error('Invalid data format received from server'); } setData(result); const tahunAngkatan = [...new Set(result.map(item => item.tahun_angkatan))].sort(); const jenisPendaftaran = [...new Set(result.map(item => item.jenis_pendaftaran))].sort(); const seriesData = jenisPendaftaran.map(jenis => ({ name: jenis, data: tahunAngkatan.map(tahun => { const item = result.find(d => d.tahun_angkatan === tahun && d.jenis_pendaftaran === jenis); return item ? item.jumlah : 0; }), })); setSeries(seriesData); setOptions(prev => ({ ...prev, xaxis: { ...prev.xaxis, categories: tahunAngkatan, }, })); } catch (err) { console.error('Error in fetchData:', err); setError(err instanceof Error ? err.message : 'An error occurred while fetching data'); } finally { setLoading(false); } }; fetchData(); }, []); if (!mounted) { return null; } if (loading) { return ( Loading... ); } if (error) { return ( Error: {error} ); } if (data.length === 0) { return ( Tidak ada data yang tersedia ); } return (
Jenis Pendaftaran Mahasiswa {showDetailButton && ( )}
); }