<?php
include("includes/header.php");
$page_title = "Manage Transactions";

$_GET = validate_input($_GET);
$tableName = "tbl_transaction";
$targetpage = "manage_transactions.php";
$limit = 10;

$searchInput = $_GET['keyword'];
if ($searchInput != "") {
    $query = "SELECT COUNT(*) as num FROM tbl_transaction WHERE tbl_transaction.`user_name` LIKE '%$searchInput%' 
    OR tbl_transaction.`user_email` LIKE '%$searchInput%' OR tbl_transaction.`plan_name` LIKE '%$searchInput%'
    OR tbl_transaction.`plan_amount` LIKE '%$searchInput%' OR tbl_transaction.`plan_duration` LIKE '%$searchInput%'
    OR tbl_transaction.`payment_date` LIKE '%$searchInput%' OR tbl_transaction.`plan_expiry_date` LIKE '%$searchInput%'";
    $total_pages = mysqli_fetch_array(mysqli_query($mysqli, $query));
    $total_pages = $total_pages['num'];
} else {
    $query = "SELECT COUNT(*) as num FROM $tableName";
    $total_pages = mysqli_fetch_array(mysqli_query($mysqli, $query));
    $total_pages = $total_pages['num'];
}

$stages = 3;
$page = 0;
if (isset($_GET['page'])) {
    $page = mysqli_real_escape_string($mysqli, $_GET['page']);
}
if ($page) {
    $start = ($page - 1) * $limit;
} else {
    $start = 0;
}

if ($searchInput != "") {
    $sql_query = "SELECT * FROM tbl_transaction WHERE tbl_transaction.`user_name` LIKE '%$searchInput%' 
    OR tbl_transaction.`user_email` LIKE '%$searchInput%' OR tbl_transaction.`plan_name` LIKE '%$searchInput%'
    OR tbl_transaction.`plan_amount` LIKE '%$searchInput%' OR tbl_transaction.`plan_duration` LIKE '%$searchInput%'
    OR tbl_transaction.`payment_date` LIKE '%$searchInput%' OR tbl_transaction.`plan_expiry_date` LIKE '%$searchInput%'
    ORDER BY tbl_transaction.`id` DESC LIMIT $start, $limit";
    $result = mysqli_query($mysqli, $sql_query) or die(mysqli_error($mysqli));
} else {
    $sql_query = "SELECT * FROM tbl_transaction ORDER BY tbl_transaction.`id` DESC LIMIT $start, $limit";
    $result = mysqli_query($mysqli, $sql_query) or die(mysqli_error($mysqli));
}
?>
<main id="main" class="main">
    <div class="row">
        <div class="col-lg-6">
            <div class="pagetitle">
                <h1><?php _e($page_title); ?></h1>
            </div>
        </div>
        <div class="col-lg-6">
            <form method="get" id="searchForm" action="">
                <input class="form-control input-sm" placeholder="Search here..." aria-controls="DataTables_Table_0" type="search" name="keyword" value="<?php if (isset($_GET['keyword'])) {
                                                                                                                                                                _e($_GET['keyword']);
                                                                                                                                                            } ?>" required="required">
            </form>
        </div>
    </div><br>
    <section class="section">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>TRANSACTION ID</th>
                                        <th>USER NAME</th>
                                        <th>USER EMAIL</th>
                                        <th>PLAN NAME</th>
                                        <th>PAYMENT DATE</th>
                                        <th>AMOUNT</th>
                                        <th>PALN DURATION</th>
                                        <th>EXPIRY DATE</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        $i = $start + 1;
                                    while ($row = mysqli_fetch_assoc($result)) {
                                    ?>
                                        <tr>
                                            <td><?php _e($i); ?></td>
                                            <td><?php _e($row['transaction_id']); ?></td>
                                            <td><?php _e($row['user_name']); ?></td>
                                            <td><?php _e($row['user_email']); ?></td>
                                            <td><?php _e($row['plan_name']); ?></td>
                                            <td><?php _e($row['payment_date']); ?></td>
                                            <td><?php _e($row['plan_amount']); ?></td>
                                            <td><?php _e($row['plan_duration']); ?></td>
                                            <td><?php _e($row['plan_expiry_date']); ?></td>
                                        </tr>
                                    <?php
                                    $i++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                            <!-- End Table with stripped rows -->
                        </div>
                    </div>
                    <div class="col-md-12 col-xs-12 d-flex justify-content-end paginationRight">
                        <nav aria-label="Page navigation example">
                            <?php
                                include("pagination.php");
                            ?>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </section>
    </div>
</main>
<?php include("includes/footer.php"); ?>