<?php 
include 'header.php';
// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

$msg = "";
$err = "";

// Handle staking cancellation
if (isset($_GET['cancel'])) {
    $staking_id = $_GET['cancel'];
    $cancel = mysqli_query($link, "UPDATE user_stakings SET status = 'cancelled' WHERE id = '$staking_id'");
    if ($cancel) {
        echo "<script>alert('Staking cancelled successfully');window.location.href = '".$_SERVER['PHP_SELF']."'</script>";
    } else {
        echo "<script>alert('Error cancelling staking: " . mysqli_error($link) . "');</script>";
    }
}
?>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<link rel="stylesheet" href=" https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href=" https://cdn.datatables.net/1.10.19/css/dataTables.jqueryui.min.css">
<link rel="stylesheet" href=" https://cdn.datatables.net/buttons/1.5.6/css/buttons.jqueryui.min.css">
<link rel="stylesheet" href=" https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href=" https://cdn.datatables.net/buttons/1.5.6/css/buttons.bootstrap.min.css">

<style>
.table-responsive {
    overflow-x: hidden;
}
@media (max-width: 8000px) {
    .table-responsive {
        overflow-x: auto;
    }
}
</style>

<div class="page-content">
    <div class="container-fluid">
        <?php 
        if ($msg != "") {
            echo "<div style='padding:20px;background-color:#dce8f7;color:black'> $msg</div class='btn btn-success'>" ."</br></br>"; 
        }
        if ($err != "") {
            echo "<div style='padding:20px;background-color:#f8d7da;color:#721c24'> $err</div>";
        }
        ?>

        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title mb-0">All User Stakings</h4>
                    </div><!-- end card header -->

                    <div class="card-body">
                        <div id="customerList">
                            <div class="table-responsive table-card mt-3 mb-1">
                                <table id="myTable" class="table table-hover table-bordered">  
                                    <thead class="table-light">
                                        <tr>
                                            <th>ID</th>
                                            <th>User</th>
                                           
                                            <th>Amount</th>
                                            <th>Duration</th>
                                            <th>APR</th>
                                            <th>Daily Return</th>
                                            <th>Start Date</th>
                                            <th>End Date</th>
                                            <th>Status</th>
                                            <th>Created At</th>
                                            <!--<th>Action</th>-->
                                        </tr>
                                    </thead>
                                    <tbody>
                                       <?php 
    $sql = "SELECT * FROM user_stakings  ORDER BY id DESC";
    $result = mysqli_query($link, $sql);
    if(mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {   
            $userId = $row['user_id'];
            $user = mysqli_query($link, "SELECT * FROM users WHERE id = '$userId'");
            $user_r = mysqli_fetch_assoc($user);
    ?>
                                        <tr>
                                            <td><?php echo $row['id']; ?></td>
                                            <td>
                                                <?php echo htmlspecialchars($user_r['first_name']." ".$user_r['last_name']); ?>
                                                <br><small><?php echo htmlspecialchars($user_r['email']); ?></small>
                                            </td>
                                          
                                            <td><?php echo number_format($row['amount'], 8); ?></td>
                                            <td><?php echo $row['duration_days']; ?> days</td>
                                            <td><?php echo $row['apr']; ?>%</td>
                                            <td><?php echo number_format($row['daily_return'], 8); ?></td>
                                            <td><?php echo date('Y-m-d H:i', strtotime($row['start_date'])); ?></td>
                                            <td><?php echo date('Y-m-d H:i', strtotime($row['end_date'])); ?></td>
                                            <td>
                                                <span class="badge 
                                                    <?php 
                                                    if($row['status'] == 'active') echo 'bg-success';
                                                    elseif($row['status'] == 'completed') echo 'bg-primary';
                                                    else echo 'bg-warning';
                                                    ?>">
                                                    <?php echo ucfirst($row['status']); ?>
                                                </span>
                                            </td>
                                            <td><?php echo date('Y-m-d H:i', strtotime($row['created_at'])); ?></td>
                                           
                                        </tr>
                                        <?php
                                            }
                                        } else {
                                        ?>
                                        <tr>
                                            <td colspan="12" class="text-center">No staking records found</td>
                                        </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div><!-- end card body -->
                </div><!-- end card -->
            </div><!-- end col -->
        </div><!-- end row -->
    </div><!-- end container-fluid -->
</div><!-- end page-content -->

<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/dataTables.jqueryui.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.jqueryui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.colVis.min.js"></script>

<script>
$(document).ready(function(){
    $('#myTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ],
        responsive: true,
        order: [[0, 'desc']]
    });
});
</script>

<?php include 'footer.php'; ?>