167 lines
7.4 KiB
PHP
167 lines
7.4 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
// Simple handling of form submissions
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['action']) && $_POST['action'] === 'add') {
|
|
$keywords = $_POST['keywords'] ?? '';
|
|
$answer = $_POST['answer'] ?? '';
|
|
if ($keywords && $answer) {
|
|
$stmt = db()->prepare("INSERT INTO faqs (keywords, answer) VALUES (?, ?)");
|
|
$stmt->execute([$keywords, $answer]);
|
|
}
|
|
} elseif (isset($_POST['action']) && $_POST['action'] === 'delete') {
|
|
$id = $_POST['id'] ?? 0;
|
|
if ($id) {
|
|
$stmt = db()->prepare("DELETE FROM faqs WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
}
|
|
} elseif (isset($_POST['action']) && $_POST['action'] === 'update_settings') {
|
|
$token = $_POST['telegram_token'] ?? '';
|
|
$stmt = db()->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('telegram_token', ?) ON DUPLICATE KEY UPDATE setting_value = ?");
|
|
$stmt->execute([$token, $token]);
|
|
}
|
|
header("Location: admin.php");
|
|
exit;
|
|
}
|
|
|
|
$faqs = db()->query("SELECT * FROM faqs ORDER BY created_at DESC")->fetchAll();
|
|
$messages = db()->query("SELECT * FROM messages ORDER BY created_at DESC LIMIT 50")->fetchAll();
|
|
|
|
$telegramToken = '';
|
|
$stmt = db()->query("SELECT setting_value FROM settings WHERE setting_key = 'telegram_token'");
|
|
$row = $stmt->fetch();
|
|
if ($row) {
|
|
$telegramToken = $row['setting_value'];
|
|
}
|
|
?>
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Admin - FAQ Manager</title>
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
<style>
|
|
.btn-delete {
|
|
background: #dc3545;
|
|
color: white;
|
|
border: none;
|
|
padding: 0.25rem 0.5rem;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
}
|
|
.btn-add {
|
|
background: #212529;
|
|
color: white;
|
|
border: none;
|
|
padding: 0.5rem 1rem;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
margin-top: 1rem;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="bg-animations">
|
|
<div class="blob blob-1"></div>
|
|
<div class="blob blob-2"></div>
|
|
<div class="blob blob-3"></div>
|
|
</div>
|
|
<div class="admin-container">
|
|
<div style="display: flex; justify-content: space-between; align-items: center;">
|
|
<h1>FAQ Manager</h1>
|
|
<a href="index.php" class="admin-link">Back to Chat</a>
|
|
</div>
|
|
|
|
<div class="admin-card" style="background: rgba(255, 255, 255, 0.6); padding: 2rem; border-radius: 20px; border: 1px solid rgba(255, 255, 255, 0.5); margin-bottom: 2.5rem; box-shadow: 0 10px 30px rgba(0,0,0,0.05);">
|
|
<h3 style="margin-top: 0; margin-bottom: 1.5rem; font-weight: 700;">Telegram Bot Settings</h3>
|
|
<form method="POST">
|
|
<input type="hidden" name="action" value="update_settings">
|
|
<div class="form-group">
|
|
<label for="telegram_token">Telegram Bot Token</label>
|
|
<input type="text" name="telegram_token" id="telegram_token" class="form-control" placeholder="Paste your bot token from @BotFather" value="<?= htmlspecialchars($telegramToken) ?>">
|
|
</div>
|
|
<p style="font-size: 0.85em; color: #555; margin-top: 0.5rem;">
|
|
Webhook URL: <code>https://<?= $_SERVER['HTTP_HOST'] ?>/api/telegram_webhook.php</code>
|
|
</p>
|
|
<button type="submit" class="btn-add" style="background: #0088cc; color: white; border: none; padding: 0.8rem 1.5rem; border-radius: 12px; cursor: pointer; font-weight: 600; width: 100%; transition: all 0.3s ease;">Save Token</button>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="admin-card" style="background: rgba(255, 255, 255, 0.6); padding: 2rem; border-radius: 20px; border: 1px solid rgba(255, 255, 255, 0.5); margin-bottom: 2.5rem; box-shadow: 0 10px 30px rgba(0,0,0,0.05);">
|
|
<h3 style="margin-top: 0; margin-bottom: 1.5rem; font-weight: 700;">Add New FAQ</h3>
|
|
<form method="POST">
|
|
<input type="hidden" name="action" value="add">
|
|
<div class="form-group">
|
|
<label for="keywords">Keywords (comma separated)</label>
|
|
<input type="text" name="keywords" id="keywords" class="form-control" placeholder="e.g. price, cost, dollar" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="answer">Answer</label>
|
|
<textarea name="answer" id="answer" class="form-control" rows="3" placeholder="Enter the answer..." required></textarea>
|
|
</div>
|
|
<button type="submit" class="btn-add" style="background: #212529; color: white; border: none; padding: 0.8rem 1.5rem; border-radius: 12px; cursor: pointer; font-weight: 600; width: 100%; transition: all 0.3s ease;">Save FAQ</button>
|
|
</form>
|
|
</div>
|
|
|
|
<h3>Existing FAQs</h3>
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>Keywords</th>
|
|
<th>Answer</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($faqs as $faq): ?>
|
|
<tr>
|
|
<td><?= htmlspecialchars($faq['keywords']) ?></td>
|
|
<td><?= htmlspecialchars($faq['answer']) ?></td>
|
|
<td>
|
|
<form method="POST" style="display:inline;" onsubmit="return confirm('Delete this FAQ?');">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="id" value="<?= $faq['id'] ?>">
|
|
<button type="submit" class="btn-delete">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
|
|
<h3 style="margin-top: 3rem; margin-bottom: 1rem;">Recent Chat History (Last 50)</h3>
|
|
<div style="overflow-x: auto; background: rgba(255, 255, 255, 0.4); padding: 1rem; border-radius: 12px; border: 1px solid rgba(255, 255, 255, 0.3);">
|
|
<table class="table" style="width: 100%;">
|
|
<thead>
|
|
<tr>
|
|
<th style="width: 15%;">Time</th>
|
|
<th style="width: 35%;">User Message</th>
|
|
<th style="width: 50%;">AI Response</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($messages)): ?>
|
|
<tr>
|
|
<td colspan="3" style="text-align: center; color: #777;">No messages yet.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($messages as $msg): ?>
|
|
<tr>
|
|
<td style="white-space: nowrap; font-size: 0.85em; color: #555;"><?= htmlspecialchars($msg['created_at']) ?></td>
|
|
<td style="background: rgba(255, 255, 255, 0.3); border-radius: 8px; padding: 8px;"><?= htmlspecialchars($msg['user_message']) ?></td>
|
|
<td style="background: rgba(255, 255, 255, 0.5); border-radius: 8px; padding: 8px;"><?= htmlspecialchars($msg['ai_response']) ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|