import Badge from '@/components/Badge'; import Button from '@/components/Button'; import Card from '@/components/Card'; import { RadioGroup, RadioGroupItem } from '@/components/input/RadioInput'; import Tooltip from '@/components/Tooltip'; import DrawerHeader from '@/components/helper/drawer/DrawerHeader'; import { formatCurrency, formatDate, formatNumber, formatTitleCase, } from '@/lib/helper'; import { ProjectFlock } from '@/types/api/production/project-flock'; import { Icon } from '@iconify/react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; import { useModal } from '@/components/Modal'; import ConfirmationModal from '@/components/modal/ConfirmationModal'; import { ProjectFlockApi } from '@/services/api/production/project-flock'; import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; import toast from 'react-hot-toast'; import ApprovalSteps, { useApprovalSteps, } from '@/components/pages/ApprovalSteps'; import { PROJECT_FLOCK_APPROVAL_LINE, PROJECT_FLOCK_KANDANGS_APPROVAL_LINE, } from '@/config/approval-line'; import useSWR from 'swr'; import { ProjectFlockKandangApi } from '@/services/api/production'; import RequirePermission from '@/components/helper/RequirePermission'; const ProjectFlockDetail = ({ projectFlock, }: { projectFlock: ProjectFlock; }) => { const router = useRouter(); const deleteModal = useModal(); const [isDeleteLoading, setIsDeleteLoading] = useState(false); const [openBudgets, setOpenBudget] = useState(false); const [selectedKandangId, setSelectedKamdangId] = useState( null ); const selectedKandang = projectFlock.kandangs?.find( (kandang) => kandang.id === Number(selectedKandangId) ); const { data: projectFlockKandang, isLoading: projectFlockKandangLoading } = useSWR( selectedKandangId ? `${ProjectFlockKandangApi.basePath}/get-detail/${selectedKandangId}` : null, selectedKandangId ? () => ProjectFlockKandangApi.getSingle( Number(selectedKandang?.project_flock_kandang_id) ) : null ); const { approvals, isLoading: approvalsLoading, refresh: refreshApprovals, } = useApprovalSteps({ latestApproval: projectFlock?.approval, approvalLines: PROJECT_FLOCK_APPROVAL_LINE, moduleName: 'PROJECT_FLOCKS', moduleId: projectFlock?.id?.toString() ?? '', }); const { approvals: kandangApprovals, isLoading: kandangApprovalsLoading } = useApprovalSteps({ latestApproval: selectedKandangId && isResponseSuccess(projectFlockKandang) ? projectFlockKandang?.data?.approval : undefined, approvalLines: PROJECT_FLOCK_KANDANGS_APPROVAL_LINE, moduleName: 'PROJECT_FLOCK_KANDANGS', moduleId: selectedKandangId && isResponseSuccess(projectFlockKandang) ? projectFlockKandang?.data?.id?.toString() : '', }); const confirmationModalDeleteClickHandler = async () => { setIsDeleteLoading(true); const deleteProjectFlockRes = await ProjectFlockApi.delete( projectFlock?.id as number ); if (isResponseSuccess(deleteProjectFlockRes)) { toast.success(deleteProjectFlockRes?.message as string); router.push('/production/project-flock'); } if (isResponseError(deleteProjectFlockRes)) { toast.error(deleteProjectFlockRes?.message as string); } setIsDeleteLoading(false); }; return ( <>
{/* Header */} {/* Informasi Umum */}

Informasi Umum

{/* Status Approval */} {approvals && !approvalsLoading && (
)} {/* Badge Row */}
= 3 ? 'error' : undefined } className={{ badge: 'rounded-lg px-2', }} > = 3 ? 'error' : undefined } />{' '} {projectFlock.approval?.step_name}
{` ${formatTitleCase(projectFlock.category ?? '')}`}
{/* Information Grid */}
Submitted
{' '} {projectFlock.created_user?.name}
{/* BARIS 1 */}
Area
{projectFlock?.area?.name}
{/* BARIS 2 */}
Lokasi
{projectFlock?.location?.name}
FCR
{projectFlock?.fcr?.name}
{' '} Standard
{projectFlock?.production_standard?.name ?? '-'}
{/* BARIS 3 (Terakhir - TIDAK PERLU garis di bawahnya) */}
{' '} Kategori
{formatTitleCase(projectFlock.category ?? '')}
{/* Kandang Aktif */}

Kandang Aktif

{kandangApprovals && !kandangApprovalsLoading && ( )} {/* Badge Row */}
{' '} Kandang Aktif ({projectFlock.kandangs?.length})
{ setOpenBudget(!openBudgets); }} > {` ${formatCurrency( (projectFlock.project_budgets ?? []).reduce( (acc, curr) => acc + curr.price * curr.qty, 0 ) )}`}
{/* Card List Project Budgets */} {openBudgets && (projectFlock.project_budgets ?? []).map((budget) => (
{' '} Jenis Produk
{budget?.nonstock?.name}
{' '} Nama Satuan
{budget?.nonstock?.uom?.name}
{' '} Jumlah Pembelian
{formatNumber(budget.qty)}
{' '} Harga Satuan
{formatCurrency(budget.price)}
{' '} Total Harga
{formatCurrency(budget.price * budget.qty)}
))} {/* Card Kandangs */} setSelectedKamdangId(e.target.value)} value={selectedKandangId?.toString()} size='md' color='neutral' disabled={projectFlock?.approval?.step_number == 1} > {projectFlock.kandangs?.map((kandang) => (
projectFlock?.approval?.step_number > 1 && setSelectedKamdangId(kandang?.id?.toString()) } >
Kapasitas {kandang?.capacity} Ekor
))}
); }; export default ProjectFlockDetail;