<?php
session_start();

// PENGUNCI HALAMAN: Hanya yang login dan memiliki role 'admin' yang bisa masuk
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    die("Akses Ditolak. Halaman ini khusus Administrator.");
}

$host = 'localhost';
$user = 'majapahi_omnich';
$pass = 'A1b2c3d4.,@';
$db   = 'majapahi_omnich';
$conn = new mysqli($host, $user, $pass, $db);

$pesan = "";

// --- PROSES CRUD (CREATE, UPDATE, DELETE) ---
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $action = $_POST['action'];
    
    if ($action == 'add') {
        $title = $conn->real_escape_string($_POST['title']);
        $content = $conn->real_escape_string($_POST['content']);
        $conn->query("INSERT INTO quick_replies (title, content) VALUES ('$title', '$content')");
        $pesan = "<div class='alert alert-success'>Balasan cepat berhasil ditambahkan!</div>";
    } 
    elseif ($action == 'edit') {
        $id = (int)$_POST['id'];
        $title = $conn->real_escape_string($_POST['title']);
        $content = $conn->real_escape_string($_POST['content']);
        $conn->query("UPDATE quick_replies SET title='$title', content='$content' WHERE id=$id");
        $pesan = "<div class='alert alert-info'>Balasan cepat berhasil diperbarui!</div>";
    } 
    elseif ($action == 'delete') {
        $id = (int)$_POST['id'];
        $conn->query("DELETE FROM quick_replies WHERE id=$id");
        $pesan = "<div class='alert alert-danger'>Balasan cepat berhasil dihapus!</div>";
    }
}

// AMBIL SEMUA DATA
$replies = $conn->query("SELECT * FROM quick_replies ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manajemen Quick Replies | Admin A&S Group</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background-color: #f0f2f5; font-family: -apple-system, sans-serif; }
        .header-admin { background-color: #008069; color: white; padding: 15px 20px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
    </style>
</head>
<body>

<div class="header-admin d-flex justify-content-between align-items-center">
    <h5 class="mb-0">⚙️ Manajemen Quick Replies</h5>
    <div>
        <a href="dashboard.php" class="btn btn-sm btn-light fw-bold text-success me-2">KEMBALI KE CHAT</a>
        <a href="logout.php" class="btn btn-sm btn-danger fw-bold">KELUAR</a>
    </div>
</div>

<div class="container mt-4">
    <?= $pesan ?>
    
    <div class="card border-0 shadow-sm">
        <div class="card-header bg-white d-flex justify-content-between align-items-center py-3">
            <h5 class="mb-0 fw-bold text-dark">Daftar Template Balasan</h5>
            <button class="btn btn-success btn-sm fw-bold" data-bs-toggle="modal" data-bs-target="#modalAdd">+ TAMBAH TEMPLATE</button>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0 align-middle">
                    <thead class="table-light">
                        <tr>
                            <th width="5%" class="text-center">ID</th>
                            <th width="20%">Judul Tombol</th>
                            <th width="55%">Isi Balasan (Gunakan [NAMA] untuk auto-nama)</th>
                            <th width="20%" class="text-center">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if($replies->num_rows > 0): ?>
                            <?php while($row = $replies->fetch_assoc()): ?>
                                <tr>
                                    <td class="text-center"><?= $row['id'] ?></td>
                                    <td><span class="badge bg-secondary"><?= htmlspecialchars($row['title']) ?></span></td>
                                    <td><small><?= nl2br(htmlspecialchars($row['content'])) ?></small></td>
                                    <td class="text-center">
                                        <button class="btn btn-sm btn-outline-primary py-0 px-2" data-bs-toggle="modal" data-bs-target="#modalEdit<?= $row['id'] ?>">Edit</button>
                                        <form action="" method="POST" class="d-inline" onsubmit="return confirm('Yakin ingin menghapus template ini?');">
                                            <input type="hidden" name="action" value="delete">
                                            <input type="hidden" name="id" value="<?= $row['id'] ?>">
                                            <button type="submit" class="btn btn-sm btn-outline-danger py-0 px-2">Hapus</button>
                                        </form>
                                    </td>
                                </tr>

                                <div class="modal fade" id="modalEdit<?= $row['id'] ?>" tabindex="-1">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <form action="" method="POST">
                                                <div class="modal-header bg-light">
                                                    <h5 class="modal-title">Edit Template</h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                </div>
                                                <div class="modal-body">
                                                    <input type="hidden" name="action" value="edit">
                                                    <input type="hidden" name="id" value="<?= $row['id'] ?>">
                                                    <div class="mb-3">
                                                        <label class="form-label fw-bold">Judul Tombol (Pendek)</label>
                                                        <input type="text" name="title" class="form-control" value="<?= htmlspecialchars($row['title']) ?>" required>
                                                    </div>
                                                    <div class="mb-3">
                                                        <label class="form-label fw-bold">Isi Pesan Balasan</label>
                                                        <textarea name="content" class="form-control" rows="5" required><?= htmlspecialchars($row['content']) ?></textarea>
                                                        <small class="text-muted">Ketik <b>[NAMA]</b> agar sistem otomatis menyebut nama pelanggan.</small>
                                                    </div>
                                                </div>
                                                <div class="modal-footer border-0">
                                                    <button type="submit" class="btn btn-primary w-100 fw-bold">SIMPAN PERUBAHAN</button>
                                                </div>
                                            </form>
                                        </div>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr><td colspan="4" class="text-center py-4 text-muted">Belum ada template balasan cepat.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="modalAdd" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form action="" method="POST">
                <div class="modal-header bg-success text-white">
                    <h5 class="modal-title">Tambah Template Baru</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <input type="hidden" name="action" value="add">
                    <div class="mb-3">
                        <label class="form-label fw-bold">Judul Tombol (Max 2-3 Kata)</label>
                        <input type="text" name="title" class="form-control" placeholder="Contoh: 📌 Info Rekening" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold">Isi Pesan Balasan</label>
                        <textarea name="content" class="form-control" rows="5" placeholder="Ketik pesan panjang di sini..." required></textarea>
                        <small class="text-muted">Ketik <b>[NAMA]</b> agar sistem otomatis menyebut nama pelanggan.</small>
                    </div>
                </div>
                <div class="modal-footer border-0">
                    <button type="submit" class="btn btn-success w-100 fw-bold">SIMPAN TEMPLATE</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
