<?php
session_start(); 
$u_uname = '';
if (isset($_COOKIE['auth_token'])) {
    $u_uname = $_COOKIE['auth_token'];
} else {
    header("location:login.php");
    exit; 
}
?>

<?php include 'include/header.php'; ?>
<?php include 'include/aside.php'; ?>
<?php include 'include/connection.php'; ?>

<!--start page wrapper -->
<div class="page-wrapper">
    <div class="page-content">
        <h6 class="mb-0 text-uppercase">Server History</h6>
        <hr/>

        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table id="example2" class="table table-striped table-bordered">
                        <thead>
                            <tr>
                                <th>SR. No</th>
                                <th>Email ID</th>
                                <th>Project Name</th>
                                <th>Amount</th>
                                <th>Start Date</th>
                                <th>End Date</th>
                                <th>Date</th>
                                <th>Package ID</th>
                                 <th>Validity</th>
                                <th>Server Status</th>
                                <th>Status Change</th>
                                <th>Order ID</th>
                                
                                
                              
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $sql = "SELECT * FROM  server order by id desc";
                            $result = $link->query($sql);
                            $sr = 1; 
                            if ($result->num_rows > 0) {
                                while($row = $result->fetch_assoc()) {
                                    ?>
                                    <tr>
                                    <td><?php echo  $sr ?></td>
                                    <td><?php echo$row['email'] ?></td>
                                    <td><?php echo $row['project_name'] ?></td>
                                    <td><?php echo $row['amount'] ?></td>
                                    <td><?php echo  $row['start_date'] ?></td>
                                    <td><?php echo$row['end_date'] ?></td>
                                    <td><?php echo $row['date'] ?></td>
                                    <td><?php echo $row['package_id'] ?></td>
                                    <td><?php echo  $row['validity'] ?></td>
                                    <td>
                                        <?php
                                          if($row['server_status'] == '0'){
                                            echo '<span class="badge bg-warning text-white rounded-pill">Pending</span>';
                                        } elseif ($row['server_status'] == '1') {
                                            echo '<span class="badge bg-success text-white rounded-pill">Running</span>';
                                        } elseif ($row['server_status'] == '2') {
                                            echo '<span class="badge bg-danger text-white rounded-pill">Expiry</span>';
                                        }
                                        ?>
                                    </td>
                                  <td>
                                            <select onchange="performAction(this.value, <?php echo $row['id']; ?>)" class="mt-2">
                                            <option value="">Select Action</option>
                                            <option value="0">Pending</option>
                                            <option value="1">Running</option>
                                            <option value="2">Expiry</option>
                                        </select>
                                    </td>

                                    <td><?php echo $row['order_id'] ?></td>
                                    </tr>
                                    <?php
                                    $sr++; 
                                }
                            } else {
                                echo "<tr><td colspan='8'>No data available</td></tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
function performAction(action, id) {
    if (action) {
        window.location.href = `actionclient.php?action=${action}&id=${id}`;
    }
}
</script>

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