<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once "db.php";

/* ================= ADMIN LOGIN ================= */

if (!isset($_SESSION['admin'])) {
    header("Location: admin-login.php");
    exit;
}

/* ================= UPDATE STATUS ================= */

$msg = "";

if ($_SERVER['REQUEST_METHOD'] == "POST") {

    $id = intval($_POST['id']);
    $status = trim($_POST['status']);
    $reply = trim($_POST['reply']);

    $stmt = $conn->prepare("
        UPDATE smod_requests
        SET
            status=?,
            reply=?
        WHERE id=?
    ");

    $stmt->bind_param("ssi", $status, $reply, $id);

    if ($stmt->execute()) {

        $msg = "
        <div class='success'>
            Request Updated Successfully
        </div>";

    } else {

        $msg = "
        <div class='error'>
            Failed To Update Request
        </div>";
    }
}

/* ================= TODAY STATS ================= */

$today = date('Y-m-d');

$todayQuery = $conn->query("
    SELECT
        COUNT(*) as total_count,
        SUM(amount) as total_spent
    FROM smod_requests
    WHERE DATE(created_at) = '$today'
");

$todayData = $todayQuery->fetch_assoc();

$todayCount = $todayData['total_count'] ?? 0;
$todaySpent = $todayData['total_spent'] ?? 0;

/* ================= WEEKLY STATS ================= */

$weekQuery = $conn->query("
    SELECT
        COUNT(*) as total_count,
        SUM(amount) as total_spent
    FROM smod_requests
    WHERE YEARWEEK(created_at, 1) = YEARWEEK(CURDATE(), 1)
");

$weekData = $weekQuery->fetch_assoc();

$weekCount = $weekData['total_count'] ?? 0;
$weekSpent = $weekData['total_spent'] ?? 0;

/* ================= MONTHLY STATS ================= */

$monthQuery = $conn->query("
    SELECT
        COUNT(*) as total_count,
        SUM(amount) as total_spent
    FROM smod_requests
    WHERE MONTH(created_at)=MONTH(CURDATE())
    AND YEAR(created_at)=YEAR(CURDATE())
");

$monthData = $monthQuery->fetch_assoc();

$monthCount = $monthData['total_count'] ?? 0;
$monthSpent = $monthData['total_spent'] ?? 0;

/* ================= FETCH REQUESTS ================= */

$get = $conn->query("
    SELECT *
    FROM smod_requests
    ORDER BY id DESC
");

?>

<!DOCTYPE html>
<html>
<head>

    <title>ASO VERIFY - Admin Panel</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <style>

        *{
            margin:0;
            padding:0;
            box-sizing:border-box;
        }

        body{
            font-family:'Poppins',sans-serif;
            background:
            linear-gradient(rgba(5,14,32,0.95), rgba(5,14,32,0.96)),
            url('https://images.unsplash.com/photo-1520607162513-77705c0f0d4a?q=80&w=1600&auto=format&fit=crop');

            background-size:cover;
            background-position:center;
            background-attachment:fixed;
            min-height:100vh;
            color:white;
            padding:20px;
        }

        .topbar{
            margin-bottom:25px;
        }

        .topbar h1{
            font-size:32px;
            font-weight:700;
        }

        .topbar span{
            color:#a8bddf;
            font-size:14px;
        }

        /* ================= STATS ================= */

        .stats-grid{
            display:grid;
            grid-template-columns:repeat(auto-fit,minmax(250px,1fr));
            gap:18px;
            margin-bottom:30px;
        }

        .stats-card{
            background:rgba(255,255,255,0.08);
            border:1px solid rgba(255,255,255,0.08);
            backdrop-filter:blur(14px);
            border-radius:22px;
            padding:22px;
        }

        .stats-card h3{
            font-size:17px;
            margin-bottom:18px;
            color:white;
        }

        .stats-box{
            display:flex;
            justify-content:space-between;
            margin-bottom:10px;
        }

        .stats-box small{
            color:#a8bddf;
            font-size:13px;
        }

        .stats-box strong{
            font-size:16px;
        }

        /* ================= ALERT ================= */

        .success{
            background:rgba(25,135,84,0.15);
            border:1px solid rgba(25,135,84,0.35);
            color:#7cffb7;
            padding:14px;
            border-radius:14px;
            margin-bottom:20px;
        }

        .error{
            background:rgba(220,53,69,0.15);
            border:1px solid rgba(220,53,69,0.35);
            color:#ff9ca7;
            padding:14px;
            border-radius:14px;
            margin-bottom:20px;
        }

        /* ================= TABLE ================= */

        .table-container{
            width:100%;
            overflow-x:auto;
            border-radius:18px;
            margin-bottom:30px;
        }

        .history-table{
            width:100%;
            min-width:1400px;
            border-collapse:collapse;
            background:#082042;
            border-radius:18px;
            overflow:hidden;
        }

        .history-table thead{
            background:rgba(255,255,255,0.03);
        }

        .history-table th{
            padding:20px 18px;
            text-align:left;
            color:white;
            font-size:14px;
            font-weight:700;
            border-bottom:1px solid rgba(255,255,255,0.08);
        }

        .history-table td{
            padding:18px;
            color:#dce6ff;
            font-size:14px;
            border-bottom:1px solid rgba(255,255,255,0.05);
            vertical-align:top;
        }

        .history-table tr:hover{
            background:rgba(255,255,255,0.03);
        }

        /* ================= STATUS ================= */

        .status-badge{
            padding:10px 22px;
            border-radius:50px;
            font-size:13px;
            font-weight:700;
            display:inline-block;
            text-transform:capitalize;
        }

        .successful{
            background:#19ff74;
            color:#041b0d;
        }

        .pending{
            background:#ffb400;
            color:#241500;
        }

        .failed{
            background:#ff4d4d;
            color:white;
        }

        /* ================= FORM ================= */

        textarea,
        select{
            width:100%;
            border:none;
            outline:none;
            border-radius:12px;
            padding:12px;
            background:rgba(255,255,255,0.08);
            color:white;
            font-size:14px;
        }

        select option{
            background:#0d1a35;
            color:white;
        }

        textarea{
            min-height:90px;
            resize:none;
            margin-top:10px;
        }

        .update-btn{
            width:100%;
            padding:13px;
            border:none;
            border-radius:12px;
            margin-top:12px;
            background:linear-gradient(135deg,#0d6efd,#4e8cff);
            color:white;
            font-size:15px;
            font-weight:600;
            cursor:pointer;
        }

        .update-btn:hover{
            opacity:0.9;
        }

        .empty{
            text-align:center;
            padding:50px;
            background:rgba(255,255,255,0.08);
            border-radius:20px;
            color:#b3c3df;
        }

        @media(max-width:768px){

            .topbar h1{
                font-size:25px;
            }

            .stats-grid{
                grid-template-columns:1fr;
            }

            .history-table th,
            .history-table td{
                padding:14px;
                font-size:13px;
            }
        }

    </style>

</head>

<body>

<div class="topbar">

    <h1>ASO VERIFY</h1>

    <span>
        Second Time Modification Admin Panel
    </span>

</div>

<!-- ================= ALERT ================= -->

<?php echo $msg; ?>

<!-- ================= STATS ================= -->

<div class="stats-grid">

    <!-- TODAY -->

    <div class="stats-card">

        <h3>Today Statistics</h3>

        <div class="stats-box">
            <small>Today Count</small>
            <strong><?php echo $todayCount; ?></strong>
        </div>

        <div class="stats-box">
            <small>Today Spent</small>
            <strong>
                ₦<?php echo number_format($todaySpent,2); ?>
            </strong>
        </div>

    </div>

    <!-- WEEK -->

    <div class="stats-card">

        <h3>Weekly Statistics</h3>

        <div class="stats-box">
            <small>Weekly Count</small>
            <strong><?php echo $weekCount; ?></strong>
        </div>

        <div class="stats-box">
            <small>Weekly Spent</small>
            <strong>
                ₦<?php echo number_format($weekSpent,2); ?>
            </strong>
        </div>

    </div>

    <!-- MONTH -->

    <div class="stats-card">

        <h3>Monthly Statistics</h3>

        <div class="stats-box">
            <small>Monthly Count</small>
            <strong><?php echo $monthCount; ?></strong>
        </div>

        <div class="stats-box">
            <small>Monthly Spent</small>
            <strong>
                ₦<?php echo number_format($monthSpent,2); ?>
            </strong>
        </div>

    </div>

</div>

<!-- ================= TABLE ================= -->

<?php if($get->num_rows > 0): ?>

<div class="table-container">

    <table class="history-table">

        <thead>

            <tr>

                <th>ID</th>
                <th>User</th>
                <th>NIN</th>
                <th>Type</th>
                <th>Price</th>
                <th>Status</th>
                <th>Reply</th>
                <th>Date</th>
                <th>Update</th>

            </tr>

        </thead>

        <tbody>

            <?php while($row = $get->fetch_assoc()): ?>

            <tr>

                <td>
                    #<?php echo $row['id']; ?>
                </td>

                <td>
                    <?php echo $row['user_email']; ?>
                </td>

                <td>
                    <?php echo $row['nin']; ?>
                </td>

                <td>
                    <?php echo $row['service_type']; ?>
                </td>

                <td>
                    ₦<?php echo number_format($row['amount']); ?>
                </td>

                <td>

                    <span class="status-badge <?php echo strtolower($row['status']); ?>">

                        <?php echo $row['status']; ?>

                    </span>

                </td>

                <td>

                    <?php
                        if(!empty($row['reply'])){
                            echo $row['reply'];
                        } else {
                            echo "—";
                        }
                    ?>

                </td>

                <td>
                    <?php echo $row['created_at']; ?>
                </td>

                <td style="min-width:280px;">

                    <form method="POST">

                        <input type="hidden"
                               name="id"
                               value="<?php echo $row['id']; ?>">

                        <select name="status" required>

                            <option value="pending"
                                <?php if($row['status']=="pending") echo "selected"; ?>>
                                Pending
                            </option>

                            <option value="successful"
                                <?php if($row['status']=="successful") echo "selected"; ?>>
                                Successful
                            </option>

                            <option value="failed"
                                <?php if($row['status']=="failed") echo "selected"; ?>>
                                Failed
                            </option>

                        </select>

                        <textarea name="reply"
                                  placeholder="Enter Admin Reply"><?php echo $row['reply']; ?></textarea>

                        <button type="submit" class="update-btn">
                            Update Request
                        </button>

                    </form>

                </td>

            </tr>

            <?php endwhile; ?>

        </tbody>

    </table>

</div>

<?php else: ?>

<div class="empty">
    No Requests Found
</div>

<?php endif; ?>

</body>
</html>