import { Head, router } from '@inertiajs/react'; import { format } from 'date-fns'; import { Plus } from 'lucide-react'; import { useState } from 'react'; import type { PaginationState } from '@/components/data-table'; import { DataTable } from '@/components/data-table'; import { DatePicker } from '@/components/date-picker'; import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog'; import { FormDialog } from '@/components/form-dialog'; import InputError from '@/components/input-error'; import { PageHeader } from '@/components/page-header'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList, } from '@/components/ui/combobox'; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { usePermissions } from '@/hooks/use-permissions'; import { useServerTable } from '@/hooks/use-server-table'; import { index as departmentIndex, destroy, store, update, } from '@/routes/admin/master/departments'; import { DegreeLevels } from '@/types/department'; import type { Department, DepartmentLecturer } from '@/types/department'; import { createDepartmentColumns } from './columns'; type Props = { departments: { data: Department[]; current_page: number; last_page: number; per_page: number; total: number; }; lecturers: DepartmentLecturer[]; highlight?: number; }; function lecturerLabel(lecturer: DepartmentLecturer): string { return `${lecturer.user?.profile?.full_name ?? 'N/A'} - ${lecturer.lecturer_number}`; } export default function DepartmentIndex({ departments, lecturers, highlight, }: Props) { const [createOpen, setCreateOpen] = useState(false); const [editing, setEditing] = useState(null); const [deleting, setDeleting] = useState(null); const [historyDepartment, setHistoryDepartment] = useState(null); const { hasPermission } = usePermissions(); const canCreate = hasPermission('create-departments'); const canUpdate = hasPermission('update-departments'); const canDelete = hasPermission('delete-departments'); const pagination: PaginationState = { current_page: departments.current_page, last_page: departments.last_page, per_page: departments.per_page, total: departments.total, }; const { search, handlePageChange, handlePerPageChange, handleSearchChange, } = useServerTable({ route: () => departmentIndex.url(), pagination, }); function handleDelete() { if (!deleting) { return; } router.delete(destroy(deleting.id), { onSuccess: () => setDeleting(null), }); } const columns = createDepartmentColumns({ handleEdit: (department) => setEditing(department), handleDeleteClick: (department) => setDeleting(department), handleViewHistory: (department) => setHistoryDepartment(department), canUpdate, canDelete, }); return ( <>
Menampilkan jurusan dari notifikasi.

) } actions={ canCreate && ( ) } /> { if (!open) { setEditing(null); } }} editing={editing} lecturers={lecturers} /> { if (!open) { setDeleting(null); } }} title="Hapus Jurusan" description={(department) => `Apakah Anda yakin ingin menghapus jurusan "${department.name}"? Tindakan ini tidak dapat dibatalkan.` } onConfirm={handleDelete} /> { if (!open) { setHistoryDepartment(null); } }} />
); } function LeadershipHistoryDialog({ department, onOpenChange, }: { department: Department | null; onOpenChange: (open: boolean) => void; }) { const entries = department?.leaderships ?? []; return ( Riwayat Kaprodi {department ? ` — ${department.name}` : ''}
{entries.length === 0 && (

Belum ada riwayat kaprodi untuk jurusan ini.

)} {entries.map((entry) => (

{entry.lecturer?.user?.profile ?.full_name ?? 'N/A'}

{format( new Date(entry.started_at), 'd MMM yyyy', )}{' '} -{' '} {entry.ended_at ? format( new Date(entry.ended_at), 'd MMM yyyy', ) : 'Sekarang'}

{!entry.ended_at && Sekarang}
))}
); } function KaprodiFields({ errors, lecturers, editing, }: { errors: Record; lecturers: DepartmentLecturer[]; editing?: Department | null; }) { const [lecturer, setLecturer] = useState( editing?.current_leader?.lecturer ?? null, ); const [startedAt, setStartedAt] = useState( editing?.current_leader ? new Date(editing.current_leader.started_at) : undefined, ); const availableLecturers = editing ? lecturers.filter((l) => l.departments.some((d) => d.id === editing.id), ) : []; return ( <>
{ setLecturer(value); if (value && !startedAt) { setStartedAt(new Date()); } }} itemToStringLabel={(l) => lecturerLabel(l)} isItemEqualToValue={(a, b) => a.id === b.id} > {editing ? 'Belum ada dosen di jurusan ini.' : 'Simpan jurusan terlebih dahulu, lalu tambahkan dosen ke jurusan ini.'} {(l: DepartmentLecturer) => ( {lecturerLabel(l)} )}
{lecturer && (
)} ); } function CreateForm({ open, onOpenChange, lecturers, }: { open: boolean; onOpenChange: (open: boolean) => void; lecturers: DepartmentLecturer[]; }) { return ( onOpenChange(false)} > {({ errors }) => (
)}
); } function EditForm({ open, onOpenChange, editing, lecturers, }: { open: boolean; onOpenChange: (open: boolean) => void; editing: Department | null; lecturers: DepartmentLecturer[]; }) { return ( onOpenChange(false)} > {({ errors }) => editing && (
) }
); }