<?php include "../parts/header.php"; ?>
<?php $activeMenu = "testimonial"; ?>
<?php include "../parts/sidepanel.php"; ?>



<?php

    include("../opendb.php");

    $sql = "SELECT * FROM testimonials";
    $result = mysqli_query($conn, $sql);

?>
<?php if (isset($_GET['success']) && $_GET['success'] == 1): ?>
    <div class="alert alert-success">Testimonial added successfully!</div>
<?php endif; ?>

<?php if (isset($_GET['error'])): ?>
    <div class="alert alert-danger">Error: <?php echo htmlspecialchars($_GET['error']); ?></div>
<?php endif; ?>

<h4 class="page-title">TESTIMONIALS</h4>
<div class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header d-flex" style="justify-content: space-between;">
                <div class="card-title">Testimonials</div>
                <div class="btn btn-sm btn-primary"><a href="add.php" class="btn btn-sm btn-primary">Add Testimonial</a></div>
            </div>
            <div class="card-body">
                <table class="table table-head-bg-primary mt-4">
                    <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Name</th>
                            <th scope="col">Role</th>
                            <th scope="col">Rating</th>
                            <th scope="col">Comments</th>
                            <th scope="col">Image</th>
                            <th scope="col">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                            if($result){
                                $i = 1;
                                while($row = mysqli_fetch_assoc($result)){
                        ?>
                        <tr>
                            <td><?= $i++ ?></td>
                            <td><?= $row['name'] ?></td>
                            <td><?= $row['role'] ?></td>
                            <td><?= $row['rating'] ?></td>
                            <td id="editor"><?= nl2br($row['comments']); ?></td>
                            <td>
                                <?php
                                $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
                                            "://{$_SERVER['HTTP_HOST']}/mentor/admin/"; 
                                if (!empty($row['image'])) { ?>
                                    <img src="<?= $baseUrl . 'uploads/' . htmlspecialchars($row['image']); ?>" alt="img" width="50">
                                <?php } else { ?>
                                    <span class="text-muted">No image</span>
                                <?php } ?>
                            </td>
                            <td>
                                <a href="edit.php?id=<?= $row['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
                                <a href="delete.php?id=<?= $row['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure?')">Delete</a>
                            </td>
                        </tr>
                        <?php 
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
					

<?php include "../parts/footer.php"; ?>
<script src="https://cdn.jsdelivr.net/npm/quill@2.0.3/dist/quill.js"></script>
<!-- <script>
    const quill = new Quill('#editor', {
        theme: 'snow',
        // modules: {
        //     toolbar: [
        //         [{ 'list': 'ordered'}, { 'list': 'bullet' }],
        //         ['bold', 'italic', 'underline']
        //     ]
        // }
    });

</script> -->