<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Cafe Posts</title>
    <style>
        /* General Styling */
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
            color: #333;
        }

        .container {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        header {
            text-align: center;
            margin-bottom: 20px;
        }

        h1 {
            color: #444;
            margin-bottom: 20px;
        }

        .posts-list {
            list-style: none;
            padding: 0;
        }

        .post-item {
            margin-bottom: 20px;
            padding: 15px;
            background: #f8f8f8;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .post-item strong {
            display: block;
            font-size: 18px;
            margin-bottom: 10px;
            color: #222;
        }

        .post-item img {
            max-width: 100%;
            height: auto;
            border-radius: 8px;
            margin-bottom: 10px;
        }

        .post-item .meta {
            font-size: 14px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>View Cafe Posts</h1>
        </header>

        <ul id="postsList" class="posts-list">
            <!-- Posts will be dynamically inserted here -->
        </ul>
    </div>

    <script>
        // Fetch and display all posts
        async function fetchPosts() {
            try {
                const response = await fetch('/api/cafe-post'); // Replace with your API URL
                const posts = await response.json();

                const postsList = document.getElementById('postsList');
                postsList.innerHTML = ''; // Clear any existing posts

                posts.forEach((post) => {
                    const listItem = document.createElement('li');
                    listItem.classList.add('post-item');
                    listItem.innerHTML = `
                        <strong>${post.postMessage}</strong>
                        <img src="/uploads/${post.image_url}" alt="Post Image">
                        <div class="meta">Posted on: ${new Date(post.createdAt).toLocaleString()}</div>
                    `;
                    postsList.appendChild(listItem);
                });
            } catch (error) {
                console.error('Error fetching posts:', error);
            }
        }

        // Fetch posts when the page loads
        fetchPosts();
    </script>
</body>
</html>
