import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import { supabase } from '@/lib/supabase'
import { LogoutButton } from '@/components/logout-button'
import { ArrowLeft, User, MapPin, Phone, Baby, Calendar, Mars, Venus } from 'lucide-react'
import Link from 'next/link'
import { StuntingTable } from './StuntingTable'
import { GrowthChart } from './GrowthChart'
function ReadField({
icon,
label,
value,
accent,
}: {
icon: React.ReactNode
label: string
value: string | null | undefined
accent?: 'blue' | 'pink'
}) {
return (
{icon}
{label}
{value || Belum diisi}
)
}
export default async function UserPerkembanganPage() {
const cookieStore = await cookies()
const sessionCookie = cookieStore.get('user_session')
if (!sessionCookie) redirect('/')
const session = JSON.parse(sessionCookie.value)
if (session.role !== 'user' && session.role !== 'admin') redirect('/dashboard')
// Fetch this specific user's child data
const { data: pengguna, error } = await supabase
.from('akun_balita')
.select('id, nama_orang_tua, alamat, no_whatsapp, nama_anak, jenis_kelamin, tanggal_lahir, username, password')
.eq('id', session.id)
.single()
if (error || !pengguna) {
return (
Data Tidak Ditemukan
Maaf, kami tidak dapat menemukan profil balita Anda. Silakan hubungi admin di posyandu terdekat.
Kembali ke Dashboard
)
}
// Fetch measurement history
const { data: hasilData } = await supabase
.from('hasil_stunting_balita')
.select('id, tinggi_badan, berat_badan, status_stunting, pesan_ai, tanggal_upload, nama_posyandu')
.eq('id_balita', pengguna.id)
.order('tanggal_upload', { ascending: false })
const formatDate = (d: string | null) => {
if (!d) return null
return new Date(d).toLocaleDateString('id-ID', {
day: 'numeric', month: 'long', year: 'numeric'
})
}
const isLaki = pengguna.jenis_kelamin?.toLowerCase().includes('laki')
const calculateAge = (birthDateStr: string | null) => {
if (!birthDateStr) return null
const birthDate = new Date(birthDateStr)
const today = new Date()
let months = (today.getFullYear() - birthDate.getFullYear()) * 12 + (today.getMonth() - birthDate.getMonth())
let days = today.getDate() - birthDate.getDate()
if (days < 0) {
months -= 1
const lastMonth = new Date(today.getFullYear(), today.getMonth(), 0)
days += lastMonth.getDate()
}
return { months, days }
}
const age = calculateAge(pengguna.tanggal_lahir)
return (
{/* Header */}
Dashboard
Perkembangan Anak
Laporan Rutin Posyandu
{/* Hero Profile Section */}
{/* Background Decorative Element */}
{pengguna.nama_anak?.charAt(0).toUpperCase() ?? '?'}
{pengguna.nama_anak}
{isLaki ? : }
{pengguna.jenis_kelamin}
Lahir {formatDate(pengguna.tanggal_lahir)}
{age && (
Umur {age.months} Bulan {age.days} Hari
)}
Total Cek
{hasilData?.length ?? 0}
{/* Top Row: Info & Chart */}
{/* Info Column */}
Informasi Keluarga
} label="Nama Ibu / Orang Tua" value={pengguna.nama_orang_tua} />
} label="Alamat Domisili" value={pengguna.alamat} />
} label="Kontak WhatsApp" value={pengguna.no_whatsapp} />
Tips Sehat ✨
Pastikan si kecil mendapatkan asupan gizi seimbang dan rutin mengikuti pemeriksaan di posyandu setiap bulan.
{/* Chart Column */}
{/* Bottom Row: Full Width Table */}
)
}