<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Project — 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); }
    .tab.active { color: #c9a84c; border-bottom: 2px solid #c9a84c; }
    .tab { border-bottom: 2px solid transparent; }
    .req-row:hover { background: rgba(255,255,255,0.03); }
    .section-header { cursor: pointer; user-select: none; }
    .section-header:hover { background: rgba(255,255,255,0.02); }
    .priority-high { background: #ef444420; color: #f87171; }
    .priority-medium { background: #f59e0b20; color: #fbbf24; }
    .priority-low { background: #22c55e20; color: #4ade80; }
    .status-open { background: #3b82f620; color: #60a5fa; }
    .status-in_progress { background: #f59e0b20; color: #fbbf24; }
    .status-answered { background: #22c55e20; color: #4ade80; }
    .status-not_applicable { background: #6b728020; color: #9ca3af; }
  </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-[#94a3b8] hover:text-white transition">Projects</a>
      <span class="text-white/20">/</span>
      <span id="projectName" class="text-sm text-white font-medium">Loading...</span>
    </div>
    <div class="flex items-center gap-4">
      <span id="userName" class="text-sm text-[#94a3b8]"></span>
      <button onclick="logout()" class="text-sm text-[#94a3b8] 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-[#94a3b8] 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>
        <a href="/app/orgs" class="sidebar-link flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm font-medium text-[#94a3b8] 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="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4"/></svg>
          Organizations</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-[#94a3b8] 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-6xl">
      <div class="flex items-start justify-between mb-6">
        <div>
          <div class="flex items-center gap-3 mb-1">
            <h1 id="projectTitle" class="text-2xl font-bold text-white">Loading...</h1>
            <span id="projectStatus" class="px-2.5 py-0.5 rounded-full text-xs font-medium"></span>
          </div>
          <p id="projectDesc" class="text-[#94a3b8] text-sm"></p>
        </div>
        <div class="flex gap-2">
          <button id="importBtn" onclick="openImportModal()" class="px-4 py-2 bg-white/[0.05] hover:bg-white/[0.08] text-white rounded-lg text-sm transition 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="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-8l-4-4m0 0L8 8m4-4v12"/></svg>
            Import
          </button>
          <button id="newRequestBtn" class="px-4 py-2 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition">+ New Request</button>
        </div>
      </div>
      <div class="flex gap-6 border-b border-white/[0.08] mb-6">
        <button class="tab active pb-3 text-sm font-medium transition" onclick="switchTab('requests', this)">Requests</button>
        <button class="tab pb-3 text-sm font-medium text-[#94a3b8] transition" onclick="switchTab('orgs', this)">Organizations</button>
        <button class="tab pb-3 text-sm font-medium text-[#94a3b8] transition" onclick="switchTab('team', this)">Team</button>
      </div>
      <div id="tab-requests">
        <div id="requestList" class="space-y-4"><div class="text-[#94a3b8] text-sm">Loading requests...</div></div>
        <div id="requestEmpty" class="hidden text-center py-16">
          <div class="text-4xl mb-3">📋</div>
          <h3 class="text-lg font-semibold text-white mb-1">No requests yet</h3>
          <p class="text-[#94a3b8] text-sm mb-4">Import a diligence checklist or create requests manually.</p>
          <button onclick="openImportModal()" class="px-4 py-2 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition">Import Checklist</button>
        </div>
      </div>
      <div id="tab-orgs" class="hidden">
        <div class="flex justify-between items-center mb-4">
          <p class="text-[#94a3b8] text-sm">Organizations participating in this deal.</p>
          <button id="addOrgBtn" class="px-3 py-1.5 bg-white/[0.05] hover:bg-white/[0.08] text-white rounded-lg text-sm transition">+ Add Org</button>
        </div>
        <div id="orgList" class="space-y-3"><div class="text-[#94a3b8] text-sm">Loading...</div></div>
      </div>
      <div id="tab-team" class="hidden">
        <div class="flex justify-between items-center mb-4">
          <p class="text-[#94a3b8] text-sm">People with access to this deal.</p>
          <button id="inviteBtn" class="px-3 py-1.5 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition">+ Invite</button>
        </div>
        <div id="teamList" class="space-y-2"><div class="text-[#94a3b8] text-sm">Loading...</div></div>
      </div>
    </main>
  </div>
  <div id="importModal" class="hidden fixed inset-0 bg-black/60 flex items-center justify-center z-50">
    <div class="bg-[#0d1f3c] rounded-2xl p-6 w-full max-w-md border border-white/[0.08]">
      <h2 class="text-lg font-semibold text-white mb-4">Import Diligence Checklist</h2>
      <form id="importForm" enctype="multipart/form-data">
        <div class="mb-4">
          <label class="block text-sm text-[#94a3b8] mb-1.5">File (CSV or XLSX)</label>
          <input type="file" id="importFile" name="file" accept=".csv,.xlsx,.xls" required class="w-full px-3 py-2 bg-white/[0.05] border border-white/[0.08] rounded-lg text-white text-sm focus:outline-none focus:border-[#c9a84c]">
        </div>
        <div class="mb-4">
          <label class="block text-sm text-[#94a3b8] mb-1.5">Import Mode</label>
          <select id="importMode" name="mode" class="w-full px-3 py-2 bg-white/[0.05] border border-white/[0.08] rounded-lg text-white text-sm focus:outline-none focus:border-[#c9a84c]">
            <option value="add">Add to existing requests</option>
            <option value="replace">Replace all requests</option>
          </select>
        </div>
        <div class="mb-4">
          <label class="block text-sm text-[#94a3b8] mb-1.5">Section Filter (optional)</label>
          <input type="text" id="sectionFilter" name="section_filter" placeholder="e.g. Financial" class="w-full px-3 py-2 bg-white/[0.05] border border-white/[0.08] rounded-lg text-white text-sm focus:outline-none focus:border-[#c9a84c] placeholder:text-[#64748b]">
        </div>
        <div class="mb-6">
          <label class="flex items-center gap-2 cursor-pointer">
            <input type="checkbox" id="createWorkstreams" name="create_workstreams" class="rounded border-white/[0.2]">
            <span class="text-sm text-[#94a3b8]">Create workstreams from sections</span>
          </label>
        </div>
        <div class="flex gap-3">
          <button type="button" onclick="closeImportModal()" class="flex-1 px-4 py-2 bg-white/[0.05] hover:bg-white/[0.08] text-white rounded-lg text-sm transition">Cancel</button>
          <button type="submit" id="importSubmitBtn" class="flex-1 px-4 py-2 bg-[#c9a84c] hover:bg-[#b8973f] text-[#0a1628] font-semibold rounded-lg text-sm transition">Import</button>
        </div>
      </form>
    </div>
  </div>
  <script>
    const token = localStorage.getItem('ds_token');
    if (!token) window.location.href = '/app/login';
    const user = JSON.parse(localStorage.getItem('ds_user') || '{}');
    const projectID = 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 instanceof FormData)) 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 = { active: 'bg-green-500/20 text-green-300', draft: 'bg-gray-500/20 text-gray-300', closed: 'bg-red-500/20 text-red-300' };
    const roleColors = { seller: 'bg-blue-500/20 text-blue-300', buyer: 'bg-green-500/20 text-green-300', ib: 'bg-[#c9a84c]/20 text-[#c9a84c]', advisor: 'bg-purple-500/20 text-purple-300' };
    const expandedSections = new Set();
    async function loadProject() {
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/detail');
        if (!res.ok) { document.getElementById('projectTitle').textContent = 'Not found'; return; }
        const p = await res.json();
        const proj = p.project;
        const d = parseData(proj.data_text);
        const name = d.name || proj.summary || 'Untitled';
        document.title = name + ' — Dealspace';
        document.getElementById('projectName').textContent = name;
        document.getElementById('projectTitle').textContent = name;
        document.getElementById('projectDesc').textContent = d.description || '';
        const status = d.status || 'active';
        const sc = statusColors[status] || 'bg-gray-500/20 text-gray-300';
        document.getElementById('projectStatus').className = 'px-2.5 py-0.5 rounded-full text-xs font-medium capitalize ' + sc;
        document.getElementById('projectStatus').textContent = status;
      } catch(e) { console.error(e); }
    }
    async function loadRequests() {
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/requests');
        const items = await res.json();
        const list = document.getElementById('requestList');
        if (!items || items.length === 0) { list.classList.add('hidden'); document.getElementById('requestEmpty').classList.remove('hidden'); return; }
        list.classList.remove('hidden'); document.getElementById('requestEmpty').classList.add('hidden');
        const sections = {};
        for (const r of items) { const sec = r.section || 'Unsorted'; if (!sections[sec]) sections[sec] = []; sections[sec].push(r); }
        const sortedSections = Object.keys(sections).sort();
        list.innerHTML = sortedSections.map(sec => {
          const reqs = sections[sec]; const isExpanded = expandedSections.has(sec);
          const statusCounts = { open: 0, in_progress: 0, answered: 0, not_applicable: 0 };
          reqs.forEach(r => { statusCounts[r.status || 'open']++; });
          return '<div class="border border-white/[0.08] rounded-xl overflow-hidden"><div class="section-header flex items-center justify-between px-5 py-3 bg-[#0d1f3c]" onclick="toggleSection(\'' + escHtml(sec) + '\')"><div class="flex items-center gap-3"><svg class="w-4 h-4 text-[#94a3b8] transition ' + (isExpanded ? 'rotate-90' : '') + '" id="chevron-' + escHtml(sec) + '" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg><span class="text-white font-medium">' + escHtml(sec) + '</span><span class="text-[#94a3b8] text-xs">(' + reqs.length + ' items)</span></div><div class="flex gap-2">' + (statusCounts.open > 0 ? '<span class="px-2 py-0.5 rounded text-xs status-open">' + statusCounts.open + ' open</span>' : '') + (statusCounts.answered > 0 ? '<span class="px-2 py-0.5 rounded text-xs status-answered">' + statusCounts.answered + ' answered</span>' : '') + '</div></div><div id="section-' + escHtml(sec) + '" class="' + (isExpanded ? '' : 'hidden') + '">' + reqs.map(r => '<a href="/app/requests/' + r.entry_id + '" class="req-row flex items-center gap-4 px-5 py-3 border-t border-white/[0.05] transition cursor-pointer"><div class="w-16 shrink-0"><span class="text-xs font-mono text-[#94a3b8]">' + escHtml(r.item_number || '—') + '</span></div><div class="flex-1 min-w-0"><span class="text-white text-sm truncate block">' + escHtml(r.title || 'Untitled') + '</span></div><span class="shrink-0 w-2 h-2 rounded-full ' + (r.priority === 'high' ? 'bg-red-400' : r.priority === 'low' ? 'bg-green-400' : 'bg-yellow-400') + '" title="' + r.priority + ' priority"></span><span class="shrink-0 px-2.5 py-0.5 rounded text-xs font-medium status-' + (r.status || 'open') + '">' + (r.status || 'open').replace('_', ' ') + '</span></a>').join('') + '</div></div>';
        }).join('');
      } catch(e) { console.error(e); document.getElementById('requestList').innerHTML = '<div class="text-red-400 text-sm">Failed to load requests.</div>'; }
    }
    function toggleSection(sec) {
      const el = document.getElementById('section-' + sec); const chevron = document.getElementById('chevron-' + sec);
      if (el.classList.contains('hidden')) { el.classList.remove('hidden'); chevron.classList.add('rotate-90'); expandedSections.add(sec); }
      else { el.classList.add('hidden'); chevron.classList.remove('rotate-90'); expandedSections.delete(sec); }
    }
    async function loadOrgs() {
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/orgs');
        const orgs = await res.json();
        const list = document.getElementById('orgList');
        if (!orgs || orgs.length === 0) { list.innerHTML = '<div class="text-[#94a3b8] text-sm">No organizations added yet.</div>'; return; }
        list.innerHTML = orgs.map(o => { const rc = roleColors[o.role] || 'bg-gray-500/20 text-gray-300'; const domains = o.org_domains || [];
          return '<div class="flex items-center gap-4 px-5 py-4 rounded-xl bg-[#0d1f3c] border border-white/[0.08]"><div class="flex-1"><div class="flex items-center gap-2 mb-1"><span class="text-white font-medium">' + escHtml(o.org_name || 'Unknown') + '</span><span class="px-2 py-0.5 rounded-full text-xs font-medium capitalize ' + rc + '">' + (o.role || '?') + '</span>' + (o.domain_lock ? '<span class="px-2 py-0.5 rounded-full text-xs bg-white/[0.05] text-[#94a3b8]">🔒 domain locked</span>' : '') + '</div>' + (domains.length > 0 ? '<div class="flex gap-1.5 flex-wrap">' + domains.map(dm => '<span class="text-xs text-[#94a3b8] font-mono bg-white/[0.05] px-2 py-0.5 rounded">@' + dm + '</span>').join('') + '</div>' : '') + '</div></div>';
        }).join('');
      } catch(e) { console.error(e); }
    }
    async function loadTeam() {
      try {
        const res = await fetchAPI('/api/projects/' + projectID + '/members');
        const members = await res.json();
        const list = document.getElementById('teamList');
        if (!members || members.length === 0) { list.innerHTML = '<div class="text-[#94a3b8] text-sm">No team members yet.</div>'; return; }
        list.innerHTML = members.map(m => '<div class="flex items-center gap-4 px-5 py-3 rounded-xl bg-[#0d1f3c] border border-white/[0.08]"><div class="w-8 h-8 rounded-full bg-[#c9a84c]/20 flex items-center justify-center text-[#c9a84c] font-semibold text-sm">' + (m.name||m.email||'?')[0].toUpperCase() + '</div><div class="flex-1"><div class="text-white text-sm font-medium">' + escHtml(m.name || m.email) + '</div>' + (m.name ? '<div class="text-[#94a3b8] text-xs">' + escHtml(m.email) + '</div>' : '') + '</div><span class="text-xs text-[#94a3b8] capitalize">' + (m.role || 'member') + '</span></div>').join('');
      } catch(e) { console.error(e); }
    }
    function switchTab(name, el) {
      document.querySelectorAll('.tab').forEach(t => { t.classList.remove('active','text-white'); t.classList.add('text-[#94a3b8]'); });
      el.classList.add('active','text-white'); el.classList.remove('text-[#94a3b8]');
      document.getElementById('tab-requests').classList.toggle('hidden', name !== 'requests');
      document.getElementById('tab-orgs').classList.toggle('hidden', name !== 'orgs');
      document.getElementById('tab-team').classList.toggle('hidden', name !== 'team');
      if (name === 'orgs') loadOrgs(); if (name === 'team') loadTeam();
    }
    function openImportModal() { document.getElementById('importModal').classList.remove('hidden'); }
    function closeImportModal() { document.getElementById('importModal').classList.add('hidden'); document.getElementById('importForm').reset(); }
    document.getElementById('importForm').onsubmit = async (e) => {
      e.preventDefault();
      const btn = document.getElementById('importSubmitBtn'); btn.disabled = true; btn.textContent = 'Importing...';
      const formData = new FormData();
      formData.append('file', document.getElementById('importFile').files[0]);
      formData.append('mode', document.getElementById('importMode').value);
      formData.append('section_filter', document.getElementById('sectionFilter').value);
      formData.append('create_workstreams', document.getElementById('createWorkstreams').checked ? 'true' : 'false');
      try {
        const res = await fetch('/api/projects/' + projectID + '/requests/import', { method: 'POST', headers: { 'Authorization': 'Bearer ' + token }, body: formData });
        const data = await res.json();
        if (res.ok) { closeImportModal(); loadRequests(); alert('Imported ' + data.imported + ' requests from ' + (data.sections?.length || 0) + ' sections. ' + (data.skipped || 0) + ' skipped.'); }
        else { alert('Import failed: ' + (data.error || 'Unknown error')); }
      } catch (err) { alert('Import failed: ' + err.message); }
      finally { btn.disabled = false; btn.textContent = 'Import'; }
    };
    document.getElementById('newRequestBtn').onclick = () => {
      const title = prompt('Request title:'); if (!title) return;
      fetchAPI('/api/projects/' + projectID + '/entries', { method: 'POST', body: JSON.stringify({ project_id: projectID, parent_id: projectID, type: 'request', depth: 1, summary: title, data: JSON.stringify({ title: title, status: 'open', priority: 'medium' }), stage: 'pre_dataroom' })
      }).then(r => r.json()).then(d => { if (d.entry_id) window.location.href = '/app/requests/' + d.entry_id; else loadRequests(); });
    };
    document.getElementById('importModal').onclick = (e) => { if (e.target.id === 'importModal') closeImportModal(); };
    loadProject(); loadRequests();
  </script>
</body>
</html>
