<?php
//include constants files
  include('../config/constants.php');
  //check whether the id and image_name value is set or not
  if (isset($_GET['id']) && isset($_GET['image_name'])) {
    // process to delete
    //1. Get id and image name
    $id = $_GET['id'];
    $image_name = $_GET['image_name'];

    //2. remove the image if available
    //check whether the image is available or not and delete only if available
    if ($image_name != "") {
      // it has img and need to be deleted frm folder
      //get the image path
      $path = "../images/products/".$image_name;
      //remove img file from folder
      $remove = unlink($path);

      //check whether the image is removed or not
      if ($remove==false) {
        // failed to remove image
        $_SESSION['upload' ] = "<div class='error'><Failed To Remove Image File.</div>";
        header('location:'.SITEURL.'backend/manage-products.php');
        dir();//to stop the process of deleting the food
      }

    }
    //3. delete food from the database
    $sql = "DELETE FROM products WHERE id=$id";
    //execute the query
    $res = mysqli_query($conn, $sql);
    //check whether the query executed or not and set session msg
    if($res==true){
      //food deleted
      $_SESSION['delete-cloth'] = "<div class='success'>Cloth Deleted Successfully.</div>";
      header('location:'.SITEURL.'backend/manage-products.php');
    }else {
      //failed to delete the food
      $_SESSION['delete-cloth'] = "<div class='error'>Failed to Delete the Cloth .</div>";
      header('location:'.SITEURL.'backend/manage-products.php');
    }
    //4. redirect to manage food with success msg

  }else {
    //redirect to manage food
    $_SESSION['delete'] = "<div class='error'>Unauthorized Access.</div>";
    header('location:'.SITEURL.'backend/manage-products.php');
  }



 ?>
