File size: 9,128 Bytes
0dff816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
session_start();
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true || $_SESSION['user_type'] !== 'admin') {
    header('Location: ../../index.php');
    exit;
}

require_once '../api/agent-functions.php';

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        if ($_POST['action'] === 'review_agent') {
            $agentId = $_POST['agent_id'];
            $status = $_POST['status'];
            $notes = $_POST['notes'] ?? '';
            
            if (reviewAgentApplication($agentId, $_SESSION['user_id'], $status, $notes)) {
                $message = "Agent application " . ($status === 'approved' ? 'approved' : 'rejected') . " successfully!";
            } else {
                $error = "Failed to update agent application.";
            }
        } elseif ($_POST['action'] === 'review_advertisement') {
            $adId = $_POST['advertisement_id'];
            $status = $_POST['status'];
            $notes = $_POST['notes'] ?? '';
            
            if (reviewAdvertisement($adId, $_SESSION['user_id'], $status, $notes)) {
                $message = "Advertisement " . ($status === 'approved' ? 'approved' : 'rejected') . " successfully!";
            } else {
                $error = "Failed to update advertisement.";
            }
        }
    }
}

// Get data for admin
$pendingAgents = getPendingAgents(0); // Get all pending agents
$pendingAds = getAdvertisementsForApproval('pending');
$adStats = getAdvertisementStats();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin - Approval System</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700;800&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
</head>
<body class="bg-gray-100 font-sans">
    <div class="container mx-auto p-4">
        <h1 class="text-3xl font-bold mb-6">Admin Approval System</h1>
        
        <?php if (isset($message)): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <?php if (isset($error)): ?>
            <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <!-- Statistics -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white p-4 rounded shadow">
                <h3 class="font-bold">Pending Agents</h3>
                <p class="text-2xl"><?php echo count($pendingAgents); ?></p>
            </div>
            <div class="bg-white p-4 rounded shadow">
                <h3 class="font-bold">Pending Ads</h3>
                <p class="text-2xl"><?php echo count($pendingAds); ?></p>
            </div>
            <div class="bg-white p-4 rounded shadow">
                <h3 class="font-bold">Total Revenue</h3>
                <p class="text-2xl">KES <?php echo number_format($adStats['total_revenue'], 2); ?></p>
            </div>
            <div class="bg-white p-4 rounded shadow">
                <h3 class="font-bold">Pending Revenue</h3>
                <p class="text-2xl">KES <?php echo number_format($adStats['pending_revenue'], 2); ?></p>
            </div>
        </div>

        <!-- Pending Advertisements -->
        <div class="bg-white rounded shadow mb-6">
            <div class="p-4 border-b">
                <h2 class="text-xl font-bold">Pending Advertisements</h2>
            </div>
            <div class="p-4">
                <?php if (empty($pendingAds)): ?>
                    <p class="text-gray-500">No pending advertisements</p>
                <?php else: ?>
                    <?php foreach ($pendingAds as $ad): ?>
                        <div class="border rounded p-4 mb-4">
                            <div class="flex justify-between items-start">
                                <div>
                                    <h3 class="font-bold"><?php echo htmlspecialchars($ad['title']); ?></h3>
                                    <p class="text-gray-600">By: <?php echo htmlspecialchars($ad['advertiser_name']); ?></p>
                                    <p class="text-gray-600">Budget: KES <?php echo number_format($ad['budget'], 2); ?></p>
                                    <p class="text-gray-600">Platform: <?php echo ucfirst($ad['target_platform']); ?></p>
                                    <?php if ($ad['agent_name']): ?>
                                        <p class="text-gray-600">Agent: <?php echo htmlspecialchars($ad['agent_name']); ?></p>
                                    <?php endif; ?>
                                </div>
                                <div class="flex gap-2">
                                    <button onclick="reviewAd(<?php echo $ad['id']; ?>, 'approved')" 
                                            class="bg-green-500 text-white px-3 py-1 rounded">Approve</button>
                                    <button onclick="reviewAd(<?php echo $ad['id']; ?>, 'rejected')" 
                                            class="bg-red-500 text-white px-3 py-1 rounded">Reject</button>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>

        <!-- Pending Agents -->
        <div class="bg-white rounded shadow">
            <div class="p-4 border-b">
                <h2 class="text-xl font-bold">Pending Agent Applications</h2>
            </div>
            <div class="p-4">
                <?php if (empty($pendingAgents)): ?>
                    <p class="text-gray-500">No pending agent applications</p>
                <?php else: ?>
                    <?php foreach ($pendingAgents as $agent): ?>
                        <div class="border rounded p-4 mb-4">
                            <div class="flex justify-between items-start">
                                <div>
                                    <h3 class="font-bold"><?php echo htmlspecialchars($agent['full_name']); ?></h3>
                                    <p class="text-gray-600">Phone: <?php echo htmlspecialchars($agent['phone']); ?></p>
                                    <p class="text-gray-600">Location: <?php echo htmlspecialchars($agent['location']); ?></p>
                                    <p class="text-gray-600">Sponsor: <?php echo htmlspecialchars($agent['username']); ?></p>
                                </div>
                                <div class="flex gap-2">
                                    <button onclick="reviewAgent(<?php echo $agent['id']; ?>, 'approved')" 
                                            class="bg-green-500 text-white px-3 py-1 rounded">Approve</button>
                                    <button onclick="reviewAgent(<?php echo $agent['id']; ?>, 'rejected')" 
                                            class="bg-red-500 text-white px-3 py-1 rounded">Reject</button>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Review Forms -->
    <form id="agent-review-form" method="POST" class="hidden">
        <input type="hidden" name="action" value="review_agent">
        <input type="hidden" id="agent-id" name="agent_id">
        <input type="hidden" id="agent-status" name="status">
        <textarea id="agent-notes" name="notes" placeholder="Review notes..."></textarea>
    </form>

    <form id="ad-review-form" method="POST" class="hidden">
        <input type="hidden" name="action" value="review_advertisement">
        <input type="hidden" id="ad-id" name="advertisement_id">
        <input type="hidden" id="ad-status" name="status">
        <textarea id="ad-notes" name="notes" placeholder="Review notes..."></textarea>
    </form>

    <script>
        function reviewAgent(agentId, status) {
            if (confirm(`Are you sure you want to ${status} this agent application?`)) {
                document.getElementById('agent-id').value = agentId;
                document.getElementById('agent-status').value = status;
                document.getElementById('agent-review-form').submit();
            }
        }

        function reviewAd(adId, status) {
            if (confirm(`Are you sure you want to ${status} this advertisement?`)) {
                document.getElementById('ad-id').value = adId;
                document.getElementById('ad-status').value = status;
                document.getElementById('ad-review-form').submit();
            }
        }

        feather.replace();
    </script>
</body>
</html>