<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CafePost Management</title>
    <style>
        /* CSS Design */
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f9f9f9;
            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;
        }

        .form-section, .posts-section {
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }

        textarea, input[type="file"], button {
            width: 100%;
            padding: 10px;
            margin: 5px 0;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }

        textarea {
            resize: vertical;
            height: 100px;
        }

        button {
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        hr {
            border: none;
            border-top: 1px solid #eee;
        }

        ul {
            list-style-type: none;
            padding: 0;
        }

        li {
            margin-bottom: 15px;
            padding: 15px;
            background: #f8f8f8;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        }

        strong {
            display: block;
            font-size: 18px;
            margin-bottom: 5px;
            color: #333;
        }

        img {
            max-width: 100px;
            display: block;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .btn-delete {
            background-color: #e74c3c;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 3px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn-delete:hover {
            background-color: #c0392b;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>CafePost Management</h1>
        </header>

        <!-- Create Post Section -->
        <section class="form-section">
            <h2>Create New Post</h2>
            <form id="postForm">
                <div class="form-group">
                    <label for="postMessage">Post Message:</label>
                    <textarea id="postMessage" name="postMessage" required></textarea>
                </div>
                <div class="form-group">
                    <label for="image">Upload Image:</label>
                    <input type="file" id="image" name="image">
                </div>
                <button type="submit" class="btn">Create Post</button>
            </form>
        </section>

        <hr>

        <!-- Display Posts Section -->
        <section class="posts-section">
            <h2>All Posts</h2>
            <ul id="postsList"></ul>
        </section>
    </div>

    <script>
        // JavaScript Logic
        const BASE_URL = 'api/cafe-post';

        // Handle form submission to create a new post
        document.getElementById('postForm').addEventListener('submit', async (event) => {
            event.preventDefault();

            const postMessage = document.getElementById('postMessage').value;
            const image = document.getElementById('image').files[0];

            const formData = new FormData();
            formData.append('postMessage', postMessage);
            if (image) {
                formData.append('image', image);
            }

            try {
                const response = await fetch(BASE_URL, {
                    method: 'POST',
                    body: formData
                });

                if (response.ok) {
                    alert('Post created successfully!');
                    document.getElementById('postForm').reset();
                    fetchPosts();
                } else {
                    alert('Failed to create post.');
                }
            } catch (error) {
                console.error('Error:', error);
            }
        });

        // Fetch and display all posts
        async function fetchPosts() {
            try {
                const response = await fetch(BASE_URL);
                const posts = await response.json();

                const postsList = document.getElementById('postsList');
                postsList.innerHTML = ''; // Clear the list

                posts.forEach((post) => {
                    const listItem = document.createElement('li');
                    listItem.innerHTML = `
                        <strong>${post.postMessage}</strong>
                        <img src="/uploads/${post.image_url}" alt="Post Image">
                        <button class="btn-delete" onclick="deletePost('${post._id}')">Delete</button>
                    `;
                    postsList.appendChild(listItem);
                });
            } catch (error) {
                console.error('Error fetching posts:', error);
            }
        }

        // Delete a post
        async function deletePost(postId) {
            try {
                const response = await fetch(`api/cafe-post/${postId}`, {
                    method: 'DELETE'
                });

                if (response.ok) {
                    alert('Post deleted successfully!');
                    fetchPosts();
                } else {
                    alert('Failed to delete post.');
                }
            } catch (error) {
                console.error('Error:', error);
            }
        }

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