import React, { useState } from 'react'; const KINDS = ['architectural', 'structural', 'mep', 'other']; const KIND_LABELS = { architectural: ['Architectural', 'معماري'], structural: ['Structural', 'إنشائي'], mep: ['MEP', 'كهرباء وميكانيك'], other: ['Other', 'أخرى'], }; function fileToDataUrl(file) { return new Promise((resolve) => { const r = new FileReader(); r.onload = () => resolve(String(r.result)); r.onerror = () => resolve(''); r.readAsDataURL(file); }); } function fmtSize(bytes) { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`; return `${(bytes / 1024 / 1024).toFixed(1)} MB`; } // Lightweight zero-dependency client-side PDF page counter function countPdfPages(file) { return new Promise((resolve) => { const reader = new FileReader(); reader.onload = function (e) { try { const arr = new Uint8Array(e.target.result); const text = new TextDecoder().decode(arr); // Find all occurrences of '/Type /Page' or '/Count' // A simple robust approach is to look for "/Count " in the catalog const match = text.match(/\/Count\s+(\d+)/); if (match && match[1]) { resolve(parseInt(match[1], 10)); return; } // Fallback: count /Type /Page objects const pageMatches = text.match(/\/Type\s*\/Page\b/g); if (pageMatches) { resolve(pageMatches.length); return; } resolve(1); } catch { resolve(1); } }; reader.onerror = () => resolve(1); reader.readAsArrayBuffer(file); }); } export default function DrawingUpload({ drawings, setDrawings, isArabic }) { const t = (en, ar) => (isArabic ? ar : en); const [busy, setBusy] = useState(false); async function onFiles(list) { if (!list || !list.length) return; setBusy(true); try { const out = []; for (const f of Array.from(list)) { const id = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; let thumb = ''; let pages = 1; if (f.type === 'application/pdf') { pages = await countPdfPages(f); thumb = ''; // Use PDF icon placeholder } else if (f.type.startsWith('image/')) { thumb = await fileToDataUrl(f); pages = 1; } out.push({ id, name: f.name, kind: 'architectural', size: f.size, pages, thumb, }); } if (out.length) { setDrawings([...drawings, ...out]); } } catch (e) { alert('Failed to read drawing: ' + (e.message || String(e))); } finally { setBusy(false); } } function remove(id) { setDrawings(drawings.filter((d) => d.id !== id)); } function setKind(id, kind) { setDrawings( drawings.map((d) => (d.id === id ? { ...d, kind } : d)) ); } return (
{t('No drawings attached for reference.', 'لا توجد مخططات مرفقة للمرجع.')}
)} {drawings.length > 0 && (