298 lines
10 KiB
JavaScript
Executable file
298 lines
10 KiB
JavaScript
Executable file
/**
|
|
* Executive Cockpit Application Controller V5.3
|
|
* Pure SQLite integration, real token auth, dynamic kanban mutations.
|
|
*/
|
|
import { Api } from './api.js?v=5.3.0';
|
|
import { Components } from './components.js?v=5.3.0';
|
|
|
|
class AppController {
|
|
constructor() {
|
|
this.user = null;
|
|
this.state = null;
|
|
this.activeTab = 'command';
|
|
this.init();
|
|
}
|
|
|
|
async init() {
|
|
const token = Api.getToken();
|
|
const role = sessionStorage.getItem('exec_role');
|
|
const name = sessionStorage.getItem('exec_name');
|
|
|
|
if (token && role && name) {
|
|
this.user = { role, name, token };
|
|
this.renderApp();
|
|
await this.refreshState();
|
|
this.initSse();
|
|
} else {
|
|
this.renderLogin();
|
|
}
|
|
}
|
|
|
|
renderLogin() {
|
|
const root = document.getElementById('app');
|
|
root.innerHTML = `
|
|
<div class="min-h-screen bg-slate-950 flex items-center justify-center p-4">
|
|
<div class="w-full max-w-md bg-slate-900 border border-slate-800 rounded-3xl p-8 shadow-2xl">
|
|
<div class="text-center mb-6">
|
|
<div class="w-12 h-12 rounded-2xl bg-indigo-600 mx-auto flex items-center justify-center text-white font-bold text-lg mb-3 shadow-lg shadow-indigo-600/30">
|
|
E
|
|
</div>
|
|
<h2 class="text-base font-bold text-white">داشبورد اجرایی اکیپا</h2>
|
|
<p class="text-xs text-slate-400 mt-1">احراز هویت توکنمحور با اعتبارسنجی سمت سرور</p>
|
|
</div>
|
|
|
|
<form id="loginForm" class="space-y-4">
|
|
<div>
|
|
<label class="block text-xs font-medium text-slate-300 mb-1.5">کلید دسترسی امن (PIN)</label>
|
|
<input type="password" id="inputPin" placeholder="••••••••••••" class="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-3 text-sm text-white placeholder-slate-600 focus:outline-none focus:border-indigo-500 font-mono tracking-widest text-center" required />
|
|
</div>
|
|
|
|
<div id="loginErrorMsg" class="hidden text-xs text-rose-400 bg-rose-500/10 border border-rose-500/20 rounded-xl p-3 text-center"></div>
|
|
|
|
<button type="submit" id="btnSubmitAuth" class="w-full py-3.5 bg-indigo-600 hover:bg-indigo-500 text-white font-bold rounded-xl text-xs transition-all shadow-lg shadow-indigo-600/20 flex items-center justify-center gap-2">
|
|
ورود به سیستم اجرایی
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
document.getElementById('loginForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const pin = document.getElementById('inputPin').value;
|
|
const btn = document.getElementById('btnSubmitAuth');
|
|
const errBox = document.getElementById('loginErrorMsg');
|
|
|
|
btn.disabled = true;
|
|
btn.innerText = 'در حال اعتبارسنجی...';
|
|
errBox.classList.add('hidden');
|
|
|
|
try {
|
|
const authData = await Api.login(pin);
|
|
this.user = { role: authData.role, name: authData.name, token: authData.token };
|
|
this.renderApp();
|
|
await this.refreshState();
|
|
this.initSse();
|
|
} catch (err) {
|
|
errBox.innerText = err.message;
|
|
errBox.classList.remove('hidden');
|
|
btn.disabled = false;
|
|
btn.innerText = 'ورود به سیستم اجرایی';
|
|
}
|
|
});
|
|
}
|
|
|
|
renderApp() {
|
|
const root = document.getElementById('app');
|
|
root.innerHTML = `
|
|
<div id="appContainer" class="min-h-screen bg-slate-950 text-slate-100 flex flex-col font-sans">
|
|
<div id="headerMount"></div>
|
|
<main id="mainMount" class="flex-1 p-6 max-w-7xl mx-auto w-full"></main>
|
|
</div>
|
|
<div id="modalMount"></div>
|
|
`;
|
|
this.updateHeader();
|
|
this.bindNav();
|
|
}
|
|
|
|
updateHeader() {
|
|
const mount = document.getElementById('headerMount');
|
|
if (!mount) return;
|
|
const timeStr = this.state?.server_time_iran || 'در حال دریافت زمان...';
|
|
mount.innerHTML = Components.renderHeader(this.user, timeStr);
|
|
|
|
if (window.lucide) window.lucide.createIcons();
|
|
|
|
// Bind Logout
|
|
document.getElementById('btnLogout')?.addEventListener('click', () => {
|
|
Api.logout();
|
|
window.location.reload();
|
|
});
|
|
|
|
// Re-bind navigation
|
|
this.bindNav();
|
|
}
|
|
|
|
bindNav() {
|
|
const tabs = ['command', 'focus', 'braindump', 'calendar'];
|
|
tabs.forEach(tab => {
|
|
const el = document.getElementById(`nav-${tab}`);
|
|
if (el) {
|
|
el.addEventListener('click', async () => {
|
|
this.activeTab = tab;
|
|
await this.refreshState();
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
async refreshState() {
|
|
try {
|
|
this.state = await Api.getState();
|
|
this.updateHeader();
|
|
this.renderTabContent();
|
|
} catch (err) {
|
|
console.error('State refresh failed:', err);
|
|
}
|
|
}
|
|
|
|
renderTabContent() {
|
|
const mount = document.getElementById('mainMount');
|
|
if (!mount || !this.state) return;
|
|
|
|
// Highlight active nav tab
|
|
document.querySelectorAll('.nav-tab').forEach(b => {
|
|
b.classList.remove('bg-indigo-600', 'text-white');
|
|
b.classList.add('text-slate-300');
|
|
});
|
|
const currentTabBtn = document.getElementById(`nav-${this.activeTab}`);
|
|
if (currentTabBtn) {
|
|
currentTabBtn.classList.add('bg-indigo-600', 'text-white');
|
|
currentTabBtn.classList.remove('text-slate-300');
|
|
}
|
|
|
|
if (this.activeTab === 'command') {
|
|
mount.innerHTML = Components.renderKanbanBoard(this.state.tasks, this.state.counts);
|
|
this.bindKanbanActions();
|
|
} else if (this.activeTab === 'focus') {
|
|
const focusTask = this.state.tasks.find(t => t.priority === 'crisis' || t.priority === 'urgent' || t.status === 'in_progress');
|
|
const remainingCount = this.state.tasks.filter(t => t.status !== 'done').length;
|
|
mount.innerHTML = Components.renderExecutiveLockdown(focusTask, remainingCount);
|
|
this.bindLockdownActions();
|
|
} else if (this.activeTab === 'braindump') {
|
|
mount.innerHTML = Components.renderSmartBrainDump();
|
|
this.bindBrainDumpActions();
|
|
} else if (this.activeTab === 'calendar') {
|
|
mount.innerHTML = Components.renderChronologicalCalendar(this.state.meetings, this.state.server_time_iran);
|
|
}
|
|
|
|
if (window.lucide) window.lucide.createIcons();
|
|
}
|
|
|
|
bindKanbanActions() {
|
|
// Normal Status Moves
|
|
document.querySelectorAll('.btn-move-status').forEach(btn => {
|
|
btn.addEventListener('click', async (e) => {
|
|
const taskId = btn.dataset.taskId;
|
|
const targetStatus = btn.dataset.targetStatus;
|
|
btn.disabled = true;
|
|
await Api.updateTaskStatus(taskId, targetStatus);
|
|
await this.refreshState();
|
|
});
|
|
});
|
|
|
|
// Move to Blocked (Prompts Reason)
|
|
document.querySelectorAll('.btn-move-blocked').forEach(btn => {
|
|
btn.addEventListener('click', async () => {
|
|
const taskId = btn.dataset.taskId;
|
|
const reason = prompt('لطفاً علت توقف یا سند مورد انتظار را بنویسید:');
|
|
if (reason) {
|
|
btn.disabled = true;
|
|
await Api.updateTaskStatus(taskId, 'blocked', reason);
|
|
await this.refreshState();
|
|
}
|
|
});
|
|
});
|
|
|
|
// New Task Modal
|
|
document.getElementById('btnOpenNewTaskModal')?.addEventListener('click', () => {
|
|
const title = prompt('عنوان اقدام جدید:');
|
|
if (title) {
|
|
Api.createTask({
|
|
title,
|
|
owner: this.user.role === 'ceo' ? 'حامد فلاحی' : 'محدثه معصومی',
|
|
priority: 'urgent',
|
|
status: 'todo'
|
|
}).then(() => this.refreshState());
|
|
}
|
|
});
|
|
}
|
|
|
|
bindLockdownActions() {
|
|
const doneBtn = document.getElementById('btnLockdownDone');
|
|
const blockedBtn = document.getElementById('btnLockdownBlocked');
|
|
const exitBtn = document.getElementById('btnExitLockdown');
|
|
|
|
if (doneBtn) {
|
|
doneBtn.addEventListener('click', async () => {
|
|
const taskId = doneBtn.dataset.taskId;
|
|
doneBtn.disabled = true;
|
|
await Api.updateTaskStatus(taskId, 'done');
|
|
await this.refreshState();
|
|
});
|
|
}
|
|
|
|
if (blockedBtn) {
|
|
blockedBtn.addEventListener('click', async () => {
|
|
const taskId = blockedBtn.dataset.taskId;
|
|
const reason = prompt('علت توقف کار چیست؟');
|
|
if (reason) {
|
|
blockedBtn.disabled = true;
|
|
await Api.updateTaskStatus(taskId, 'blocked', reason);
|
|
await this.refreshState();
|
|
}
|
|
});
|
|
}
|
|
|
|
if (exitBtn) {
|
|
exitBtn.addEventListener('click', () => {
|
|
this.activeTab = 'command';
|
|
this.renderTabContent();
|
|
});
|
|
}
|
|
}
|
|
|
|
bindBrainDumpActions() {
|
|
const btn = document.getElementById('btnSubmitNlp');
|
|
const input = document.getElementById('nlpInputText');
|
|
const feedback = document.getElementById('nlpFeedbackBox');
|
|
|
|
btn?.addEventListener('click', async () => {
|
|
const text = input.value.trim();
|
|
if (!text) return;
|
|
btn.disabled = true;
|
|
btn.innerText = 'در حال تحلیل هوشمند...';
|
|
|
|
try {
|
|
const res = await Api.smartNlpParse(text);
|
|
input.value = '';
|
|
feedback.className = 'p-4 rounded-xl bg-emerald-500/20 border border-emerald-500/40 text-emerald-300 text-xs block';
|
|
feedback.innerHTML = `
|
|
<strong>تحلیل موفق:</strong> عنوان «${res.parsed.title}» با مالکیت ${res.parsed.owner} و اولویت ${res.parsed.priority} مستقیماً در دیتابیس ثبت شد.
|
|
`;
|
|
await this.refreshState();
|
|
} catch (err) {
|
|
alert(err.message);
|
|
} finally {
|
|
btn.disabled = false;
|
|
btn.innerText = 'ثبت و پردازش در دیتابیس';
|
|
}
|
|
});
|
|
}
|
|
|
|
initSse() {
|
|
try {
|
|
const evtSource = new EventSource('/executive/api/events');
|
|
evtSource.onmessage = (event) => {
|
|
try {
|
|
const data = JSON.parse(event.data);
|
|
if (data.type === 'crisis_alert') {
|
|
alert(`هشدار بحران جدید: ${data.task.title}`);
|
|
}
|
|
this.refreshState();
|
|
} catch (e) {}
|
|
};
|
|
} catch (e) {
|
|
console.warn('SSE could not connect:', e);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Start application safely
|
|
if (document.readyState === 'loading') {
|
|
window.addEventListener('DOMContentLoaded', () => {
|
|
window.app = new AppController();
|
|
});
|
|
} else {
|
|
window.app = new AppController();
|
|
}
|