<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['adminlogin'])==0){
    header('location:index.php');
} else {

    
    if(isset($_GET['approve'])){
    $id = intval($_GET['approve']);

    
    $sql = "SELECT * FROM payment WHERE id=:id";
    $query = $dbh->prepare($sql);
    $query->bindParam(':id',$id,PDO::PARAM_INT);
    $query->execute();
    $payment = $query->fetch(PDO::FETCH_OBJ);

    if($payment){

        
        if(strtolower(trim($payment->status)) == "success"){
            $msg = "Already Approved!";
        } else {

            
            $sql = "UPDATE payment SET status='success' WHERE id=:id";
            $query = $dbh->prepare($sql);
            $query->bindParam(':id',$id,PDO::PARAM_INT);
            $query->execute();


            $sql = "UPDATE users SET balance = balance + :amount WHERE email = :email";
            $query = $dbh->prepare($sql);
            $query->bindParam(':amount',$payment->amount,PDO::PARAM_STR);
            $query->bindParam(':email',$payment->email,PDO::PARAM_STR);
            $query->execute();

            $msg = "Payment Approved & Balance Added!";
        }
    }
}
    // Reject
    if(isset($_GET['reject'])){
        $id = intval($_GET['reject']);
        $sql = "UPDATE payment SET status='Rejected' WHERE id=:id";
        $query = $dbh->prepare($sql);
        $query->bindParam(':id',$id,PDO::PARAM_INT);
        $query->execute();
        $msg="Payment Rejected Successfully";
    }
?>
<!doctype html>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<title>Manage Recharge Requests</title>

<!-- Font awesome -->
<link rel="stylesheet" href="css/font-awesome.min.css">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css">
<!-- Admin style -->
<link rel="stylesheet" href="css/style.css">
<style>
    body{font-family:Arial, sans-serif; background:#f5f5f5;}
    .succWrap{padding:10px; margin:0 0 20px 0; background:#5cb85c; color:#fff; box-shadow:0 1px 1px rgba(0,0,0,0.1);}
    table{width:100%; border-collapse:collapse; background:#fff; border-radius:6px; overflow:hidden; box-shadow:0 2px 10px rgba(0,0,0,0.05);}
    th,td{padding:12px 10px; text-align:center; border-bottom:1px solid #eee;}
    th{background:#e2136e; color:#fff; font-weight:bold;}
    tr:hover{background:#f9f9f9;}
    
    .status-pending{background:#ff9800;color:#fff;padding:5px 10px;border-radius:5px;}
    .status-success{background:#4caf50;color:#fff;padding:5px 10px;border-radius:5px;}
    .status-rejected{background:#f44336;color:#fff;padding:5px 10px;border-radius:5px;}

    /* Method Highlight */
    .method-bkash{background:#e2136e;color:#fff;padding:5px 10px;border-radius:5px; display:inline-block;}
    .method-nagad{background:#ff5722;color:#fff;padding:5px 10px;border-radius:5px; display:inline-block;}

    .btn{padding:6px 12px; border:none; border-radius:5px; color:#fff; cursor:pointer; text-decoration:none; display:inline-block; margin:2px;}
    .btn-approve{background:#4caf50;}
    .btn-approve:hover{background:#45a049;}
    .btn-reject{background:#f44336;}
    .btn-reject:hover{background:#d32f2f;}

    @media(max-width:768px){
        th,td{font-size:14px;padding:8px;}
        .btn{padding:5px 8px;font-size:13px;}
    }
</style>
</head>
<body>
<?php include('includes/header.php');?>
<div class="ts-main-content">
<?php include('includes/leftbar.php');?>

<div class="content-wrapper">
<div class="container-fluid">

<div class="row">
    <div class="col-md-12">
        <h2 class="page-title">Manage Recharge Requests</h2>

        <div class="panel panel-default">
            <div class="panel-heading">Recharge Request List</div>
            <div class="panel-body">
                <?php if($msg){?><div class="succWrap" id="msgshow"><?php echo htmlentities($msg);?></div><?php }?>
                <div style="overflow-x:auto;">
                <table>
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>User Email</th>
                            <th>Number</th>
                            <th>Method</th>
                            <th>Amount</th>
                            <th>Transaction ID</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                        $sql="SELECT * FROM payment ORDER BY id DESC";
                        $query = $dbh->prepare($sql);
                        $query->execute();
                        $results=$query->fetchAll(PDO::FETCH_OBJ);
                        $cnt=1;
                        if($query->rowCount()>0){
                            foreach($results as $result){
                                echo "<tr>";
                                echo "<td>".$cnt."</td>";
                                echo "<td>".htmlentities($result->email)."</td>";
                                echo "<td>0".htmlentities($result->customerMsisdn)."</td>";
                                
                        
                                if(strtolower($result->method)=='bkash'){
                                    echo "<td><span class='method-bkash'>bKash</span></td>";
                                }elseif(strtolower($result->method)=='nagad'){
                                    echo "<td><span class='method-nagad'>Nagad</span></td>";
                                }else{
                                    echo "<td>".htmlentities($result->method)."</td>";
                                }

                                echo "<td>".htmlentities($result->amount)."৳</td>";
                                echo "<td>".htmlentities($result->trxID)."</td>";

                                
                                if($result->status=="pending"){
                                    echo "<td><span class='status-pending'>pending</span></td>";
                                }elseif($result->status=="success"){
                                    echo "<td><span class='status-success'>success</span></td>";
                                }else{
                                    echo "<td><span class='status-rejected'>Rejected</span></td>";
                                }

                                
                                if($result->status=="pending"){
                                    echo "<td>
                                        <a class='btn btn-approve' href='?approve=".$result->id."' onclick='return confirm(\"Approve this payment?\");'>Approve</a>
                                        <a class='btn btn-reject' href='?reject=".$result->id."' onclick='return confirm(\"Reject this payment?\");'>Reject</a>
                                    </td>";
                                }else{
                                    echo "<td>—</td>";
                                }

                                echo "</tr>";
                                $cnt++;
                            }
                        }else{
                            echo "<tr><td colspan='8'>No recharge requests found.</td></tr>";
                        }
                    ?>
                    </tbody>
                </table>
                </div>
            </div>
        </div>

    </div>
</div>

</div>
</div>
</div>

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/main.js"></script>
<script>
$(document).ready(function(){
    setTimeout(function(){$('.succWrap').slideUp("slow");},3000);
});
</script>

</body>
</html>
<?php } ?>