  // ── Link Pull Request ──────────────────────────────────────────
  // Mirrors the old dashboard's PR-tab dialog: POST /api/pull-requests/link
  // with { url, title, project, context, autoObserve }. Opened from the action
  // footer button, the Active PRs tile chip, and the PR detail modal header.
  function openLinkPrModal() {
    var modal = document.getElementById('slim-linkpr-modal');
    if (!modal) return;
    var projects = (lastStatusData && Array.isArray(lastStatusData.projects) ? lastStatusData.projects : [])
      .map(function(p) { return p && p.name ? String(p.name) : null; })
      .filter(Boolean);
    var sel = document.getElementById('slim-linkpr-project');
    sel.textContent = '';
    var auto = document.createElement('option');
    auto.value = '';
    auto.textContent = 'Auto-detect from URL (central if no unique match)';
    sel.appendChild(auto);
    projects.forEach(function(name) {
      var o = document.createElement('option');
      o.value = name;
      o.textContent = name;
      sel.appendChild(o);
    });
    var warn = document.getElementById('slim-linkpr-noproj');
    if (warn) warn.style.display = projects.length ? 'none' : '';
    document.getElementById('slim-linkpr-url').value = '';
    document.getElementById('slim-linkpr-title').value = '';
    document.getElementById('slim-linkpr-context').value = '';
    document.getElementById('slim-linkpr-observe').checked = false;
    modal.classList.add('open');
    setTimeout(function() { var u = document.getElementById('slim-linkpr-url'); if (u) u.focus(); }, 40);
  }
  function closeLinkPrModal() {
    var modal = document.getElementById('slim-linkpr-modal');
    if (modal) modal.classList.remove('open');
  }
  async function submitLinkPr() {
    var url = (document.getElementById('slim-linkpr-url').value || '').trim();
    if (!url) { alert('PR URL is required'); return; }
    var title = (document.getElementById('slim-linkpr-title').value || '').trim();
    var project = document.getElementById('slim-linkpr-project').value || '';
    var context = document.getElementById('slim-linkpr-context').value || '';
    var autoObserve = !!document.getElementById('slim-linkpr-observe').checked;
    var btn = document.getElementById('slim-linkpr-submit');
    if (btn) { btn.disabled = true; btn.textContent = 'Linking…'; }
    try {
      var res = await fetch('/api/pull-requests/link', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ url: url, title: title, project: project, context: context, autoObserve: autoObserve }),
      });
      var data = await res.json().catch(function() { return {}; });
      if (res.ok) {
        closeLinkPrModal();
        scheduleStatusRefresh(400);   // refresh the Active PRs tile
      } else {
        alert('Failed to link PR: ' + (data.error || ('HTTP ' + res.status)));
      }
    } catch (e) {
      alert('Error linking PR: ' + (e && e.message ? e.message : e));
    } finally {
      if (btn) { btn.disabled = false; btn.textContent = 'Link PR'; }
    }
  }
  bindModalClose('slim-linkpr-modal', 'slim-linkpr-close');
  (function bindLinkPrUi() {
    ['slim-linkpr-btn', 'slim-tile-linkpr-chip', 'slim-tile-modal-linkpr'].forEach(function(id) {
      var el = document.getElementById(id);
      if (!el) return;
      el.addEventListener('click', function(ev) {
        ev.stopPropagation();           // don't trigger the cockpit tile's own click
        if (id === 'slim-tile-modal-linkpr') {
          var tm = document.getElementById('slim-tile-modal');
          if (tm) tm.classList.remove('open');
        }
        openLinkPrModal();
      });
    });
    var cancel = document.getElementById('slim-linkpr-cancel');
    if (cancel) cancel.addEventListener('click', closeLinkPrModal);
    var submit = document.getElementById('slim-linkpr-submit');
    if (submit) submit.addEventListener('click', submitLinkPr);
  })();

