<?php
require_once __DIR__ . '/db/config.php';

if (isset($_POST['add'])) {
    $name = $_POST['name'];
    $city = $_POST['city'] ?? null;
    $active = isset($_POST['active']) ? 1 : 0;
    $display_order = $_POST['display_order'] ?? 0;

    if (empty($name)) {
        $_SESSION['error_message'] = 'Name is required.';
        header('Location: bni_groups.php');
        exit;
    }

    try {
        $pdo = db();
        $stmt = $pdo->prepare("INSERT INTO bni_groups (name, city, active, display_order) VALUES (:name, :city, :active, :display_order)");
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':city', $city);
        $stmt->bindParam(':active', $active, PDO::PARAM_INT);
        $stmt->bindParam(':display_order', $display_order, PDO::PARAM_INT);
        $stmt->execute();

        $_SESSION['success_message'] = 'BNI Group added successfully!';
    } catch (PDOException $e) {
        $_SESSION['error_message'] = 'Error adding BNI group: ' . $e->getMessage();
    }
}

header('Location: bni_groups.php');
exit;
