<!DOCTYPE html>
<html lang="en">
   <head>
      <!-- Required meta tags -->
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <title>Purple Admin</title>
      <!-- plugins:css -->
      <link rel="stylesheet" href="assets/vendors/mdi/css/materialdesignicons.min.css">
      <link rel="stylesheet" href="assets/vendors/ti-icons/css/themify-icons.css">
      <link rel="stylesheet" href="assets/vendors/css/vendor.bundle.base.css">
      <link rel="stylesheet" href="assets/vendors/font-awesome/css/font-awesome.min.css">
      <!-- endinject -->
      <!-- Plugin css for this page -->
      <link rel="stylesheet" href="assets/vendors/font-awesome/css/font-awesome.min.css" />
      <link rel="stylesheet" href="assets/vendors/bootstrap-datepicker/bootstrap-datepicker.min.css">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
      <!-- End plugin css for this page -->
      <!-- inject:css -->
      <!-- endinject -->
      <!-- Layout styles -->
      <link rel="stylesheet" href="assets/css/style.css">
      <!-- End layout styles -->
      <link rel="shortcut icon" href="assets/images/favicon.png" />
   </head>
   <body>
      <div class="container-scroller">
         <!-- partial:partials/_navbar.html -->
         <!-- partial:partials/_navbar.html -->
         <?php
            $title = "Php Header Footer";
            include "header.php";
            ?>
         <!-- partial -->
         <div class="container-fluid page-body-wrapper">
            <!-- partial:partials/_sidebar.html -->
            <?php
               $title = "Php Header Footer";
               include "sidebar.php";
               ?>
            <!-- partial -->
            <!-- partial -->
            <div class="main-panel">
               <div class="content-wrapper">
                  <!-- <div class="page-header">
                     <h3 class="page-title">
                       <span class="page-title-icon bg-gradient-primary text-white me-2">
                         <i class="mdi mdi-home"></i>
                       </span> Dashboard
                     </h3>
                     <nav aria-label="breadcrumb">
                       <ul class="breadcrumb">
                         <li class="breadcrumb-item active" aria-current="page">
                           <span></span>Overview <i class="mdi mdi-alert-circle-outline icon-sm text-primary align-middle"></i>
                         </li>
                       </ul>
                     </nav>
                     </div> -->
<?php
include 'config.php'; // DB conection

$edit_mode = false;
$edit_id = '';
$edit_name = '';

// CREATE
if (isset($_POST['add_category'])) {
    $category = mysqli_real_escape_string($con, $_POST['category']);
    if (!empty($category)) {
        // Check duplicate
        $check = mysqli_query($con, "SELECT * FROM tbl_categories WHERE category_name='$category'");
        if (mysqli_num_rows($check) > 0) {
            echo "<script>alert('Category already exists!');</script>";
        } else {
            mysqli_query($con, "INSERT INTO tbl_categories (category_name) VALUES ('$category')");
        }
    }
}

// UPDATE
if (isset($_POST['update_category'])) {
    $id = intval($_POST['cat_id']);
    $category = mysqli_real_escape_string($con, $_POST['category']);

    $check = mysqli_query($con, "SELECT * FROM tbl_categories WHERE category_name='$category' AND id != $id");
    if (mysqli_num_rows($check) > 0) {
        echo "<script>alert('Category name already exists!');</script>";
    } else {
        mysqli_query($con, "UPDATE tbl_categories SET category_name='$category' WHERE id=$id");
       echo "<script>alert('Category Updated Successfully!'); window.location='add_category.php';</script>";
        exit;
    }
}

// DELETE
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    mysqli_query($con, "DELETE FROM tbl_categories WHERE id=$id");
}

// EDIT MODE SET
if (isset($_GET['edit'])) {
    $edit_id = intval($_GET['edit']);
    $edit_res = mysqli_query($con, "SELECT * FROM tbl_categories WHERE id=$edit_id");
    if ($edit_data = mysqli_fetch_assoc($edit_res)) {
        $edit_mode = true;
        $edit_name = $edit_data['category_name'];
    }
}

// PAGINATION
$limit = 20; 
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$start = ($page - 1) * $limit;
$result = mysqli_query($con, "SELECT * FROM tbl_categories ORDER BY id DESC LIMIT $start, $limit");
$total_rows = mysqli_num_rows(mysqli_query($con, "SELECT * FROM tbl_categories"));
$total_pages = ceil($total_rows / $limit);
?>


<div class="">
    <div class="row g-1">
        <!-- Add / Edit Category -->
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title"><?= $edit_mode ? 'Edit Category' : 'Add Category' ?></h4>
                    <form method="post" class="forms-sample">
                        <div class="form-group">
                            <div class="form-floating">
                            <input type="text" name="category" class="form-control" placeholder="Category"
                                   value="<?= htmlspecialchars($edit_name) ?>" required>
                                   <label for="floatingPassword">Select Category</label>
                                   </div>
                        </div>
                        <?php if ($edit_mode): ?>
                            <input type="hidden" name="cat_id" value="<?= $edit_id ?>">
                            <button type="submit" name="update_category" class="btn btn-primary me-2">Update</button>
                            <a href="add_category.php" class="btn btn-secondary">Cancel</a>
                        <?php else: ?>
                            <button type="submit" name="add_category" class="btn btn-gradient-primary me-2">Submit</button>
                        <?php endif; ?>
                    </form>
                </div>
            </div>
        </div>

        <!-- View tbl_categories -->
        <div class="col-md-8">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Stock</h4>
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                            <tr>
                                <th>SI.No</th>
                                <th>Category Name</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            $i = $start + 1;
                            while ($row = mysqli_fetch_assoc($result)) {
                                echo "<tr>
                                          <td>{$i}</td>
                                          <td>{$row['category_name']}</td>
                                          <td>
                                                <a href='?edit={$row['id']}' class='text-warning' title='Edit'>
                                                   <i class='fas fa-edit'></i>
                                                </a>
                                                <a href='?delete={$row['id']}' class='text-danger' onclick='return confirm(\"Delete this category?\")' title='Delete'>
                                                   <i class='fas fa-trash-alt'></i>
                                                </a>
                                          </td>
                                          </tr>";

                                $i++;
                            }
                            ?>
                            </tbody>
                        </table>
                    </div>
<br>
                    <!-- Pagination -->
                    <nav aria-label="Page navigation example">
                        <ul class="pagination">
                            <?php for ($p = 1; $p <= $total_pages; $p++): ?>
                                <li class="page-item <?php if ($p == $page) echo 'active'; ?>">
                                    <a class="page-link" href="?page=<?= $p ?>"><?= $p ?></a>
                                </li>
                            <?php endfor; ?>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>








                  <div class="col-md-8"></div>
               </div>
               <!-- content-wrapper ends -->
               <!-- partial:partials/_footer.html -->
               <?php
               $title = "Php Header Footer";
               include "footer.php";
               ?>
               <!-- partial -->
            </div>
            <!-- main-panel ends -->
         </div>
         <!-- page-body-wrapper ends -->
      </div>
      <!-- container-scroller -->
      <!-- plugins:js -->
      <script src="assets/vendors/js/vendor.bundle.base.js"></script>
      <!-- endinject -->
      <!-- Plugin js for this page -->
      <script src="assets/vendors/chart.js/chart.umd.js"></script>
      <script src="assets/vendors/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
      <!-- End plugin js for this page -->
      <!-- inject:js -->
      <script src="assets/js/off-canvas.js"></script>
      <script src="assets/js/misc.js"></script>
      <script src="assets/js/settings.js"></script>
      <script src="assets/js/todolist.js"></script>
      <script src="assets/js/jquery.cookie.js"></script>
      <!-- endinject -->
      <!-- Custom js for this page -->
      <script src="assets/js/dashboard.js"></script>
      <!-- End custom js for this page -->
   </body>
</html>