<?php include('site-header.php'); ?>
 

  <?php include('left-nevigation.php'); ?>

  
 

  <main id="playground">

 

 <?php include('site-top-header.php'); ?>



   

    <?php

    if(isset($_POST['del']))

    {

        $id = $_POST['id'];

        if(mysqli_query($link,"DELETE FROM `tbl_certification` WHERE id=$id"))

        {

            $msg= "<p style='color:red;'>Delete Successfully</p>";

        }

    }

    ?>

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set limit per page
$limit = 200;

// Get current page from URL, default to 1
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Count total records
$totalResult = mysqli_query($link, "SELECT COUNT(*) as total FROM tbl_certification");
$totalRow = mysqli_fetch_assoc($totalResult);
$total = $totalRow['total'];
$totalPages = ceil($total / $limit);

// Fetch paginated records
$ReadSql = "SELECT * FROM tbl_certification ORDER BY id DESC LIMIT $offset, $limit";
$sel = mysqli_query($link, $ReadSql);
if (!$sel) {
    die('Query Error: ' . mysqli_error($link));
}

$sn = $offset + 1;
?>

<section class="panel panel-primary">
  <header class="panel-heading" style="
    display: flex;
    justify-content: center;
    gap: 50px;
    align-items: center;
">
    <h3 class="panel-title">Existing Certification  </h3>
    <input type="search" placeholder="Enter Certificate No.." name="" id="search_certificate" style="
    color: #000000;
    padding: 8px 14px;
    border-radius: 50px;
    outline: none;
    border: 0.2px solid black;
">
  </header>

  <table class="table table-hover table-responsive" width="100%">
    <thead>
      <tr>
        <th style="color:black;">S. No</th>
        <th style="color:black;">Certificate Number</th>
        <th style="color:black;">Company Name</th>
        <th style="color:black;">Address</th>
        <th style="color:black;">Scheme</th>
        <th style="color:black;">Valid Upto</th>
        <th style="color:black;">Created On</th>
        <th style="color:black;">Updated On</th>
        <th style="color:black;">Actions</th>
      </tr>
    </thead>
    <tbody id="search_result">
      <?php while ($arr = mysqli_fetch_assoc($sel)): ?>
        <tr>
          <td><?= $sn++ ?></td>
          <td><?= htmlspecialchars($arr['certificationNumber']) ?></td>
          <td><?= htmlspecialchars($arr['companyName']) ?></td>
          <td><?= htmlspecialchars(mb_strimwidth($arr['address'], 0, 60, "...")) ?></td>
          <td><?= htmlspecialchars($arr['scheme']) ?></td>
          <td><?= htmlspecialchars($arr['validTill']) ?></td>
          <td><?= htmlspecialchars($arr['createdOn']) ?></td>
          <td><?= htmlspecialchars($arr['updatedOn']) ?></td>
          <td>
            <a class="btn btn-success" href="add-products.php?edit=<?= $arr['id'] ?>">Edit</a>
            <form method="post" style="display:inline;">
              <input type="hidden" name="id" value="<?= $arr['id'] ?>">
              <input type="submit" name="del" value="Delete" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this certification?');">
            </form>
          </td>
        </tr>
      <?php endwhile; ?>
    </tbody>
  </table>

  <!-- Pagination UI -->
  <div class="text-center">
    <ul class="pagination">
      <?php if ($page > 1): ?>
        <li><a href="?page=<?= $page - 1 ?>">&laquo; Prev</a></li>
      <?php endif; ?>
      
      <?php for ($i = 1; $i <= $totalPages; $i++): ?>
        <li class="<?= ($i == $page) ? 'active' : '' ?>">
          <a href="?page=<?= $i ?>"><?= $i ?></a>
        </li>
      <?php endfor; ?>

      <?php if ($page < $totalPages): ?>
        <li><a href="?page=<?= $page + 1 ?>">Next &raquo;</a></li>
      <?php endif; ?>
    </ul>
  </div>
</section>


     
    <!-- / container-fluid -->

 <?php include('site-footer.php'); ?>

 <script>
   
    $("#search_certificate").on("input",function () {

      var search_val=$(this).val();
   
      

     $.ajax({
      type: "post",
      url: "search_ajax.php",
      data: {search_val:search_val},
      dataType: "html",
      success: function (data) {
        $("#search_result").html(data)
        if (search_val=="") {
    
          setTimeout(() => {
            window.location.reload();          
          }, 1500);
        }
      }
     });
    })
 
 </script>