<?php
session_start();

if(!isset($_SESSION['user_id'])){
    header("Location: ../login.php");
    exit();
}

include_once "../config/database.php";
include_once "../includes/header.php";
include_once "../includes/sidebar.php";
?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<?php

$search="";

$where="";

if(isset($_GET['search'])){

$search=mysqli_real_escape_string($conn,$_GET['search']);

$where=" WHERE category_name LIKE '%$search%' ";

}

$limit=10;

$page=isset($_GET['page'])?(int)$_GET['page']:1;

if($page<1){
$page=1;
}

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

$total=mysqli_fetch_assoc(mysqli_query($conn,"SELECT COUNT(*) total FROM categories $where"));

$totalRecords=$total['total'];

$totalPages=ceil($totalRecords/$limit);

$query=mysqli_query($conn,"
SELECT *
FROM categories
$where
ORDER BY id DESC
LIMIT $start,$limit
");

?>

<div class="container-fluid">

<div class="d-flex justify-content-between align-items-center flex-wrap mb-4">

<div>

<h3 class="fw-bold mb-1">

Categories

</h3>

<p class="text-muted mb-0">

Manage Product Categories

</p>

</div>

<a href="add.php" class="btn btn-primary rounded-3">

<i class="fa fa-plus me-2"></i>

Add Category

</a>

</div>

<div class="card border-0 shadow rounded-4">

<div class="card-body">

<form method="GET">

<div class="row">

<div class="col-lg-4 col-md-6 col-12">

<input
type="text"
name="search"
class="form-control"
placeholder="Search Category..."
value="<?=htmlspecialchars($search)?>">

</div>

<div class="col-lg-2 col-md-3 col-6">

<button class="btn btn-dark w-100">

Search

</button>

</div>

<div class="col-lg-2 col-md-3 col-6">

<a href="index.php" class="btn btn-secondary w-100">

Reset

</a>

</div>

</div>

</form>

<div class="table-responsive mt-4">

<table class="table table-hover align-middle">

<thead class="table-light">

<tr>

<th width="70">

ID

</th>

<th>

Category Name

</th>

<th>

Created

</th>

<th width="170">

Action

</th>

</tr>

</thead>

<tbody>

<?php

if(mysqli_num_rows($query)>0){

while($row=mysqli_fetch_assoc($query)){

?>

<tr>

<td>

<?=$row['id']?>

</td>

<td>

<?=htmlspecialchars($row['category_name'])?>

</td>

<td>

<?=date("d M Y",strtotime($row['created_at']))?>

</td>

<td>

<a
href="edit.php?id=<?=$row['id']?>"
class="btn btn-warning btn-sm">

<i class="fa fa-edit"></i>

</a>

<a
href="delete.php?id=<?=$row['id']?>"
class="btn btn-danger btn-sm"
onclick="return confirm('Delete this category?')">

<i class="fa fa-trash"></i>

</a>

</td>

</tr>

<?php

}

}else{

?>

<tr>

<td colspan="4" class="text-center">

No Categories Found

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

<?php

if($totalPages>1){

?>

<nav>

<ul class="pagination justify-content-end">

<?php

for($i=1;$i<=$totalPages;$i++){

?>

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

<a
class="page-link"
href="?page=<?=$i?>&search=<?=$search?>">

<?=$i?>

</a>

</li>

<?php

}

?>

</ul>

</nav>

<?php } ?>

</div>

</div>

</div>

<?php include_once "../includes/footer.php"; ?>

</div>