"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogClose } from "@/components/ui/dialog"; import { Upload, FileSpreadsheet, Loader2, Download } from "lucide-react"; import { useToast } from "@/components/ui/toast-provider"; interface UploadFileMataKuliahProps { onUploadSuccess: () => void; } export default function UploadFileMataKuliah({ onUploadSuccess }: UploadFileMataKuliahProps) { const { showSuccess, showError } = useToast(); const [isDialogOpen, setIsDialogOpen] = useState(false); const [selectedFile, setSelectedFile] = useState(null); const [isUploading, setIsUploading] = useState(false); // Handle file selection const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { // Validate file type const validTypes = [ 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv' ]; if (!validTypes.includes(file.type)) { showError("Error!", "File harus berformat Excel (.xlsx, .xls) atau CSV (.csv)"); return; } setSelectedFile(file); } }; // Handle file upload const handleUpload = async () => { if (!selectedFile) { showError("Error!", "Silakan pilih file terlebih dahulu"); return; } try { setIsUploading(true); const formData = new FormData(); formData.append('file', selectedFile); const response = await fetch('/api/keloladata/upload-mata-kuliah', { method: 'POST', body: formData, }); const result = await response.json(); if (!response.ok) { throw new Error(result.message || 'Upload failed'); } showSuccess("Berhasil!", `${result.successCount} mata kuliah berhasil diimport`); onUploadSuccess(); setIsDialogOpen(false); setSelectedFile(null); // Reset file input const fileInput = document.getElementById('file-upload') as HTMLInputElement; if (fileInput) { fileInput.value = ''; } } catch (error) { console.error('Upload error:', error); showError("Gagal!", error instanceof Error ? error.message : "Terjadi kesalahan saat upload"); } finally { setIsUploading(false); } }; // Download template const downloadTemplate = () => { // Create CSV template const csvContent = "kode_mk,nama_mk,sks,semester,jenis_mk,kode_prasyarat\nINF101,Pengantar Informatika,3,1,Wajib,\nINF102,Algoritma dan Pemrograman,4,2,Wajib,INF101\nINF301,Basis Data Lanjut,3,5,Pilihan Wajib,\nINF401,Topik Khusus,2,7,Pilihan,\n"; const blob = new Blob([csvContent], { type: 'text/csv' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'template_mata_kuliah.csv'; document.body.appendChild(a); a.click(); document.body.removeChild(a); window.URL.revokeObjectURL(url); }; return ( <> Import Data Mata Kuliah

Format yang didukung: .xlsx, .xls, .csv (Max: 10MB)

{selectedFile && (

File terpilih:

{selectedFile.name}

Ukuran: {(selectedFile.size / 1024 / 1024).toFixed(2)} MB

)}

Format File:

• Kolom yang diperlukan: kode_mk, nama_mk, sks, semester, jenis_mk

• Kolom opsional: kode_prasyarat (kode mata kuliah prasyarat)

• Baris pertama harus berisi header kolom

• SKS dan semester harus berupa angka positif

• jenis_mk harus salah satu: Wajib, Pilihan Wajib, Pilihan

); }