<?php
ob_start();
include_once '../includes/constants.php';
include_once '../includes/functions.php';
include_once '../includes/allstripslashes.php';

$partners = new partners;
$mysqli = $partners->connection($host, $user, $pass, $db);

$act = $_REQUEST['act'] ?? '';
$page = $_REQUEST['page'] ?? '';
$msg = '';

switch ($act) {
    case 'Add a Category':
        $category = trim($_REQUEST['category'] ?? '');

        if (!empty($category)) {
            $stmt = $mysqli->prepare("SELECT * FROM partners_category WHERE cat_name = ?");
            $stmt->bind_param("s", $category);
            $stmt->execute();
            $result = $stmt->get_result();

            if ($result->num_rows > 0) {
                $msg = "Category $category already exists";
            } else {
                $stmt = $mysqli->prepare("INSERT INTO partners_category (cat_name) VALUES (?)");
                $stmt->bind_param("s", $category);
                if ($stmt->execute()) {
                    $msg = "Category " . stripslashes($category) . " added";
                    // copy("ads/ad_template.html", "ads/$category.html"); // Uncomment if needed
                } else {
                    $msg = "Error: " . $stmt->error;
                }
            }

            $stmt->close();
        }
        break;

    case 'Update':
        $result = $mysqli->query("SELECT cat_id, cat_name FROM partners_category");
        $i = $del = $mod = 0;

        while ($row = $result->fetch_object()) {
            $nameKey = "name$i";
            $idKey = "id$i";

            $name = trim($_REQUEST[$nameKey] ?? '');
            $id = (int)($_REQUEST[$idKey] ?? 0);

            if (empty($name)) {
                $stmt = $mysqli->prepare("DELETE FROM partners_category WHERE cat_id = ?");
                $stmt->bind_param("i", $id);
                $stmt->execute();
                $stmt->close();

                // @unlink("ads/$row->cat_name.html"); // Uncomment if needed
                $del++;
            } else {
                if ($name !== $row->cat_name) {
                    $stmt = $mysqli->prepare("SELECT * FROM partners_category WHERE cat_name = ?");
                    $stmt->bind_param("s", $name);
                    $stmt->execute();
                    $checkResult = $stmt->get_result();

                    if ($checkResult->num_rows === 0) {
                        $updateStmt = $mysqli->prepare("UPDATE partners_category SET cat_name = ? WHERE cat_id = ?");
                        $updateStmt->bind_param("si", $name, $id);
                        $updateStmt->execute();
                        $updateStmt->close();

                        // @rename("ads/$row->cat_name.html", "ads/$name.html"); // Uncomment if needed
                        $mod++;
                    }

                    $stmt->close();
                }
            }

            $i++;
        }

        $msg = "$del category(s) removed and $mod category(s) updated";
        break;
}

// Redirect with message
header("Location: index.php?Act=category&msg=" . urlencode($msg) . "&page=" . urlencode($page));
exit;
?>
