<?php
include('../includes/connection.php');
$id = $_GET['id'];
$sql = "SELECT * FROM category_add WHERE id='$id'";
$result = mysqli_query($con, $sql);
$data = mysqli_fetch_assoc($result);
?>

<?php
include('../includes/sidebar.php');
include('../includes/header.php');
?>

<!-- [ Main Content ] start -->
<div class="pc-container">
  <div class="pc-content">
    <!-- [ breadcrumb ] start -->
    <div class="page-header">
      <div class="page-block">
        <div class="page-header-title">
          <h5 class="mb-0 font-medium">Edit Category</h5>
        </div>
        <ul class="breadcrumb">
          <li class="breadcrumb-item"><a href="../dashboard/index.html">Home</a></li>
          <li class="breadcrumb-item"><a href="javascript:void(0)">Other</a></li>
          <li class="breadcrumb-item" aria-current="page">Edit Category</li>
        </ul>
      </div>
    </div>
    <!-- [ breadcrumb ] end -->

    <!-- [ Main Content ] start -->
    <div class="grid grid-cols-12 gap-x-6">
      <div class="col-span-12">
        <div class="card">
          <div class="card-header">
            <h5>Edit Category Details</h5>
          </div>
          <div class="card-body">
            <div class="container">
              <div class="row justify-content-center align-items-center">
                <div class="col-sm-6">
                  <form action="" method="post" enctype="multipart/form-data">
                    
                    <div class="mb-3 text-center">
                      <img src="<?php echo $data['image']; ?>" alt="Category Image" style="height:100px;width:100px;border-radius:10px;">
                    </div>

                    <div class="mb-3">
                      <label class="form-label">Image</label>
                      <input type="file" class="form-control" name="image">
                      <input type="hidden" name="old_image" value="<?php echo $data['image']; ?>">
                    </div>

                    <div class="mb-3">
                      <label class="form-label">Name</label>
                      <input type="text" class="form-control" name="name" value="<?php echo $data['name']; ?>">
                    </div>

                    <div class="mb-3">
                      <label class="form-label">Status</label>
                      <select name="status" class="form-control">
                        <option value="">--Select--</option>
                        <option value="1" <?php if ($data['status'] == '1') echo 'selected'; ?>>Active</option>
                        <option value="0" <?php if ($data['status'] == '0') echo 'selected'; ?>>Inactive</option>
                      </select>
                    </div>

                    <div class="modal-footer">
                      <button type="submit" class="btn btn-primary" name="submit">Update</button>
                      <a href="category.php" class="btn btn-secondary">Cancel</a>
                    </div>
                  </form>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div> 
    </div>
    <!-- [ Main Content ] end -->
  </div>
</div>
<!-- [ Main Content ] end -->

<?php include('../includes/footer.php'); ?>

<?php
if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $status = $_POST['status'];
  $filename = $_FILES['image']['name'];
  $tempname = $_FILES['image']['tmp_name'];
  $old_image = $_POST['old_image'];
  
  $folder = "../images/" . $filename;

  if ($filename == "") {
    $folder = $old_image;
  }

  $sql = "UPDATE category_add SET image='$folder', name='$name', status='$status' WHERE id='$id'";
  $result = mysqli_query($con, $sql);

  if ($result) {
    if ($filename != "") {
      move_uploaded_file($tempname, $folder);
    }
    echo "<script>alert('Category updated successfully');window.location.href='category.php';</script>";
  } else {
    echo "<script>alert('Update failed! Please try again.');</script>";
  }
}
?>
