<?php 
include 'header.php';

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


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


<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">
  <link rel="stylesheet" href="">
 
  

  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
 

  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/dataTables.jqueryui.min.js"></script>
  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>

  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.jqueryui.min.js"></script>
   
  <script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
  <script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
  <script type="text/javascript" charset="utf8" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
  <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.colVis.min.js"></script>
<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 customAlert("error", $err);
    }

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

			            <div class="card-body">
			                <div id="customerList">
			                    

			                    <div class="table-responsive table-card mt-3 mb-1">
			                    	<table id="investmentTable" class="table table-bordered table-striped">
                                <thead class="table-light">
                                    <tr>
                                       
                                        <th>Investment Amount</th>
                                        <th>Plan Name</th>
                                        <th>Increase (%)</th>
                                        <th>Duration</th>
                                        <th>Total Profit</th>
                                        <th>Start Date</th>
                                        <th>End Date</th>
                                        <th>Status</th>
                                        <th>Date Created</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $investment_sql = "SELECT * FROM users_investment ORDER BY id DESC";
                                    $investment_result = mysqli_query($link, $investment_sql);
                                    
                                    if(mysqli_num_rows($investment_result) > 0) {
                                        while($investment = mysqli_fetch_assoc($investment_result)) {
                                            // Get user details
                                            $user_id = $investment['userId'];
                                            $user_sql = "SELECT fname, lname, email FROM users WHERE id = '$user_id'";
                                            $user_result = mysqli_query($link, $user_sql);
                                            $user = mysqli_fetch_assoc($user_result);
                                    ?>
                                    <tr>
                                       
                                        <td>$<?php echo number_format($investment['invest_amt'], 2); ?></td>
                                        <td><?php echo $investment['plan_name']; ?></td>
                                        <td><?php echo $investment['increase']; ?>%</td>
                                        <td><?php echo $investment['duration']; ?> days</td>
                                        <td>$<?php echo number_format($investment['total_profit'], 2); ?></td>
                                        <td><?php echo date('M d, Y', strtotime($investment['start_date'])); ?></td>
                                        <td><?php echo date('M d, Y', strtotime($investment['end_date'])); ?></td>
                                        <td>
                                            <span class="badge bg-<?php echo ($investment['status'] == 'Active') ? 'success' : 'warning'; ?>">
                                                <?php echo $investment['status']; ?>
                                            </span>
                                        </td>
                                        <td><?php echo date('M d, Y H:i', strtotime($investment['date'])); ?></td>
                                        <td>
                                            <div class="btn-group">
                                               
                                                <a href="?delete_investment=<?php echo $investment['id']; ?>" class="btn btn-sm btn-danger" 
                                                   onclick="return confirm('Are you sure you want to delete this investment?')">
                                                   <i class="fas fa-trash"></i> Delete
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php
                                        }
                                    } else {
                                        echo '<tr><td colspan="13" class="text-center">No investments found</td></tr>';
                                    }
                                    ?>
                                </tbody>
                            </table>
			                        
			                    </div>


			                </div>
			            </div><!-- end card -->
			        </div>
			        <!-- end col -->
			    </div>
			    <!-- end col -->
			</div>
			<!-- end row -->


	</div>
</div>
<script>
$(document).ready(function(){
    $('#myTable').dataTable();
});
</script>

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