<%

  var c = cfg; var ar = c.anchorRules || {};
%>
<%- include('../partials/lb-shell') %>

<p class="claim-lede">How link building runs for <em><%= brand %></em>.</p>
<p class="claim-sub">Changes apply straight away. The client never sees this page.</p>

<div class="bento" style="margin-top:18px">
  <section class="card b12 rp-sec">
    <form class="js-rep" method="POST" action="/api/links/settings">
      <div class="rs-sec">
        <div class="rs-sh"><span class="rs-n">1</span><div><b>The quota</b><span>What the client is promised each month. A link counts once, the day it passes its first check on the live page; writing or sending an article does not count.</span></div></div>
        <div class="rs-grid rs-two">
          <label class="rs-f"><span class="rs-l">Links per month</span><span class="rs-num"><input name="quotaMonthly" type="number" min="0" max="1000" value="<%= c.quotaMonthly || 0 %>"><span>0 hides the quota</span></span><span class="rs-hint">Shown on the Overview as “12 of 30 this month”.</span></label>
          <label class="rs-f"><span class="rs-l">Check live links every</span><span class="rs-num"><input name="checkEveryDays" type="number" min="1" max="60" value="<%= c.checkEveryDays || 14 %>"><span>days</span></span><span class="rs-hint">Read from the publisher's page itself: no API, no cost. A link with a problem is read again the next day.</span></label>
          <label class="rs-f"><span class="rs-l">Replace a lost link within</span><span class="rs-num"><input name="replaceLostWithinDays" type="number" min="0" max="365" value="<%= c.replaceLostWithinDays != null ? c.replaceLostWithinDays : 30 %>"><span>days</span></span><span class="rs-hint">The promise printed under Live Links. 0 removes it.</span></label>
        </div>
      </div>
      <div class="rs-sec">
        <div class="rs-sh"><span class="rs-n">2</span><div><b>What the client approves</b><span>The client always sees what is built. These decide what waits for their yes.</span></div></div>
        <div class="rs-grid rs-two">
          <label class="rs-f"><span class="rs-l">The site, before we write for it</span><select name="requireSiteApproval"><option value="1"<%= c.requireSiteApproval ? ' selected' : '' %>>Yes, the client approves each site</option><option value="0"<%= c.requireSiteApproval ? '' : ' selected' %>>No, we choose the sites</option></select><span class="rs-hint">When on, proposed sites show on the client's Sites page with Approve and Turn down.</span></label>
          <label class="rs-f"><span class="rs-l">The article, before it is published</span><select name="requireContentApproval"><option value="1"<%= c.requireContentApproval !== false ? ' selected' : '' %>>Yes, each version</option><option value="0"<%= c.requireContentApproval === false ? ' selected' : '' %>>No, we publish without asking</option></select><span class="rs-hint">Approval is for one version. A change after it goes back to the client.</span></label>
        </div>
      </div>
      <div class="rs-sec">
        <div class="rs-sh"><span class="rs-n">3</span><div><b>Anchor rules</b><span>A warning on Link Plan when the live links drift from these. Set for this client, not one ratio for everyone. Checked from 5 live links up.</span></div></div>
        <div class="rs-grid rs-two">
          <label class="rs-f"><span class="rs-l">Exact-match anchors, at most</span><span class="rs-num"><input name="exactMaxPct" type="number" min="0" max="100" value="<%= ar.exactMaxPct != null ? ar.exactMaxPct : 15 %>"><span>%</span></span></label>
          <label class="rs-f"><span class="rs-l">Branded anchors, at least</span><span class="rs-num"><input name="brandedMinPct" type="number" min="0" max="100" value="<%= ar.brandedMinPct != null ? ar.brandedMinPct : 30 %>"><span>%</span></span></label>
        </div>
      </div>
      <div class="rs-sec">
        <div class="rs-sh"><span class="rs-n">4</span><div><b>Team digest in HRMS</b><span>When one of our articles is cited by AI for the first time, the team gets one message in the HRMS chat group, with the article, the engine, the prompt and how long after placement it was first seen.</span></div></div>
        <div class="rs-grid rs-two">
          <label class="rs-f"><span class="rs-l">Send the digest</span><select name="hrmsOn"><option value="1"<%= c.hrmsOn !== false ? ' selected' : '' %>>Yes</option><option value="0"<%= c.hrmsOn === false ? ' selected' : '' %>>No, keep it on the portal only</option></select></label>
          <label class="rs-f"><span class="rs-l">HRMS group</span><input name="hrmsGroup" value="<%= c.hrmsGroup || 'Link Building' %>" maxlength="80"><span class="rs-hint"><%= hrmsReady ? 'HRMS is connected.' : 'HRMS is not connected yet: digests wait below and go out once it is.' %></span></label>
        </div>
      </div>
      <div class="rs-foot"><span>Saved for <b><%= brand %></b> only.</span><button class="btn btn-primary btn-sm" type="submit">Save settings</button></div>
    </form>
  </section>

  <section class="card b12 rp-sec">
    <div class="rp-sh"><div><div class="rp-kick">Outbox</div><h3>Team digests</h3></div><div class="rp-sm"><%= hrmsReady ? 'Sent to HRMS' : 'Waiting for the HRMS connection' %></div></div>
    <% if (!digests.length) { %><div class="rp-empty"><b>No digest yet</b>One is written the first time an article of ours is cited by AI.</div><% } else { %>
    <ul class="rp-list"><% digests.forEach(function (d) { %>
      <li class="rp-item" style="grid-template-columns:minmax(0, 1fr) auto"><div><div class="rp-src"><%= h.date(d.createdAt) %>, <%= h.time(d.createdAt) %> · to <b><%= d.group %></b> · <%= (d.citations || []).length %> article<%= (d.citations || []).length === 1 ? '' : 's' %></div><details class="rp-det"><summary>Read the message</summary><div class="rp-box lb-digest"><%= d.text %></div><button type="button" class="btn btn-sm lb-copy" style="margin-top:6px">Copy</button></details><% if (d.error && !d.sentAt) { %><div class="rp-why" style="color:var(--warn-ink)"><%= d.error %></div><% } %></div>
        <div class="rp-side"><span class="rp-lab sc-<%= d.sentAt ? 'approved' : 'waiting' %>"><%= d.sentAt ? 'Sent ' + h.date(d.sentAt) : 'Waiting' %></span><% if (!d.sentAt && hrmsReady) { %><form class="js-rep" method="POST" action="/api/links/digests/<%= d._id %>/retry"><button class="btn btn-sm" type="submit">Send now</button></form><% } %></div></li>
    <% }) %></ul>
    <% } %>
  </section>

  <section class="card b12 rp-sec">
    <div class="rp-sh"><div><h3>Changes to these settings</h3></div></div>
    <div class="rp-body"><% if (!(c.log || []).length) { %><div class="rp-help">None yet.</div><% } else { %><ul class="rep-log"><% c.log.slice().reverse().forEach(function (e) { %><li><b><%= e.by %></b> <%= e.what %> <span class="rr-meta">· <%= h.date(e.at) %></span></li><% }) %></ul><% } %></div>
  </section>
</div>
<style>
  .rs-sec{padding:20px 22px 22px; border-top:1px solid var(--line-2)} .rs-sec:first-child{border-top:0}
  .rs-sh{display:flex; gap:12px; align-items:flex-start; margin-bottom:16px}
  .rs-sh b{display:block; font-size:15px; color:var(--ink); line-height:1.3}
  .rs-sh span{display:block; font-size:12.5px; color:var(--muted); line-height:1.5; margin-top:2px}
  .rs-n{flex:none; width:26px; height:26px; border-radius:8px; background:var(--surface-2); color:var(--ink-2); font-size:12.5px; font-weight:700; display:inline-flex !important; align-items:center; justify-content:center; margin-top:1px}
  .rs-grid{display:grid; grid-template-columns:repeat(2, minmax(0, 1fr)); gap:18px 24px; padding-left:38px}
  .rs-f{display:flex; flex-direction:column; gap:6px; min-width:0}
  .rs-l{font-size:13px; font-weight:600; color:var(--ink)}
  .rs-hint{font-size:12px; line-height:1.5; color:var(--muted)}
  .rs-f input, .rs-f select{width:100%; padding:10px 12px; border:1px solid var(--line); border-radius:10px; background:var(--surface); color:var(--ink); font:inherit; font-size:14px; box-sizing:border-box}
  .rs-f input:focus, .rs-f select:focus{outline:none; border-color:var(--accent-strong); box-shadow:0 0 0 3px color-mix(in srgb, var(--accent-strong) 18%, transparent)}
  .rs-num{display:flex; align-items:center; gap:8px; font-size:13px; color:var(--ink-2)} .rs-num input{width:96px; text-align:center}
  .rs-foot{display:flex; justify-content:space-between; align-items:center; gap:12px; flex-wrap:wrap; padding:14px 22px; background:var(--surface-2); border-top:1px solid var(--line-2); font-size:12.5px; color:var(--muted)}
  .rs-foot b{color:var(--ink-2)}
  @media (max-width:860px){ .rs-grid{grid-template-columns:minmax(0, 1fr); padding-left:0} .rs-sec{padding:16px} }
</style>
<script>
(function () {
  if (window.__lbCopy) return; window.__lbCopy = true;
  document.addEventListener('click', function (e) {
    var b = e.target.closest && e.target.closest('.lb-copy'); if (!b) return;
    var t = b.parentNode.querySelector('.lb-digest'); if (!t || !navigator.clipboard) return;
    navigator.clipboard.writeText(t.textContent).then(function () { b.textContent = 'Copied'; setTimeout(function () { b.textContent = 'Copy'; }, 1500); });
  });
})();
</script>
<%- include('../partials/rep-foot') %>
