<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Buku Tamu Digital</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
:root {
--primary-color: #1a73e8;
--bg-color: #f8f9fa;
}
body { background-color: var(--bg-color); font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
.card { border-radius: 15px; border: none; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.btn-primary { background-color: var(--primary-color); border: none; padding: 10px 25px; border-radius: 8px; }
#canvas { display: none; }
#photo-preview { width: 100%; border-radius: 12px; display: none; margin-top: 15px; border: 1px solid #ddd; }
.nav-custom { background: white; box-shadow: 0 2px 4px rgba(0,0,0,0.05); margin-bottom: 30px; }
.loading-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(255,255,255,0.8); display: none; align-items: center; justify-content: center; z-index: 9999; }
</style>
</head>
<body>
<div class="loading-overlay" id="loading">
<div class="spinner-border text-primary" role="status"></div>
<span class="ms-2">Mohon tunggu...</span>
</div>
<nav class="navbar navbar-expand-lg nav-custom">
<div class="container">
<a class="navbar-brand fw-bold text-primary" href="#" onclick="showView('landing')">GUESTBOOK</a>
<button class="btn btn-outline-primary btn-sm" onclick="showView('dashboard')">Admin Dashboard</button>
</div>
</nav>
<div class="container mb-5">
<!-- View: Landing Page / Form -->
<div id="view-landing">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card p-4">
<h3 class="text-center mb-4 fw-bold">Selamat Datang</h3>
<p class="text-muted text-center mb-4">Silakan isi data kunjungan Anda</p>
<form id="guestForm">
<div class="mb-3">
<label class="form-label">Nama Lengkap</label>
<input type="text" class="form-control" name="nama" required placeholder="Contoh: Budi Santoso">
</div>
<div class="mb-3">
<label class="form-label">Instansi / Perusahaan</label>
<input type="text" class="form-control" name="instansi" required placeholder="Contoh: PT. Maju Jaya">
</div>
<div class="mb-3">
<label class="form-label">Tujuan Kunjungan</label>
<input type="text" class="form-control" name="tujuan" required placeholder="Contoh: Meeting Koordinasi">
</div>
<div class="mb-3">
<label class="form-label">Staff yang Ditemui</label>
<input type="text" class="form-control" name="staff" required placeholder="Contoh: Ibu Siti (HRD)">
</div>
<div class="mb-4">
<label class="form-label">Upload Foto Identitas / Tamu</label>
<input type="file" id="photo-input" accept="image/*" class="form-control" required>
<canvas id="canvas"></canvas>
<img id="photo-preview" src="" alt="Pratinjau Foto">
<div id="file-help" class="form-text">Pilih file gambar (JPG/PNG). Foto akan dikompres secara otomatis.</div>
</div>
<button type="submit" class="btn btn-primary w-100">Kirim Data</button>
</form>
</div>
</div>
</div>
</div>
<!-- View: Dashboard -->
<div id="view-dashboard" style="display:none;">
<div class="card p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold mb-0">Riwayat Tamu</h3>
<button class="btn btn-sm btn-outline-secondary" onclick="loadDashboardData()">Refresh Data</button>
</div>
<div class="table-responsive">
<table class="table table-hover">
<thead class="table-light">
<tr>
<th>Waktu</th>
<th>Nama</th>
<th>Instansi</th>
<th>Tujuan</th>
<th>Aksi</th>
</tr>
</thead>
<tbody id="dashboard-body">
<!-- Data dimuat di sini -->
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
let photoBase64 = "";
const canvas = document.getElementById('canvas');
const photoPreview = document.getElementById('photo-preview');
const photoInput = document.getElementById('photo-input');
const loading = document.getElementById('loading');
// --- Navigasi View ---
function showView(view) {
document.getElementById('view-landing').style.display = view === 'landing' ? 'block' : 'none';
document.getElementById('view-dashboard').style.display = view === 'dashboard' ? 'block' : 'none';
if(view === 'dashboard') loadDashboardData();
}
// --- Logika Unggah File ---
photoInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.onload = function() {
// Logika Resize untuk menjaga ukuran file tetap aman di GAS (Max 800px)
const maxWidth = 800;
const maxHeight = 800;
let width = img.width;
let height = img.height;
if (width > height) {
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
} else {
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
}
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, width, height);
// Kompresi kualitas menjadi 0.7
photoBase64 = canvas.toDataURL('image/jpeg', 0.7);
photoPreview.src = photoBase64;
photoPreview.style.display = "block";
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
});
// --- Logika Formulir ---
document.getElementById('guestForm').addEventListener('submit', function(e) {
e.preventDefault();
if(!photoBase64) return alert("Silakan pilih foto terlebih dahulu.");
loading.style.display = "flex";
const formData = {
nama: this.nama.value,
instansi: this.instansi.value,
tujuan: this.tujuan.value,
staff: this.staff.value,
photoBase64: photoBase64
};
google.script.run
.withSuccessHandler(res => {
loading.style.display = "none";
if(res.success) {
alert("Terima kasih! Data Anda telah tersimpan.");
this.reset();
photoPreview.style.display = "none";
photoBase64 = "";
} else {
alert("Error: " + res.message);
}
})
.processForm(formData);
});
// --- Logika Dashboard ---
function loadDashboardData() {
loading.style.display = "flex";
google.script.run
.withSuccessHandler(data => {
const tbody = document.getElementById('dashboard-body');
tbody.innerHTML = "";
data.forEach(row => {
tbody.innerHTML += `
<tr>
<td><small>${row.Timestamp}</small></td>
<td><strong>${row.Nama}</strong></td>
<td>${row.Instansi}</td>
<td>${row.Tujuan}</td>
<td>
<button class="btn btn-sm btn-primary" onclick="printPDF('${row.ID}')">Cetak PDF</button>
</td>
</tr>
`;
});
loading.style.display = "none";
})
.getGuestData();
}
function printPDF(id) {
loading.style.display = "flex";
google.script.run
.withSuccessHandler(res => {
loading.style.display = "none";
if(res.success) {
window.open(res.url, '_blank');
} else {
alert("Gagal cetak: " + res.message);
}
})
.generatePDF(id);
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
Tidak ada komentar:
Posting Komentar