<?php
/**
 * userTransactions.php -- User Transactions
 */

include '../inc/config.inc.php'; // Configuration

ob_start();
session_start();

//date_default_timezone_set('Africa/Lagos');

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

if ( !isset($_SESSION['admin']) || $_SESSION["loggedin"] !== true || trim($_SESSION['admin']) == '' ){
	//header('Location: ' . $BASE_URL . 'wp-admin/');
    header('Location: ' . $BASE_URL . 'signin');
	exit;
}

if ( isset($_GET['id']) ){
    $user_id = intval($_GET['id']);
    // Users
    $users = $db_con->prepare("SELECT id, fullname, currency FROM users WHERE id='$user_id'");
    $users->execute();
    $user = $users->fetch(PDO::FETCH_ASSOC);

?>

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>User Transactions - Alert Signals Nation</title>
    <!-- Favicon -->

    <link rel="shortcut icon" href="../../img/Signals-Alert-3.png" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Typography CSS -->
    <link rel="stylesheet" href="css/typography.css">
    <!-- Style CSS -->
    <link rel="stylesheet" href="css/style.css">
    <!-- Responsive CSS -->
    <link rel="stylesheet" href="css/responsive.css">

    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,600,700" rel="stylesheet" />
    <style>
        /*------ Google Font Style ------ */
        body {font-family:'Roboto'!important;}
    </style>

</head>
<body class="sidebar-main-active right-column-fixed">
    <!-- loader Start -->
    <div id="loading">
        <div id="loading-center">
        </div>
    </div>
    <!-- loader END -->
    <!-- Wrapper Start -->
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php include 'navbar.php' ?>
        <!-- Page Content  -->
        <div id="content-page" class="content-page">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-12 col-lg-12">
                        <div class="iq-card">
                            <div class="iq-card-header d-flex justify-content-between">
                                <div class="iq-header-title">
                                    <h4 class="card-title" style="font-family: 'Roboto'!important;">Transaction table</h4>
                                </div>
                            </div>
                            <div class="iq-card-body">
                                <p>All Transactions are saved here for refrencing. You can also <code>print</code> Tranactions</p>
                                <div class="table-responsive">
                                    <table id="datatable" class="table table-striped table-bordered">
                                        <thead>
                                            <tr>
                                                <th>Name</th>
                                                <th>Type</th>
                                                <th>Method</th>
                                                <th>Amount</th>
                                                <th>File</th>
                                                <th>Narration</th>
                                                <th>Time</th>
                                                <th>Date</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $transactions = $db_con->prepare("SELECT * FROM user_transactions WHERE user_id='".$user['id']."' ORDER BY created DESC");

                                        if ( $transactions->execute() ){
                                            $i = 0;
                                            while ( $trans = $transactions->fetch(PDO::FETCH_ASSOC) ){
                                                $i++;
                                        ?>
                                                <tr>
                                                    <td><?= $trans['fullname']; ?></td>
                                                    <td><?= $trans['type']; ?></td>
                                                    <td><?= $trans['method']; ?></td>
                                                    <td>
                                                        <?
                                                        if ( !empty($trans['amount']) ){
                                                            echo $user['currency'] . number_format($trans['amount'], 2);
                                                        }
                                                        ?>
                                                    </td>
                                                    <td>
                                                        <?php if ( !empty($trans['file']) ){ ?>
                                                            <a href="../wp-user/uploads/<?= $trans['file']; ?>" target="_blank">View Image</a>
                                                        <?php } else { echo 'Not Available'; } ?>
                                                    </td>
                                                    <td><?= $trans['narration']; ?></td>
                                                    <td><?= date('h:iA', strtotime($trans['time'])); ?></td>
                                                    <td><?= date('Y/m/d', strtotime($trans['date'])); ?></td>

                                                    <td>
                                                        <? if ($trans['status']=='Pending'){ ?><b style="color:#ffa500;">Pending...</b><? }
                                                        elseif ($trans['status']=='Approved'){ ?><b style="color:green;">Approved...</b><? }
                                                        elseif ($trans['status']=='Declined'){ ?><b style="color:red;">Declined...</b><? }
                                                        else { ?><b><?= $trans['status']; ?>...</b><? } ?>
                                                    </td>
                                                    <!--<td style="color:red;" data-toggle="modal" data-target=".bd-example-modal-sm-<?= $i; ?>"><b><i class="las la-exclamation-circle" style="color:red;"></i><?= $trans['status']; ?>...</b></td>
                                                    <div class="modal fade bd-example-modal-sm-<?= $i; ?>" tabindex="-1" role="dialog" aria-hidden="true">
                                                    <div class="modal-dialog modal-sm">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title">Status Update</h5>
                                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                                    <span aria-hidden="true">&times;</span>
                                                                </button>
                                                            </div>
                                                            <form action="" method="post">
                                                                <div class="modal-body">
                                                                    <input type="hidden" name="user_id" value="<?= $trans['user_id']; ?>"><br>
                                                                    <input type="hidden" name="trans_id" value="<?= $trans['transaction_id']; ?>"><br>
                                                                    <select class="form-control" name="status">
                                                                        <option value="Pending" <? if ($trans['status']=='Pending') echo 'selected'; ?>>Pending</option>
                                                                        <option value="Approved" <? if ($trans['status']=='Approved') echo 'selected'; ?>>Approved</option>
                                                                        <option value="Declined" <? if ($trans['status']=='Declined') echo 'selected'; ?>>Declined</option>
                                                                    </select>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                                                    <button type="submit" name="update_status" class="btn btn-primary">Submit</button>
                                                                </div>
                                                            </form>
                                                            <?php
                                                            if ( isset($_POST['update_status']) ){
                                                                error_reporting(E_ALL);
                            
                                                                $user_id = $_POST['user_id'];
                                                                $trans_id = $_POST['trans_id'];
                                                                $status = $_POST['status'];
                            
                                                                $update = $db_con->prepare("UPDATE user_transactions SET status='$status' WHERE transaction_id='$trans_id' AND user_id='$user_id'");
                                                                $update->execute();

                                                                header('Location: userTransactions?id=' . $user_id );
                                                            }
                                                            ?>
                                                        </div>
                                                    </div>-->
                                                </tr>
                                        <?php
                                            }
                                        }
                                        ?>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Name</th>
                                                <th>Type</th>
                                                <th>Method</th>
                                                <th>Amount</th>
                                                <th>File</th>
                                                <th>Narration</th>
                                                <th>Time</th>
                                                <th>Date</th>
                                                <th>Status</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                    <button type="button" class="btn btn-primary mr-2" onclick="window.print()"><i class="las la-download"></i>Print Transactions</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Wrapper END -->
    <!-- Footer -->
    <?php include 'footer.php' ?>
    <!-- color-customizer END -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="js/jquery.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Appear JavaScript -->
    <script src="js/jquery.appear.js"></script>
    <!-- Countdown JavaScript -->
    <script src="js/countdown.min.js"></script>
    <!-- Counterup JavaScript -->
    <script src="js/waypoints.min.js"></script>
    <script src="js/jquery.counterup.min.js"></script>
    <!-- Wow JavaScript -->
    <script src="js/wow.min.js"></script>
    <!-- Apexcharts JavaScript -->
    <script src="js/apexcharts.js"></script>
    <!-- Slick JavaScript -->
    <script src="js/slick.min.js"></script>
    <!-- Select2 JavaScript -->
    <script src="js/select2.min.js"></script>
    <!-- Owl Carousel JavaScript -->
    <script src="js/owl.carousel.min.js"></script>
    <!-- Magnific Popup JavaScript -->
    <script src="js/jquery.magnific-popup.min.js"></script>
    <!-- Smooth Scrollbar JavaScript -->
    <script src="js/smooth-scrollbar.js"></script>
    <!-- lottie JavaScript -->
    <script src="js/lottie.js"></script>
    <!-- am core JavaScript -->
    <script src="js/core.js"></script>
    <!-- am charts JavaScript -->
    <script src="js/charts.js"></script>
    <!-- am animated JavaScript -->
    <script src="js/animated.js"></script>
    <!-- am kelly JavaScript -->
    <script src="js/kelly.js"></script>
    <!-- am maps JavaScript -->
    <script src="js/maps.js"></script>
    <!-- am worldLow JavaScript -->
    <script src="js/worldLow.js"></script>
    <!-- Style Customizer -->
    <script src="js/style-customizer.js"></script>
    <!-- Chart Custom JavaScript -->
    <script src="js/chart-custom.js"></script>
    <!-- Custom JavaScript -->
    <script src="js/custom.js"></script>

</body>
</html>

<?php
}
?>