<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header2.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";

?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>List</h2>
                    </div>
                </div>

                 <!-- /. ROW  -->
                <hr />                
            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             List of all members
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-hover" id="dataTables-example">
                                    <thead>
                                        <tr>
                                            <th>#</th>
                                            <th>Foto</th>
                                            <th>Username</th>
                                            <th>Full Name</th>
                                            <th>MyKad No.</th>
                                            <th>Package</th>
                                            <th>Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $c = 1;
                                        $sql = "SELECT id, username, photo, nama, kp, pakej, status FROM ahli WHERE username NOT IN ('admin','developer')";
                                        $result = mysqli_query($con, $sql);
                                        
                                        while ($row = mysqli_fetch_array($result)) {
                                            $photoFile = "uploads/profile/{$row['username']}.jpg";  // based on username or your logic
                                            $defaultImage = "assets/img/find_user.png";
                                        
                                            // check both DB photo path and actual file existence
                                            if (!empty($row['photo']) && file_exists($row['photo'])) {
                                                $imgSrc = $row['photo'];
                                            } elseif (file_exists($photoFile)) {
                                                $imgSrc = $photoFile;
                                            } else {
                                                $imgSrc = $defaultImage;
                                            }
                                        ?>
                                            <tr class="even">
                                                <td><?= $c++ ?></td>
                                                <td class="text-left align-middle">
                                                    <img src="<?= htmlspecialchars($imgSrc) ?>"
                                                         class="img-thumbnail"
                                                         style="width:30px; height:30px; border-radius:15%; object-fit:cover; border:1px solid #ddd;"
                                                         alt="User profile image" />
                                                </td>
                                                <td><code><?= htmlspecialchars($row['username']) ?></code></td>
                                                <td><?= htmlspecialchars($row['nama']) ?></td>
                                                <td><?= htmlspecialchars($row['kp']) ?></td>
                                                <td><?= htmlspecialchars($row['pakej']) ?></td>
                                                <td>
                                                    <?php
                                                    if (empty($row['status']) || strtolower($row['status']) == "inactive") {
                                                        echo '<span class="btn btn-default btn-xs">INACTIVE</span>';
                                                    } else {
                                                        echo '<span class="btn btn-success btn-xs">'.strtoupper($row['status']).'</span>';
                                                    }
                                                    ?>
                                                </td>
                                                <td>
                                                    <a href="profile.php?u=<?= urlencode($row['username']) ?>" class="btn btn-primary btn-xs">
                                                        <i class="fa fa-edit"></i> Edit
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>

                                </table>
                            </div>
                            
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>          
    </div>
             <!-- /. PAGE INNER  -->
            </div>
<?
    include "_footer2.php";
}
?>
