<?php
//include constants files
  include('../config/constants.php');
  //check whether the id and image_name value is set or not
  if (isset($_GET['id']) AND isset($_GET['image_name'])) {
    // get the value and delete
    //echo "get data";
    $id= $_GET['id'];
    $image_name = $_GET['image_name'];
    // remove the physical image file if is avalaible
    if ($image_name != "") {
      // image is available. so remove it
      $path = "../images/sub-category/".$image_name;
      //remove the image
      $remove = unlink($path);
      if ($remove =false) {
        // set the session msg
        $_SESSION['remove'] ="<div class='error'>Failed to remove the Category.</div>";
        //redirect to manage category
        header('location:'.SITEURL.'backend/manage-sub-category.php');
        //stop the proccess
        dir();
      }
    }
    //delete data frm db
    //sql query to delete data frm db
    $sql = "DELETE FROM sub_categories WHERE id=$id";
    //execute the query
    $res = mysqli_query($conn, $sql);
    //check whether the data is deleted frm the db or not
    if ($res==true) {
      // redirect to the manage category with a success msg
      $_SESSION['delete'] = "<div class='success'>Category Deleted Successfully.</div>";
      header('location:'.SITEURL.'backend/manage-sub-category.php');
    }else{
      //redirect to manage category with error msg
      $_SESSION['delete'] = "<div class='error'>Failed to Delete the Category .</div>";
      header('location:'.SITEURL.'backend/manage-sub-category.php');
    }
    //redirect to manage category page with a msg

  }else{
    //redirect to the manage-category page
    header('location:'.SITEURL.'backend/manage-sub-category.php');
  }
 ?>
