<?php
include('../helpers/init.php');
if(isset($_POST['addCategory']))
{
    $category = $_POST['category'];
    $cname = str_replace("'","",$_POST['category']);
	$cname = str_replace('"','\"',$cname);
	$cslug = slug($cname);
    $sub_category = $_POST['title'];
    $name = str_replace("'","",$_POST['title']);
	$name = str_replace('"','\"',$name);
	$slug = slug($name);
    $finalslug = $cslug."-".$slug;
    $error = "";
    /*check for the pre-existed category*/
	$clients = mysqli_query($con,"select * from subcategory where slug='$finalslug'");
	if(num_rows($clients)){
			header('Location:../pages/add_category.php?result=duplicate'); 
	}
    else
    {
    
    $sql = "insert into subcategory(sub_category_name,category,slug) values('$sub_category','$category','$finalslug')";
		if(query($sql)){
			header('Location:../pages/add_category.php?&result=success');
		}else{
			$error = error();
			header('Location:../pages/add_category.php?&result=failer');
		}
    }
	
}

//-----------------------Add Image Edns Here ---------------



if(isset($_GET['action']) && $_GET['action']=="delCat")
{
	$id=$_GET['id'];
    $clients = getWhere("clients","subcategory",$id);
	if(num_rows($clients)){
		$error = " Cannot be deleted because ".num_rows($clients)." Clients are listed in this Sub-Category.";
		header("Location:../pages/add_category.php?result=failer&message=".$error);
	}
    else
    {
		$sql = "delete from subcategory where id=$id";
		if(query($sql))
        {
			header("Location:../pages/add_category.php?result=dsuccess");
		}
            else{
			$error =  error();
			header("Location:../pages/add_category.php?result=failer&message=".$error);
		  }
    }

}

//------------- Delete Category Ends Here-------------------
?>