// ── UI: markup + toast + bottom sheet ──────────────────────── import { humanDateTime } from "./naming.js"; import { formatExposure } from "./exif.js"; export const esc = (s) => String(s ?? "") .replace(/&/g, "&").replace(//g, ">") .replace(/"/g, """).replace(/'/g, "'"); export function fmtBytes(n) { if (!Number.isFinite(n) || n <= 0) return "0 B"; if (n < 1024) return n + " B"; if (n < 1024 ** 2) return (n / 1024).toFixed(1) + " KB"; if (n < 1024 ** 3) return (n / 1024 ** 2).toFixed(1) + " MB"; return (n / 1024 ** 3).toFixed(2) + " GB"; } const ICON = { torch: ``, flip: ``, grid: ``, pencil: ``, trash: ``, zip: ``, share: ``, camera: ``, lock: ``, block: ``, folder: ``, image: ``, }; // ── Toast ── let toastTimer = null; export function toast(msg, ms = 2600) { const el = document.getElementById("toast"); el.textContent = msg; el.classList.add("show"); clearTimeout(toastTimer); toastTimer = setTimeout(() => el.classList.remove("show"), ms); } // ── Bottom sheet ── export function openSheet(html) { const overlay = document.getElementById("sheet-overlay"); document.getElementById("sheet-content").innerHTML = html; overlay.hidden = false; overlay.setAttribute("aria-hidden", "false"); } export function closeSheet() { const overlay = document.getElementById("sheet-overlay"); overlay.hidden = true; overlay.setAttribute("aria-hidden", "true"); document.getElementById("sheet-content").innerHTML = ""; } export function isSheetOpen() { return !document.getElementById("sheet-overlay").hidden; } // ── Camera ── export function cameraShell() { return `
`; } export function overlay(html) { const el = document.getElementById("cam-overlay"); if (el) el.innerHTML = html; } export function hero(kind, { title, text = "", steps = [], action = null, note = "" }) { const stepsHtml = steps.length ? `
    ${steps.map((s) => `
  1. ${s}
  2. `).join("")}
` : ""; const actionHtml = action ? `` : ""; return `
${kind === "denied" ? ICON.block : kind === "insecure" ? ICON.lock : ICON.camera}

${esc(title)}

${text ? `

${esc(text)}

` : ""} ${stepsHtml ? `
${stepsHtml}
` : ""} ${actionHtml} ${note ? `

${note}

` : ""}
`; } export function syncCameraControls({ torchAvailable, torchOn, zoomRange, zoomValue, gridOn, count, thumbUrl, ready, ratio }) { const chip = document.getElementById("chip-torch"); if (chip) { chip.hidden = !torchAvailable; chip.setAttribute("aria-pressed", torchOn ? "true" : "false"); } const pills = document.getElementById("zoom-pills"); if (pills) { const min = zoomRange ? Number(zoomRange.min) : 1; const max = zoomRange ? Number(zoomRange.max) : 1; if (zoomRange && max > min) { const levels = [1, 2, 3].filter((z) => z >= min - 0.05 && z <= max + 0.05); if (!levels.length) levels.push(min); const current = zoomValue ?? min; pills.hidden = false; pills.innerHTML = levels .map( (z) => `` ) .join(""); } else { pills.hidden = true; pills.innerHTML = ""; } } const frame = document.getElementById("cam-frame"); const ratioChip = document.getElementById("chip-ratio"); if (frame) { const portrait = ratio === "3:4"; frame.classList.toggle("ratio-43", !portrait); frame.classList.toggle("ratio-34", portrait); } if (ratioChip) ratioChip.textContent = ratio || "4:3"; const grid = document.getElementById("grid-lines"); if (grid) grid.classList.toggle("on", !!gridOn); const cnt = document.getElementById("shot-count"); if (cnt) cnt.textContent = count ? count + " รูป" : ""; const thumb = document.getElementById("last-thumb"); if (thumb && thumbUrl) thumb.innerHTML = `รูปล่าสุด`; const sh = document.getElementById("shutter"); if (sh) sh.disabled = !ready; } // ── Folders ── export function renderFolders({ folders, counts, activeId, storage }) { const total = counts.reduce((a, b) => a + b, 0); let html = `

โฟลเดอร์

${folders.length} โฟลเดอร์ · ${total} รูป
`; if (!folders.length) { html += `
${ICON.folder}ยังไม่มีโฟลเดอร์
`; } else { html += `
`; folders.forEach((f, i) => { const active = f.id === activeId; html += `
${esc(f.name)}
${counts[i]} รูป${active ? " · ใช้งานอยู่" : ""}
`; }); html += `
`; } html += `
`; html += `
พื้นที่เก็บในเครื่อง${storage ? ` · ใช้ ${fmtBytes(storage.usage)} จาก ${fmtBytes(storage.quota)}` : ""}
`; html += `
`; return html; } // ── Gallery ── export function renderGallery({ folder, images, thumbUrl }) { const bytes = images.reduce((a, i) => a + (i.size || i.blob?.size || 0), 0); let html = ``; } if (!images.length) { return html + `
${ICON.image}ยังไม่มีรูปในโฟลเดอร์นี้
`; } html += ``; html += `
`; return html; } // ── Photo sheet ── export function photoSheet({ record, url, folderName }) { const m = record.meta || {}; const rows = [ ["ไฟล์", record.name || "-"], ["ขนาด", fmtBytes(record.size || record.blob?.size || 0)], ["ความละเอียด", m.width && m.height ? `${m.width} × ${m.height} (${(m.width * m.height / 1e6).toFixed(1)} MP)` : "—"], ["ถ่ายเมื่อ", humanDateTime(record.createdAt)], ["โฟลเดอร์", folderName || "-"], ["อุปกรณ์", m.device || "—"], ["กล้อง", m.facingLabel || "—"], ["ที่มาไฟล์", m.sourceLabel || "—"], ]; if (m.gps) rows.push(["พิกัด GPS", `${m.gps.lat.toFixed(6)}, ${m.gps.lng.toFixed(6)}${m.gps.accuracy ? ` (±${Math.round(m.gps.accuracy)} ม.)` : ""}`]); if (m.exif && (m.exif.fNumber || m.exif.iso || m.exif.exposureTime)) rows.push(["ค่าแสง EXIF", formatExposure(m.exif)]); if (m.exif?.model) { const make = (m.exif.make || "").trim(); const model = m.exif.model.trim(); const label = make && !model.toLowerCase().startsWith(make.toLowerCase()) ? `${make} ${model}` : model; rows.push(["รุ่นใน EXIF", label]); } if (m.exif?.orientation) rows.push(["Orientation", String(m.exif.orientation)]); if (m.exif?.dateTimeOriginal) rows.push(["EXIF DateTimeOriginal", m.exif.dateTimeOriginal]); if (m.exif?.software) rows.push(["Software", m.exif.software]); if (m.exif?.gps?.dateStamp) { const t = Array.isArray(m.exif.gps.timeStamp) ? m.exif.gps.timeStamp.map((x) => String(Math.round(x)).padStart(2, "0")).join(":") : ""; rows.push(["GPS date/time", `${m.exif.gps.dateStamp}${t ? " " + t : ""}`]); } if (m.exif?.uniqueId) rows.push(["ImageUniqueID", m.exif.uniqueId.slice(0, 16) + "…"]); const rowsHtml = rows .map(([k, v]) => `
${esc(k)}${esc(v)}
`) .join(""); return `

${esc(record.name || "รูปภาพ")}

${esc(humanDateTime(record.createdAt))}
${rowsHtml}
ข้อมูลทั้งหมดฝังอยู่ในไฟล์ JPEG (EXIF) แล้ว — ติดไปกับรูปตอนแชร์/ดาวน์โหลด ไม่มีไฟล์แยก
`; } // ── Folder sheet ── export function folderSheet(mode, { id = "", name = "", suggested = "", count = 0 }) { if (mode === "delete") { return `

ลบโฟลเดอร์นี้?

"${esc(name)}" · ${count} รูป จะถูกลบทั้งหมดและกู้คืนไม่ได้
`; } const isNew = mode === "new"; return `

${isNew ? "โฟลเดอร์ใหม่" : "เปลี่ยนชื่อโฟลเดอร์"}

ชื่อเริ่มต้น = Site + ตัวอักษรถัดไป + ปี.เดือน.วัน.ชั่วโมง.นาที (แก้ได้)
${isNew ? `
ถ้าชื่อซ้ำ ระบบจะเติม (2) ให้อัตโนมัติ
` : ""}
`; } // ── QR ลิงก์ทดสอบบนมือถือ ──────────────────────────── export function qrSheet(url) { return `

ทดสอบบนมือถือ

สแกนด้วยกล้อง Samsung S23 แล้วเปิดใน Chrome
${esc(url)}
กล้องใช้ได้เฉพาะหน้า HTTPS — ถ้าลิงก์ขึ้นต้น http:// กล้องจะถูกบล็อก
`; } // ── CSV import ────────────────────────────────────────────── export function csvSheet() { return `

นำเข้าโฟลเดอร์จาก CSV

สร้างทีละหลายโฟลเดอร์ในครั้งเดียว — ชื่อที่ได้จะใช้กฎ Site + ตัวอักษร + ปี.เดือน.วัน.ชั่วโมง.นาที
siteคำนำหน้า เช่น Site (เว้นว่าง = Site)
lettera…z, aa… (เว้นว่าง = ไล่ต่ออัตโนมัติ)
datetime2569.10.25.21.45
nameชื่อเต็ม (ถ้าใส่ จะทับชื่อที่ประกอบ)
ตัวอย่างแถว
Site,a,2569.10.25.21.45,
Site,b,2569.10.25.22.10,
ไฟล์เปล่า
`; } export function importResultSheet({ created, skipped, errors = [], total }) { const list = errors.length ? `
ข้าม ${skipped} แถว
    ${errors.slice(0, 12).map((e) => `
  1. แถว ${e.row}: ${esc(e.reason)}
  2. `).join("")}
${errors.length > 12 ? `
…อีก ${errors.length - 12} แถว
` : ""}
` : ""; return `

สร้างโฟลเดอร์แล้ว ${created} รายการ

อ่าน ${total} แถว · สำเร็จ ${created} · ข้าม ${skipped}
${list}
`; } // ── พร้อมแชร์ (หลังสร้าง ZIP) ──────────────────────────────── export function shareReadySheet({ name, size, canShare, reason }) { const status = canShare ? "พร้อมส่งเข้าแอปอื่น" : "เบราว์เซอร์นี้ไม่รองรับการแชร์ไฟล์"; const note = canShare ? "แตะปุ่มด้านล่างเพื่อเปิด share sheet ของ Samsung" : "บันทึกไฟล์แล้วแชร์ผ่าน Files/My Files หรือเปิดด้วย Chrome"; return `

ZIP พร้อมแล้ว

ไฟล์${esc(name)}
ขนาด${esc(fmtBytes(size))}
การแชร์${esc(status)}
${esc(note)}${reason ? " · " + esc(reason) : ""}
`; } // ── Diagnostics ── export function diagnosticsSheet(rows) { const body = rows .map(([k, v]) => `
${esc(k)}${esc(v)}
`) .join(""); return `

ข้อมูลอุปกรณ์และกล้อง

ใช้ตรวจว่ารันบน S23 จริงและกล้องได้ความละเอียดเท่าไร
${body}
`; } // ── Progress ── export function progressSheet(label) { openSheet(`
${esc(label)}
`); } export function progressUpdate(pct, label) { const card = document.getElementById("progress-card"); if (!card) return; const p = Math.max(0, Math.min(100, Math.round(pct))); card.innerHTML = `
${esc(label || "")} ${p}%
`; }