<?php
// STEP 2F-4B ADMIN DIRECTORY STRONG TITLE FIX


if (!function_exists('app_setting')) {
    function app_setting(PDO $pdo, string $key, string $default = ''): string {
        try {
            $stmt = $pdo->prepare("SELECT setting_value FROM app_settings WHERE setting_key = ? LIMIT 1");
            $stmt->execute([$key]);
            $value = $stmt->fetchColumn();
            return ($value !== false && $value !== null && $value !== '') ? (string)$value : $default;
        } catch (Throwable $e) {
            return $default;
        }
    }
}

$adminDirectoryTitle = app_setting($pdo, 'admin_label_directory', '<?php echo h($adminDirectoryTitle); ?>');

$pageTitle = $adminDirectoryTitle;
require_once __DIR__ . '/bootstrap.php';
require_admin();
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'save') {
        $id = (int)($_POST['id'] ?? 0);
        $letter = strtoupper(trim($_POST['letter'] ?? ''));
        $name = trim($_POST['name'] ?? '');
        $phone = trim($_POST['phone'] ?? '');
        $sort = (int)($_POST['sort_order'] ?? 0);
        if ($id > 0) {
            $stmt = $pdo->prepare('UPDATE directory SET letter=?, name=?, phone=?, sort_order=? WHERE id=?');
            $stmt->execute([$letter, $name, $phone, $sort, $id]);
        } else {
            $stmt = $pdo->prepare('INSERT INTO directory (letter, name, phone, sort_order) VALUES (?, ?, ?, ?)');
            $stmt->execute([$letter, $name, $phone, $sort]);
        }
        $message = 'Directory saved.';
    } elseif ($action === 'delete') {
        $id = (int)($_POST['id'] ?? 0);
        $pdo->prepare('DELETE FROM directory WHERE id=?')->execute([$id]);
        $message = 'Entry deleted.';
    }
}
$stmt = $pdo->query('SELECT * FROM directory ORDER BY sort_order ASC, letter ASC, name ASC');
$rows = $stmt->fetchAll();
require_once __DIR__ . '/includes/header.php';
?>
<main><section class="section"><div class="container"><div class="panel">
<h1><?php echo h($adminDirectoryTitle); ?></h1>
<?php if ($message): ?><div class="notice-good"><?php echo h($message); ?></div><?php endif; ?>
<h2><?php echo h($adminDirectoryTitle); ?></h2>
<form method="post"><input type="hidden" name="action" value="save"><input type="hidden" name="id" value="0">
<div class="grid-2"><div class="form-row"><label>Letter</label><input name="letter" maxlength="2" required></div><div class="form-row"><label>Sort Order</label><input name="sort_order" type="number" value="0"></div></div>
<div class="grid-2"><div class="form-row"><label>Name</label><input name="name" required></div><div class="form-row"><label>Phone</label><input name="phone"></div></div>
<button class="btn btn-primary" type="submit">Add Brother</button></form>
<hr style="border-color:var(--border);margin:1.5rem 0;">
<h2><?php echo h($adminDirectoryTitle); ?></h2>
<?php foreach ($rows as $row): ?>
<form method="post" class="card" style="margin-bottom:.8rem;"><input type="hidden" name="action" value="save"><input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>">
<div class="grid-4"><div class="form-row"><label>Letter</label><input name="letter" value="<?php echo h($row['letter']); ?>"></div><div class="form-row"><label>Name</label><input name="name" value="<?php echo h($row['name']); ?>"></div><div class="form-row"><label>Phone</label><input name="phone" value="<?php echo h($row['phone']); ?>"></div><div class="form-row"><label>Sort</label><input name="sort_order" type="number" value="<?php echo (int)$row['sort_order']; ?>"></div></div>
<div class="actions"><button class="btn btn-primary" type="submit">Save</button></form><form method="post" onsubmit="return confirm('Delete this entry?');"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?php echo (int)$row['id']; ?>"><button class="btn btn-danger" type="submit">Delete</button></form></div>
<?php endforeach; ?>
</div></div></section></main>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
