<?php
session_start();
if(!isset($_SESSION['id']) || $_SESSION['id']=='')
{
	http_response_code(401);
    echo json_encode('Authentication Problem');
    exit;
}
if(!isset($_SESSION['user_type']) || ($_SESSION['user_type'] != 'admin'))
{
	//http_response_code(401);
   // echo json_encode('Authentication Problem');
   // exit;
}

//include_once "../ichat/admin/route.php";
require_once 'database.php';
/*if(isset($_SESSION[$elhash]) && $_SESSION[$elhash] != "") { 
    $random_hash = $_SESSION[$elhash];
} else {
    $random_hash = uniqid();
    $_SESSION[$elhash] = $random_hash;
}
*/

class delete extends database
{
    public function index($id)
    {
        $data['cl_deleted_at']=date('Y-m-d H:i:s');
        $where['id'] = $id;
        return $this->update('category', $data, $where);
    }
	public function check($id)
    {
        
        $where['cat_id'] = $id;
        return $this->fetchAll('subcategory', $where);
    }
  
}

if(!isset($_GET['id']) || !is_numeric($_GET['id']))
{
    http_response_code(400);
    echo json_encode('Category Not Found');
    exit;
}
$id = $_GET['id'];

$update = new delete();

$msg = "";
$chk= $update->check($id);
if($chk)
{
	http_response_code(400);
    echo json_encode('Category is connected to some subcategories');
    exit;
}
	
$res = $update->index($id);
if($res)
{
    http_response_code(200);
    echo json_encode('Category Deleted Successfully');
    exit;
}
                
http_response_code(500);
echo json_encode('Something went wrong');
exit;
?>