<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Request — Dealspace</title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <script src="https://cdn.tailwindcss.com"></script>
  <style>* { font-family: 'Inter', sans-serif; } body { background: #0a1628; }
    .sidebar-link.active { background: rgba(201,168,76,0.1); color: #c9a84c; border-left: 3px solid #c9a84c; }
    .sidebar-link:hover:not(.active) { background: rgba(255,255,255,0.04); }
  </style>
</head>
<body>
  <header class="bg-[#0d1f3c] border-b border-white/[0.08] px-6 py-3 flex items-center justify-between sticky top-0 z-50">
    <div class="flex items-center gap-3">
      <a href="/app/tasks" 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-[#b0bec5] hover:text-white transition">Projects</a>
      <span class="text-white/20">/</span>
      <a id="backToProject" href="/app/projects" class="text-sm text-[#b0bec5] 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>
    <div class="flex items-center gap-4">
      <span id="userName" class="text-sm text-[#b0bec5]"></span>
      <button onclick="logout()" class="text-sm text-[#b0bec5] hover:text-white transition">Logout</button>
    </div>
  </header>
  <div class="flex">
    <nav class="w-56 bg-[#0d1f3c] border-r border-white/[0.08] min-h-[calc(100vh-49px)] sticky top-[49px] shrink-0">
      <div class="p-3 space-y-0.5">
        <a href="/app/tasks" class="sidebar-link flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-[#b0bec5] transition">
          <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="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"/></svg>
          My Tasks</a>
        <a href="/app/projects" class="sidebar-link active flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-white transition">
          <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="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"/></svg>
          Projects</a>
        <div id="adminLinks" class="hidden"><div class="border-t border-white/[0.08] my-3"></div>
          <a href="/admin" class="sidebar-link flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-[#b0bec5] transition">
            <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="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>
            Admin</a></div>
      </div>
    </nav>
    <main class="flex-1 p-8 max-w-4xl">
      <!-- Request Header -->
      <div id="reqHeader" class="mb-8">
        <div class="flex items-start gap-4 mb-3">
          <div class="flex-1">
            <h1 id="reqTitle" class="text-2xl font-bold text-white mb-2">Loading...</h1>
            <p id="reqDesc" class="text-[#b0bec5] text-sm"></p>
          </div>
          <span id="reqStatus" class="shrink-0 px-3 py-1 rounded-full text-sm font-medium"></span>
        </div>
        <div class="flex gap-3 flex-wrap text-xs text-[#8899a6]">
          <span id="reqDue"></span>
          <span id="reqAssignee"></span>
        </div>
      </div>

      <!-- Answer / Upload -->
      <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6 mb-6">
        <h2 class="text-sm font-semibold text-[#b0bec5] uppercase tracking-wider mb-4">Response</h2>
        <div id="answers" class="space-y-4 mb-6"></div>
        <div id="uploadArea" class="border-2 border-dashed border-white/[0.08] rounded-xl p-8 text-center hover:border-[#c9a84c]/40 transition cursor-pointer" onclick="document.getElementById('fileInput').click()">
          <div class="text-3xl mb-2">📎</div>
          <p class="text-[#b0bec5] text-sm">Drop files here or click to upload</p>
          <p class="text-[#8899a6] 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-3 text-sm text-[#b0bec5]"></div>
      </div>

      <!-- Activity / Comments -->
      <div class="bg-[#0d1f3c] border border-white/[0.08] rounded-xl p-6">
        <h2 class="text-sm font-semibold text-[#b0bec5] uppercase tracking-wider mb-4">Comments</h2>
        <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-[#8899a6] 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>
    </main>
  </div>

  <script>
    const token = localStorage.getItem('ds_token');
    if (!token) window.location.href = '/app/login';
    const user = JSON.parse(localStorage.getItem('ds_user') || '{}');
    const reqID = location.pathname.split('/').pop();
    document.getElementById('userName').textContent = user.name || user.email || '';
    if (user.is_super_admin) document.getElementById('adminLinks').classList.remove('hidden');

    function fetchAPI(path, opts = {}) {
      opts.headers = { ...opts.headers, 'Authorization': 'Bearer ' + token };
      if (opts.body && typeof opts.body === 'string') opts.headers['Content-Type'] = 'application/json';
      return fetch(path, opts).then(r => { if (r.status === 401) { localStorage.removeItem('ds_token'); window.location.href = '/app/login'; } return r; });
    }
    function logout() { fetchAPI('/api/auth/logout', { method: 'POST' }).finally(() => { localStorage.clear(); window.location.href = '/app/login'; }); }
    function parseData(t) { try { return JSON.parse(t); } catch { return {}; } }
    function escHtml(s) { const d = document.createElement('div'); d.textContent = s; return d.innerHTML; }

    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' };

    async function loadRequest() {
      try {
        const res = await fetchAPI('/api/entries/' + reqID);
        if (!res.ok) { document.getElementById('reqTitle').textContent = 'Not found'; return; }
        const req = await res.json();
        const d = parseData(req.data_text);
        const title = d.title || req.summary || 'Untitled';
        document.title = title + ' — Dealspace';
        document.getElementById('reqRef').textContent = d.ref || 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 status = d.status || 'open';
        const sc = statusColors[status] || 'bg-gray-500/20 text-gray-300';
        document.getElementById('reqStatus').className = 'shrink-0 px-3 py-1 rounded-full text-sm font-medium capitalize ' + sc;
        document.getElementById('reqStatus').textContent = status;
        if (d.due_date) document.getElementById('reqDue').textContent = '📅 Due: ' + d.due_date;
        if (d.assignee) document.getElementById('reqAssignee').textContent = '👤 ' + d.assignee;
      } catch(e) {}
    }

    async function loadAnswers() {
      try {
        const res = await fetchAPI('/api/entries/' + reqID + '/children?type=answer');
        const items = await res.json();
        const el = document.getElementById('answers');
        if (!items || items.length === 0) { el.innerHTML = '<p class="text-[#8899a6] text-sm">No documents uploaded yet.</p>'; return; }
        el.innerHTML = items.map(a => {
          const d = parseData(a.data_text);
          const name = d.filename || d.name || a.summary || 'Document';
          return `<div class="flex items-center gap-3 px-4 py-3 rounded-lg bg-[#0a1628] border border-white/[0.05]">
            <span class="text-2xl">${name.endsWith('.pdf') ? '📄' : name.match(/\.(jpg|jpeg|png|gif)$/i) ? '🖼️' : '📎'}</span>
            <div class="flex-1 min-w-0">
              <div class="text-white text-sm font-medium truncate">${escHtml(name)}</div>
              <div class="text-[#8899a6] text-xs">${new Date(a.created_at).toLocaleString()}</div>
            </div>
            <a href="/api/entries/${a.entry_id}/download" class="text-[#c9a84c] text-sm hover:underline">Download</a>
          </div>`;
        }).join('');
      } catch(e) {}
    }

    async function uploadFiles(files) {
      const status = document.getElementById('uploadStatus');
      for (const file of files) {
        status.textContent = 'Uploading ' + file.name + '...';
        const fd = new FormData();
        fd.append('file', file);
        fd.append('parent_id', reqID);
        try {
          const res = await fetchAPI('/api/upload', { method: 'POST', body: fd });
          if (res.ok) { status.textContent = file.name + ' uploaded.'; loadAnswers(); }
          else { status.textContent = 'Upload failed for ' + file.name; }
        } catch(e) { status.textContent = 'Error: ' + e.message; }
      }
      setTimeout(() => status.textContent = '', 3000);
    }

    async function loadComments() {
      try {
        const res = await fetchAPI('/api/entries/' + reqID + '/children?type=comment');
        const items = await res.json();
        const el = document.getElementById('comments');
        if (!items || items.length === 0) { el.innerHTML = '<p class="text-[#8899a6] text-sm">No comments yet.</p>'; return; }
        el.innerHTML = items.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-[#8899a6] text-xs">${new Date(c.created_at).toLocaleString()}</span>
              </div>
              <p class="text-[#b0bec5] text-sm">${escHtml(d.text||'')}</p>
            </div>
          </div>`;
        }).join('');
      } catch(e) {}
    }

    async function postComment() {
      const text = document.getElementById('commentText').value.trim();
      if (!text) return;
      try {
        const res = await fetchAPI('/api/entries/' + reqID + '/children', { method: 'POST', body: JSON.stringify({ type: 'comment', data: { text, author: user.name || user.email } }) });
        if (res.ok) { document.getElementById('commentText').value = ''; loadComments(); }
      } 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); });

    loadRequest();
    loadAnswers();
    loadComments();
  </script>
</body>
</html>
