<?php
require_once('header.php');

if(!isset($_REQUEST['id']) || !is_numeric($_REQUEST['id'])) {
    header('location: logout.php');
    exit;
}

$id = (int)$_REQUEST['id'];

$statement = $pdo->prepare("SELECT * FROM tbl_top_category WHERE id=?");
$statement->execute([$id]);
$category = $statement->fetch(PDO::FETCH_ASSOC);

if(!$category) {
    header('location: logout.php');
    exit;
}

if(!empty($category['banner'])) {
    $banner_path = 'assets/images/category/' . $category['banner'];

    if(file_exists($banner_path)) {
        unlink($banner_path);
    }
}

$statement = $pdo->prepare("DELETE FROM tbl_top_category WHERE id=?");
$statement->execute([$id]);

header('location: top-category.php');
exit;
?>