<META NAME="robots" CONTENT="noindex,nofollow">


<?php 

include 'config.php';

session_start();



if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {

  header("Location: login.php");

  exit;

}



$filter = $_GET['filter'] ?? 'PRESENT';

$type = $_GET['type'] ?? 'ALL';

$search = $_GET['search'] ?? '';

$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;

$limit = 15;

$offset = ($page - 1) * $limit;



$params = [];

$count_sql = "SELECT COUNT(*) FROM ipo_post WHERE 1 ";

$sql = "SELECT * FROM ipo_post WHERE 1 ";



if ($filter !== 'ALL' && in_array($filter, ['PRESENT', 'NEW', 'PAST'])) {

  $count_sql .= "AND status = ? ";

  $sql .= "AND status = ? ";

  $params[] = $filter;

}



if ($type !== 'ALL' && in_array($type, ['M', 'S'])) {

  $count_sql .= "AND type = ? ";

  $sql .= "AND type = ? ";

  $params[] = $type;

}



if (!empty($search)) {

  $count_sql .= "AND LOWER(c_name) LIKE ? ";

  $sql .= "AND LOWER(c_name) LIKE ? ";

  $params[] = "%" . strtolower($search) . "%";

}



$sql .= "ORDER BY id DESC LIMIT $limit OFFSET $offset";



$count_stmt = $pdo->prepare($count_sql);

$count_stmt->execute(array_slice($params, 0));

$total_rows = $count_stmt->fetchColumn();

$total_pages = ceil($total_rows / $limit);



$stmt = $pdo->prepare($sql);

$stmt->execute($params);

?>

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <title>IPO Admin Panel</title>

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

  <style>
    :root {
      --primary: #4f46e5;
      --primary-light: #e0e7ff;
      --primary-dark: #3730a3;
      --surface: #ffffff;
      --surface-alt: #f8fafc;
      --border: #e2e8f0;
      --text: #1e293b;
      --text-muted: #64748b;
      --success: #059669;
      --success-light: #ecfdf5;
      --warning: #d97706;
      --warning-light: #fffbeb;
      --danger: #dc2626;
      --danger-light: #fef2f2;
      --info: #0284c7;
      --info-light: #f0f9ff;
      --radius: 12px;
      --radius-sm: 8px;
      --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
      --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
      --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.08), 0 2px 4px -2px rgba(0,0,0,0.04);
    }

    * { box-sizing: border-box; }

    body {
      font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
      background: #f1f5f9;
      color: var(--text);
      min-height: 100vh;
    }

    /* ── Top Bar ── */
    .top-bar {
      background: var(--surface);
      border-bottom: 1px solid var(--border);
      padding: 16px 0;
      position: sticky;
      top: 0;
      z-index: 100;
      box-shadow: var(--shadow-sm);
    }
    .top-bar .inner {
      max-width: 1280px;
      margin: 0 auto;
      padding: 0 24px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-wrap: wrap;
      gap: 12px;
    }
    .brand {
      display: flex;
      align-items: center;
      gap: 10px;
      font-weight: 700;
      font-size: 20px;
      color: var(--primary);
    }
    .brand-icon {
      width: 36px;
      height: 36px;
      background: var(--primary);
      color: #fff;
      border-radius: 10px;
      display: grid;
      place-items: center;
      font-size: 18px;
      flex-shrink: 0;
    }
    .btn-logout {
      background: var(--danger-light);
      color: var(--danger);
      border: 1px solid #fecaca;
      padding: 8px 16px;
      border-radius: var(--radius-sm);
      font-weight: 500;
      font-size: 13px;
      text-decoration: none;
      transition: all 0.2s;
    }
    .btn-logout:hover {
      background: var(--danger);
      color: #fff;
      border-color: var(--danger);
    }

    /* ── Main Container ── */
    .main-wrap {
      max-width: 1280px;
      margin: 0 auto;
      padding: 24px;
    }

    /* ── Quick Actions ── */
    .quick-actions {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      margin-bottom: 20px;
    }
    .qa-btn {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      padding: 10px 18px;
      border-radius: var(--radius-sm);
      font-size: 13px;
      font-weight: 500;
      text-decoration: none;
      transition: all 0.2s;
      border: 1px solid var(--border);
      background: var(--surface);
      color: var(--text);
      box-shadow: var(--shadow-sm);
    }
    .qa-btn:hover {
      border-color: var(--primary);
      color: var(--primary);
      box-shadow: var(--shadow);
      transform: translateY(-1px);
    }
    .qa-btn.qa-primary {
      background: var(--primary);
      color: #fff;
      border-color: var(--primary);
    }
    .qa-btn.qa-primary:hover {
      background: var(--primary-dark);
      border-color: var(--primary-dark);
      color: #fff;
    }

    /* ── Controls Row ── */
    .controls-row {
      display: flex;
      flex-wrap: wrap;
      gap: 12px;
      align-items: center;
      margin-bottom: 20px;
    }

    /* ── Filter Pills ── */
    .filter-pills {
      display: flex;
      background: var(--surface);
      border-radius: var(--radius-sm);
      border: 1px solid var(--border);
      overflow: hidden;
      box-shadow: var(--shadow-sm);
    }
    .filter-pills button {
      padding: 9px 18px;
      border: none;
      background: transparent;
      font-size: 13px;
      font-weight: 500;
      color: var(--text-muted);
      cursor: pointer;
      transition: all 0.2s;
      border-right: 1px solid var(--border);
    }
    .filter-pills button:last-child { border-right: none; }
    .filter-pills button:hover { background: var(--surface-alt); color: var(--text); }
    .filter-pills button.active {
      background: var(--primary);
      color: #fff;
    }

    /* ── Type Toggles ── */
    .type-toggles {
      display: flex;
      background: var(--surface);
      border-radius: var(--radius-sm);
      border: 1px solid var(--border);
      overflow: hidden;
      box-shadow: var(--shadow-sm);
    }
    .type-toggles .btn-check { display: none; }
    .type-toggles label {
      padding: 9px 18px;
      font-size: 13px;
      font-weight: 500;
      color: var(--text-muted);
      cursor: pointer;
      transition: all 0.2s;
      border-right: 1px solid var(--border);
      border-radius: 0 !important;
      border: none;
      border-right: 1px solid var(--border);
      background: transparent;
    }
    .type-toggles label:last-of-type { border-right: none; }
    .type-toggles label:hover { background: var(--surface-alt); color: var(--text); }
    .type-toggles .btn-check:checked + label {
      background: var(--text);
      color: #fff;
    }

    /* ── Search ── */
    .search-box {
      display: flex;
      align-items: center;
      background: var(--surface);
      border: 1px solid var(--border);
      border-radius: var(--radius-sm);
      overflow: hidden;
      box-shadow: var(--shadow-sm);
      margin-left: auto;
      min-width: 260px;
      transition: border-color 0.2s, box-shadow 0.2s;
    }
    .search-box:focus-within {
      border-color: var(--primary);
      box-shadow: 0 0 0 3px var(--primary-light);
    }
    .search-box input {
      border: none;
      outline: none;
      padding: 9px 14px;
      font-size: 13px;
      flex: 1;
      background: transparent;
      color: var(--text);
    }
    .search-box input::placeholder { color: var(--text-muted); }
    .search-box button {
      border: none;
      background: transparent;
      padding: 9px 14px;
      cursor: pointer;
      color: var(--text-muted);
      transition: color 0.2s;
    }
    .search-box button:hover { color: var(--primary); }

    /* ── Stats bar ── */
    .stats-bar {
      font-size: 13px;
      color: var(--text-muted);
      margin-bottom: 16px;
      font-weight: 500;
    }
    .stats-bar span {
      background: var(--primary-light);
      color: var(--primary);
      padding: 2px 10px;
      border-radius: 20px;
      font-weight: 600;
      font-size: 12px;
    }

    /* ── Table Card ── */
    .table-card {
      background: var(--surface);
      border-radius: var(--radius);
      border: 1px solid var(--border);
      box-shadow: var(--shadow);
      overflow: hidden;
    }
    .table-card table {
      width: 100%;
      border-collapse: collapse;
      margin: 0;
    }
    .table-card thead th {
      background: var(--surface-alt);
      padding: 14px 16px;
      font-size: 11px;
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.05em;
      color: var(--text-muted);
      border-bottom: 1px solid var(--border);
      white-space: nowrap;
    }
    .table-card tbody td {
      padding: 14px 16px;
      font-size: 14px;
      border-bottom: 1px solid var(--border);
      vertical-align: middle;
    }
    .table-card tbody tr:last-child td { border-bottom: none; }
    .table-card tbody tr { transition: background 0.15s; }
    .table-card tbody tr:hover { background: var(--surface-alt); }

    .company-cell {
      display: flex;
      align-items: center;
      gap: 10px;
    }
    .logo-img {
      height: 32px;
      width: 32px;
      object-fit: contain;
      border-radius: 8px;
      border: 1px solid var(--border);
      padding: 2px;
      background: #fff;
    }
    .company-name {
      font-weight: 500;
      color: var(--text);
    }
    .id-badge {
      background: var(--surface-alt);
      color: var(--text-muted);
      font-size: 12px;
      font-weight: 600;
      padding: 3px 10px;
      border-radius: 6px;
      font-variant-numeric: tabular-nums;
    }
    .date-cell {
      font-variant-numeric: tabular-nums;
      color: var(--text-muted);
      font-size: 13px;
    }

    /* ── Action buttons ── */
    .action-btns {
      display: flex;
      gap: 6px;
      flex-wrap: nowrap;
    }
    .act-btn {
      padding: 6px 12px;
      border-radius: 6px;
      font-size: 12px;
      font-weight: 500;
      border: 1px solid transparent;
      cursor: pointer;
      text-decoration: none;
      display: inline-flex;
      align-items: center;
      gap: 4px;
      transition: all 0.2s;
      white-space: nowrap;
    }
    .act-view {
      background: var(--info-light);
      color: var(--info);
      border-color: #bae6fd;
    }
    .act-view:hover { background: var(--info); color: #fff; border-color: var(--info); }
    .act-edit {
      background: var(--warning-light);
      color: var(--warning);
      border-color: #fed7aa;
    }
    .act-edit:hover { background: var(--warning); color: #fff; border-color: var(--warning); }
    .act-delete {
      background: var(--danger-light);
      color: var(--danger);
      border-color: #fecaca;
    }
    .act-delete:hover { background: var(--danger); color: #fff; border-color: var(--danger); }

    /* ── Pagination ── */
    .pagination-wrap {
      display: flex;
      justify-content: center;
      padding: 20px 0 8px;
    }
    .pagination-wrap .pagination {
      gap: 4px;
      flex-wrap: wrap;
      justify-content: center;
    }
    .pagination-wrap .page-item .page-link {
      border: 1px solid var(--border);
      border-radius: var(--radius-sm);
      color: var(--text-muted);
      font-size: 13px;
      font-weight: 500;
      padding: 8px 14px;
      background: var(--surface);
      transition: all 0.2s;
    }
    .pagination-wrap .page-item .page-link:hover {
      border-color: var(--primary);
      color: var(--primary);
      background: var(--primary-light);
    }
    .pagination-wrap .page-item.active .page-link {
      background: var(--primary);
      border-color: var(--primary);
      color: #fff;
    }
    .pagination-wrap .page-item.disabled .page-link {
      opacity: 0.4;
      pointer-events: none;
    }

    .empty-state {
      text-align: center;
      padding: 60px 20px;
      color: var(--text-muted);
    }
    .empty-state-icon { font-size: 40px; margin-bottom: 12px; }
    .empty-state-text { font-size: 15px; font-weight: 500; }

    /* ── Responsive ── */
    @media (max-width: 768px) {
      .main-wrap { padding: 16px; }
      .controls-row { flex-direction: column; align-items: stretch; }
      .search-box { margin-left: 0; min-width: unset; }
      .filter-pills, .type-toggles { width: 100%; }
      .filter-pills button, .type-toggles label { flex: 1; text-align: center; padding: 10px 8px; }
      .quick-actions { gap: 8px; }
      .qa-btn { padding: 8px 14px; font-size: 12px; }
      .table-card thead th, .table-card tbody td { padding: 10px 12px; font-size: 12px; }
      .act-btn { padding: 5px 8px; font-size: 11px; }
      .act-btn .act-label { display: none; }
      .brand { font-size: 17px; }
    }
    @media (max-width: 480px) {
      .logo-img { height: 26px; width: 26px; }
    }
  </style>

  <script>

    function updateType(type) {

      const url = new URL(window.location.href);

      url.searchParams.set('type', type);

      url.searchParams.delete('search');

      window.location.href = url.toString();

    }

    function updateFilter(filter) {

      const url = new URL(window.location.href);

      url.searchParams.set('filter', filter);

      url.searchParams.delete('search');

      window.location.href = url.toString();

    }

    let deleteTimer = null;



    function confirmDelete(id) {

      if (confirm("Are you sure you want to delete this IPO?")) {

        const deleteButton = document.getElementById("delete-btn-" + id);

        deleteButton.disabled = true;

        deleteButton.innerText = "⏳ 5s...";



        let countdown = 5;

        const originalText = "🗑️ Delete";



        deleteTimer = setInterval(() => {

          countdown--;

          deleteButton.innerText = `⏳ ${countdown}s...`;



          if (countdown <= 0) {

            clearInterval(deleteTimer);

            const secondConfirm = confirm("Final confirmation to delete?");

            if (secondConfirm) {

              window.location.href = 'deleteipo.php?id=' + id;

            } else {

              deleteButton.disabled = false;

              deleteButton.innerText = originalText;

            }

          }

        }, 1000);

      }

    }



  </script>

</head>

<body>

<!-- Top Bar -->
<div class="top-bar">
  <div class="inner">
    <div class="brand">
      <div class="brand-icon">📊</div>
      IPO Admin
    </div>
    <a href="logout.php" class="btn-logout">🚪 Logout</a>
  </div>
</div>

<div class="main-wrap">

  <!-- Quick Actions -->
  <div class="quick-actions">
    <a href="add.php" class="qa-btn qa-primary">➕ Add New IPO</a>
    <a href="sequence.php" class="qa-btn">🔃 Change Sequence</a>
    <a href="IPOCalandar.php" class="qa-btn">📅 IPO Calendar</a>
    <a href="https://sharemarketipo.in/ImageGenerator/LogoMakerForIPOAPP/index.php" class="qa-btn">🖼️ Logo Maker</a>
    <a href="bunny.php" class="qa-btn">🐰 Logo Upload BunnyCDN</a>
    <a href="https://cipherteam.in/admin3/scraper_servers.php" class="qa-btn">⏱️ Allotment Cron</a>
  </div>

  <!-- Controls Row -->
  <div class="controls-row">
    <div class="filter-pills">
      <button class="<?= $filter == 'ALL' ? 'active' : '' ?>" onclick="updateFilter('ALL')">All</button>
      <button class="<?= $filter == 'PRESENT' ? 'active' : '' ?>" onclick="updateFilter('PRESENT')">Present</button>
      <button class="<?= $filter == 'NEW' ? 'active' : '' ?>" onclick="updateFilter('NEW')">New</button>
      <button class="<?= $filter == 'PAST' ? 'active' : '' ?>" onclick="updateFilter('PAST')">Past</button>
    </div>

    <div class="type-toggles">
      <input type="radio" class="btn-check" name="type" id="typeAll" value="ALL" autocomplete="off" <?= $type == 'ALL' ? 'checked' : '' ?> onclick="updateType('ALL')">
      <label for="typeAll">All</label>

      <input type="radio" class="btn-check" name="type" id="typeMain" value="M" autocomplete="off" <?= $type == 'M' ? 'checked' : '' ?> onclick="updateType('M')">
      <label for="typeMain">Mainline</label>

      <input type="radio" class="btn-check" name="type" id="typeSME" value="S" autocomplete="off" <?= $type == 'S' ? 'checked' : '' ?> onclick="updateType('S')">
      <label for="typeSME">SME</label>
    </div>

    <form method="GET" class="search-box" role="search">
      <input type="hidden" name="filter" value="<?= htmlspecialchars($filter) ?>">
      <input type="hidden" name="type" value="<?= htmlspecialchars($type) ?>">
      <input type="search" name="search" value="<?= htmlspecialchars($search) ?>" placeholder="Search company...">
      <button type="submit">🔍</button>
    </form>
  </div>

  <!-- Stats -->
  <div class="stats-bar">
    Showing <span><?= $total_rows ?></span> IPO<?= $total_rows !== 1 ? 's' : '' ?> found
  </div>

  <!-- Table -->
  <div class="table-card">
    <div class="table-responsive">
      <table>
        <thead>
          <tr>
            <th>ID</th>
            <th>Company</th>
            <th>IPO Open</th>
            <th>IPO Close</th>
            <th>Actions</th>
          </tr>
        </thead>
        <tbody>
          <?php if ($stmt->rowCount() > 0): ?>
            <?php while ($row = $stmt->fetch()): ?>
            <tr>
              <td><span class="id-badge">#<?= $row['id'] ?></span></td>
              <td>
                <div class="company-cell">
                  <?php if (!empty($row['img'])): ?>
                    <img src="<?= htmlspecialchars($row['img']) ?>" alt="Logo" class="logo-img">
                  <?php endif; ?>
                  <span class="company-name"><?= htmlspecialchars($row['c_name']) ?></span>
                </div>
              </td>
              <td><span class="date-cell"><?= htmlspecialchars($row['ipo_open']) ?></span></td>
              <td><span class="date-cell"><?= htmlspecialchars($row['ipo_close']) ?></span></td>
              <td>
                <div class="action-btns">
                  <a href="viewipo.php?id=<?= $row['id'] ?>" class="act-btn act-view">👁️ <span class="act-label">View</span></a>
                  <a href="editipo.php?id=<?= $row['id'] ?>" class="act-btn act-edit">✏️ <span class="act-label">Edit</span></a>
                  <button id="delete-btn-<?= $row['id'] ?>" onclick="confirmDelete(<?= $row['id'] ?>)" class="act-btn act-delete">🗑️ <span class="act-label">Delete</span></button>
                </div>
              </td>
            </tr>
            <?php endwhile; ?>
          <?php else: ?>
            <tr>
              <td colspan="5">
                <div class="empty-state">
                  <div class="empty-state-icon">📭</div>
                  <div class="empty-state-text">No IPOs found.</div>
                </div>
              </td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>

  <?php if ($total_pages > 1): ?>
  <nav class="pagination-wrap">
    <ul class="pagination">
      <li class="page-item <?= $page == 1 ? 'disabled' : '' ?>">
        <a class="page-link" href="?page=1&filter=<?= urlencode($filter) ?>&type=<?= urlencode($type) ?>">« First</a>
      </li>
      <li class="page-item <?= $page == 1 ? 'disabled' : '' ?>">
        <a class="page-link" href="?page=<?= $page - 1 ?>&filter=<?= urlencode($filter) ?>&type=<?= urlencode($type) ?>">‹ Prev</a>
      </li>

      <?php

        $start = max(1, $page - 2);

        $end = min($total_pages, $page + 2);



        if ($start > 1) {

          echo '<li class="page-item disabled"><span class="page-link">...</span></li>';

        }



        for ($i = $start; $i <= $end; $i++): ?>

          <li class="page-item <?= $i == $page ? 'active' : '' ?>">

            <a class="page-link" href="?page=<?= $i ?>&filter=<?= urlencode($filter) ?>&type=<?= urlencode($type) ?>"><?= $i ?></a>

          </li>

      <?php endfor;



        if ($end < $total_pages) {

          echo '<li class="page-item disabled"><span class="page-link">...</span></li>';

        }

      ?>

      <li class="page-item <?= $page == $total_pages ? 'disabled' : '' ?>">
        <a class="page-link" href="?page=<?= $page + 1 ?>&filter=<?= urlencode($filter) ?>&type=<?= urlencode($type) ?>">Next ›</a>
      </li>
      <li class="page-item <?= $page == $total_pages ? 'disabled' : '' ?>">
        <a class="page-link" href="?page=<?= $total_pages ?>&filter=<?= urlencode($filter) ?>&type=<?= urlencode($type) ?>">Last »</a>
      </li>
    </ul>