<?php
include_once '../includes/constants.php';
include_once '../includes/functions.php';

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

$msg = "";
$msg1 = isset($_GET['msg']) ? $_GET['msg'] : "";

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = trim($_POST['text1'] ?? '');
    $name = trim($_POST['category'] ?? '');

    if ($action !== "") {
        if ($name === "") {
            $msg = "Please enter a valid Category!";
        } else if ($action === "add") {
            $stmt = $partners->conn->prepare("INSERT INTO partners_category (cat_name) VALUES (?)");
            $stmt->bind_param("s", $name);

            if ($stmt->execute()) {
                $msg = htmlspecialchars($name) . " has been added to the Category list!";
            } else {
                $msg = htmlspecialchars($name) . " already exists in the Category list!";
            }

            $stmt->close();
        }
    }
}

// Fetch all categories
$categories = [];
$result = $partners->conn->query("SELECT cat_name FROM partners_category");
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $categories[] = $row['cat_name'];
    }
    $result->free();
}
?>
<script>
function add_onclick() {
    document.f1.text1.value = "add";
    document.f1.submit();
}
</script>

<form method="post" action="category_manage.php" id="f1" name="f1">
    <input type="hidden" id="text1" name="text1" value="" />
    <table border="0" cellpadding="0" cellspacing="0" width="40%" class="tablebdr">
        <tr>
            <td colspan="5" class="tdhead" align="center"><b>Add or Remove Category</b></td>
        </tr>
        <tr>
            <td colspan="5" class="textred" align="center" height="30"><?= htmlspecialchars($msg) ?></td>
        </tr>
        <tr>
            <td width="2%">&nbsp;</td>
            <td width="45%">Category</td>
            <td width="3%"><b>:</b></td>
            <td width="48%"><input name="category" size="21" id="category" value="" required /></td>
            <td width="2%">&nbsp;</td>
        </tr>
        <tr><td colspan="5" height="15">&nbsp;</td></tr>
        <tr>
            <td colspan="5" align="center">
                <input type="button" value="Add" onclick="return add_onclick()" />
            </td>
        </tr>
        <tr><td colspan="5" height="15">&nbsp;</td></tr>
    </table>
</form>

<br/>

<form name="FormName" action="category_remove.php" method="post">
    <table border="0" cellpadding="0" cellspacing="0" width="40%" class="tablebdr">
        <tr>
            <th colspan="4" class="tdhead"><p>Remove Category</p></th>
        </tr>
        <tr>
            <td colspan="4" class="textred" align="center"><?= htmlspecialchars($msg1) ?></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>Category</td>
            <td align="left">
                <select name="categorycompo" required>
                    <option value="" selected disabled>Choose a Category</option>
                    <?php foreach ($categories as $cat): ?>
                        <option value="<?= htmlspecialchars($cat) ?>"><?= htmlspecialchars($cat) ?></option>
                    <?php endforeach; ?>
                </select>
            </td>
        </tr>
        <tr><td colspan="4" height="15">&nbsp;</td></tr>
        <tr>
            <th colspan="4" align="center">
                <input type="submit" value="Remove" />
            </th>
        </tr>
        <tr><td colspan="4" height="15">&nbsp;</td></tr>
    </table>
</form>

<br/>
