This commit is contained in:
2026-06-30 10:39:29 +07:00
parent aaafa6ada9
commit d9c8e3930b
28 changed files with 3040 additions and 167 deletions

View File

@@ -437,45 +437,291 @@ body {
font-family: monospace;
}
.clock-display-summary {
display: flex;
flex-wrap: wrap;
gap: 0.45rem;
margin-bottom: 0.45rem;
padding-bottom: 0.45rem;
border-bottom: 1px dashed var(--border-color);
flex: 0 0 auto;
.card-title-row--shifts {
margin-bottom: 0.2rem;
flex-wrap: nowrap;
gap: 0.35rem 0.5rem;
align-items: center;
}
.clock-chip {
.clock-inline {
display: inline-flex;
align-items: center;
gap: 0.35rem;
padding: 0.2rem 0.5rem;
border-radius: 6px;
border: 1px solid var(--border-color);
background: var(--bg-subtle);
font-size: 0.72rem;
}
.clock-chip em {
font-style: normal;
flex-wrap: wrap;
margin-left: auto;
font-size: 0.68rem;
color: var(--text-muted);
font-weight: 600;
}
.clock-chip strong {
.clock-inline-item {
display: inline-flex;
align-items: center;
gap: 0.2rem;
}
.clock-inline-item strong {
font-family: 'Share Tech Mono', monospace;
font-size: 0.78rem;
font-size: 0.7rem;
font-weight: 700;
}
.clock-chip--online strong {
color: var(--online-color);
.clock-inline-item strong.online { color: var(--online-color); }
.clock-inline-item strong.offline { color: var(--offline-color); }
.clock-inline-dot {
width: 5px;
height: 5px;
border-radius: 50%;
flex-shrink: 0;
}
.clock-chip--offline strong {
color: var(--offline-color);
.clock-inline-dot.online { background: var(--online-color); }
.clock-inline-dot.offline { background: var(--offline-color); }
.clock-inline-sep {
opacity: 0.35;
font-size: 0.65rem;
}
.card-title-row--shifts .card-title-sub {
font-size: 0.62rem;
margin-left: 0.35rem;
white-space: nowrap;
}
/* Student chat messenger */
.student-chat-dock {
position: fixed;
right: 12px;
bottom: 12px;
z-index: 1000;
}
.student-chat-toast {
position: absolute;
right: 0;
bottom: 64px;
width: 260px;
padding: 0.65rem 0.75rem;
border-radius: 10px;
background: #1e293b;
color: #fff;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
font-size: 0.78rem;
display: flex;
flex-direction: column;
gap: 0.2rem;
}
.student-chat-fab {
position: relative;
width: 48px;
height: 48px;
border-radius: 50%;
border: none;
background: var(--accent);
color: #fff;
font-size: 1.15rem;
cursor: pointer;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}
.student-chat-badge {
position: absolute;
top: -2px;
right: -2px;
min-width: 16px;
height: 16px;
padding: 0 4px;
border-radius: 8px;
background: #ef4444;
color: #fff;
font-size: 0.62rem;
font-weight: 700;
line-height: 16px;
}
.student-chat-messenger {
position: absolute;
right: 0;
bottom: 58px;
width: min(520px, calc(100vw - 24px));
height: min(400px, calc(100vh - 80px));
background: var(--bg-card);
border: 1px solid var(--border-color);
border-radius: 12px;
box-shadow: 0 12px 36px rgba(0, 0, 0, 0.3);
display: flex;
flex-direction: column;
overflow: hidden;
}
.student-chat-messenger-head {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.55rem 0.7rem;
border-bottom: 1px solid var(--border-color);
font-size: 0.82rem;
flex-shrink: 0;
}
.student-chat-close {
border: none;
background: none;
color: var(--text-muted);
font-size: 1.1rem;
cursor: pointer;
}
.student-chat-messenger-body {
display: flex;
flex: 1;
min-height: 0;
}
.student-chat-sidebar {
width: 150px;
flex-shrink: 0;
border-right: 1px solid var(--border-color);
overflow-y: auto;
background: rgba(255, 255, 255, 0.02);
display: flex;
flex-direction: column;
}
.student-chat-conv-btn {
width: 100%;
text-align: left;
border: none;
background: transparent;
padding: 0.55rem 0.5rem;
cursor: pointer;
border-bottom: 1px solid var(--border-color);
color: var(--text-primary);
}
.student-chat-conv-btn.active {
background: rgba(187, 33, 38, 0.12);
border-left: 2px solid var(--accent);
}
.student-chat-conv-name {
display: flex;
justify-content: space-between;
font-size: 0.72rem;
font-weight: 600;
}
.student-chat-conv-unread {
font-style: normal;
background: var(--accent);
color: #fff;
border-radius: 8px;
padding: 0 5px;
font-size: 0.6rem;
}
.student-chat-conv-preview {
display: block;
font-size: 0.65rem;
color: var(--text-muted);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-top: 0.15rem;
}
.student-chat-thread {
flex: 1;
display: flex;
flex-direction: column;
min-width: 0;
}
.student-chat-thread-head {
padding: 0.45rem 0.65rem;
border-bottom: 1px solid var(--border-color);
font-size: 0.75rem;
font-weight: 600;
flex-shrink: 0;
}
.student-chat-messages {
flex: 1;
overflow-y: auto;
padding: 0.55rem;
display: flex;
flex-direction: column;
gap: 0.4rem;
background: rgba(0, 0, 0, 0.12);
}
.student-chat-empty {
font-size: 0.72rem;
color: var(--text-muted);
text-align: center;
padding: 1rem 0.5rem;
}
.student-chat-bubble {
max-width: 88%;
font-size: 0.76rem;
}
.student-chat-bubble--staff { align-self: flex-start; }
.student-chat-bubble--student { align-self: flex-end; }
.student-chat-label {
font-size: 0.6rem;
color: var(--text-muted);
margin-bottom: 0.1rem;
}
.student-chat-body {
padding: 0.4rem 0.55rem;
border-radius: 10px;
line-height: 1.35;
word-break: break-word;
}
.student-chat-bubble--staff .student-chat-body {
background: rgba(255, 255, 255, 0.06);
border: 1px solid var(--border-color);
}
.student-chat-bubble--student .student-chat-body {
background: var(--accent);
color: #fff;
}
.student-chat-time {
font-size: 0.58rem;
color: var(--text-muted);
margin-top: 0.1rem;
}
.student-chat-compose {
display: flex;
gap: 0.35rem;
padding: 0.45rem 0.55rem;
border-top: 1px solid var(--border-color);
flex-shrink: 0;
}
.student-chat-compose input {
flex: 1;
font-size: 0.74rem;
padding: 0.4rem 0.5rem;
border-radius: 8px;
border: 1px solid var(--border-color);
background: var(--bg-base);
color: var(--text-primary);
}
.student-chat-compose input:disabled {
opacity: 0.5;
}
.shifts-wrap {
@@ -622,15 +868,15 @@ body {
.clocks-card {
display: flex;
flex-direction: column;
gap: 0.4rem;
gap: 0.3rem;
flex: 1;
min-height: 0;
overflow: hidden;
padding: 0.75rem 0.85rem;
padding: 0.5rem 0.65rem;
}
.card-title {
font-size: 0.85rem;
font-size: 0.78rem;
font-weight: 700;
color: var(--text-primary);
letter-spacing: -0.2px;

View File

@@ -23,6 +23,11 @@ let stats = {
shifts: []
};
let chatOpen = false;
let chatMessages = [];
let chatConversations = [];
let activeStaffId = 0;
// Quản lý webcam
let webcamStream = null;
let webcamInterval = null;
@@ -46,6 +51,15 @@ function init() {
window.runtime.EventsOn('start_webcam_stream', startWebcam);
window.runtime.EventsOn('stop_webcam_stream', stopWebcam);
window.runtime.EventsOn('chat:message', () => {
updateChatBadge();
loadChatConversations().catch(console.error);
if (chatOpen && activeStaffId) loadChatMessages(activeStaffId).catch(console.error);
});
window.runtime.EventsOn('chat:notify', (data) => {
updateChatBadge();
showChatToast(data);
});
checkLogin();
}
@@ -57,6 +71,8 @@ async function checkLogin() {
studentInfo = await window.go.main.App.GetStudentInfo();
renderDashboard();
startStatsTicker();
ensureChatWidget();
updateChatBadge();
} else {
loggedIn = false;
renderLoginPrompt();
@@ -226,19 +242,14 @@ function renderDashboard() {
</div>
<div class="clocks-card card">
<div class="card-title-row">
<div class="card-title-row card-title-row--shifts">
<div class="card-title">Hôm nay — theo ca</div>
<div class="card-title-sub" id="session-date">${stats.sessionDate || ''}</div>
</div>
<div class="clock-display clock-display-summary">
<span class="clock-chip clock-chip--online">
<em>Trực tuyến</em>
<strong id="clock-online">00:00:00</strong>
</span>
<span class="clock-chip clock-chip--offline">
<em>Ngoại tuyến</em>
<strong id="clock-offline">00:00:00</strong>
</span>
<div class="clock-inline">
<span class="clock-inline-item" title="Tổng trực tuyến"><span class="clock-inline-dot online"></span><strong id="clock-online" class="online">00:00:00</strong></span>
<span class="clock-inline-sep">·</span>
<span class="clock-inline-item" title="Tổng ngoại tuyến"><span class="clock-inline-dot offline"></span><strong id="clock-offline" class="offline">00:00:00</strong></span>
<span class="card-title-sub" id="session-date">${stats.sessionDate || ''}</span>
</div>
</div>
<div class="shifts-wrap" id="shifts-wrap">
${renderShiftsTable(stats.shifts)}
@@ -254,11 +265,177 @@ function renderDashboard() {
loggedIn = false;
studentInfo = null;
stopWebcam();
const w = document.getElementById('student-chat-widget');
if (w) w.remove();
await window.go.main.App.Logout();
}
});
}
function ensureChatWidget() {
if (document.getElementById('student-chat-widget')) return;
const wrap = document.createElement('div');
wrap.id = 'student-chat-widget';
wrap.className = 'student-chat-dock';
wrap.innerHTML = `
<div class="student-chat-toast" id="student-chat-toast" hidden></div>
<button type="button" class="student-chat-fab" id="student-chat-fab" title="Tin nhắn">
💬<span class="student-chat-badge" id="student-chat-badge" hidden>0</span>
</button>
<div class="student-chat-messenger" id="student-chat-messenger" hidden>
<header class="student-chat-messenger-head">
<strong>Tin nhắn</strong>
<button type="button" class="student-chat-close" id="student-chat-close">×</button>
</header>
<div class="student-chat-messenger-body">
<aside class="student-chat-sidebar" id="student-chat-conv-list"></aside>
<section class="student-chat-thread">
<div class="student-chat-thread-head" id="student-chat-thread-head">Chọn hội thoại</div>
<div class="student-chat-messages" id="student-chat-messages"></div>
<div class="student-chat-compose">
<input id="student-chat-input" placeholder="Nhập tin nhắn..." disabled />
<button type="button" class="btn btn-primary btn-sm" id="student-chat-send" disabled>Gửi</button>
</div>
</section>
</div>
</div>
`;
document.body.appendChild(wrap);
document.getElementById('student-chat-fab').addEventListener('click', toggleChat);
document.getElementById('student-chat-close').addEventListener('click', () => setChatOpen(false));
document.getElementById('student-chat-send').addEventListener('click', sendStudentChat);
document.getElementById('student-chat-input').addEventListener('keydown', (e) => {
if (e.key === 'Enter') { e.preventDefault(); sendStudentChat(); }
});
}
function showChatToast(data) {
const toast = document.getElementById('student-chat-toast');
if (!toast) return;
const from = data?.from || 'Giảng viên';
const preview = data?.preview || 'Bạn có tin nhắn mới';
toast.innerHTML = `<strong>${escapeHtml(from)}</strong><span>${escapeHtml(preview)}</span>`;
toast.hidden = false;
clearTimeout(showChatToast._t);
showChatToast._t = setTimeout(() => { toast.hidden = true; }, 5000);
}
function setChatOpen(open) {
chatOpen = open;
const panel = document.getElementById('student-chat-messenger');
if (panel) panel.hidden = !open;
if (open) {
loadChatConversations().catch(console.error);
if (activeStaffId) loadChatMessages(activeStaffId).catch(console.error);
}
}
function toggleChat() {
setChatOpen(!chatOpen);
}
async function loadChatConversations() {
const rows = await window.go.main.App.GetChatConversations();
chatConversations = Array.isArray(rows) ? rows : [];
renderChatConversations();
updateChatBadge();
}
function renderChatConversations() {
const list = document.getElementById('student-chat-conv-list');
if (!list) return;
if (!chatConversations.length) {
list.innerHTML = '<div class="student-chat-empty">Chưa có tin nhắn</div>';
return;
}
list.innerHTML = chatConversations.map((c) => {
const sid = Number(c.staffId || 0);
const unread = Number(c.unread || 0);
const active = sid === activeStaffId ? ' active' : '';
const name = escapeHtml(c.staffName || c.staffEmail || 'Giảng viên');
const preview = escapeHtml(c.lastMessage || '');
return `<button type="button" class="student-chat-conv-btn${active}" data-staff-id="${sid}">
<span class="student-chat-conv-name">${name}${unread > 0 ? `<em class="student-chat-conv-unread">${unread}</em>` : ''}</span>
<span class="student-chat-conv-preview">${preview}</span>
</button>`;
}).join('');
list.querySelectorAll('.student-chat-conv-btn').forEach((btn) => {
btn.addEventListener('click', () => {
const sid = Number(btn.getAttribute('data-staff-id'));
pickStaffChat(sid);
});
});
}
async function pickStaffChat(staffId) {
activeStaffId = staffId;
const head = document.getElementById('student-chat-thread-head');
const conv = chatConversations.find((c) => Number(c.staffId) === staffId);
if (head) head.textContent = conv?.staffName || conv?.staffEmail || 'Giảng viên';
const input = document.getElementById('student-chat-input');
const sendBtn = document.getElementById('student-chat-send');
if (input) input.disabled = !staffId;
if (sendBtn) sendBtn.disabled = !staffId;
renderChatConversations();
if (staffId) await loadChatMessages(staffId);
}
async function loadChatMessages(staffId) {
const msgs = await window.go.main.App.GetChatMessages(staffId);
chatMessages = Array.isArray(msgs) ? msgs : [];
renderChatMessages();
updateChatBadge();
loadChatConversations().catch(console.error);
}
function renderChatMessages() {
const box = document.getElementById('student-chat-messages');
if (!box) return;
if (!chatMessages.length) {
box.innerHTML = '<div class="student-chat-empty">Chưa có tin nhắn</div>';
return;
}
box.innerHTML = chatMessages.map((m) => {
const role = m.senderRole === 'student' ? 'student' : 'staff';
const time = m.createdAt ? new Date(m.createdAt).toLocaleTimeString('vi-VN', { hour: '2-digit', minute: '2-digit' }) : '';
const label = role === 'staff' ? (m.staffName || 'Giảng viên') : 'Bạn';
return `<div class="student-chat-bubble student-chat-bubble--${role}"><div class="student-chat-label">${label}</div><div class="student-chat-body">${escapeHtml(m.body || '')}</div><div class="student-chat-time">${time}</div></div>`;
}).join('');
box.scrollTop = box.scrollHeight;
}
function escapeHtml(s) {
return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
async function sendStudentChat() {
const input = document.getElementById('student-chat-input');
if (!input || !input.value.trim() || !activeStaffId) return;
try {
await window.go.main.App.SendChatMessage(input.value.trim());
input.value = '';
await loadChatMessages(activeStaffId);
} catch (err) {
alert(err?.message || err || 'Gửi tin thất bại');
}
}
async function updateChatBadge() {
const badge = document.getElementById('student-chat-badge');
if (!badge || !window.go?.main?.App?.GetChatUnread) return;
try {
const n = await window.go.main.App.GetChatUnread();
if (n > 0) {
badge.hidden = false;
badge.textContent = n > 9 ? '9+' : String(n);
} else {
badge.hidden = true;
}
} catch {
/* ignore */
}
}
function formatDuration(totalSeconds) {
const hrs = Math.floor(totalSeconds / 3600);
const mins = Math.floor((totalSeconds % 3600) / 60);
@@ -330,6 +507,8 @@ function startStatsTicker() {
if (indicator) {
indicator.className = `status-indicator ${stats.serverReachable ? 'online' : 'offline'}`;
}
updateChatBadge();
} catch (err) {
console.error('Error fetching stats:', err);
}