<?php
$tt_page_info = array(
  'Page_Info' =>   array(
    'Title' => 'আমাদের প্রতিনিধিগণ',
    'Description' => 'দেশজুড়ে সংবাদ সংগ্রহে নিয়োজিত আমাদের গর্বিত টিম',
  ),
  'Social_og' => array(
    'Image' => '',
    'Type' => 'website',
    'URL' => 'reporters/',
  ),
);

require_once('./header.php'); // Include Header
?>

<style>
  /* ========== CUSTOM STYLES ========== */
  :root {
    --primary: #0a66c2;
    --primary-dark: #004182;
    --gray-bg: #f8fafc;
    --card-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.05), 0 8px 10px -6px rgba(0, 0, 0, 0.02);
    --card-hover: 0 20px 30px -12px rgba(0, 0, 0, 0.1);
  }
  
  /* Thin Red Border Around Page */
  body {
    border: 2px solid #ff0000 !important;
    min-height: 100vh;
    margin: 0;
    padding: 0;
  }
  
  /* Ensure content doesn't get hidden behind border */
  .wrapper, .main-content, header, footer {
    border: none;
  }

  /* Hero Section */
  .hero-team {
    background: linear-gradient(135deg, #f0f7ff 0%, #e9f0fa 100%);
    border-bottom: 1px solid rgba(10, 102, 194, 0.1);
    padding: 1.5rem 0;
    margin-bottom: 1rem;
  }

  .team-badge {
    display: inline-block;
    background: var(--primary);
    color: white;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 0.2rem 0.9rem;
    border-radius: 30px;
    margin-bottom: 0.5rem;
  }

  .proud-team-title {
    font-size: 1.8rem;
    font-weight: 800;
    background: linear-gradient(135deg, #0a2b4e, #0a66c2);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    margin-bottom: 0.3rem;
    line-height: 1.3; /* Added for better text rendering */
    padding: 0 0.5rem; /* Added to prevent cutting */
  }

  .team-sub {
    font-size: 0.95rem;
    color: #334155;
    max-width: 600px;
    margin: 0 auto;
    line-height: 1.4; /* Added for better text rendering */
    padding: 0 0.5rem; /* Added to prevent cutting */
  }

  /* Search Section */
  .search-master {
    background: white;
    border-radius: 80px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
    padding: 0.3rem;
    max-width: 750px;
    margin: 0 auto;
    transition: all 0.2s;
  }

  .search-master:focus-within {
    box-shadow: 0 8px 35px rgba(10, 102, 194, 0.15);
    transform: scale(1.01);
  }

  .search-input-group {
    display: flex;
    align-items: center;
    background: white;
    border-radius: 60px;
    overflow: hidden;
  }

  .search-icon {
    padding: 0 1rem 0 1.5rem;
    color: #6c757d;
    font-size: 1.1rem;
  }

  .search-input {
    flex: 1;
    border: none;
    padding: 0.8rem 0.5rem 0.8rem 0;
    font-size: 1rem;
    outline: none;
    background: transparent;
    font-weight: 500;
  }

  .search-input::placeholder {
    color: #a0aec0;
    font-size: 0.95rem;
  }

  .search-clear-btn {
    background: none;
    border: none;
    padding: 0 1rem;
    color: #94a3b8;
    cursor: pointer;
    font-size: 1.1rem;
  }

  .search-clear-btn:hover {
    color: #0a66c2;
  }

  .search-submit {
    background: var(--primary);
    border: none;
    padding: 0.6rem 1.8rem;
    border-radius: 50px;
    color: white;
    font-weight: 600;
    margin: 0.2rem;
    transition: all 0.2s;
    font-size: 0.9rem;
    cursor: pointer;
  }

  .search-submit:hover {
    background: var(--primary-dark);
  }

  /* Reporter Cards Grid */
  .reporters-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 1.2rem;
    margin-top: 0.8rem;
  }

  /* Reporter Card */
  .reporter-card {
    background: white;
    border-radius: 1.2rem;
    overflow: hidden;
    transition: all 0.3s ease;
    box-shadow: var(--card-shadow);
    border: 1px solid rgba(0, 0, 0, 0.05);
    text-align: center;
    padding: 1.2rem 0.8rem;
  }

  .reporter-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--card-hover);
    border-color: rgba(10, 102, 194, 0.3);
  }

  .card-img-wrapper {
    width: 100px;
    height: 100px;
    margin: 0 auto 0.8rem auto;
    border-radius: 50%;
    overflow: hidden;
    border: 3px solid var(--primary);
    background: #f1f5f9;
  }

  .card-img-wrapper img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .reporter-name {
    font-size: 1rem;
    font-weight: 700;
    color: #0f172a;
    margin-bottom: 0.3rem;
  }

  .reporter-designation {
    font-size: 0.7rem;
    color: white;
    background: #0f172a;
    display: inline-block;
    padding: 0.2rem 0.7rem;
    border-radius: 30px;
    font-weight: 500;
    margin-bottom: 0.5rem;
  }

  /* Verify Button */
  .verify-btn {
    background: transparent;
    border: 1px solid var(--primary);
    color: var(--primary);
    padding: 0.25rem 0.9rem;
    border-radius: 50px;
    font-size: 0.65rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-block;
  }

  .verify-btn:hover {
    background: var(--primary);
    color: white;
  }

  /* Team Section */
  .team-section {
    margin-top: 1.2rem;
    margin-bottom: 0.5rem;
  }

  .team-name-badge {
    font-size: 1.5rem;
    font-weight: 700;
    color: #0f172a;
    border-left: 5px solid var(--primary);
    padding-left: 1rem;
    margin-bottom: 0.8rem;
  }

  /* No result */
  .no-result {
    text-align: center;
    padding: 2rem;
    background: #f8fafc;
    border-radius: 1.5rem;
    margin: 1rem 0;
  }

  /* Modal */
  .id-card-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.75);
    z-index: 9999;
    justify-content: center;
    align-items: center;
  }

  .id-card-content {
    background: white;
    border-radius: 1.2rem;
    max-width: 400px;
    width: 90%;
    padding: 1.5rem;
    position: relative;
    animation: slideUp 0.3s ease;
  }

  .close-modal {
    position: absolute;
    top: 0.8rem;
    right: 1rem;
    font-size: 1.5rem;
    cursor: pointer;
    color: #94a3b8;
  }

  .close-modal:hover {
    color: #ef4444;
  }

  .id-card-header {
    text-align: center;
    margin-bottom: 1rem;
    border-bottom: 2px solid var(--primary);
    padding-bottom: 0.6rem;
  }

  .id-card-header img {
    max-width: 160px;
    height: auto;
  }

  .id-photo {
    width: 90px;
    height: 90px;
    border-radius: 50%;
    margin: 0 auto 0.8rem auto;
    border: 3px solid var(--primary);
    overflow: hidden;
  }

  .id-photo img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .id-info {
    text-align: left;
    margin-top: 0.8rem;
  }

  .id-info-item {
    display: flex;
    justify-content: space-between;
    padding: 0.4rem 0;
    border-bottom: 1px solid #e2e8f0;
    font-size: 0.85rem;
  }

  .id-label {
    font-weight: 600;
    color: #475569;
  }

  .id-value {
    color: #0f172a;
  }

  .id-card-footer {
    margin-top: 1rem;
    text-align: center;
    font-size: 0.65rem;
    color: #94a3b8;
  }

  .download-card-btn {
    background: #10b981;
    color: white;
    border: none;
    padding: 0.4rem 0.8rem;
    border-radius: 50px;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    width: 100%;
    margin-top: 0.6rem;
  }

  .download-card-btn:hover {
    background: #059669;
  }

  /* Popup */
  .custom-popup-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 10001;
    justify-content: center;
    align-items: center;
  }

  .custom-popup {
    background: white;
    border-radius: 1rem;
    max-width: 320px;
    width: 85%;
    padding: 1.2rem;
    text-align: center;
  }

  .custom-popup h4 {
    color: var(--primary);
    margin-bottom: 0.5rem;
    font-size: 1.1rem;
  }

  .custom-popup p {
    font-size: 0.85rem;
    color: #475569;
  }

  .instruction-box {
    background: #f1f5f9;
    padding: 0.5rem;
    border-radius: 10px;
    margin: 0.8rem 0;
    font-size: 0.8rem;
  }

  .digit-input {
    width: 100%;
    padding: 0.6rem;
    font-size: 1rem;
    text-align: center;
    letter-spacing: 3px;
    border: 2px solid #e2e8f0;
    border-radius: 10px;
    margin: 0.6rem 0;
  }

  .popup-buttons {
    display: flex;
    gap: 0.6rem;
    margin-top: 0.5rem;
  }

  .popup-confirm, .popup-cancel {
    flex: 1;
    padding: 0.5rem;
    border-radius: 40px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.8rem;
  }

  .popup-confirm {
    background: var(--primary);
    color: white;
    border: none;
  }

  .popup-cancel {
    background: #e2e8f0;
    color: #475569;
    border: none;
  }

  .error-message {
    color: #ef4444;
    font-size: 0.7rem;
    display: none;
  }

  .show-number-btn {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.2rem 0.6rem;
    border-radius: 50px;
    font-size: 0.65rem;
    cursor: pointer;
    margin-left: 0.3rem;
  }

  @keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }

  /* Responsive */
  @media (max-width: 1199px) {
    .reporters-grid { grid-template-columns: repeat(4, 1fr); gap: 1rem; }
  }
  @media (max-width: 991px) {
    .reporters-grid { grid-template-columns: repeat(3, 1fr); }
    .proud-team-title { font-size: 1.5rem; }
  }
  @media (max-width: 767px) {
    .reporters-grid { grid-template-columns: repeat(2, 1fr); gap: 0.8rem; }
    .card-img-wrapper { width: 85px; height: 85px; }
    .reporter-name { font-size: 0.9rem; }
    .search-submit { padding: 0.5rem 1.2rem; }
    .search-input { padding: 0.6rem 0.3rem 0.6rem 0; font-size: 0.9rem; }
    .team-name-badge { font-size: 1.2rem; }
    .hero-team { padding: 1rem 0; }
    .proud-team-title { font-size: 1.3rem; line-height: 1.3; }
    .team-sub { font-size: 0.85rem; }
  }
  @media (max-width: 480px) {
    .reporters-grid { gap: 0.6rem; }
    .card-img-wrapper { width: 75px; height: 75px; }
    .proud-team-title { font-size: 1.2rem; }
  }
  
  .btn-primary { 
    background: var(--primary); 
    border: none; 
    border-radius: 40px; 
    padding: 0.4rem 1.2rem; 
    color: white; 
    cursor: pointer; 
  }
  .btn-primary:hover { 
    background: var(--primary-dark); 
  }
</style>

<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>

<section class="hero-team">
  <div class="container">
    <div class="row text-center">
      <div class="col-12">
        <span class="team-badge">🇧🇩 SKY-TIMES.NET পরিবার</span>
        <h1 class="proud-team-title">আমাদের গর্বিত টিম</h1>
        <div class="team-sub">দেশজুড়ে সংবাদ সংগ্রহে নিয়োজিত আমাদের গর্বিত টিম</div>
      </div>
    </div>
  </div>
</section>

<section class="py-2">
  <div class="container">
    <!-- Search Section - ONLY searches reporter name -->
    <div class="row justify-content-center mb-3">
      <div class="col-lg-8 col-xl-7">
        <div class="search-master">
          <div class="search-input-group">
            <span class="search-icon">
              <svg width="18" height="18" fill="currentColor" viewBox="0 0 16 16">
                <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
              </svg>
            </span>
            <input type="text" id="searchReporterInput" class="search-input" placeholder="রিপোর্টারের নাম লিখুন..." autocomplete="off">
            <button id="clearSearchBtn" class="search-clear-btn" style="display: none;">✕</button>
            <button id="searchButton" class="search-submit">খুঁজুন</button>
          </div>
        </div>
      </div>
    </div>

    <!-- Reporter List -->
    <div id="reportersContainer">
      <?php
      $tt_team = showAllttdata("mahamudfutureit_team", "WHERE Status='Show' ORDER BY Position ASC");
      if (!empty($tt_team)) {
          foreach ($tt_team as $the_team) {
              $team_id = $the_team['ID'];
              $tt_reporter = showAllttdata("tt_accounts", "WHERE FamilyPosition='$team_id' ORDER BY UserID ASC");
              if (!empty($tt_reporter)) {
      ?>
                <div class="team-section">
                  <div class="team-name-badge"><?php echo htmlspecialchars($the_team['TeamName'], ENT_QUOTES, 'utf-8'); ?></div>
                  <div class="reporters-grid">
                    <?php foreach ($tt_reporter as $the_reporter) { 
                        // Get raw UserName without htmlspecialchars for search attribute
                        $rawReporterName = $the_reporter['UserName'];
                        $reporterNameForDisplay = htmlspecialchars($rawReporterName, ENT_QUOTES, 'utf-8');
                        $reporterId = $the_reporter['UserID'];
                        
                        $reporterPhone = isset($the_reporter['UserMobile']) ? $the_reporter['UserMobile'] : '';
                        $reporterPhoneFull = $reporterPhone;
                        
                        $hiddenPhone = '';
                        if(!empty($reporterPhone) && strlen($reporterPhone) > 5) {
                            $visiblePart = substr($reporterPhone, 0, -5);
                            $hiddenPhone = $visiblePart . '*****';
                        } elseif(!empty($reporterPhone)) {
                            $hiddenPhone = str_repeat('*', strlen($reporterPhone));
                        } else {
                            $hiddenPhone = 'নাই';
                            $reporterPhoneFull = 'নাই';
                        }
                        
                        $reporterEmail = isset($the_reporter['UserEmail']) ? htmlspecialchars($the_reporter['UserEmail'], ENT_QUOTES, 'utf-8') : 'নাই';
                        
                        $joinDate = isset($the_reporter['JoinDate']) ? date('d M Y', strtotime($the_reporter['JoinDate'])) : 'নাই';
                        if(empty($joinDate) || $joinDate == 'নাই') {
                            $joinDate = isset($the_reporter['CreatedDate']) ? date('d M Y', strtotime($the_reporter['CreatedDate'])) : 'নাই';
                        }
                        
                        $designation = '';
                        if (!empty($the_reporter['UserDesignation'])) {
                            $designation = htmlspecialchars($the_reporter['UserDesignation'], ENT_QUOTES, 'utf-8');
                        } elseif (!empty($the_reporter['UserDesignationEn'])) {
                            $designation = htmlspecialchars($the_reporter['UserDesignationEn'], ENT_QUOTES, 'utf-8');
                        } else {
                            $designation = htmlspecialchars($the_team['TeamName'], ENT_QUOTES, 'utf-8');
                        }
                        $avatarUrl = userAvater($the_reporter['UserID']);
                    ?>
                        <div class="reporter-card" 
                             data-reporter-name="<?php echo strtolower($rawReporterName); ?>" 
                             data-reporter-id="<?php echo $reporterId; ?>" 
                             data-reporter-email="<?php echo $reporterEmail; ?>" 
                             data-reporter-phone-full="<?php echo htmlspecialchars($reporterPhoneFull, ENT_QUOTES, 'utf-8'); ?>"
                             data-reporter-phone-hidden="<?php echo htmlspecialchars($hiddenPhone, ENT_QUOTES, 'utf-8'); ?>"
                             data-reporter-designation="<?php echo $designation; ?>" 
                             data-reporter-avatar="<?php echo $avatarUrl; ?>"
                             data-join-date="<?php echo $joinDate; ?>">
                          <div class="card-img-wrapper">
                            <img src="<?php echo $avatarUrl; ?>" alt="<?php echo $reporterNameForDisplay; ?>" onerror="this.src='https://ui-avatars.com/api/?background=0a66c2&color=fff&bold=true&size=100&name=<?php echo urlencode($reporterNameForDisplay); ?>'">
                          </div>
                          <div class="reporter-name"><?php echo $reporterNameForDisplay; ?></div>
                          <div class="reporter-designation"><?php echo $designation; ?></div>
                          <button class="verify-btn" onclick="showIdCard(this)">🔍 ভেরিফাই</button>
                        </div>
                    <?php } ?>
                  </div>
                </div>
      <?php 
              }
          }
      } else {
          echo '<div class="alert alert-info text-center p-4">কোনো টিম তথ্য পাওয়া যায়নি।</div>';
      }
      ?>
    </div>
    
    <div id="noResultMessage" style="display: none;" class="no-result">
      <div>
        <svg width="40" height="40" fill="currentColor" viewBox="0 0 16 16">
          <path d="M6.5 4.5a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm8.354 8.354-3.5-3.5a.5.5 0 0 0-.708 0l-2.5 2.5a.5.5 0 0 0 .708.708l2.146-2.147 3.146 3.147a.5.5 0 0 0 .708-.708zM8 4a4 4 0 1 1 0 8 4 4 0 0 1 0-8z"/>
        </svg>
        <h5 class="mt-2">কোনো রিপোর্টার খুঁজে পাওয়া যায়নি</h5>
        <p>আপনার সার্চ টার্ম "<span id="searchedKeyword"></span>" এর সাথে মিলে এমন রিপোর্টার নেই।</p>
        <button id="resetSearchBtn" class="btn-primary mt-2">সব রিপোর্টার দেখুন</button>
      </div>
    </div>
  </div>
</section>

<!-- ID Card Modal -->
<div id="idCardModal" class="id-card-modal">
  <div class="id-card-content" id="idCardContent">
    <span class="close-modal" onclick="closeIdCard()">&times;</span>
    <div class="id-card-header">
      <img src="https://sky-times.net/uploads/IMG_20260622_233800.png" alt="Logo">
    </div>
    <div class="id-card-details">
      <div class="id-photo"><img id="modalPhoto" src=""></div>
      <div class="id-info">
        <div class="id-info-item"><span class="id-label">নাম:</span><span class="id-value" id="modalName"></span></div>
        <div class="id-info-item"><span class="id-label">পদবী:</span><span class="id-value" id="modalDesignation"></span></div>
        <div class="id-info-item"><span class="id-label">আইডি নং:</span><span class="id-value" id="modalId"></span></div>
        <div class="id-info-item"><span class="id-label">যোগদান:</span><span class="id-value" id="modalJoinDate"></span></div>
        <div class="id-info-item"><span class="id-label">ইমেইল:</span><span class="id-value" id="modalEmail"></span></div>
        <div class="id-info-item"><span class="id-label">মোবাইল:</span><span><span id="modalPhoneHidden"></span><button class="show-number-btn" onclick="openNumberPopup()">📱 দেখুন</button></span></div>
      </div>
    </div>
    <div class="id-card-footer">✓ ভেরিফাইড সাংবাদিক</div>
    <button class="download-card-btn" onclick="downloadCardAsImage()">📸 কার্ড ডাউনলোড করুন</button>
  </div>
</div>

<!-- Number Popup -->
<div id="numberPopup" class="custom-popup-overlay">
  <div class="custom-popup">
    <h4>🔐 মোবাইল নম্বর ভেরিফাই</h4>
    <p>নিরাপত্তার জন্য শেষ ৪ ডিজিট লিখুন</p>
    <div class="instruction-box">📌 রিপোর্টারের মোবাইল নম্বরের শেষ ৪ ডিজিট লিখুন</div>
    <input type="text" id="verifyDigits" class="digit-input" maxlength="4" placeholder="_ _ _ _">
    <div id="popupError" class="error-message">❌ ভুল নম্বর! আবার চেষ্টা করুন।</div>
    <div class="popup-buttons">
      <button class="popup-confirm" onclick="verifyAndShowNumber()">✅ যাচাই করুন</button>
      <button class="popup-cancel" onclick="closeNumberPopup()">বাতিল</button>
    </div>
  </div>
</div>

<script>
  // ==================== ID CARD & MODAL FUNCTIONS ====================
  let currentFullPhone = '';
  let currentPhoneHiddenSpan = null;

  function showIdCard(button) {
    const card = button.closest('.reporter-card');
    document.getElementById('modalName').innerText = card.querySelector('.reporter-name').innerText;
    document.getElementById('modalDesignation').innerText = card.querySelector('.reporter-designation').innerText;
    document.getElementById('modalId').innerText = 'BCN-' + card.getAttribute('data-reporter-id');
    document.getElementById('modalJoinDate').innerText = card.getAttribute('data-join-date');
    document.getElementById('modalEmail').innerText = card.getAttribute('data-reporter-email');
    document.getElementById('modalPhoto').src = card.querySelector('.card-img-wrapper img').src;
    
    currentFullPhone = card.getAttribute('data-reporter-phone-full');
    currentPhoneHiddenSpan = document.getElementById('modalPhoneHidden');
    currentPhoneHiddenSpan.innerHTML = card.getAttribute('data-reporter-phone-hidden');
    
    const btn = document.querySelector('.show-number-btn');
    if(btn) { btn.innerHTML = '📱 দেখুন'; btn.disabled = false; btn.style.opacity = '1'; }
    
    document.getElementById('idCardModal').style.display = 'flex';
  }

  function closeIdCard() {
    document.getElementById('idCardModal').style.display = 'none';
    closeNumberPopup();
  }

  function openNumberPopup() {
    if(currentFullPhone === 'নাই' || !currentFullPhone) { alert('মোবাইল নম্বর নেই'); return; }
    document.getElementById('numberPopup').style.display = 'flex';
    document.getElementById('verifyDigits').value = '';
    document.getElementById('popupError').style.display = 'none';
  }

  function closeNumberPopup() { document.getElementById('numberPopup').style.display = 'none'; }

  function verifyAndShowNumber() {
    const digits = document.getElementById('verifyDigits').value.trim();
    if(digits.length !== 4 || isNaN(digits)) {
      document.getElementById('popupError').style.display = 'block';
      document.getElementById('popupError').innerHTML = '❌ সঠিক ৪ ডিজিট দিন!';
      return;
    }
    if(currentFullPhone && currentFullPhone !== 'নাই' && digits === currentFullPhone.slice(-4)) {
      currentPhoneHiddenSpan.innerHTML = currentFullPhone;
      const btn = document.querySelector('.show-number-btn');
      if(btn) { btn.innerHTML = '✓ দেখা হয়েছে'; btn.disabled = true; btn.style.opacity = '0.6'; }
      closeNumberPopup();
    } else {
      document.getElementById('popupError').style.display = 'block';
      document.getElementById('popupError').innerHTML = '❌ ভুল নম্বর! শেষ ৪ ডিজিট মিলেনি।';
      document.getElementById('verifyDigits').value = '';
    }
  }

  function downloadCardAsImage() {
    html2canvas(document.getElementById('idCardContent'), { scale: 2.5, backgroundColor: '#ffffff', useCORS: true })
      .then(canvas => {
        let link = document.createElement('a');
        link.download = `ID_Card_${document.getElementById('modalName').innerText}.png`;
        link.href = canvas.toDataURL();
        link.click();
      }).catch(() => alert('ডাউনলোড করতে সমস্যা হয়েছে'));
  }

  window.onclick = function(e) {
    if(e.target == document.getElementById('idCardModal')) closeIdCard();
    if(e.target == document.getElementById('numberPopup')) closeNumberPopup();
  }

  document.getElementById('verifyDigits')?.addEventListener('input', function(e) {
    this.value = this.value.replace(/[^0-9]/g, '').slice(0, 4);
  });

  // ==================== SEARCH FUNCTIONALITY ====================
  // This search ONLY looks at reporter name (data-reporter-name attribute)
  // No relation with team sections, positions, or any other data
  (function() {
    const searchInput = document.getElementById('searchReporterInput');
    const searchBtn = document.getElementById('searchButton');
    const clearBtn = document.getElementById('clearSearchBtn');
    const reportersContainer = document.getElementById('reportersContainer');
    const noResultDiv = document.getElementById('noResultMessage');
    const resetSearchBtn = document.getElementById('resetSearchBtn');
    
    let allReporterCards = [];
    let allTeamSections = [];

    // Cache all reporter cards and team sections
    function cacheAllElements() {
      allReporterCards = Array.from(document.querySelectorAll('#reportersContainer .reporter-card'));
      allTeamSections = Array.from(document.querySelectorAll('#reportersContainer .team-section'));
    }

    // Main search function - ONLY checks reporter name
    function performSearch() {
      const searchTerm = searchInput.value.trim().toLowerCase();
      
      // Show/hide clear button based on input
      if (searchTerm !== '') {
        clearBtn.style.display = 'block';
      } else {
        clearBtn.style.display = 'none';
      }
      
      // If search term is empty, show everything
      if (searchTerm === '') {
        showAllReporters();
        return;
      }
      
      // Filter cards - ONLY by reporter name from data-reporter-name attribute
      const matchedCards = allReporterCards.filter(card => {
        const reporterName = (card.getAttribute('data-reporter-name') || '').toLowerCase();
        return reporterName.includes(searchTerm);
      });
      
      // If no matches found
      if (matchedCards.length === 0) {
        // Hide reporters container, show no result message
        reportersContainer.style.display = 'none';
        noResultDiv.style.display = 'block';
        document.getElementById('searchedKeyword').innerText = searchInput.value;
        return;
      }
      
      // Show results
      reportersContainer.style.display = 'block';
      noResultDiv.style.display = 'none';
      
      // First hide all cards
      allReporterCards.forEach(card => {
        card.style.display = 'none';
      });
      
      // Show only matched cards
      matchedCards.forEach(card => {
        card.style.display = 'block';
      });
      
      // Handle team sections: hide sections that have no visible cards
      allTeamSections.forEach(section => {
        const gridContainer = section.querySelector('.reporters-grid');
        if (gridContainer) {
          const visibleCards = Array.from(gridContainer.querySelectorAll('.reporter-card')).some(card => card.style.display !== 'none');
          section.style.display = visibleCards ? 'block' : 'none';
        }
      });
    }

    // Show all reporters (reset search)
    function showAllReporters() {
      // Show all cards
      allReporterCards.forEach(card => {
        card.style.display = 'block';
      });
      
      // Show all team sections
      allTeamSections.forEach(section => {
        section.style.display = 'block';
      });
      
      // Show container, hide no result message
      reportersContainer.style.display = 'block';
      noResultDiv.style.display = 'none';
      
      // Clear input and hide clear button
      searchInput.value = '';
      clearBtn.style.display = 'none';
    }

    // Event Listeners
    if (searchBtn) {
      searchBtn.addEventListener('click', performSearch);
    }
    
    if (searchInput) {
      searchInput.addEventListener('keyup', function(e) {
        if (e.key === 'Enter') {
          performSearch();
        }
        // If input becomes empty, automatically reset
        if (searchInput.value.trim() === '') {
          showAllReporters();
        }
      });
    }
    
    if (clearBtn) {
      clearBtn.addEventListener('click', function() {
        showAllReporters();
        searchInput.focus();
      });
    }
    
    if (resetSearchBtn) {
      resetSearchBtn.addEventListener('click', function() {
        showAllReporters();
        searchInput.focus();
      });
    }
    
    // Initialize on page load
    if (document.readyState === 'loading') {
      document.addEventListener('DOMContentLoaded', function() {
        cacheAllElements();
        showAllReporters();
      });
    } else {
      cacheAllElements();
      showAllReporters();
    }
  })();
</script>

<?php
require_once('./footer.php');
?>