<?php
session_start();

error_reporting(E_ALL);
ini_set('display_errors', 1);

include('con.php');


/* ================= LOGIN CHECK ================= */

if (!isset($_SESSION['phone'])) {

    header("Location: login.php");
    exit;
}

$username = $_SESSION['phone'];

/* ================= USER DATA ================= */

$getUser = mysqli_query(
    $ahk_conn,
    "SELECT * FROM users WHERE phone='$username' LIMIT 1"
);

$user = mysqli_fetch_assoc($getUser);

/* ================= ADMIN CHECK ================= */

if ($user['type'] != 'admin') {

    die("
    <div style='
        font-family:Arial;
        height:100vh;
        display:flex;
        justify-content:center;
        align-items:center;
        background:#f4f6fb;
    '>

        <div style='
            background:#fff;
            padding:40px;
            border-radius:18px;
            box-shadow:0 10px 30px rgba(0,0,0,.1);
            text-align:center;
            max-width:400px;
        '>

            <h2 style='color:#dc3545;margin-bottom:15px;'>
                Access Denied
            </h2>

            <p style='color:#555;'>
                Only admin can access this page.
            </p>

            <a href='dashboard.php'
               style='
               display:inline-block;
               margin-top:20px;
               background:#0d6efd;
               color:#fff;
               padding:12px 22px;
               border-radius:10px;
               text-decoration:none;
               font-weight:600;
               '>

               Back Dashboard

            </a>

        </div>

    </div>
    ");
}

?>

<!DOCTYPE html>
<html>
<head>

<meta charset="UTF-8">

<title>Today's Wallet History - All Users</title>

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

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"/>

<style>

body{
    background:#f3f7fd;
    font-family:Poppins,sans-serif;
}

/* ================= CARD ================= */

.card-box{
    background:#fff;
    padding:25px;
    border-radius:20px;
    box-shadow:0 8px 25px rgba(0,0,0,.08);
}

/* ================= HEADER ================= */
.admin-badge{
    display:inline-flex;
    align-items:center;
    background:linear-gradient(135deg,#2563eb,#1d4ed8);
    color:#fff;
    padding:12px 22px;
    border-radius:12px;
    font-weight:600;
    font-size:15px;
    box-shadow:0 6px 18px rgba(37,99,235,.25);
    transition:.3s;
}

.admin-badge:hover{
    background:linear-gradient(135deg,#1d4ed8,#1e40af);
    transform:translateY(-2px);
    color:#fff;
}
.page-header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    flex-wrap:wrap;
    gap:15px;
    margin-bottom:25px;
}

.page-title{
    font-size:28px;
    font-weight:700;
    color:#0d6efd;
}

.admin-badge{
    background:linear-gradient(135deg,#0d6efd,#6610f2);
    color:#fff;
    padding:10px 18px;
    border-radius:30px;
    font-weight:600;
    box-shadow:0 5px 15px rgba(13,110,253,.25);
}

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

.table{
    border-radius:15px;
    overflow:hidden;
}

.table thead{
    background:#0d1b2a;
    color:#fff;
}

.table th{
    padding:14px;
    border:none;
    white-space:nowrap;
}

.table td{
    padding:14px;
    vertical-align:middle;
    white-space:nowrap;
}

/* ================= BADGES ================= */

.success-badge{
    background:#d1fae5;
    color:#065f46;
    padding:6px 14px;
    border-radius:30px;
    font-size:12px;
    font-weight:700;
}

.pending-badge{
    background:#fef3c7;
    color:#92400e;
    padding:6px 14px;
    border-radius:30px;
    font-size:12px;
    font-weight:700;
}

.credit-badge{
    background:#dcfce7;
    color:#166534;
    padding:6px 14px;
    border-radius:30px;
    font-size:12px;
    font-weight:700;
}

.debit-badge{
    background:#fee2e2;
    color:#991b1b;
    padding:6px 14px;
    border-radius:30px;
    font-size:12px;
    font-weight:700;
}

/* ================= MOBILE ================= */

@media(max-width:768px){

    .page-title{
        font-size:22px;
    }

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

}

</style>

</head>

<body>

<div class="container-fluid px-4 py-4">

<div class="card-box">

<!-- ================= HEADER ================= -->

<div class="page-header">

<div>

<div class="page-title">
<i class="fa-solid fa-wallet me-2"></i>
Today's Wallet History
</div>

<div class="text-muted mt-1">
All users wallet transactions for today
</div>

</div>

<a href="admindash.php" class="admin-badge text-decoration-none">
    <i class="fa-solid fa-user-shield me-2"></i>
    Back To Admin Panel
</a>
</div>

<?php

/* ================= FETCH TODAY HISTORY ================= */

$todayHistory = mysqli_query($ahk_conn, "

    SELECT *
    FROM wallethistory
    WHERE DATE(`date`) = CURDATE()
    ORDER BY id DESC

");

?>

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

<div class="table-responsive">

<table class="table table-bordered table-hover align-middle text-center">

<thead>

<tr>

<th>ID</th>
<th>User ID</th>
<th>Amount</th>
<th>Balance</th>
<th>Purpose</th>
<th>Status</th>
<th>Type</th>
<th>Date</th>

</tr>

</thead>

<tbody>

<?php if(mysqli_num_rows($todayHistory) > 0): ?>

<?php while($row = mysqli_fetch_assoc($todayHistory)): ?>

<tr>

<td>
#<?= $row['id'] ?>
</td>

<td>
<?= htmlspecialchars($row['userid']) ?>
</td>

<td class="fw-bold text-primary">
₹<?= number_format($row['amount'],2) ?>
</td>

<td class="fw-bold text-success">
₹<?= number_format($row['balance'],2) ?>
</td>

<td>
<?= htmlspecialchars($row['purpose']) ?>
</td>

<td>

<?php if($row['status'] == 1){ ?>

<span class="success-badge">
Success
</span>

<?php } else { ?>

<span class="pending-badge">
Pending
</span>

<?php } ?>

</td>

<td>

<?php if(strtolower($row['type']) == 'credit'){ ?>

<span class="credit-badge">
Credit
</span>

<?php } else { ?>

<span class="debit-badge">
Debit
</span>

<?php } ?>

</td>

<td>
<?= date('d M Y h:i A', strtotime($row['date'])) ?>
</td>

</tr>

<?php endwhile; ?>

<?php else: ?>

<tr>

<td colspan="8">

<div class="py-4">

<h5 class="text-muted">
No wallet history found for today
</h5>

</div>

</td>

</tr>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>

<?php include('footer.php'); ?>