<%

  var st = !!locals.showStaff;
  var keys = st ? ['proposed', 'approved', 'published', 'rejected', 'blacklisted'] : ['proposed', 'approved', 'published'];
  var CH = [['', 'All', sites.length]].concat(keys.map(function (k) { return [k, k === 'proposed' && !st ? 'To decide' : lb.SITE[k], counts[k] || 0]; }));
  var asked = function (s) { return s.status === 'proposed' && (cfg.requireSiteApproval || s.askedAt) && !(s.clientDecision && s.clientDecision.decision); };
  var toDecide = sites.filter(asked);

  var DR_SRC = ['Ahrefs', 'Semrush', 'Moz', 'Majestic', 'Other'];
  var TR_SRC = ['Ahrefs', 'Semrush', 'SimilarWeb', 'Search Console', 'Other'];
  var opts = function (list, cur) { var v = cur || 'Ahrefs'; var all = list.indexOf(v) < 0 ? [v].concat(list) : list; return all.map(function (o) { return '<option' + (o === v ? ' selected' : '') + '>' + h.esc(o) + '</option>'; }).join(''); };
  var metric = function (s) { return [s.dr != null ? 'DR ' + s.dr + (s.drSource ? ' (' + s.drSource + ')' : '') : '', s.traffic != null ? h.int(s.traffic) + ' visits/mo' + (s.trafficSource ? ' (' + s.trafficSource + ')' : '') : '', s.country, s.language, s.niche].filter(Boolean).join(' · '); };
%>
<%- include('../partials/lb-shell') %>

<p class="claim-lede"><% if (!sites.length) { %>No sites yet.<% } else if (!st && toDecide.length) { %><em><%= toDecide.length %> site<%= toDecide.length === 1 ? '' : 's' %></em> waiting for your decision.<% } else { %><%= sites.length %> site<%= sites.length === 1 ? '' : 's' %><%= counts.published ? ', ' + counts.published + ' already published on' : '' %>.<% } %></p>
<p class="claim-sub"><%= st ? 'Every site considered for this client, with where each metric came from and when. Price and contact stay internal.' : (cfg.requireSiteApproval ? 'Sites we would like to publish on for you. Say yes or no to each before we write for it.' : 'The sites we publish on for you.') %></p>

<div class="bento" style="margin-top:18px">
  <% if (st) { %>
  <section class="card b12 rp-sec">
    <details class="rc-new"<%= sites.length ? '' : ' open' %>><summary class="rp-sh" style="cursor:pointer; list-style:none"><div><div class="rp-kick">Staff</div><h3>+ Add a site</h3></div></summary>
      <form class="js-rep gpf" method="POST" action="/api/links/sites">
        <%- include('../partials/lb-site-fields', { s: {}, edit: false, opts: opts, DR_SRC: DR_SRC, TR_SRC: TR_SRC, lb: lb }) %>
        <div class="gpf-foot"><span>Added as <b>Proposed</b>.<%= cfg.requireSiteApproval ? ' The client is asked to approve it before we write for it.' : '' %></span><button class="btn btn-primary btn-sm" type="submit">Add site</button></div>
      </form>
    </details>
  </section>
  <% } %>

  <section class="card b12 rp-sec rep-fbox">
    <div class="rp-sh"><div><h3>Sites</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] %>"><% if (i) { %><i></i><% } %><%= c[1] %> <b><%= c[2] %></b></button><% }) %></div>
    </div>
    <% if (!sites.length) { %><div class="rp-empty"><b><%= st ? 'No sites yet' : 'Nothing to show yet' %></b><%= st ? 'Add the first one above.' : 'Sites appear here when we propose or use them.' %></div><% } else { %>
    <ul class="rp-list">
      <% sites.forEach(function (s) { var waitingMe = asked(s); var used = usedOf.get(String(s._id)) || 0; %>
        <li class="rp-item<%= s.status === 'blacklisted' || s.status === 'rejected' ? ' is-done' : '' %>" data-k="<%= s.status %>" id="s-<%= s._id %>">
          <div class="rp-lead" style="padding-top:3px"><img src="https://www.google.com/s2/favicons?domain=<%= s.domain %>&sz=32" alt="" width="18" height="18"></div>
          <div>
            <a class="rp-t" href="https://<%= s.domain %>/" target="_blank" rel="noopener nofollow"><%= s.domain %></a>
            <div class="rp-why"><%= metric(s) || 'No metrics yet' %><%= s.metricsAt ? ' · measured ' + lb.dmy(s.metricsAt) : '' %></div>
            <% if (st && s.qualityNote) { %><div class="rp-sn"><%= s.qualityNote %></div><% } %>
            <% if (st && (s.contact || s.price != null)) { %><div class="rp-why" title="Internal"><%= s.price != null ? h.money(s.price, s.currency) : '' %><% String(s.contact || '').split(/\n+/).filter(Boolean).forEach(function (c, i) { var link = /^https?:\/\//i.test(c); %><%= (i || s.price != null) ? ' · ' : '' %><% if (link) { %><a href="<%= c %>" target="_blank" rel="noopener nofollow"><%= lb.host(c) %></a><% } else { %><%= c %><% } %><% }) %></div><% } %>
            <% if (s.clientDecision && s.clientDecision.decision) { %><div class="rp-why"><b><%= s.clientDecision.by %></b> <%= s.clientDecision.decision %> it on <%= lb.dmy(s.clientDecision.at) %><%= s.clientDecision.note ? ': ' + s.clientDecision.note : '' %></div><% } %>
            <% if (waitingMe) { %>
              <div class="rp-ctl row">
                <form class="js-rep" method="POST" action="/api/links/sites/<%= s._id %>/decision"><input type="hidden" name="decision" value="approve"><button class="btn btn-primary btn-sm" type="submit">Approve</button></form>
                <form class="js-rep" method="POST" action="/api/links/sites/<%= s._id %>/decision" style="display:flex; gap:6px; flex:1; min-width:240px"><input type="hidden" name="decision" value="reject"><input class="rp-in" name="note" placeholder="Why not? (helps us pick better)" style="flex:1"><button class="btn btn-sm" type="submit">Turn down</button></form>
              </div>
            <% } %>
            <% if (st) { %><details class="rp-det"><summary>Edit</summary>
              <form class="js-rep gpf lb-edit" method="POST" action="/api/links/sites/<%= s._id %>">
                <%- include('../partials/lb-site-fields', { s: s, edit: true, opts: opts, DR_SRC: DR_SRC, TR_SRC: TR_SRC, lb: lb }) %>
                <div class="gpf-foot"><span></span><button class="btn btn-sm btn-primary" type="submit">Save</button></div>
              </form>
              <% if (s.status !== 'proposed' || (s.clientDecision && s.clientDecision.decision)) { %><form class="js-rep" method="POST" action="/api/links/sites/<%= s._id %>/ask" style="margin-top:8px"><button class="btn btn-sm" type="submit">Ask the client to approve it</button></form><% } %>
            </details><% } %>
          </div>
          <div class="rp-side">
            <span class="rp-lab sc-<%= lb.SITE_C[s.status] %>"><%= waitingMe && !st ? 'To decide' : lb.SITE[s.status] %></span>
            <% if (used) { %><span class="rr-meta"><%= used %> guest post<%= used === 1 ? '' : 's' %></span><% } %>
          </div>
        </li>
      <% }) %>
    </ul>
    <% } %>
    <div class="rp-note"><b>About the metrics.</b> DR and traffic are estimates from third-party tools, not facts, and every figure shows which tool and when. A site that links to a competitor is not a promise it will publish for you.</div>
  </section>
</div>
<style>.rc-new > summary::-webkit-details-marker{display:none} .lb-edit{margin-top:10px; border:1px solid var(--line-2); border-radius:12px; overflow:hidden; background:var(--surface)} .rc-new[open] > summary{border-bottom:1px solid var(--line-2)}</style>
<%- include('../partials/rep-foot') %>
