/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-explicit-any */ 'use client' import jumlahPengangguranState from '@/app/admin/(dashboard)/_state/ekonomi/jumlah-pengangguran'; import colors from '@/con/colors'; import { Box, Button, Group, Loader, NumberInput, Paper, Select, Stack, Text, Title } from '@mantine/core'; import { IconArrowBack } from '@tabler/icons-react'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; import { toast } from 'react-toastify'; import { useProxy } from 'valtio/utils'; function CreateJumlahPengangguran() { const stateDetail = useProxy(jumlahPengangguranState.jumlahPengangguran); const [chartData, setChartData] = useState([]); const router = useRouter(); const [isSubmitting, setIsSubmitting] = useState(false); // Check if form is valid const isFormValid = () => { return ( stateDetail.create.form.month?.trim() !== '' && stateDetail.create.form.year !== null && stateDetail.create.form.year > 0 && stateDetail.create.form.educatedUnemployment !== null && stateDetail.create.form.educatedUnemployment >= 0 && stateDetail.create.form.uneducatedUnemployment !== null && stateDetail.create.form.uneducatedUnemployment >= 0 ); }; const monthOptions = [ 'Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Agu', 'Sep', 'Okt', 'Nov', 'Des' ]; const resetForm = () => { stateDetail.create.form = { month: monthOptions[new Date().getMonth()], // default bulan sekarang year: new Date().getFullYear(), // default tahun sekarang totalUnemployment: 0, educatedUnemployment: 0, uneducatedUnemployment: 0, percentageChange: 0, }; }; const calculateTotalAndChange = async () => { const total = stateDetail.create.form.educatedUnemployment + stateDetail.create.form.uneducatedUnemployment; stateDetail.create.form.totalUnemployment = total; // hitung perubahan dibanding bulan sebelumnya const monthOrder = monthOptions; const currentIndex = monthOrder.findIndex( (m) => m.toLowerCase() === stateDetail.create.form.month.toLowerCase() ); if (currentIndex > 0) { const prevMonth = monthOrder[currentIndex - 1]; const prev = await stateDetail.findByMonthYear.load({ month: prevMonth, year: stateDetail.create.form.year, }); if (prev?.totalUnemployment) { const change = ((total - prev.totalUnemployment) / prev.totalUnemployment) * 100; stateDetail.create.form.percentageChange = Number(change.toFixed(1)); } else { stateDetail.create.form.percentageChange = 0; } } else { stateDetail.create.form.percentageChange = 0; } }; const handleSubmit = async () => { try { setIsSubmitting(true); await calculateTotalAndChange(); const id = await stateDetail.create.create(); if (id) { await stateDetail.findUnique.load(String(id)); if (stateDetail.findUnique.data) { setChartData([stateDetail.findUnique.data]); } resetForm(); router.push('/admin/ekonomi/jumlah-pengangguran'); } } catch (error) { console.error("Error creating jumlah pengangguran:", error); toast.error("Gagal menambahkan data pengangguran"); } finally { setIsSubmitting(false); } }; return ( {/* Header */} Tambah Data Pengangguran {/* Form Card */}