dealspace/portal/templates/app/request.html

401 lines
21 KiB
HTML

{{define "header-left"}}
<div class="flex items-center gap-3">
<a href="/app/projects" class="text-xl font-bold text-white tracking-tight"><span class="text-[#c9a84c]">Deal</span>space</a>
<span class="text-white/20">/</span>
<a href="/app/projects" class="text-sm text-[#94a3b8] hover:text-white transition">Projects</a>
<span class="text-white/20">/</span>
<a id="backToProject" href="/app/projects" class="text-sm text-[#94a3b8] hover:text-white transition">Project</a>
<span class="text-white/20">/</span>
<span id="reqRef" class="text-sm text-white font-medium">Request</span>
</div>
{{end}}
{{define "content"}}
<div class="p-8 max-w-6xl">
<!-- Two-column layout: Request + Response -->
<div class="grid grid-cols-1 lg:grid-cols-12 gap-6 mb-6">
<!-- LEFT: Request -->
<div class="lg:col-span-7 bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6">
<div class="flex items-center gap-2 mb-5">
<span class="text-lg">&#128203;</span>
<h2 class="text-sm font-semibold text-[#94a3b8] uppercase tracking-wider">Request</h2>
</div>
<div class="flex items-start gap-3 mb-4">
<h1 id="reqTitle" class="text-xl font-bold text-white leading-tight flex-1">Loading...</h1>
<button id="editReqBtn" onclick="startEditRequest()" class="shrink-0 p-1.5 rounded-lg hover:bg-white/[0.08] text-[#94a3b8] hover:text-white transition" title="Edit request">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"/></svg>
</button>
<span id="reqItemBadge" class="hidden shrink-0 px-2.5 py-1 rounded-md bg-[#c9a84c]/15 text-[#c9a84c] text-xs font-semibold"></span>
</div>
<p id="reqDesc" class="text-[#94a3b8] text-sm leading-relaxed mb-5"></p>
<!-- Edit mode form -->
<div id="reqEditMode" class="hidden mb-5">
<div class="space-y-3">
<div>
<label class="block text-xs font-medium text-[#94a3b8] mb-1">Title</label>
<input type="text" id="editReqTitle" class="w-full px-3 py-2 bg-[#0a1628] border border-white/[0.08] rounded-lg text-white text-sm focus:outline-none focus:border-[#c9a84c]">
</div>
<div>
<label class="block text-xs font-medium text-[#94a3b8] mb-1">Description</label>
<textarea id="editReqDesc" rows="3" class="w-full px-3 py-2 bg-[#0a1628] border border-white/[0.08] rounded-lg text-white text-sm focus:outline-none focus:border-[#c9a84c] resize-none"></textarea>
</div>
<div class="flex gap-3">
<div class="flex-1">
<label class="block text-xs font-medium text-[#94a3b8] mb-1">Priority</label>
<select id="editReqPriority" class="w-full px-3 py-2 bg-[#0a1628] border border-white/[0.08] rounded-lg text-white text-sm focus:outline-none focus:border-[#c9a84c]">
<option value="critical">Critical</option>
<option value="high">High</option>
<option value="medium">Medium</option>
<option value="low">Low</option>
</select>
</div>
<div class="flex-1">
<label class="block text-xs font-medium text-[#94a3b8] mb-1">Status</label>
<select id="editReqStatus" class="w-full px-3 py-2 bg-[#0a1628] border border-white/[0.08] rounded-lg text-white text-sm focus:outline-none focus:border-[#c9a84c]">
<option value="open">Open</option>
<option value="in_process">In Process</option>
<option value="partial">Partial</option>
<option value="complete">Complete</option>
</select>
</div>
</div>
<div class="flex gap-2">
<button onclick="saveEditRequestDetail()" class="px-4 py-2 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition">Save</button>
<button onclick="cancelEditRequestDetail()" class="px-4 py-2 bg-white/[0.05] hover:bg-white/[0.08] text-white rounded-lg text-sm transition">Cancel</button>
</div>
</div>
</div>
<div id="reqMetaBar" class="border-t border-white/[0.06] pt-4 flex flex-wrap items-center gap-3">
<span id="reqPriority" class="hidden px-2.5 py-1 rounded-full text-xs font-medium"></span>
<span id="reqDue" class="hidden text-xs text-[#475569]"></span>
<span id="reqStatus" class="px-2.5 py-1 rounded-full text-xs font-medium capitalize"></span>
</div>
</div>
<!-- RIGHT: Response -->
<div class="lg:col-span-5 bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6 flex flex-col">
<div class="flex items-center gap-2 mb-5">
<span class="text-lg">&#128193;</span>
<h2 class="text-sm font-semibold text-[#94a3b8] uppercase tracking-wider">Response</h2>
</div>
<div id="answeredBanner" class="hidden mb-4 px-4 py-2.5 rounded-lg bg-green-500/10 border border-green-500/20 text-green-300 text-sm font-medium flex items-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
Answered
</div>
<div id="answers" class="space-y-2 mb-4 flex-1"></div>
<div id="uploadArea" class="border-2 border-dashed border-white/[0.08] rounded-xl p-6 text-center hover:border-[#c9a84c]/40 transition cursor-pointer" onclick="document.getElementById('fileInput').click()">
<div class="text-2xl mb-1">&#128206;</div>
<p class="text-[#94a3b8] text-sm">Drop files to submit response</p>
<p class="text-[#475569] text-xs mt-1">PDF, DOCX, XLSX, images</p>
<input id="fileInput" type="file" multiple class="hidden" onchange="uploadFiles(this.files)">
</div>
<div id="uploadStatus" class="mt-2 text-sm text-[#94a3b8]"></div>
<button id="markAnsweredBtn" onclick="markAnswered()" class="hidden mt-4 w-full px-4 py-2.5 bg-green-600 hover:bg-green-700 text-white font-semibold rounded-lg text-sm transition flex items-center justify-center gap-2">
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>
Mark as Answered
</button>
</div>
</div>
<!-- BOTTOM: Discussion (full width) -->
<div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6">
<div class="flex items-center gap-2 mb-5">
<span class="text-lg">&#128172;</span>
<h2 class="text-sm font-semibold text-[#94a3b8] uppercase tracking-wider">Discussion</h2>
</div>
<div id="comments" class="space-y-3 mb-4"></div>
<div class="flex gap-3">
<textarea id="commentText" rows="2" placeholder="Add a comment..." class="flex-1 px-4 py-2.5 bg-[#0a1628] border border-white/[0.08] rounded-lg text-white placeholder-[#475569] focus:outline-none focus:border-[#c9a84c] focus:ring-1 focus:ring-[#c9a84c] text-sm resize-none"></textarea>
<button onclick="postComment()" class="px-4 py-2 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition self-end">Post</button>
</div>
</div>
</div>
{{end}}
{{define "scripts"}}
<script>
const reqID = location.pathname.split('/').pop();
const statusColors = { open: 'bg-yellow-500/20 text-yellow-300', answered: 'bg-green-500/20 text-green-300', closed: 'bg-gray-500/20 text-gray-300', 'under-review': 'bg-blue-500/20 text-blue-300' };
const priorityColors = { high: 'bg-red-500/20 text-red-300', medium: 'bg-yellow-500/20 text-yellow-300', low: 'bg-blue-500/20 text-blue-300' };
let currentRequest = null;
let currentData = null;
let currentAnswers = [];
async function loadAll() {
try {
const res = await fetchAPI('/api/requests/' + reqID);
if (!res.ok) { document.getElementById('reqTitle').textContent = 'Not found'; return; }
const json = await res.json();
currentRequest = json.request;
currentData = parseData(currentRequest.data_text);
const children = json.children || [];
renderRequest(currentRequest, currentData);
currentAnswers = children.filter(c => c.type === 'answer' || c.type === 'document');
renderAnswers(currentAnswers);
renderComments(children.filter(c => c.type === 'comment'));
} catch(e) {
document.getElementById('reqTitle').textContent = 'Error loading request';
}
}
function renderRequest(req, d) {
const title = d.title || req.summary || 'Untitled';
document.title = title + ' \u2014 Dealspace';
document.getElementById('reqRef').textContent = d.item_number || title;
document.getElementById('reqTitle').textContent = title;
document.getElementById('reqDesc').textContent = d.description || '';
if (req.project_id) document.getElementById('backToProject').href = '/app/projects/' + req.project_id;
const badge = document.getElementById('reqItemBadge');
const badgeParts = [d.section, d.item_number].filter(Boolean);
if (badgeParts.length) { badge.textContent = badgeParts.join(' \u00b7 '); badge.classList.remove('hidden'); }
const prioEl = document.getElementById('reqPriority');
if (d.priority) {
const pc = priorityColors[d.priority] || 'bg-gray-500/20 text-gray-300';
prioEl.className = 'px-2.5 py-1 rounded-full text-xs font-medium capitalize ' + pc;
prioEl.textContent = d.priority + ' priority';
prioEl.classList.remove('hidden');
}
if (d.due_date) {
const dueEl = document.getElementById('reqDue');
dueEl.textContent = '\ud83d\udcc5 Due: ' + d.due_date;
dueEl.classList.remove('hidden');
}
const status = d.status || 'open';
const sc = statusColors[status] || 'bg-gray-500/20 text-gray-300';
const statusEl = document.getElementById('reqStatus');
statusEl.className = 'px-2.5 py-1 rounded-full text-xs font-medium capitalize ' + sc;
statusEl.textContent = status;
if (status === 'answered') {
document.getElementById('answeredBanner').classList.remove('hidden');
document.getElementById('markAnsweredBtn').classList.add('hidden');
} else {
document.getElementById('answeredBanner').classList.add('hidden');
document.getElementById('markAnsweredBtn').classList.remove('hidden');
}
}
// ---- Edit request detail ----
function startEditRequest() {
if (!currentData) return;
document.getElementById('editReqTitle').value = currentData.title || '';
document.getElementById('editReqDesc').value = currentData.description || '';
document.getElementById('editReqPriority').value = currentData.priority || 'medium';
document.getElementById('editReqStatus').value = currentData.status || 'open';
document.getElementById('reqTitle').classList.add('hidden');
document.getElementById('reqDesc').classList.add('hidden');
document.getElementById('editReqBtn').classList.add('hidden');
document.getElementById('reqMetaBar').classList.add('hidden');
document.getElementById('reqEditMode').classList.remove('hidden');
}
function cancelEditRequestDetail() {
document.getElementById('reqTitle').classList.remove('hidden');
document.getElementById('reqDesc').classList.remove('hidden');
document.getElementById('editReqBtn').classList.remove('hidden');
document.getElementById('reqMetaBar').classList.remove('hidden');
document.getElementById('reqEditMode').classList.add('hidden');
}
async function saveEditRequestDetail() {
if (!currentRequest || !currentData) return;
const projectID = currentRequest.project_id;
const updatedData = {
...currentData,
title: document.getElementById('editReqTitle').value.trim(),
description: document.getElementById('editReqDesc').value.trim(),
priority: document.getElementById('editReqPriority').value,
status: document.getElementById('editReqStatus').value
};
try {
const res = await fetchAPI('/api/projects/' + projectID + '/entries/' + reqID, {
method: 'PUT',
body: JSON.stringify({ data: JSON.stringify(updatedData), version: currentRequest.version })
});
if (res.ok) {
cancelEditRequestDetail();
loadAll();
}
} catch(e) {
console.error('Save failed:', e);
}
}
// ---- Answers / Files ----
function renderAnswers(docs) {
const el = document.getElementById('answers');
if (!docs || docs.length === 0) {
el.innerHTML = '<p class="text-[#475569] text-sm">No response submitted yet.</p>';
return;
}
const projectID = currentRequest.project_id;
el.innerHTML = docs.map(a => {
const d = parseData(a.data_text);
const name = d.filename || d.name || a.summary || 'Document';
const icon = name.endsWith('.pdf') ? '\ud83d\udcc4' : name.match(/\.(jpg|jpeg|png|gif)$/i) ? '\ud83d\uddbc\ufe0f' : name.match(/\.(xlsx?|csv)$/i) ? '\ud83d\udcca' : '\ud83d\udcce';
const objectID = d.object_id || '';
const downloadUrl = objectID ? '/api/projects/' + projectID + '/objects/' + objectID + '?filename=' + encodeURIComponent(name) : '#';
const previewUrl = objectID ? '/api/projects/' + projectID + '/objects/' + objectID + '/preview?filename=' + encodeURIComponent(name) : '';
const isVideo = /\.(mp4|mov|avi|mkv|webm)$/i.test(name);
return `<div class="flex items-center gap-3 px-3 py-2.5 rounded-lg bg-[#0a1628] border border-white/[0.05]">
<span class="text-xl">${icon}</span>
<div class="flex-1 min-w-0">
<div class="flex items-center gap-1">
<span class="text-white text-sm font-medium truncate" id="fname-${a.entry_id}">${escHtml(name)}</span>
<button onclick="startRenameFile('${a.entry_id}')" class="shrink-0 p-0.5 rounded hover:bg-white/[0.08] text-[#94a3b8] hover:text-white transition" title="Rename">
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"/></svg>
</button>
</div>
<div class="text-[#475569] text-xs">${a.created_at ? new Date(a.created_at * 1000).toLocaleString() : ''}</div>
</div>
<div class="flex items-center gap-2 shrink-0">
${objectID && previewUrl ? `<a href="${previewUrl}" target="_blank" class="px-2 py-1 rounded text-xs font-medium bg-white/[0.06] text-[#94a3b8] hover:text-white hover:bg-white/[0.1] transition">${isVideo ? 'Play' : 'Preview'}</a>` : ''}
${objectID ? `<a href="${downloadUrl}" class="text-[#c9a84c] text-sm hover:underline">Download</a>` : ''}
</div>
</div>`;
}).join('');
}
// ---- Rename file ----
function startRenameFile(answerId) {
const span = document.getElementById('fname-' + answerId);
if (!span) return;
const currentName = span.textContent;
const container = span.parentElement;
container.innerHTML = `<input type="text" value="" id="rename-input-${answerId}"
style="flex:1;padding:2px 6px;background:#0a1628;border:1px solid rgba(255,255,255,.12);border-radius:4px;color:white;font-size:13px;outline:none"
onkeydown="handleRenameKey(event,'${answerId}')" onblur="saveRenameFile('${answerId}')">`;
const input = document.getElementById('rename-input-' + answerId);
if (input) { input.value = currentName; input.focus(); input.select(); }
}
function handleRenameKey(e, answerId) {
if (e.key === 'Enter') { e.preventDefault(); e.target.blur(); }
if (e.key === 'Escape') { e.target.dataset.cancelled = 'true'; e.target.blur(); }
}
async function saveRenameFile(answerId) {
const input = document.getElementById('rename-input-' + answerId);
if (!input || input.dataset.cancelled === 'true') { loadAll(); return; }
const newName = input.value.trim();
if (!newName) { loadAll(); return; }
const answer = currentAnswers.find(a => a.entry_id === answerId);
if (!answer) { loadAll(); return; }
const projectID = currentRequest.project_id;
const d = parseData(answer.data_text);
d.filename = newName;
d.name = newName;
try {
await fetchAPI('/api/projects/' + projectID + '/entries/' + answerId, {
method: 'PUT',
body: JSON.stringify({ data: JSON.stringify(d), version: answer.version || 0 })
});
} catch(e) {
console.error('Rename failed:', e);
}
loadAll();
}
function renderComments(comments) {
const el = document.getElementById('comments');
if (!comments || comments.length === 0) {
el.innerHTML = '<p class="text-[#475569] text-sm">No comments yet.</p>';
return;
}
el.innerHTML = comments.map(c => {
const d = parseData(c.data_text);
return `<div class="flex gap-3">
<div class="w-7 h-7 rounded-full bg-[#c9a84c]/20 flex items-center justify-center text-[#c9a84c] text-xs font-semibold shrink-0 mt-0.5">${(d.author||'?')[0].toUpperCase()}</div>
<div>
<div class="flex items-baseline gap-2 mb-1">
<span class="text-white text-sm font-medium">${escHtml(d.author||'Unknown')}</span>
<span class="text-[#475569] text-xs">${c.created_at ? new Date(c.created_at * 1000).toLocaleString() : ''}</span>
</div>
<p class="text-[#94a3b8] text-sm">${escHtml(d.text||'')}</p>
</div>
</div>`;
}).join('');
}
async function uploadFiles(files) {
if (!currentRequest) return;
const projectID = currentRequest.project_id;
const status = document.getElementById('uploadStatus');
for (const file of files) {
status.textContent = 'Uploading ' + file.name + '...';
const fd = new FormData();
fd.append('file', file);
try {
const uploadRes = await fetchAPI('/api/projects/' + projectID + '/objects', { method: 'POST', body: fd });
if (!uploadRes.ok) { status.textContent = 'Upload failed for ' + file.name; continue; }
const uploadData = await uploadRes.json();
const entryBody = {
project_id: projectID,
parent_id: reqID,
type: 'document',
data: JSON.stringify({ filename: file.name, name: file.name, content_type: file.type, object_id: uploadData.object_id })
};
const entryRes = await fetchAPI('/api/projects/' + projectID + '/entries', { method: 'POST', body: JSON.stringify(entryBody) });
if (entryRes.ok) { status.textContent = file.name + ' uploaded.'; }
else { status.textContent = 'Failed to save ' + file.name; }
} catch(e) { status.textContent = 'Error: ' + e.message; }
}
setTimeout(() => status.textContent = '', 3000);
loadAll();
}
async function postComment() {
if (!currentRequest) return;
const text = document.getElementById('commentText').value.trim();
if (!text) return;
const projectID = currentRequest.project_id;
const body = {
project_id: projectID,
parent_id: reqID,
type: 'comment',
data: JSON.stringify({ text, author: user.name || user.email })
};
try {
const res = await fetchAPI('/api/projects/' + projectID + '/entries', { method: 'POST', body: JSON.stringify(body) });
if (res.ok) { document.getElementById('commentText').value = ''; loadAll(); }
} catch(e) {}
}
async function markAnswered() {
if (!currentRequest || !currentData) return;
const projectID = currentRequest.project_id;
const updatedData = { ...currentData, status: 'answered' };
const body = {
data: JSON.stringify(updatedData),
version: currentRequest.version
};
try {
const res = await fetchAPI('/api/projects/' + projectID + '/entries/' + reqID, { method: 'PUT', body: JSON.stringify(body) });
if (res.ok) loadAll();
} catch(e) {}
}
// Drag and drop
const ua = document.getElementById('uploadArea');
ua.addEventListener('dragover', e => { e.preventDefault(); ua.classList.add('border-[#c9a84c]/60'); });
ua.addEventListener('dragleave', () => ua.classList.remove('border-[#c9a84c]/60'));
ua.addEventListener('drop', e => { e.preventDefault(); ua.classList.remove('border-[#c9a84c]/60'); uploadFiles(e.dataTransfer.files); });
loadAll();
</script>
{{end}}