<?php
if (session_status() === PHP_SESSION_NONE) session_start();
include __DIR__ . '/config/db.php';
include __DIR__ . '/config/auth.php';
require_page_access();

// ── Filters ───────────────────────────────────────────────────
$type    = $_GET['type']   ?? '';   // recommended | excluded | ''
$search  = trim($_GET['q'] ?? '');
$page    = max(1, (int)($_GET['page'] ?? 1));
$per     = 20;
$offset  = ($page - 1) * $per;

$where = ['1=1']; $params = [];

if ($type && in_array($type, ['recommended','excluded'])) {
    $where[] = 'sr.reviewer_type = ?'; $params[] = $type;
}
if ($search) {
    $where[] = "(sr.first_name LIKE ? OR sr.last_name LIKE ? OR sr.email LIKE ?
                 OR sr.institution LIKE ? OR sr.affiliation_name LIKE ?
                 OR mi.article_title LIKE ? OR u.first_name LIKE ? OR u.last_name LIKE ?)";
    $s = "%$search%";
    for($i=0;$i<8;$i++) $params[] = $s;
}

$w = implode(' AND ', $where);

// Count
$cnt = $pdo->prepare("
    SELECT COUNT(*)
    FROM suggested_reviewers sr
    LEFT JOIN submissions s ON s.submission_id = sr.submission_id
    LEFT JOIN users u ON u.user_id = s.user_id
    LEFT JOIN manuscript_info mi ON mi.submission_id = sr.submission_id
    WHERE $w
");
$cnt->execute($params);
$total = (int)$cnt->fetchColumn();
$pages = max(1, (int)ceil($total / $per));

// Fetch
$stmt = $pdo->prepare("
    SELECT sr.*,
           s.submission_id,
           COALESCE(s.submission_status,'draft') AS sub_status,
           mi.article_title,
           u.first_name AS auth_first, u.last_name AS auth_last,
           u.email AS auth_email, u.user_id AS auth_id
    FROM suggested_reviewers sr
    LEFT JOIN submissions s ON s.submission_id = sr.submission_id
    LEFT JOIN users u ON u.user_id = s.user_id
    LEFT JOIN manuscript_info mi ON mi.submission_id = sr.submission_id
    WHERE $w
    ORDER BY sr.created_at DESC
    LIMIT $per OFFSET $offset
");
$stmt->execute($params);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Stats
$stats = $pdo->query("
    SELECT
        COUNT(*) AS total,
        SUM(reviewer_type='recommended') AS recommended,
        SUM(reviewer_type='excluded')    AS excluded,
        COUNT(DISTINCT submission_id)    AS submissions
    FROM suggested_reviewers
")->fetch(PDO::FETCH_ASSOC);

function buildQ(array $ov=[]): string {
    $q = array_merge($_GET, $ov); unset($q['msg']);
    return http_build_query(array_filter($q, fn($v)=>$v!==''));
}
function p($v){ return htmlspecialchars($v??''); }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Suggested Reviewers — Admin</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">
<style>
:root{--brand:#1d4ed8;--page-bg:#f4f7fc;--card-bg:#fff;--border:#e2e8f0;--text:#1e293b;--muted:#64748b;--radius:10px;}
body{font-family:'Plus Jakarta Sans',sans-serif;background:var(--page-bg);color:var(--text);}
.stat-card{background:var(--card-bg);border-radius:var(--radius);padding:18px 22px;border:1.5px solid var(--border);display:flex;align-items:center;gap:14px;transition:box-shadow .2s;}
.stat-card:hover{box-shadow:0 4px 18px rgba(0,0,0,.07);}
.stat-icon{width:44px;height:44px;border-radius:10px;display:flex;align-items:center;justify-content:center;font-size:1.1rem;flex-shrink:0;}
.stat-val{font-size:1.5rem;font-weight:800;line-height:1;}
.stat-lbl{font-size:.75rem;color:var(--muted);font-weight:500;margin-top:2px;}
.table-card{background:var(--card-bg);border-radius:var(--radius);border:1.5px solid var(--border);overflow:hidden;}
.th{font-size:.68rem;font-weight:700;text-transform:uppercase;letter-spacing:.5px;color:var(--muted);background:#f8fafc;border-bottom:1.5px solid var(--border);padding:10px 12px;white-space:nowrap;}
.td{font-size:.82rem;padding:11px 12px;vertical-align:middle;border-bottom:1px solid #f1f5f9;}
tbody tr:hover{background:#f8fbff;}
tbody tr:last-child td{border-bottom:none;}
/* Type badges */
.badge-rec{background:#dcfce7;color:#15803d;font-size:.68rem;font-weight:700;padding:3px 9px;border-radius:20px;display:inline-flex;align-items:center;gap:4px;}
.badge-exc{background:#fee2e2;color:#b91c1c;font-size:.68rem;font-weight:700;padding:3px 9px;border-radius:20px;display:inline-flex;align-items:center;gap:4px;}
/* Submission status */
.ss-draft{background:#f1f5f9;color:#64748b;}
.ss-submitted{background:#eff6ff;color:#2563eb;}
.ss-under_review{background:#fef3c7;color:#d97706;}
.ss-accepted{background:#dcfce7;color:#15803d;}
.ss-rejected{background:#fee2e2;color:#b91c1c;}
.ss{font-size:.65rem;font-weight:700;padding:2px 8px;border-radius:20px;}
/* Reviewer card in modal */
.info-row{display:flex;gap:8px;margin-bottom:8px;}
.info-lbl{min-width:140px;font-size:.75rem;font-weight:700;color:var(--muted);flex-shrink:0;}
.info-val{font-size:.82rem;color:var(--text);}
/* Filter active state */
.btn-type-active{font-weight:700;}
.av-sm{width:30px;height:30px;border-radius:8px;background:linear-gradient(135deg,#1d4ed8,#60a5fa);display:inline-flex;align-items:center;justify-content:center;font-size:.68rem;font-weight:700;color:#fff;flex-shrink:0;}
.sub-link{color:#1d4ed8;font-weight:600;text-decoration:none;font-size:.78rem;}
.sub-link:hover{text-decoration:underline;}
.page-link{font-size:.8rem;color:var(--brand);border-color:var(--border);}
.page-item.active .page-link{background:var(--brand);border-color:var(--brand);}
</style>
</head>
<body>
<?php include __DIR__ . '/include/sidebar.php'; ?>

<div class="rsis-page">
<div class="p-4">

  <!-- Header -->
  <div class="d-flex align-items-center justify-content-between mb-4">
    <div>
      <h4 class="mb-0 fw-800 d-flex align-items-center gap-2">
        <i class="fas fa-users-cog text-primary"></i> Suggested &amp; Excluded Reviewers
      </h4>
      <p class="text-muted mb-0" style="font-size:.83rem">
        Authors dwara suggest ya exclude kiye gaye reviewers — submission ke saath linked
      </p>
    </div>
    <div class="d-flex gap-2">
      <a href="reviewer_applications.php" class="btn btn-outline-secondary btn-sm">
        <i class="fas fa-id-card me-1"></i> Reviewer Applications
      </a>
    </div>
  </div>

  <!-- Stats -->
  <div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
      <div class="stat-card">
        <div class="stat-icon" style="background:#eff6ff;color:#1d4ed8"><i class="fas fa-list"></i></div>
        <div><div class="stat-val"><?= number_format((int)$stats['total']) ?></div><div class="stat-lbl">Total Entries</div></div>
      </div>
    </div>
    <div class="col-6 col-md-3">
      <div class="stat-card">
        <div class="stat-icon" style="background:#dcfce7;color:#16a34a"><i class="fas fa-thumbs-up"></i></div>
        <div><div class="stat-val" style="color:#16a34a"><?= number_format((int)$stats['recommended']) ?></div><div class="stat-lbl">Recommended</div></div>
      </div>
    </div>
    <div class="col-6 col-md-3">
      <div class="stat-card">
        <div class="stat-icon" style="background:#fee2e2;color:#dc2626"><i class="fas fa-thumbs-down"></i></div>
        <div><div class="stat-val" style="color:#dc2626"><?= number_format((int)$stats['excluded']) ?></div><div class="stat-lbl">Excluded</div></div>
      </div>
    </div>
    <div class="col-6 col-md-3">
      <div class="stat-card">
        <div class="stat-icon" style="background:#fef3c7;color:#d97706"><i class="fas fa-file-signature"></i></div>
        <div><div class="stat-val" style="color:#d97706"><?= number_format((int)$stats['submissions']) ?></div><div class="stat-lbl">Linked Submissions</div></div>
      </div>
    </div>
  </div>

  <!-- Table card -->
  <div class="table-card">

    <!-- Toolbar -->
    <div class="p-3 border-bottom d-flex flex-wrap gap-3 align-items-center" style="background:#fafbff">
      <!-- Type quick filters -->
      <div class="d-flex gap-2">
        <a href="?<?= buildQ(['type'=>'','page'=>1]) ?>"
           class="btn btn-sm <?= $type===''?'btn-primary':'btn-outline-secondary' ?>">
          All <span class="badge bg-white text-dark ms-1" style="font-size:.65rem"><?= $stats['total'] ?></span>
        </a>
        <a href="?<?= buildQ(['type'=>'recommended','page'=>1]) ?>"
           class="btn btn-sm <?= $type==='recommended'?'btn-success':'btn-outline-success' ?>">
          <i class="fas fa-thumbs-up me-1"></i>Recommended
          <span class="badge bg-white text-success ms-1" style="font-size:.65rem"><?= $stats['recommended'] ?></span>
        </a>
        <a href="?<?= buildQ(['type'=>'excluded','page'=>1]) ?>"
           class="btn btn-sm <?= $type==='excluded'?'btn-danger':'btn-outline-danger' ?>">
          <i class="fas fa-ban me-1"></i>Excluded
          <span class="badge bg-white text-danger ms-1" style="font-size:.65rem"><?= $stats['excluded'] ?></span>
        </a>
      </div>

      <!-- Search -->
      <form method="GET" class="d-flex gap-2 flex-1 ms-auto">
        <input type="hidden" name="type" value="<?= p($type) ?>">
        <input type="search" name="q" class="form-control form-control-sm"
               style="min-width:250px;font-size:.82rem"
               placeholder="Search reviewer name, email, submission title, author…"
               value="<?= p($search) ?>">
        <button type="submit" class="btn btn-primary btn-sm">
          <i class="fas fa-search"></i>
        </button>
        <?php if($search||$type): ?>
        <a href="suggested_reviewers.php" class="btn btn-outline-secondary btn-sm">
          <i class="fas fa-times"></i>
        </a>
        <?php endif; ?>
      </form>

      <span class="text-muted ms-1" style="font-size:.8rem;white-space:nowrap">
        <?= $total ?> result<?= $total!==1?'s':'' ?>
      </span>
    </div>

    <!-- Table -->
    <div class="table-responsive">
      <table class="table mb-0" style="border-collapse:separate;border-spacing:0">
        <thead>
          <tr>
            <th class="th">#</th>
            <th class="th">Type</th>
            <th class="th">Reviewer</th>
            <th class="th">Email</th>
            <th class="th">Affiliation / Dept</th>
            <th class="th">Submission</th>
            <th class="th">Submitting Author</th>
            <th class="th">Relationship</th>
            <th class="th">Comment</th>
            <th class="th">Date</th>
            <th class="th">Actions</th>
          </tr>
        </thead>
        <tbody>
        <?php if(empty($rows)): ?>
        <tr>
          <td colspan="11" class="text-center text-muted py-5">
            <i class="fas fa-inbox fa-2x mb-2 d-block opacity-25"></i>
            No reviewers found<?= ($search||$type)?' matching your filters':'' ?>.
          </td>
        </tr>
        <?php endif; ?>
        <?php foreach($rows as $i=>$r):
          $isRec = $r['reviewer_type'] === 'recommended';
          $revName = trim(($r['title']??'').' '.($r['first_name']??'').' '.($r['middle_name']??'').' '.($r['last_name']??''));
          $authName = trim(($r['auth_first']??'').' '.($r['auth_last']??''));
          $authInit = strtoupper(substr($r['auth_first']??'?',0,1).substr($r['auth_last']??'',0,1));
          $revInit  = strtoupper(substr($r['first_name']??'?',0,1).substr($r['last_name']??'',0,1));
          $subStatus = $r['sub_status'] ?? 'draft';
          $affilLine = trim(($r['affiliation_name']??'').($r['department']?(' · '.$r['department']):''));
        ?>
        <tr>
          <td class="td text-muted" style="font-size:.75rem"><?= $offset+$i+1 ?></td>

          <!-- Type badge -->
          <td class="td">
            <?php if($isRec): ?>
            <span class="badge-rec"><i class="fas fa-thumbs-up"></i> Suggested</span>
            <?php else: ?>
            <span class="badge-exc"><i class="fas fa-ban"></i> Excluded</span>
            <?php endif; ?>
          </td>

          <!-- Reviewer -->
          <td class="td" style="min-width:160px">
            <div class="d-flex align-items-center gap-2">
              <div class="av-sm" style="background:<?= $isRec?'linear-gradient(135deg,#16a34a,#4ade80)':'linear-gradient(135deg,#dc2626,#f87171)' ?>">
                <?= p($revInit) ?>
              </div>
              <div>
                <div style="font-size:.83rem;font-weight:600;color:var(--text)"><?= p($revName) ?: '<em class="text-muted">—</em>' ?></div>
                <?php if($r['expertise']): ?>
                <div style="font-size:.7rem;color:var(--muted)" class="text-truncate" style="max-width:140px">
                  <?= p(mb_strimwidth($r['expertise'],0,40,'…')) ?>
                </div>
                <?php endif; ?>
              </div>
            </div>
          </td>

          <!-- Email -->
          <td class="td">
            <?php if($r['email']): ?>
            <a href="mailto:<?= p($r['email']) ?>" style="font-size:.78rem;color:#1d4ed8">
              <?= p($r['email']) ?>
            </a>
            <?php else: ?>
            <span class="text-muted">—</span>
            <?php endif; ?>
          </td>

          <!-- Affiliation -->
          <td class="td" style="max-width:180px">
            <?php if($affilLine): ?>
            <div style="font-size:.78rem;color:var(--text)" class="text-truncate" title="<?= p($affilLine) ?>">
              <?= p(mb_strimwidth($affilLine,0,50,'…')) ?>
            </div>
            <?php else: ?>
            <span class="text-muted" style="font-size:.75rem">—</span>
            <?php endif; ?>
          </td>

          <!-- Submission -->
          <td class="td" style="min-width:180px">
            <?php if($r['submission_id']): ?>
            <div>
              <a href="submission_view.php?id=<?= $r['submission_id'] ?>" class="sub-link">
                #<?= $r['submission_id'] ?>
                <?php if($r['article_title']): ?>
                — <?= p(mb_strimwidth($r['article_title'],0,35,'…')) ?>
                <?php endif; ?>
              </a>
              <div class="mt-1">
                <span class="ss ss-<?= p($subStatus) ?>">
                  <?= ucfirst(str_replace('_',' ',$subStatus)) ?>
                </span>
              </div>
            </div>
            <?php else: ?>
            <span class="text-muted" style="font-size:.75rem">—</span>
            <?php endif; ?>
          </td>

          <!-- Submitting Author -->
          <td class="td" style="min-width:150px">
            <?php if($authName): ?>
            <div class="d-flex align-items-center gap-2">
              <div class="av-sm"><?= p($authInit) ?></div>
              <div>
                <div style="font-size:.8rem;font-weight:600"><?= p($authName) ?></div>
                <?php if($r['auth_email']): ?>
                <div style="font-size:.7rem;color:var(--muted)"><?= p($r['auth_email']) ?></div>
                <?php endif; ?>
              </div>
            </div>
            <?php else: ?>
            <span class="text-muted" style="font-size:.75rem">—</span>
            <?php endif; ?>
          </td>

          <!-- Relationship -->
          <td class="td">
            <?php if($r['relationship']): ?>
            <span style="font-size:.75rem;background:#f1f5f9;padding:2px 7px;border-radius:4px;color:#475569">
              <?= p($r['relationship']) ?>
            </span>
            <?php else: ?>
            <span class="text-muted" style="font-size:.75rem">—</span>
            <?php endif; ?>
          </td>

          <!-- Comment -->
          <td class="td" style="max-width:160px">
            <?php $comment = trim($r['additional_comment']??''); if($comment): ?>
            <span style="font-size:.75rem;color:var(--text)" title="<?= p($comment) ?>">
              <?= p(mb_strimwidth($comment,0,40,'…')) ?>
            </span>
            <?php else: ?>
            <span class="text-muted" style="font-size:.75rem">—</span>
            <?php endif; ?>
          </td>

          <!-- Date -->
          <td class="td" style="white-space:nowrap;font-size:.75rem;color:var(--muted)">
            <?= date('d M Y', strtotime($r['created_at'])) ?><br>
            <span style="font-size:.68rem"><?= date('H:i', strtotime($r['created_at'])) ?></span>
          </td>

          <!-- Actions -->
          <td class="td">
            <div class="d-flex gap-1">
              <button class="btn btn-sm btn-outline-primary" style="padding:3px 9px;font-size:.72rem"
                      onclick="openModal(<?= htmlspecialchars(json_encode($r), ENT_QUOTES) ?>)"
                      title="View full details">
                <i class="fas fa-eye"></i>
              </button>
              <?php if($r['submission_id']): ?>
              <a href="submission_view.php?id=<?= $r['submission_id'] ?>"
                 class="btn btn-sm btn-outline-secondary" style="padding:3px 9px;font-size:.72rem"
                 title="View submission">
                <i class="fas fa-file-alt"></i>
              </a>
              <?php endif; ?>
            </div>
          </td>
        </tr>
        <?php endforeach; ?>
        </tbody>
      </table>
    </div>

    <!-- Pagination -->
    <?php if($pages > 1): ?>
    <div class="d-flex justify-content-between align-items-center px-4 py-3 border-top">
      <small class="text-muted">
        Showing <?= $offset+1 ?>–<?= min($offset+$per,$total) ?> of <?= $total ?>
      </small>
      <nav>
        <ul class="pagination pagination-sm mb-0 gap-1">
          <?php for($pg=1;$pg<=$pages;$pg++): ?>
            <?php if($pg===1||$pg===$pages||abs($pg-$page)<=2): ?>
            <li class="page-item <?= $pg===$page?'active':'' ?>">
              <a class="page-link rounded" href="?<?= buildQ(['page'=>$pg]) ?>"><?= $pg ?></a>
            </li>
            <?php elseif(abs($pg-$page)===3): ?>
            <li class="page-item disabled"><span class="page-link">…</span></li>
            <?php endif; ?>
          <?php endfor; ?>
        </ul>
      </nav>
    </div>
    <?php endif; ?>

  </div><!-- /table-card -->

</div>
</div>

<!-- DETAIL MODAL -->
<div class="modal fade" id="detailModal" tabindex="-1">
  <div class="modal-dialog modal-lg modal-dialog-scrollable">
    <div class="modal-content border-0 shadow">
      <div class="modal-header border-0 pb-0" id="modalHdr" style="border-radius:10px 10px 0 0">
        <div>
          <h5 class="modal-title fw-700 mb-0" id="modalTitle">Reviewer Detail</h5>
          <small id="modalSub" class="opacity-75"></small>
        </div>
        <button type="button" class="btn-close" id="modalClose" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body p-4" id="modalBody"></div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
function esc(s){const d=document.createElement('div');d.textContent=String(s??'');return d.innerHTML;}
function row(lbl,val,raw=false){
  const v=raw?(val||'<em class="text-muted">—</em>'):esc(val||'—');
  return `<div class="info-row"><div class="info-lbl">${lbl}</div><div class="info-val">${v}</div></div>`;
}
function badge(type){
  return type==='recommended'
    ? '<span class="badge-rec"><i class="fas fa-thumbs-up me-1"></i>Suggested by Author</span>'
    : '<span class="badge-exc"><i class="fas fa-ban me-1"></i>Excluded by Author</span>';
}
function statusBadge(s){
  const map={draft:'#f1f5f9:#64748b',submitted:'#eff6ff:#2563eb',under_review:'#fef3c7:#d97706',accepted:'#dcfce7:#16a34a',rejected:'#fee2e2:#dc2626'};
  const [bg,color]=(map[s]||'#f1f5f9:#64748b').split(':');
  return `<span style="background:${bg};color:${color};font-size:.7rem;font-weight:700;padding:2px 8px;border-radius:20px">${s.replace('_',' ')}</span>`;
}

function openModal(r) {
  const isRec = r.reviewer_type === 'recommended';
  const color = isRec ? '#16a34a' : '#dc2626';
  const bg    = isRec ? '#f0fdf4' : '#fef2f2';

  const hdr = document.getElementById('modalHdr');
  hdr.style.background = bg;
  hdr.style.borderBottom = `2px solid ${color}`;
  document.getElementById('modalClose').style.filter = 'none';

  const revName = [r.title,r.first_name,r.middle_name,r.last_name].filter(Boolean).join(' ');
  document.getElementById('modalTitle').textContent = revName || 'Reviewer Details';
  document.getElementById('modalSub').innerHTML = badge(r.reviewer_type);

  const affilLine = [r.affiliation_name, r.department].filter(Boolean).join(' · ');
  const authName  = [r.auth_first, r.auth_last].filter(Boolean).join(' ');

  document.getElementById('modalBody').innerHTML = `
    <div class="row g-4">

      <div class="col-md-6">
        <h6 class="fw-700 mb-3" style="color:${color}"><i class="fas fa-user me-2"></i>Reviewer Info</h6>
        ${row('Full Name', revName)}
        ${row('Email', r.email ? `<a href="mailto:${esc(r.email)}">${esc(r.email)}</a>` : '—', true)}
        ${row('Institution', r.institution)}
        ${row('Affiliation', r.affiliation_name)}
        ${row('Department', r.department)}
        ${row('Expertise', r.expertise)}
        ${row('Relationship', r.relationship)}
        ${row('Type', badge(r.reviewer_type), true)}
      </div>

      <div class="col-md-6">
        <h6 class="fw-700 mb-3 text-primary"><i class="fas fa-file-signature me-2"></i>Submission</h6>
        ${r.submission_id ? `
          ${row('Submission ID', `<a href="submission_view.php?id=${r.submission_id}" class="sub-link">#${r.submission_id}</a>`, true)}
          ${row('Article Title', r.article_title || '<<untitled>>')}
          ${row('Status', statusBadge(r.sub_status||'draft'), true)}
        ` : row('Submission', 'Not linked')}

        <h6 class="fw-700 mb-3 mt-4 text-primary"><i class="fas fa-user-edit me-2"></i>Submitting Author</h6>
        ${authName ? `
          ${row('Name', authName)}
          ${row('Email', r.auth_email ? `<a href="mailto:${esc(r.auth_email)}">${esc(r.auth_email)}</a>` : '—', true)}
          ${r.auth_id ? row('Profile', `<a href="user_view.php?id=${r.auth_id}" class="sub-link">View Author #${r.auth_id}</a>`, true) : ''}
        ` : row('Author', 'Unknown')}
      </div>

      ${r.additional_comment ? `
      <div class="col-12">
        <h6 class="fw-700 mb-2" style="color:${color}"><i class="fas fa-comment me-2"></i>
          ${isRec ? 'Reason for Suggestion' : 'Reason for Exclusion'}
        </h6>
        <div style="background:${bg};border:1px solid ${color}30;border-radius:8px;padding:14px;font-size:.85rem;line-height:1.7;color:var(--text)">
          ${esc(r.additional_comment)}
        </div>
      </div>` : ''}

      <div class="col-12">
        <div class="d-flex gap-2 flex-wrap">
          ${r.submission_id ? `<a href="submission_view.php?id=${r.submission_id}" class="btn btn-sm btn-primary"><i class="fas fa-file-alt me-1"></i>View Submission</a>` : ''}
          ${r.auth_id ? `<a href="user_view.php?id=${r.auth_id}" class="btn btn-sm btn-outline-secondary"><i class="fas fa-user me-1"></i>View Author</a>` : ''}
          ${r.email ? `<a href="mailto:${esc(r.email)}" class="btn btn-sm btn-outline-primary"><i class="fas fa-envelope me-1"></i>Email Reviewer</a>` : ''}
        </div>
      </div>

    </div>
  `;

  new bootstrap.Modal(document.getElementById('detailModal')).show();
}
</script>
</body>
</html>