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

$statusMsg = "";

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['update'])) {
    $trxID = $_POST['trxID'];
    $newStatus = $_POST['status'];

    $sql = "UPDATE payment SET status = :status WHERE trxID = :trxID";
    $query = $dbh->prepare($sql);
    $query->bindParam(':status', $newStatus);
    $query->bindParam(':trxID', $trxID);
    $query->execute();

    if ($newStatus === "approved") {
        $sql = "SELECT email, amount FROM payment WHERE trxID = :trxID";
        $query = $dbh->prepare($sql);
        $query->bindParam(':trxID', $trxID);
        $query->execute();
        $result = $query->fetch(PDO::FETCH_OBJ);

        if ($result) {
            $checkSql = "SELECT COUNT(*) FROM balance_log WHERE trxID = :trxID";
            $checkQuery = $dbh->prepare($checkSql);
            $checkQuery->bindParam(':trxID', $trxID);
            $checkQuery->execute();
            $exists = $checkQuery->fetchColumn();

            if (!$exists) {
                $updateSql = "UPDATE users SET balance = balance + :amount WHERE email = :email";
                $updateQuery = $dbh->prepare($updateSql);
                $updateQuery->bindParam(':amount', $result->amount);
                $updateQuery->bindParam(':email', $result->email);
                $updateQuery->execute();

                $logSql = "INSERT INTO balance_log (email, trxID, amount) VALUES (:email, :trxID, :amount)";
                $logQuery = $dbh->prepare($logSql);
                $logQuery->bindParam(':email', $result->email);
                $logQuery->bindParam(':trxID', $trxID);
                $logQuery->bindParam(':amount', $result->amount);
                $logQuery->execute();
            }
        }
    }

    $statusMsg = "স্ট্যাটাস সফলভাবে আপডেট হয়েছে।";
}

$emailSearch = $_POST['email'] ?? '';
$results = [];

if ($emailSearch) {
    $sql = "SELECT * FROM payment WHERE email = :email AND status = 'pending' ORDER BY id DESC";
    $query = $dbh->prepare($sql);
    $query->bindParam(':email', $emailSearch);
    $query->execute();
    $results = $query->fetchAll(PDO::FETCH_OBJ);
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>এডমিন রিচার্জ ম্যানেজমেন্ট</title>
    <link href="assets/libs/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: url('https://i.ibb.co/DHMBCRcY/image.jpg') no-repeat center center fixed;
            background-size: cover;
            backdrop-filter: blur(2px);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            font-family: 'Segoe UI', sans-serif;
        }
        .panel {
            background: rgba(255, 255, 255, 0.92);
            border: 2px solid rgba(0, 0, 0, 0.08);
            border-radius: 8px;
            padding: 25px;
            width: 100%;
            max-width: 700px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.2);
        }
        h2 {
            color: #222;
            margin-bottom: 20px;
            text-align: center;
        }
        .form-control, .form-select {
            border-radius: 4px;
        }
        .table th, .table td {
            vertical-align: middle;
        }
        .btn-primary, .btn-success {
            border-radius: 4px;
        }
        .input-group {
            justify-content: center;
        }
        .form-control.w-50 {
            max-width: 300px;
        }
        .alert {
            text-align: center;
        }
    </style>
</head>
<body>

<div class="panel">
    <h2>এডমিন রিচার্জ প্যানেল</h2>

    <?php if ($statusMsg): ?>
        <div class="alert alert-success"><?php echo $statusMsg; ?></div>
    <?php endif; ?>

    <form method="POST" class="mb-4">
        <div class="input-group">
            <input type="text" name="email" class="form-control w-50" placeholder="ইমেইল লিখুন" value="<?php echo htmlentities($emailSearch); ?>" required>
            <button type="submit" class="btn btn-primary">সার্চ করুন</button>
        </div>
    </form>

    <?php if (!empty($results)): ?>
        <div class="table-responsive">
            <table class="table table-bordered table-striped text-center">
                <thead class="table-dark">
                    <tr>
                        <th>আইডি</th>
                        <th>ইমেইল</th>
                        <th>টাকা</th>
                        <th>ট্রানজেকশন আইডি</th>
                        <th>সময়</th>
                        <th>স্ট্যাটাস</th>
                        <th>আপডেট</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($results as $row): ?>
                        <tr>
                            <td><?php echo $row->id; ?></td>
                            <td><?php echo htmlentities($row->email); ?></td>
                            <td><?php echo htmlentities($row->amount); ?> টাকা</td>
                            <td><?php echo htmlentities($row->trxID); ?></td>
                            <td><?php echo htmlentities($row->paymentExecuteTime); ?></td>
                            <td><span class="badge bg-warning text-dark"><?php echo htmlentities($row->status); ?></span></td>
                            <td>
                                <form method="POST" class="d-flex justify-content-center align-items-center gap-2">
                                    <input type="hidden" name="trxID" value="<?php echo $row->trxID; ?>">
                                    <select name="status" class="form-select w-auto">
                                        <option value="approved">এপ্রুভ</option>
                                        <option value="cancelled">বাতিল</option>
                                    </select>
                                    <button type="submit" name="update" class="btn btn-success btn-sm">সেভ</button>
                                </form>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php elseif($emailSearch): ?>
        <div class="alert alert-info">এই ইমেইলের জন্য কোনো পেন্ডিং রিচার্জ পাওয়া যায়নি।</div>
    <?php endif; ?>
</div>

<script src="assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
