import { RowActions } from '@/components/data-display'; import { ImagePreviewButton } from '@/components/dialogs'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover'; import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; import { useCan } from '@/hooks/use-can'; import { formatNumber } from '@/lib/format'; import { share as cuttingShare } from '@/routes/admin/manage/cuttings'; import { ChevronDown, Pencil, ScissorsIcon, Share2, Trash2 } from 'lucide-react'; import { useState } from 'react'; import type { Cutting } from './columns'; function generateShareLink(cuttingId: number): string { return window.location.origin + cuttingShare.url(cuttingId); } function generateWhatsappText(cutting: Cutting): string { const shareLink = generateShareLink(cutting.id); let text = `*Cutting #${cutting.id}*\n`; text += `Status: ${cutting.status === 'completed' ? 'Selesai' : cutting.status === 'cancelled' ? 'Dibatalkan' : 'Dikerjakan'}\n`; text += `Tanggal: ${cutting.formatted_created_at}\n`; text += `Oleh: ${cutting.created_by?.user_profile?.full_name ?? '-'}\n`; if (cutting.description) { text += `Deskripsi: ${cutting.description}\n`; } if (cutting.product_name) { text += `\nNama Produk: ${cutting.product_name}\n`; } if (cutting.cutting_result) { text += `Total Hasil Cutting: ${formatNumber(cutting.cutting_result)} pcs\n`; } text += `\nLihat detail lengkap:\n${shareLink}`; return text; } function openWAWeb(text: string) { const encoded = encodeURIComponent(text); window.open(`https://wa.me/?text=${encoded}`, '_blank'); } function openWAAndroid(text: string, pkg: string) { const encoded = encodeURIComponent(text); window.open(`intent://send?text=${encoded}#Intent;scheme=whatsapp;package=${pkg};end`, '_blank'); } function shareViaWhatsAppMessenger(text: string) { if (/android/i.test(navigator.userAgent)) { openWAAndroid(text, 'com.whatsapp'); } else { openWAWeb(text); } } function shareViaWhatsAppBusiness(text: string) { if (/android/i.test(navigator.userAgent)) { openWAAndroid(text, 'com.whatsapp.w4b'); } else { openWAWeb(text); } } export type CuttingCardRowParams = { cutting: Cutting; index: number; isExpanded: boolean; onToggleExpand: () => void; onComplete: (cutting: Cutting) => void; onEdit: (cutting: Cutting) => void; onDelete: (cutting: Cutting) => void; }; export function CuttingCardRow({ cutting, index, isExpanded, onToggleExpand, onComplete, onEdit, onDelete, }: CuttingCardRowParams) { const { can } = useCan(); const [shareOpen, setShareOpen] = useState(false); const materialsCount = cutting.materials_count ?? 0; const totalUsage = cutting.total_usage ?? 0; const productName = cutting.product_name ?? '-'; const cuttingResult = cutting.cutting_result ?? null; const shareText = generateWhatsappText(cutting); function handleShareWAMessenger() { shareViaWhatsAppMessenger(shareText); setShareOpen(false); } function handleShareWABusiness() { shareViaWhatsAppBusiness(shareText); setShareOpen(false); } return ( <>
{index}.

{productName}

{materialsCount > 0 && ( {materialsCount} bahan baku )} {cutting.description && ( ยท {cutting.description} )}
{cutting.formatted_created_at} {cutting.status === 'completed' ? 'Selesai' : cutting.status === 'cancelled' ? 'Dibatalkan' : 'Dikerjakan'}
Pemakaian:{' '} {formatNumber(totalUsage)} {cuttingResult && ( Hasil:{' '} {formatNumber(cuttingResult)} )} {cutting.formatted_cost_per_unit && ( Per Produk:{' '} {cutting.formatted_cost_per_unit} )} {cutting.formatted_total_material_cost && ( Biaya Keseluruhan:{' '} {cutting.formatted_total_material_cost} )}
{cutting.photo_urls && cutting.photo_urls.length > 0 && (
)}
Bagikan {(can('cuttings.update') || can('cuttings.delete')) && ( , show: can('cuttings.complete') && cutting.status === 'in_progress', onClick: () => onComplete(cutting), }, { label: 'Edit', icon: , show: can('cuttings.update'), onClick: () => onEdit(cutting), }, { label: 'Hapus', icon: ( ), show: can('cuttings.delete'), onClick: () => onDelete(cutting), }, ]} wrapperClassName="flex shrink-0 items-center gap-1" /> )}
); }