/home/hdwebsolution/www/ns-interior/admin-pages/edit-category.php
<?php include('site-header.php'); ?>
<?php include('left-nevigation.php'); ?>
<script src="ckeditor/ckeditor.js"></script>

<main id="playground">
<?php include('site-top-header.php'); ?>

<?php
$msg = '';
$id = $_GET['edit'] ?? 0;

if (isset($_POST['sub'])) {
    $category_name = $_POST['category_name'];
    $cate_desc = $_POST['cate_desc'];
    $about_cat = $_POST['about_cat'];

    // Existing image/PDF file names
    $himage = $_POST['himage'];
    $hsidebar_image = $_POST['hsidebar_image'];
    $hpdf = $_POST['hpdf'];

    // Uploaded files
    $cat_img_name = $_FILES['att']['name'];
    $sidebar_img_name = $_FILES['sidebar_img']['name'];
    $catalogue_pdf_name = $_FILES['catalogue_pdf']['name'];

    $cat_tmp = $_FILES['att']['tmp_name'];
    $sidebar_tmp = $_FILES['sidebar_img']['tmp_name'];
    $catalogue_pdf_tmp = $_FILES['catalogue_pdf']['tmp_name'];

    $new_cat_image = $himage;
    $new_sidebar_image = $hsidebar_image;
    $new_catalogue_pdf = $hpdf;

    // Handle new category image
    if (!empty($cat_img_name)) {
        $cat_ext = pathinfo($cat_img_name, PATHINFO_EXTENSION);
        $new_cat_image = rand() . ".$cat_ext";
        move_uploaded_file($cat_tmp, "../category-image/$new_cat_image");
        if (!empty($himage) && file_exists("../category-image/$himage")) {
            unlink("../category-image/$himage");
        }
    }

    // Handle new sidebar image
    if (!empty($sidebar_img_name)) {
        $sidebar_ext = pathinfo($sidebar_img_name, PATHINFO_EXTENSION);
        $new_sidebar_image = rand() . ".$sidebar_ext";
        move_uploaded_file($sidebar_tmp, "../category-image/$new_sidebar_image");
        if (!empty($hsidebar_image) && file_exists("../category-image/$hsidebar_image")) {
            unlink("../category-image/$hsidebar_image");
        }
    }

    // Handle new catalogue PDF
    if (!empty($catalogue_pdf_name)) {
        $pdf_ext = pathinfo($catalogue_pdf_name, PATHINFO_EXTENSION);
        if (strtolower($pdf_ext) === 'pdf') {
            $new_catalogue_pdf = rand() . ".pdf";
            move_uploaded_file($catalogue_pdf_tmp, "../category-image/$new_catalogue_pdf");
            if (!empty($hpdf) && file_exists("../category-image/$hpdf")) {
                unlink("../category-image/$hpdf");
            }
        }
    }

    // Update DB
    $sql_up = "UPDATE `tbl_category` SET 
        `cate_name`='$category_name',
        `category_image`='$new_cat_image',
        `cate_desc`='$cate_desc',
        `sidebar_image`='$new_sidebar_image',
        `catalogue`='$new_catalogue_pdf',
        `about_cat`='$about_cat'
        WHERE cat_id=$id";

    if (mysqli_query($link, $sql_up)) {
        $msg = "<p style='color:green;'>Category Updated!</p>";
    } else {
        $msg = "<p style='color:red;'>Failed to update category. Try again!<br>" . mysqli_error($link) . "</p>";
    }
}

// Fetch existing data
if ($id) {
    $sel = mysqli_query($link, "SELECT * FROM tbl_category WHERE cat_id=$id");
    $arr = mysqli_fetch_assoc($sel);
}
?>

<section id="page-title" class="row">
  <div class="col-md-8">
    <h1>Category</h1>
    <p class="lead">Update your Category below</p>
  </div>
</section>

<div class="container-fluid">
  <form method="post" enctype="multipart/form-data">
    <div class="row">
      <div class="col-md-12">
        <a href="update-category.php" class="btn btn-primary">Back</a>
      </div>
      <div class="col-md-6">
        <div class="form-group">
          <label class="text-danger"><?= $msg; ?></label><br>
          <label>Product Category</label>
          <input type="text" class="form-control" name="category_name" value="<?= $arr['cate_name']; ?>" placeholder="Enter Your Category Name">

          <!--<label>Description</label>-->
          <!--<input type="text" class="form-control" name="cate_desc" value="<?= $arr['cate_desc']; ?>" placeholder="Enter Category briefing">-->

          <label>About Category</label>
          <textarea class="form-control" name="about_cat" id="about_cat" rows="6"><?= $arr['about_cat']; ?></textarea>
        </div>

         Category Image 
        <div class="form-group">
          <label>Image for Category Page</label><br>
          <input type="hidden" name="himage" value="<?= $arr['category_image']; ?>">
          <?php if (!empty($arr['category_image'])): ?>
            <img src="../category-image/<?= $arr['category_image']; ?>" width="100"><br><br>
          <?php endif; ?>
          <input type="file" class="form-control" name="att">
        </div>

        <!-- Sidebar Image -->
        <!--<div class="form-group">-->
        <!--  <label>Image for Sidebar</label><br>-->
        <!--  <input type="hidden" name="hsidebar_image" value="<?= $arr['sidebar_image']; ?>">-->
        <!--  <?php if (!empty($arr['sidebar_image'])): ?>-->
        <!--    <img src="../category-image/<?= $arr['sidebar_image']; ?>" width="100"><br><br>-->
        <!--  <?php endif; ?>-->
        <!--  <input type="file" class="form-control" name="sidebar_img">-->
        <!--</div>-->

        <!-- Catalogue PDF -->
      <!--  <div class="form-group">-->
      <!--    <label>Catalogue (PDF)</label><br>-->
      <!--    <input type="hidden" name="hpdf" value="<?= $arr['catalogue']; ?>">-->
      <!--    <?php if (!empty($arr['catalogue'])): ?>-->
      <!--      <a href="../category-image/<?= $arr['catalogue']; ?>" target="_blank">View Current PDF</a><br><br>-->
      <!--    <?php endif; ?>-->
      <!--    <input type="file" class="form-control" name="catalogue_pdf" accept="application/pdf">-->
      <!--  </div>-->
      <!--</div>-->

      <div class="col-md-12">
        <div class="form-group">
          <input type="submit" class="btn btn-primary" name="sub" value="SUBMIT">
        </div>
      </div>
    </div>
  </form>
</div>

<script>
  CKEDITOR.replace('about_cat');
</script>

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