<?php
session_start();
require "db.php";

if(!isset($_SESSION['admin'])){
    die("Admin only");
}

$price = 1000;

/* ================= UPDATE ================= */
if(isset($_POST['update'])){

    $id = (int)$_POST['id'];
    $bvn = trim($_POST['bvn']);
    $status = strtolower(trim($_POST['status']));

    // update ONLY (no refund)
    $stmt = $conn->prepare("
        UPDATE bvn_phone_logs 
        SET bvn=?, status=? 
        WHERE id=?
    ");
    $stmt->bind_param("ssi",$bvn,$status,$id);
    $stmt->execute();
    $stmt->close();

    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}

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

function countTx($conn,$cond){
$r=$conn->query("SELECT COUNT(*) c FROM bvn_phone_logs WHERE $cond");
return $r->fetch_assoc()['c'];
}

$today=date("Y-m-d");
$week=date("Y-m-d",strtotime("monday this week"));
$month=date("Y-m-01");

$d=countTx($conn,"DATE(created_at)='$today'");
$w=countTx($conn,"DATE(created_at)>='$week'");
$m=countTx($conn,"DATE(created_at)>='$month'");

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

$res=$conn->query("SELECT * FROM bvn_phone_logs ORDER BY id DESC");
?>

<!DOCTYPE html>
<html>
<head>
<title>BVN Admin</title>

<style>
body{background:#071a2f;color:#fff;font-family:Arial;padding:20px}
.boxes{display:flex;gap:10px;margin-bottom:20px}
.box{flex:1;background:#0b2545;padding:15px;border-radius:10px}
table{width:100%;border-collapse:collapse;background:#fff;color:#000}
th,td{padding:10px;border-bottom:1px solid #ddd}
th{background:#0b2545;color:#fff}
input,select{padding:6px}
button{padding:6px 10px;background:#00bfff;border:none}
.success{color:green}
.failed{color:red}
.pending{color:orange}
</style>

</head>

<body>

<h2>BVN Admin Dashboard</h2>

<div class="boxes">

<div class="box">
<b>Transactions</b><br>
Daily: <?=$d?><br>
Weekly: <?=$w?><br>
Monthly: <?=$m?>
</div>

<div class="box">
<b>Amount</b><br>
Daily: ₦<?=number_format($d*$price,2)?><br>
Weekly: ₦<?=number_format($w*$price,2)?><br>
Monthly: ₦<?=number_format($m*$price,2)?>
</div>

<div class="box">
<b>Profit (₦200 each)</b><br>
Daily: ₦<?=number_format($d*200,2)?><br>
Weekly: ₦<?=number_format($w*200,2)?><br>
Monthly: ₦<?=number_format($m*200,2)?>
</div>

</div>

<table>

<tr>
<th>User</th>
<th>Phone</th>
<th>BVN</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>

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

<tr>

<form method="post">

<td><?=$row['user_email']?></td>
<td><?=$row['phone']?></td>

<td>
<input type="text" name="bvn" 
value="<?= $row['status']=='success' ? htmlspecialchars($row['bvn']) : '' ?>"
placeholder="Enter BVN">
</td>

<td class="<?=$row['status']?>">

<select name="status">
<option value="pending" <?=$row['status']=='pending'?'selected':''?>>Pending</option>
<option value="success" <?=$row['status']=='success'?'selected':''?>>Success</option>
<option value="failed" <?=$row['status']=='failed'?'selected':''?>>Failed</option>
</select>

</td>

<td><?=$row['created_at']?></td>

<td>
<input type="hidden" name="id" value="<?=$row['id']?>">
<button name="update">Update</button>
</td>

</form>

</tr>

<?php endwhile; ?>

</table>

</body>
</html>