<?php 
include 'header.php';

if (isset($_GET['del'])) {
    $tid = $_GET['del'];
    $del = mysqli_query($link, "DELETE FROM transaction WHERE id = '$tid' ");
    if($del){
       echo "<script>alert('Transaction deleted successfully');window.location.href = 'transaction.php' </script>";
    }
}

if (isset($_GET['app'])) {
    $tid = $_GET['app'];

    $sq = mysqli_query($link, "SELECT * FROM transaction WHERE id = '$tid' ");
    if (mysqli_num_rows($sq) > 0) {
        $rw = mysqli_fetch_assoc($sq);
        $userId = $rw['userid'];
        $type = $rw['type'];
        $coin = $rw['coin'];
        $amount = $rw['amount'];
        $status = $rw['status'];
        $date = $rw['date'];
        $transactionid = $rw['transactionid'];
    }

    if ($status == "Approved") {
        echo "<script>alert('Transaction already approved successfully')</script>";
    } else {
        $update = mysqli_query($link, "UPDATE transaction SET status = 'Approved' WHERE id = '$tid'  ");
        if ($type == "Receive") {
            switch ($coin) {
                case 'Stellar':
                    $cl = 'xlmbal';
                    break;
                case 'Ripple':
                    $cl = 'xrpbal';
                    break;
                case 'Bitcoin':
                    $cl = 'btcbal';
                    break;
                case 'Ethereum':
                    $cl = 'ethbal';
                    break;
                case 'Tether':
                    $cl = 'usdtbal';
                    break;
                case 'Dogecoin':
                    $cl = 'dogebal';
                    break;
                case 'Litecoin':
                    $cl = 'ltcbal';
                    break;
                case 'Shibainu':
                    $cl = 'shibbal';
                    break;
            }
            mysqli_query($link, "UPDATE users SET $cl = $cl + '$amount' WHERE id = '$userId' ");
        }

        //send mail
        $user = mysqli_query($link, "SELECT email, fname FROM users WHERE id = '$userId' ");
        if(mysqli_num_rows($user) > 0){
            $ur = mysqli_fetch_assoc($user);
            $fname = $ur['fname'];
            $email = $ur['email'];

            $subject = "Transaction Approved";
            $body = "Dear $fname, <br>

<p>I hope this email finds you well. We are pleased to inform you that the $type transaction with ID $transactionid has been approved. Below are the details of the transaction: </p> <br>

<p>Transaction ID - $transactionid</p> <br>
<p>Amount - $amount USD</p> <br>
<p>Transaction Date and Time - $date </p> <br>

<p>If you have any concerns or questions regarding this transaction, feel free to reach out to our support team. </p> <br>

<p>Best regards, </p> <br>

$sitename";
            
            sendMail($email, $subject, $body);
        }
       
        echo "<script>alert('Transaction approved successfully');window.location.href = 'transaction.php' </script>";
    }
}
?>

<!-- Card start -->
<div class="card">
    <div class="card-header">
        <div class="card-title"></div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table id="scrollVertical" class="table custom-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Transaction id</th>
                        <th>User</th>
                        <th>Type</th>
                        <th>Coin</th>
                        <th>Amount</th>
                        <th>Wallet</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $i = 0;
                    $select = mysqli_query($link, "SELECT * FROM  transaction  WHERE status = 'Pending' ");
                    if (mysqli_num_rows($select) > 0) {
                        while ($row = mysqli_fetch_assoc($select)) {
                            $id = $row ['id']; 
                            $userId = $row['userid'];
                            $user = mysqli_query($link, "SELECT email, fname FROM users WHERE id = '$userId' ");
                            if(mysqli_num_rows($user) > 0){
                                $ur = mysqli_fetch_assoc($user);
                                $fname = $ur['fname'];
                                $email = $ur['email'];
                            }
                            $i++;        
                    ?>  
                    <tr>
                        <td><?php echo $i ?></td>
                        <td><?php echo $row['transactionid'] ?></td>
                        <td><?php echo $fname ?>/<br><?php echo $email ?></td>
                        <td><?php echo $row['type'] ?></td>
                        <td><?php echo $row['coin'] ?></td>
                        <td><?php echo $row['amount'] ?> USD</td>
                        <td><?php echo $row['wallet'] ?></td>
                        <td><?php echo $row['status'] ?></td>
                        <td>
                            <a href="?app=<?php echo $row['id'] ?>" class="btn btn-primary">Approve</a>
                            <a href="?del=<?php echo $row['id'] ?>" class="btn btn-danger">Delete</a>
                        </td>
                    </tr>
                    <?php }} ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php
include 'footer.php';
?>
