File size: 4,804 Bytes
d5ec5c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ehe Treat - Admin Panel</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <main>
        <h1>Admin <span class="text-accent">Panel</span></h1>
        
        <div id="login-section" class="admin-panel">
            <h2>Login</h2>
            <input type="password" id="admin-pass" placeholder="Enter Admin Password">
            <button onclick="login()">Login</button>
        </div>

        <div id="dashboard-section" style="display: none;">
            <div class="admin-panel">
                <h2>Add New Item</h2>
                <select id="item-type">
                    <option value="menu">Regular Menu Item</option>
                    <option value="offers">Special Offer</option>
                </select>
                <input type="text" id="title" placeholder="Food Name / Offer Title" required>
                <input type="number" id="price" placeholder="Price (₦)" required>
                <textarea id="description" placeholder="Description (Only for offers)"></textarea>
                <input type="file" id="image" accept="image/*" required>
                <button onclick="addItem()">Upload & Save</button>
                <p id="status-msg"></p>
            </div>

            <h2>Current Database</h2>
            <div class="grid" id="admin-items"></div>
        </div>
    </main>

    <script>
        let token = '';

        function login() {
            token = document.getElementById('admin-pass').value;
            if(token) {
                document.getElementById('login-section').style.display = 'none';
                document.getElementById('dashboard-section').style.display = 'block';
                loadAdminItems();
            }
        }

        async function addItem() {
            const type = document.getElementById('item-type').value;
            const formData = new FormData();
            formData.append('title', document.getElementById('title').value);
            formData.append('price', document.getElementById('price').value);
            formData.append('description', document.getElementById('description').value);
            formData.append('image', document.getElementById('image').files[0]);

            const msg = document.getElementById('status-msg');
            msg.innerText = "Uploading...";

            try {
                const res = await fetch(`/api/admin/${type}`, {
                    method: 'POST',
                    headers: { 'Authorization': `Bearer ${token}` },
                    body: formData
                });
                
                if (res.ok) {
                    msg.innerText = "Added successfully!";
                    loadAdminItems();
                } else {
                    msg.innerText = "Failed: " + await res.text();
                }
            } catch (e) {
                msg.innerText = "Error uploading.";
            }
        }

        async function loadAdminItems() {
            const container = document.getElementById('admin-items');
            container.innerHTML = 'Loading...';
            
            const [menuRes, offersRes] = await Promise.all([
                fetch('/api/menu'), fetch('/api/offers')
            ]);
            const menu = await menuRes.json();
            const offers = await offersRes.json();

            let html = '';
            const renderItem = (item, type) => `
                <div class="card">
                    <img src="${item.image_url}" alt="${item.title}">
                    <div class="card-content">
                        <span style="font-size:0.8rem; color:gray;">[${type.toUpperCase()}]</span>
                        <div class="card-title">${item.title}</div>
                        <div class="card-price">₦${item.price}</div>
                        <button class="delete-btn" onclick="deleteItem(${item.id}, '${type}')">Delete</button>
                    </div>
                </div>
            `;
            
            offers.forEach(item => html += renderItem(item, 'offers'));
            menu.forEach(item => html += renderItem(item, 'menu'));
            container.innerHTML = html;
        }

        async function deleteItem(id, type) {
            if(!confirm("Are you sure you want to delete this?")) return;
            await fetch(`/api/admin/${type}`, {
                method: 'DELETE',
                headers: { 
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ id })
            });
            loadAdminItems();
        }
    </script>
</body>
</html>