<?php
require_once __DIR__.'/../inc/config.php';
require_once __DIR__.'/../inc/db.php';
require_once __DIR__.'/../inc/helpers.php';
require_once __DIR__.'/../inc/csrf.php';

// ... require_once ...
if(empty($_SESSION['admin_id'])){ header('Location: login.php'); exit; } // 1. Semak login dahulu
if ($_SESSION['admin_role'] !== 'admin') { // 2. Kemudian baru semak role
    exit('Akses disekat. Anda tiada kebenaran untuk melihat halaman ini.');
}

if(empty($_SESSION['admin_id'])){ header('Location: login.php'); exit; }

$feedback_msg = '';
$feedback_type = 'error';

// Logik untuk simpan tajuk utama bahagian
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_title'])) {
    if (csrf_verify()) {
        set_setting('PROBLEM_TITLE', $_POST['problem_title'] ?? '');
        $feedback_msg = 'Tajuk bahagian telah berjaya disimpan.';
        $feedback_type = 'success';
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && csrf_verify()) {
    $action = $_POST['action'] ?? '';
    $id = (int)($_POST['id'] ?? 0);

    if ($action === 'delete' && $id > 0) {
        db()->prepare("DELETE FROM problems WHERE id=?")->execute([$id]);
        $feedback_msg = 'Masalah telah berjaya dipadam.';
        $feedback_type = 'success';
    } elseif ($action === 'save') {
        $icon = trim($_POST['icon'] ?? '');
        $headline = trim($_POST['headline'] ?? '');
        $text = trim($_POST['text'] ?? '');
        $active = !empty($_POST['active']) ? 1 : 0;

        if ($headline && $text) {
            if ($id > 0) {
                db()->prepare("UPDATE problems SET icon=?, headline=?, text=?, active=? WHERE id=?")->execute([$icon, $headline, $text, $active, $id]);
                $feedback_msg = 'Masalah berjaya dikemaskini.';
            } else {
                db()->prepare("INSERT INTO problems(icon, headline, text, active) VALUES(?,?,?,?)")->execute([$icon, $headline, $text, $active]);
                $feedback_msg = 'Masalah baru berjaya ditambah.';
            }
            $feedback_type = 'success';
        } else {
            $feedback_msg = 'Tajuk dan Teks diperlukan.';
        }
    }
}

$problems = db()->query("SELECT * FROM problems ORDER BY id ASC")->fetchAll();
$editing_problem = null;
if (isset($_GET['edit'])) {
    $edit_id = (int)$_GET['edit'];
    $st = db()->prepare("SELECT * FROM problems WHERE id=?");
    $st->execute([$edit_id]);
    $editing_problem = $st->fetch();
}
?>
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../public/assets/css/style.css"><title>Urus Masalah</title>
<style>
    .admin-wrapper { display: flex; align-items: flex-start; gap: 24px; } .admin-sidebar { width: 220px; flex-shrink: 0; } .admin-content { flex-grow: 1; } .feedback { padding: 12px; border-radius: 12px; margin-bottom: 20px; } .feedback.success { background-color: #dcfce7; color: #166534; } .feedback.error { background-color: #fee2e2; color: #991b1b; } table { width: 100%; border-collapse: collapse; } th, td { padding: 10px; text-align: left; border-bottom: 1px solid #e5e7eb; } th { background-color: #f8fafc; } .form-group label { display: block; margin-bottom: 16px; font-weight: 600; } .form-group input, .form-group textarea { width: 100%; padding: 10px; border: 1px solid #d1d5db; border-radius: 8px; margin-top: 6px; box-sizing: border-box; } .form-check { margin-top: 10px; }
</style>
</head><body><div class="container">
<div class="admin-wrapper">
    <aside class="admin-sidebar"><?php require_once '_menu.php'; ?></aside>
    <main class="admin-content">
        <h1>Urus Masalah Pelanggan</h1>
        
        <div class="card">
    <h2>Tetapan Bahagian</h2>
    <form action="problems.php" method="post">
        <?= csrf_field() ?>
        <div class="form-group">
            <label>Tajuk Utama Bahagian Masalah
                <input name="problem_title" value="<?= e(setting('PROBLEM_TITLE', 'Adakah Ini Masalah Anda di Dapur?')) ?>">
            </label>
        </div>
        <button type="submit" name="save_title" class="cta">Simpan Tajuk</button>
    </form>
</div>
        
        <?php if ($feedback_msg): ?><div class="feedback <?= $feedback_type ?>"><?= e($feedback_msg) ?></div><?php endif; ?>

        <div class="card">
            <h2>Senarai Masalah</h2>
            <table>
                <tr><th>ID</th><th>Ikon</th><th>Tajuk</th><th>Status</th><th>Tindakan</th></tr>
                <?php foreach($problems as $p): ?>
                <tr>
                    <td><?= $p['id'] ?></td>
                    <td><?= e($p['icon']) ?></td>
                    <td style="max-width:300px;"><?= e($p['headline']) ?></td>
                    <td><?= $p['active'] ? 'Aktif' : 'Tidak Aktif' ?></td>
                    <td>
                        <a href="?edit=<?= $p['id'] ?>" class="cta" style="font-size:12px; padding:6px 10px; background-color:#4f46e5;">Edit</a>
                        <form action="problems.php" method="post" style="display:inline-block;" onsubmit="return confirm('Anda pasti mahu padam ini?');">
                            <?= csrf_field() ?><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?= $p['id'] ?>">
                            <button type="submit" style="background-color:#ef4444; color:white; border:none; border-radius: 8px; padding:6px 10px; font-size:12px; cursor:pointer;">Padam</button>
                        </form>
                    </td>
                </tr>
                <?php endforeach; ?>
            </table>
        </div>

        <div class="card">
            <h2><?= $editing_problem ? 'Edit Masalah' : 'Tambah Masalah Baru' ?></h2>
            <form action="problems.php" method="post">
                <?= csrf_field() ?><input type="hidden" name="action" value="save"><input type="hidden" name="id" value="<?= $editing_problem['id'] ?? 0 ?>">
                <div class="form-group"><label>Ikon (cth: 😬, 😖, ⏰)<input name="icon" value="<?= e($editing_problem['icon'] ?? '') ?>"></label></div>
                <div class="form-group"><label>Tajuk<input name="headline" value="<?= e($editing_problem['headline'] ?? '') ?>" required></label></div>
                <div class="form-group"><label>Teks<textarea name="text" rows="4" required><?= e($editing_problem['text'] ?? '') ?></textarea></label></div>
                <div class="form-check"><label><input type="checkbox" name="active" value="1" <?= ($editing_problem['active'] ?? 1) ? 'checked' : '' ?>> Aktifkan</label></div>
                <button type="submit" class="cta" style="margin-top:20px;"><?= $editing_problem ? 'Simpan Perubahan' : 'Tambah Masalah' ?></button>
                <?php if ($editing_problem): ?><a href="problems.php" style="margin-left:10px;">Batal</a><?php endif; ?>
            </form>
        </div>
    </main>
</div>
</div></body></html>