<?php 

require_once("header.php");


$user_id = $_SESSION['SESS_ADMINUSERID'] ?? null;

// Fetch current user info
$stmt = $pdo->prepare("SELECT * FROM logins WHERE id = :id");
$stmt->execute([':id' => $user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
?>




<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
        <!-- ============================================================== -->
        <!-- End Left Sidebar - style you can find in sidebar.scss  -->
        <!-- ============================================================== -->
        <!-- ============================================================== -->
        <!-- Page wrapper  -->
        <!-- ============================================================== -->
        <div class="page-wrapper">
            <!-- ============================================================== -->
            <!-- Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
             <div class="page-breadcrumb">
                <div class="row">
                    <div class="col-12 d-flex no-block align-items-center">
                        <h4 class="page-title">Dashboard - <?php echo htmlspecialchars($user['firstname']); ?></h4>
                        <div class="ml-auto text-right">
                            <nav aria-label="breadcrumb">
                                <ol class="breadcrumb">
                                    <li class="breadcrumb-item"><a href="#">Home</a></li>
                                    <li class="breadcrumb-item active" aria-current="page">Library</li>
                                </ol>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
            <!-- ============================================================== -->
            <!-- End Bread crumb and right sidebar toggle -->
            <!-- ============================================================== -->
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            <div class="container-fluid">
                <!-- ============================================================== -->
                <!-- Sales Cards  -->
                <!-- ============================================================== -->
               
               
               
               <?php
// Reusable count function using PDO
function getCount($pdo, $query) {
    $stmt = $pdo->query($query);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    return $row['total'] ?? 0;
}
?>

<div class="row">

    <!-- INBOX -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="inbox.php">
                <div class="box bg-warning text-center">
                    <h1 class="font-light text-white">
                        <img src="img/dash/inbox.png" width="60" height="40">
                        <span class="label label-important">
                            <?php echo getCount($pdo, "SELECT COUNT(id) AS total FROM message WHERE status='0'"); ?>
                        </span>
                    </h1>
                    <h6 class="text-white">INBOX</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- OUTBOX -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="outbox.php">
                <div class="box bg-danger text-center">
                    <h1 class="font-light text-white">
                        <img src="img/dash/outbox.png" width="60" height="40">
                        <span class="label label-important">
                            <?php echo getCount($pdo, "SELECT COUNT(id) AS total FROM message WHERE status='1'"); ?>
                        </span>
                    </h1>
                    <h6 class="text-white">OUTBOX</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- CUSTOMERS -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="index.php">
                <div class="box bg-info text-center">
                    <h1 class="font-light text-white">
                        <img src="img/dash/customers.png" width="60" height="40">
                        <span class="label label-important">
                            <?php echo getCount($pdo, "SELECT COUNT(id) AS total FROM logins") - 2; ?>
                        </span>
                    </h1>
                    <h6 class="text-white">CUSTOMERS</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- SETTINGS -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="settings.php?id=1">
                <div class="box bg-success text-center">
                    <h1 class="font-light text-white">
                        <img src="img/dash/settings.png" width="60" height="40">
                    </h1>
                    <h6 class="text-white">SETTINGS</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- PASSWORD -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="password.php">
                <div class="box bg-success text-center">
                    <h1 class="font-light text-white">
                        <i class="fa fa-lock" style="font-size:48px;color:red"></i>
                    </h1>
                    <h6 class="text-white">PASSWORD</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- TRANSACTION HISTORY -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="transactions.php">
                <div class="box bg-success text-center">
                    <h1 class="font-light text-white">
                        <img src="img/dash/fundout.png" width="45" height="45">
                        <span class="label label-important">
                            <?php echo getCount($pdo, "SELECT COUNT(id) AS total FROM transactions"); ?>
                        </span>
                    </h1>
                    <h6 class="text-white">TRANSACTION HISTORY</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- WEBMAIL -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="webmail.php?id=1">
                <div class="box bg-success text-center">
                    <h1 class="font-light text-white">
                        <i class="fa fa-envelope" style="font-size:48px;color:red"></i>
                    </h1>
                    <h6 class="text-white">WEBMAIL</h6>
                </div>
            </a>
        </div>
    </div>

    <!-- CONTACT WEBMASTER -->
    <div class="col-md-6 col-lg-2 col-xlg-3">
        <div class="card card-hover">
            <a href="https://wa.me/<?php echo $webmasta ?>/?text=Hi Webmaster, I Have an issue on my website.... <?php echo $url ?>">
                <div class="box bg-success text-center">
                    <h1 class="font-light text-white">
                        <i class="fab fa-whatsapp-square" style="font-size:48px;color:red"></i>
                    </h1>
                    <h6 class="text-white">Contact Webmaster</h6>
                </div>
            </a>
        </div>
    </div>

</div>

         
         
   <?php
// Assuming $pdo is your PDO database connection and $config_basedir is defined

// Helper function to validate the ID
function validate_id($id, $config_basedir) {
    if (!is_numeric($id)) {
        header("Location: $config_basedir/login.php");
        exit();
    }
    return (int)$id; // Return sanitized integer ID
}

// Delete user
if (isset($_GET['del']) && isset($_GET['del']) === TRUE) {
    $validid = validate_id($_GET['del'], $config_basedir);
    $stmt = $pdo->prepare("DELETE FROM logins WHERE id = ?");
    $stmt->execute([$validid]);
    $delete = 2;
}

// Billing
if (isset($_GET['funcBILL1']) && $_GET['funcBILL1'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET bill = 2 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcBILL2']) && $_GET['funcBILL2'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET bill = 1 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

// Transaction
if (isset($_GET['funcTFac']) && $_GET['funcTFac'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET err = 0 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcTFde']) && $_GET['funcTFde'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET err = 1 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

// Photo
if (isset($_GET['funcPHOTO1']) && $_GET['funcPHOTO1'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET photo = 2 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcPHOTO2']) && $_GET['funcPHOTO2'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET photo = 3 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

// Utility
if (isset($_GET['funcULTAPRV']) && $_GET['funcULTAPRV'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET ultility_doc = 2 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcULTDIS']) && $_GET['funcULTDIS'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET ultility_doc = 3 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

// Doc Verification
if (isset($_GET['funcDOCap']) && $_GET['funcDOCap'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET verified = 2 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcDOCdc']) && $_GET['funcDOCdc'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET verified = 3 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcDOCre']) && $_GET['funcDOCre'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET verified = 1 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

// Deposit
if (isset($_GET['funcDEPOSITap']) && $_GET['funcDEPOSITap'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET confirm = 2 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcDEPOSITdc']) && $_GET['funcDEPOSITdc'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET confirm = 3 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcDEPOSITre']) && $_GET['funcDEPOSITre'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET confirm = 1 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

// Withdraw
if (isset($_GET['funcWITHDRap']) && $_GET['funcWITHDRap'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET confirm2 = 2 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcWITHDRdc']) && $_GET['funcWITHDRdc'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET confirm2 = 3 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcWITHDRre']) && $_GET['funcWITHDRre'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET confirm2 = 1 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}





// Account enable/disable
if (isset($_GET['funcSTATenable']) && $_GET['funcSTATenable'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET able = 1 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcSTATdisable']) && $_GET['funcSTATdisable'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET able = 0 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}











// User status change
if (isset($_GET['funcSTATac']) && $_GET['funcSTATac'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET status = 0 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcSTATonhold']) && $_GET['funcSTATonhold'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET status = 1 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}

if (isset($_GET['funcSTATde']) && $_GET['funcSTATde'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET status = 2 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}







if (isset($_GET['funcSTATdsuspecious']) && $_GET['funcSTATdsuspecious'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET status = 3 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}


if (isset($_GET['funcSTATfrz']) && $_GET['funcSTATfrz'] == "conf") {
    $validid = validate_id($_GET['id'], $config_basedir);
    $stmt = $pdo->prepare("UPDATE logins SET status = 4 WHERE id = ?");
    $stmt->execute([$validid]);
    header("Location: {$config_basedir}login.php");
}





?>

      
      
      
      
    
    
                   
                   <nav aria-label="breadcrumb">
    <ol class="breadcrumb">
        <li class="btn btn-success"><a href="index.php">Home</a></li>
        <li class="btn btn-info"><a href="index.php">All Users</a></li>

        <?php  
        $get = $_SESSION['role'];
        $getv = $_SESSION['ad'];

        if (($get == 'admin' || $get == 'staff') && ($getv == 1)) {
            // Admin or staff with active status
            echo "<li class='btn btn-warning'><a href='../connx/add_user.php'>Add User</a></li>";
        } else {
            // Non-admin users or inactive admins
            echo "<li class='btn btn-warning'><a target='_blank' href='../connx/add_user.php'>Add User</a></li>"; // '#' here will make the link unclickable
        }
        ?>
    </ol>
</nav>

           
            <div class='container-fluid'>
               
                <div class='row'>
                    <div class='col-12'>
                        <div class='card'>
                           
                  <?php

// Fetch records using PDO (replace 'logins' with your actual table name)
$sql = "SELECT * FROM logins LIMIT 2,1000000";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Check if there is a success message (for deletion)
// if (isset($delete)) {
//     echo "<span style='color:red'>Successfully deleted user</span>";
// }

echo "
<input type='text' id='myInput' onkeyup='myFunction()' placeholder='Search for names..' title='Type in a name' class='table table-striped table-bordered'>
<div class='card'>
    <div class='card-body'>
        <h5 class='card-title'></h5>
        <div class='table-responsive'>
            <table id='myTable' class='table table-striped table-bordered'>
                <tr class='header'>
                    <th>DATE</th>
                    <th>Full Name</th>
                    <th>User Name</th>
                    <th>Phone No</th>
                    <th>Account No</th>
                    <th>State</th>
                    <th>City</th>
                    <th>Address</th>
                    <th>E-mail Address</th>
                    <th>Password</th>
                    <th>Transaction Pin</th>
                    <th>Currency</th>
                    <th>Account Balance</th>
                    <th>STATUS</th>
                    <th>ACCOUNT</th>
                    <th>PHOTO</th>
                    <th>FRONT CARD</th>
                    <th>BACK CARD</th>
                    <th>MANAGE PROFILE</th>
                    <th>MANAGE EMAILS</th>
                    <th>MANAGE ADDRESS</th>
                    <th>MANAGE ACCOUNT</th>
                    <th>MANAGE SECURITY</th>
                    <th>MANAGE BILLINGS</th>
                    <th>ACTION(DEL.....)</th>
                </tr>";

foreach ($rows as $row) {
    echo "<tr>";
    echo "<td>" . htmlspecialchars($row['date']) . "</td>";
    echo "<td>" . htmlspecialchars($row['firstname'] . " " . $row['lastname']) . "</td>";
    echo "<td>" . htmlspecialchars($row['username']) . "</td>";
    echo "<td>" . htmlspecialchars($row['phone']) . "</td>";
    echo "<td>" . htmlspecialchars($row['acc_no']) . "</td>";
    echo "<td>" . htmlspecialchars($row['state']) . "</td>";
    echo "<td>" . htmlspecialchars($row['city']) . "</td>";
    echo "<td>" . htmlspecialchars($row['address']) . "</td>";
    echo "<td>" . htmlspecialchars($row['email']) . "</td>";
    echo "<td>" . htmlspecialchars($row['cpassword']) . "</td>";
    echo "<td>" . htmlspecialchars($row['pin']) . "</td>";

    // Handling Currency
    $currency = [
        1 => 'USD',
        2 => 'GBP',
        3 => 'EUR',
        4 => 'AUD',
        5 => 'CAD',
        6 => 'CNY',
        7 => 'ZAR',
        8 => 'NGN'
    ];
    echo "<td>" . (isset($currency[$row['currency']]) ? $currency[$row['currency']] : 'N/A') . "</td>";
    echo "<td>" . htmlspecialchars($row['balance']) . "</td>";

    // Account Status Logic
  
  echo "<td>";
//   $getu=$_SESSION['role'];
//           $get=$_SESSION['st'];
//           if($get==1){
           
         if($row['status'] == 0)
         {
          echo "***Account is Active***";
         }
         
         if($row['status'] == 1)
         {
          echo "***Account is OnHold***";
         }
         
          if($row['status'] == 2)
         {
          echo "***Account is Blocked***";
         }
         
          if($row['status'] == 3)
         {
          echo "***Suspecious Login***";
         }
         
          if($row['status'] == 4)
         {
          echo "***Account is Freezed***";
         }
          
          
         
             echo "
             
 <div class='container'>
 <div class='btn-group'>
    <a href='index.php?funcSTATac=conf&id=" . $row['id'] . "' type='button' class='btn btn-primary'>Activate</a>
    
    <a href='index.php?funcSTATonhold=conf&id=" . $row['id'] . "' type='button' class='btn btn-primary'>OnHold</a>
    
    <a href='index.php?funcSTATde=conf&id=" . $row['id'] . "' type='button' class='btn btn-primary'>Block</a>
    
    <a href='index.php?funcSTATdsuspecious=conf&id=" . $row['id'] . "' type='button' class='btn btn-primary'>Suspecious</a>
    
     <a href='index.php?funcSTATfrz=conf&id=" . $row['id'] . "' type='button' class='btn btn-primary'>Freeze</a>
  </div>
</div>
      
             
             
             
           
       
             ";
    //  }
           
     
     
     
     
     
         
    

     
    echo "</td>";


    // Account Actions
    echo "<td>";
    if ($row['able'] != 1) {
        echo "Account is Disabled";
        echo "<a class='btn btn-success' href='index.php?funcSTATenable=conf&id=" . $row['id'] . "'>Enable Account</a>";
    } else {
        echo "Account is Enabled";
        echo "<a class='btn btn-danger' href='index.php?funcSTATdisable=conf&id=" . $row['id'] . "'>Disable Account</a>";
    }
    echo "</td>";

    // Handling Image URLs
    echo "<td>";
    $photoUrl = ($row['photo'] == 1) ? $row['P_location'] : 'default.png';
    echo "<a href='../$photoUrl' style='color: black;'><img src='../$photoUrl' alt='" . htmlspecialchars($row['username']) . "' style='width:100px;height:100px'></a>";
    echo "</td>";
    
     // Handling Image URLs
echo "<td>";
$photoUrl = (!empty($row['DF_Location'])) ? $row['DF_Location'] : 'default.png';
echo "<a href='../$photoUrl' style='color: black;'><img src='../$photoUrl' alt='" . htmlspecialchars($row['username']) . "' style='width:100px;height:100px'></a>";
echo "</td>";

    
   // Handling Image URLs
echo "<td>";
$photoUrl = (!empty($row['DB_Location'])) ? $row['DB_Location'] : 'default.png';
echo "<a href='../$photoUrl' style='color: black;'><img src='../$photoUrl' alt='" . htmlspecialchars($row['username']) . "' style='width:100px;height:100px'></a>";
echo "</td>";


    // Managing Profile
    echo "<td><a class='btn btn-success' href='view_user.php?id=" . $row['id'] . "' style='color: white;'>Profile</a></td>";
    echo "<td><a class='btn btn-success' href='view_email.php?id=" . $row['id'] . "' style='color: white;'>Email</a></td>";
    echo "<td><a class='btn btn-warning' href='view_address.php?id=" . $row['id'] . "' style='color: white;'>Address</a></td>";
    echo "<td><a class='btn btn-warning' href='view_account.php?id=" . $row['id'] . "' style='color: white;'>Account</a></td>";
    echo "<td><a class='btn btn-warning' href='view_security.php?id=" . $row['id'] . "' style='color: white;'>Security</a></td>";

    // Manage Billing
    echo "<td><a class='btn btn-warning' href='billing.php?id=" . $row['id'] . "' style='color: white;'>Billing</a></td>";

  // Delete Action (Optional)
echo "<td><a class='btn btn-danger' href='#' onclick='confirmDelete(" . $row['id'] . ")'>Delete</a></td>";
echo "</tr>";

// Add this script at the bottom of your HTML
echo "<script>
    function confirmDelete(id) {
        var confirmDelete = confirm('Are you sure you want to delete this item?');
        if (confirmDelete) {
            // Wait for 5 seconds before redirecting
            setTimeout(function() {
                window.location.href = 'index.php?del=' + id;
            }, 2000); // 2000 milliseconds = 5 seconds
        }
    }
</script>";

}

echo "</table></div></div></div>";

?>

        


                
                    </div>
                </div>
               
            </div>
        


                        
                        
                    </div>
                </div>
               
            </div>
       
                <!-- ============================================================== -->
                <!-- Sales chart -->
                <!-- ============================================================== -->
              
                <!-- ============================================================== -->
                <!-- Recent comment and chats -->
                <!-- ============================================================== -->
            </div>
            <!-- ============================================================== -->
            <!-- End Container fluid  -->
            <!-- ============================================================== -->
            <!-- ============================================================== -->
            
            
            
            
            
            
            
            
           <?php require ("footer.php")?>