<?php
// admin/index.php - Admin index page
require_once '../src/Database.php';
require_once '../src/Auth.php';

$auth = new Auth();
$auth->requireLogin();

$db = new Database();
$conn = $db->getConnection();

// Count paybills
$stmt = $conn->query('SELECT COUNT(*) as total FROM ussd_mpesa_credentials');
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$totalPaybills = $row['total'] ?? 0;

// Today's collections (sum of paid amounts for today)
$today = date('Y-m-d');
$stmt3 = $conn->prepare("SELECT SUM(amount) as total_collections FROM mpesa_stk_queue WHERE is_paid = 'yes' AND DATE(created_at) = ?");
$stmt3->execute([$today]);
$row3 = $stmt3->fetch(PDO::FETCH_ASSOC);
$totalCollections = $row3['total_collections'] ?? 0;

// Recent payments (last 5 paid)
$stmt4 = $conn->query("SELECT phone, amount, created_at FROM mpesa_stk_queue WHERE is_paid = 'yes' ORDER BY created_at DESC LIMIT 5");
$recentPayments = $stmt4->fetchAll(PDO::FETCH_ASSOC);

// Recent paybill updates (last 5)
$stmt2 = $conn->query('SELECT * FROM ussd_mpesa_credentials ORDER BY id DESC LIMIT 5');
$recent = $stmt2->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dashboard</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="max-w-4xl mx-auto py-8">
        <h1 class="text-4xl font-bold text-center text-blue-700 mb-8">Admin Dashboard</h1>
        <nav class="flex justify-center mb-8">
            <ul class="flex flex-wrap gap-4">
                <li>
                    <a href="update_paybills.php" class="inline-flex items-center gap-2 bg-green-600 hover:bg-green-700 text-white font-semibold py-2 px-6 rounded shadow transition">
                        <svg xmlns='http://www.w3.org/2000/svg' class='h-5 w-5' fill='none' viewBox='0 0 24 24' stroke='currentColor'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M12 4v16m8-8H4'/></svg>
                        Update Paybills
                    </a>
                </li>
                <li>
                    <a href="dashboard.php" class="inline-flex items-center gap-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-6 rounded shadow transition">
                        <svg xmlns='http://www.w3.org/2000/svg' class='h-5 w-5' fill='none' viewBox='0 0 24 24' stroke='currentColor'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M3 10h4v11H3zM17 3h4v18h-4zM10 14h4v7h-4z'/></svg>
                        Dashboard
                    </a>
                </li>
                <li>
                    <a href="export_phones.php" class="inline-flex items-center gap-2 bg-yellow-500 hover:bg-yellow-600 text-white font-semibold py-2 px-6 rounded shadow transition">
                        <svg xmlns='http://www.w3.org/2000/svg' class='h-5 w-5' fill='none' viewBox='0 0 24 24' stroke='currentColor'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M12 4v16m8-8H4'/></svg>
                        Export Phones (CSV)
                    </a>
                </li>
                <li>
                    <a href="update_paybills.php#delete" class="inline-flex items-center gap-2 bg-red-600 hover:bg-red-700 text-white font-semibold py-2 px-6 rounded shadow transition">
                        <svg xmlns='http://www.w3.org/2000/svg' class='h-5 w-5' fill='none' viewBox='0 0 24 24' stroke='currentColor'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M6 18L18 6M6 6l12 12'/></svg>
                        Delete STK Record
                    </a>
                </li>
                
                <li>
                    <a href="https://chomoahela.co.ke/ussd/upload/upload/" class="inline-flex items-center gap-2 bg-orange-600 hover:bg-orange-700 text-white font-semibold py-2 px-6 rounded shadow transition">
                        <svg xmlns='http://www.w3.org/2000/svg' class='h-5 w-5' fill='none' viewBox='0 0 24 24' stroke='currentColor'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M6 18L18 6M6 6l12 12'/></svg>
                        Whitelist Contacts
                    </a>
                </li>
            </ul>
        </nav>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
            <div class="bg-white rounded-lg shadow p-6">
                <div class="text-gray-500">Total Paybills</div>
                <div class="text-3xl font-bold text-blue-700"><?= $totalPaybills ?></div>
            </div>
            <div class="bg-white rounded-lg shadow p-6">
                <div class="text-gray-500">Today's Collections</div>
                <div class="text-3xl font-bold text-green-600">KES <?= number_format($totalCollections) ?></div>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow p-6 mb-8">
            <h2 class="text-2xl font-semibold text-gray-700 mb-4">Recent Payments</h2>
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm text-left">
                    <thead class="bg-blue-50">
                        <tr>
                            <th class="py-2 px-4 font-semibold">Phone</th>
                            <th class="py-2 px-4 font-semibold">Amount</th>
                            <th class="py-2 px-4 font-semibold">Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recentPayments as $row): ?>
                        <tr class="border-b hover:bg-blue-50">
                            <td class="py-2 px-4"><?= htmlspecialchars($row['phone']) ?></td>
                            <td class="py-2 px-4">KES <?= number_format($row['amount']) ?></td>
                            <td class="py-2 px-4 text-gray-500"><?= htmlspecialchars($row['created_at']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow p-6 mb-8">
            <h2 class="text-2xl font-semibold text-gray-700 mb-4">Recent Paybill Updates</h2>
            <div class="overflow-x-auto">
                <table class="min-w-full text-sm text-left">
                    <thead class="bg-blue-50">
                        <tr>
                            <th class="py-2 px-4 font-semibold">ID</th>
                            <th class="py-2 px-4 font-semibold">Paybill</th>
                            <th class="py-2 px-4 font-semibold">Account/Party B</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recent as $row): ?>
                        <tr class="border-b hover:bg-blue-50">
                            <td class="py-2 px-4"><?= htmlspecialchars($row['id']) ?></td>
                            <td class="py-2 px-4"><?= htmlspecialchars($row['shortcode']) ?></td>
                            <td class="py-2 px-4"><?= htmlspecialchars($row['party_b']) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>

       
    </div>
</body>
</html>
