<%

  var st = !!locals.showStaff;
  var CH = [['', 'All', links.length]].concat(['live', 'recovered', 'review', 'pending', 'lost', 'replaced'].map(function (k) { return [k, lb.STATUS[k], counts[k] || 0]; }));
  var today = new Date().toISOString().slice(0, 10);
%>
<%- include('../partials/lb-shell') %>

<p class="claim-lede"><% if (!links.length) { %>No links yet.<% } else { %><em><%= (counts.live || 0) + (counts.recovered || 0) %> live</em> on <%= domains %> site<%= domains === 1 ? '' : 's' %><%= counts.lost ? ', ' + counts.lost + ' lost' : '' %><%= st && counts.review ? ', ' + counts.review + ' to review' : '' %>.<% } %></p>
<p class="claim-sub">Every page is read every <%= cfg.checkEveryDays || 14 %> days, straight from the publisher's site (no API). A link with a problem is read again the next day, and only called lost when the page loads without it on two checks in a row; a site that is slow or blocks the check is retried, not counted against you.</p>

<div class="bento" style="margin-top:18px">
  <% if (st) { %>
  <section class="card b12 rp-sec">
    <details class="rc-new"><summary class="rp-sh" style="cursor:pointer; list-style:none"><div><div class="rp-kick">Staff</div><h3>+ Add a link by hand</h3></div><span class="rp-sm">For a link not placed through Guest Posts</span></summary>
      <form class="js-rep gpf" method="POST" action="/api/links/backlinks">
        <div class="gpf-sec">
          <div class="gpf-h"><span class="gpf-n">1</span><div><b>The page</b><span>Where the links are, and when they went up.</span></div></div>
          <div class="gpf-grid c4">
            <label class="gpf-f" style="grid-column:span 2"><span>Page the links are on</span><input name="sourceUrl" required placeholder="https://publisher.com/article"></label>
            <label class="gpf-f"><span>Placed on</span><input name="placedAt" type="date" value="<%= today %>"></label>
            <label class="gpf-f"><span>Placed by</span><input name="placedBy" value="<%= (locals.currentUser && currentUser.name) || '' %>"></label>
            <label class="gpf-f"><span>Link type agreed</span><select name="rel"><option value="follow">Follow</option><option value="nofollow">Nofollow</option><option value="sponsored">Sponsored</option><option value="ugc">UGC</option></select></label>
          </div>
        </div>
        <div class="gpf-sec">
          <div class="gpf-h"><span class="gpf-n">2</span><div><b>Links on that page</b><span>One row per link to the client's site. Each is checked on its own.</span></div></div>
          <div class="gpf-in"><%- include('../partials/lb-link-rows', { rows: [], targets: locals.targets || [], lb: lb }) %></div>
        </div>
        <div class="gpf-foot"><span>Checked the moment you add them.</span><button class="btn btn-primary btn-sm" type="submit">Add and check</button></div>
      </form>
    </details>
  </section>
  <% } %>

  <section class="card b12 rp-sec rep-fbox">
    <div class="rp-sh"><div><h3>Links</h3></div>
      <div class="rp-filters rep-f"><% CH.forEach(function (c, i) { if (i && !c[2]) return; %><button type="button" class="rp-chip<%= i ? '' : ' on' %>" data-f="<%= c[0] %>" style="--c:var(--<%= c[0] === 'lost' ? 'neg' : c[0] === 'live' || c[0] === 'recovered' ? 'pos' : 'muted-2' %>)"><% if (i) { %><i></i><% } %><%= c[1] %> <b><%= c[2] %></b></button><% }) %></div>
    </div>
    <% if (!links.length) { %><div class="rp-empty"><b>No links yet</b><%= st ? 'A link is added here when a guest post is marked published, or by hand above.' : 'Links appear here as soon as an article goes live.' %></div><% } else { %>
    <div class="lb-wrap"><table class="lb-tbl">
      <thead><tr><th>Site</th><th>Anchor · type</th><th>Points to</th><th>Placed</th><th>Last check</th><th>Status</th></tr></thead>
      <tbody>
      <% links.forEach(function (l) { var s = l.seen || {}; var gp = l.guestPost && gpOf.get(String(l.guestPost)); %>
        <tr data-k="<%= l.status %>">
          <td><div style="display:flex; gap:8px; align-items:flex-start"><img src="https://www.google.com/s2/favicons?domain=<%= l.domain %>&sz=32" alt="" width="16" height="16" style="margin-top:2px"><div><a href="/link-building/links/<%= l._id %>" style="font-weight:600; color:var(--ink)"><%= l.domain %></a><% if (cited.has(String(l._id))) { %> <span class="lb-new" title="An AI answer cites this article">AI</span><% } %><div class="rr-meta" style="max-width:260px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap"><%= gp && gp.title ? gp.title : lb.path(l.sourceUrl) %></div></div></div></td>
          <td><%= s.found ? '“' + s.anchor + '”' : (l.expect && l.expect.anchor ? '“' + l.expect.anchor + '”' : '—') %><div class="rr-meta"><%= s.found ? s.rel : (l.expect && l.expect.rel) || 'follow' %><%= s.noindex ? ' · page noindex' : '' %></div></td>
          <td><a href="<%= l.targetUrl %>" target="_blank" rel="noopener" class="rr-meta"><%= lb.path(l.targetUrl) %></a></td>
          <td><%= lb.dmy(l.placedAt) %><div class="rr-meta"><%= l.placedBy || '' %></div></td>
          <td><%= l.lastCheckedAt ? h.date(l.lastCheckedAt) : 'not yet' %><div class="rr-meta"><%= s.http ? 'HTTP ' + s.http : '' %><%= l.failStreak ? ' · ' + l.failStreak + ' failed check' + (l.failStreak === 1 ? '' : 's') : '' %></div></td>
          <td><span class="rp-lab sc-<%= lb.STATUS_C[l.status] %>"><%= lb.STATUS[l.status] %></span><% if (st && !['replaced'].includes(l.status)) { %><form class="js-rep" method="POST" action="/api/links/backlinks/<%= l._id %>/check" style="margin-top:6px"><button class="btn btn-sm" type="submit" style="padding:3px 9px; font-size:12px">Check</button></form><% } %></td>
        </tr>
      <% }) %>
      </tbody>
    </table></div>
    <% } %>
    <div class="rp-note"><b>What the statuses mean.</b> <b>Live</b>: on the page as agreed. <b>Needs review</b>: something changed (the anchor, a nofollow added, the page set to noindex) or the site could not be read three times in a row. <b>Lost</b>: the page loaded without the link twice<%= cfg.replaceLostWithinDays ? ', and we replace or restore it within ' + cfg.replaceLostWithinDays + ' days' : '' %>. <b>Recovered</b>: back after being lost.</div>
  </section>
</div>
<style>.rc-new > summary::-webkit-details-marker{display:none} .rc-new[open] > summary{border-bottom:1px solid var(--line-2)} .lb-tbl tr[hidden]{display:none}</style>
<%- include('../partials/rep-foot') %>
