  <?php require ("header.php")?>
  
 
     
     <?php

// Define base path for redirection
$config_basedir = "/admin/"; // Change this to your actual admin base path

// Function to validate numeric ID
function pf_validate_number($value, $redirect, $base) {
    if (isset($value) && is_numeric($value)) {
        return (int)$value;
    } else {
        header("Location: " . $base . $redirect);
        exit();
    }
}

// DELETE message
if (isset($_GET['id']) && !isset($_GET['func'])) {
    $validid = pf_validate_number($_GET['id'], "inbox.php", $config_basedir);

    $stmt = $pdo->prepare("DELETE FROM message WHERE id = :id");
    $stmt->execute(['id' => $validid]);

    echo "
<script>
    // Show alert
    alert('Successfully deleted mail from outbox');

    // Auto-redirect after 5 seconds
    setTimeout(function() {
        window.location.href = 'inbox.php';
    }, 5000);
</script>";
   
}

// VERIFY message (confirm)
if (isset($_GET['func']) && $_GET['func'] === "conf" && isset($_GET['id'])) {
    $validid = pf_validate_number($_GET['id'], "inbox.php", $config_basedir);

    $stmt = $pdo->prepare("UPDATE message SET verified = 2 WHERE id = :id");
    $stmt->execute(['id' => $validid]);

    header("Location: " . $config_basedir . "inbox.php");
    exit();
}
?>

       
       
        
    <div class="main-panel">
        <div class="content bg-light">
            <div class="page-inner">
               
               
                <div class="mt-2 mb-4">
                    <h1 class="title1 text-dark">INBOX</h1>
                </div>
                
                
               
               
                <div class="mb-5 row">
                    <div class="col-md-12 ">
                        <!--<div class="card shadow p-4 bg-light">-->
                           
                           
   
           <?php 
if(isset($success)){
    echo $success;
}
?>

                           
           
           
           
          <div class="page-breadcrumb">
    <div class="row">
        <div class="col-12 d-flex no-block align-items-center">
            <h4 class="page-title">
                INBOX MAILS (
                <?php
                $stmt = $pdo->query("SELECT COUNT(id) AS total FROM message WHERE status = 1");
                $result = $stmt->fetch(PDO::FETCH_ASSOC);
                echo $result['total'];
                ?>
                )
            </h4>
            <div class="ml-auto text-right">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Inbox</li>
                    </ol>
                </nav>
            </div>
        </div>
    </div>
</div>

        

  <div class='container-fluid'>
               
               
                <div class='row'>
                    <div class='col-md-10'>
                        <div class=''>
                            
                            
                            <div class='card-body'>
                                <h4 class='card-title m-b-0'>Recent Posts</h4>
                            </div>
                            
                            
                           <div class='comment-widgets scrollable'>
                                 <!--Comment Row -->
                               
                               
                               
                               
                      <?php

// Fetch messages from database
$orderssql = "SELECT * FROM message ORDER BY id DESC";
$ordersres = $pdo->query($orderssql);
$rows = $ordersres->fetchAll(PDO::FETCH_ASSOC);

// Show delete success message
if (isset($delete)) {
    echo "<center>
        <span style='color:red'>Successfully deleted mail from outbox</span><br><br>
        <div class='loader'></div>
        <meta http-equiv='refresh' content='5; url=inbox.php'/>
    </center>";
}

// Loop through messages
foreach ($rows as $row) {
    if ($row['status'] == 1) {
        echo "
        <div class='d-flex flex-row comment-row m-t-0'>
            <div class='p-2'>";
        
        // Profile image logic
        if ($row['status'] > 0) {
            echo "
            <img src='../accounts/upload/photo/default.png' class='rounded-circle' alt='" . htmlspecialchars($row['username']) . "' style='width:50px;height:50px'>";
        } elseif ($row['status'] == 1) {
            echo "
            <img src='../" . htmlspecialchars($row['P_location']) . "' class='rounded-circle' alt='" . htmlspecialchars($row['username']) . "' style='width:50px;height:50px'>";
        }

        echo "
            </div>
            <div class='comment-text w-100'>
                <h6 class='font-medium'>" . htmlspecialchars($row['name']) . "</h6>
                <span class='m-b-15 d-block'>" . nl2br(htmlspecialchars($row['note'])) . "</span>
                <div class='comment-footer'>
                    <span class='btn btn-success btn-sm float-right'>" . htmlspecialchars($row['date']) . "</span>
                    <a class='btn btn-info btn-sm' href='reply_inbox.php?id=" . $row['id'] . "'>View</a>
                    <a class='btn btn-success btn-sm' #='reply_msg.php?id=" . $row['id'] . "'>Reply</a>
                    <a class='btn btn-danger btn-sm' href='inbox.php?id=" . $row['id'] . "'>Delete</a>
                </div>
            </div>
        </div>";
    }
}
?>

                                
                                
                                </div></div></div>
                            </div>
                        </div>
                          <!-- accoridan part -->
                          
                          
                          
                       
               </div></div></div></div></div>
               
            </div>
           
             <?php require ("footer.php")?>