<?php
session_start();

// Check if the user is logged in
if (!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true) {
    header("Location: login.php");
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Contact Leads Dashboard</title>

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
        href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
        rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">

</head>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

            <!-- Sidebar - Brand -->
            <a class="sidebar-brand d-flex align-items-center justify-content-center" href="index.html">
                <div class="sidebar-brand-icon rotate-n-15">
                    <i class="fas fa-laugh-wink"></i>
                </div>
                <div class="sidebar-brand-text mx-3">Campaigns<sup>gtp</sup></div>
            </a>

            <!-- Divider -->
            <hr class="sidebar-divider my-0">

            <!-- Nav Item - Dashboard -->
            <li class="nav-item active">
                <a class="nav-link" href="dash.php">
                    <i class="fas fa-fw fa-tachometer-alt"></i>
                    <span>Dashboard</span></a>
            </li>
 <hr class="sidebar-divider my-0">
            <li class="nav-item active">
                <a class="nav-link" href="news.php">
                    <!--<i class="fas fa-fw fa-tachometer-alt"></i>-->
                    <span>Newsletter</span></a>
            </li>
            
            <!-- Sidebar Toggler (Sidebar) -->
            <div class="text-center d-none d-md-inline">
                <button class="rounded-circle border-0" id="sidebarToggle"></button>
            </div>
           
        </ul>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" >

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                   

                    <!-- Topbar Navbar -->
                    <ul class="navbar-nav ml-auto">


                        <div class="topbar-divider d-none d-sm-block"></div>

                        <!-- Nav Item - User Information -->
                        <li class="nav-item dropdown no-arrow">
                            <a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
                                data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <span class="mr-2 d-none d-lg-inline text-gray-600 small"><?php echo $_SESSION["username"];?></span>
                                <img class="img-profile rounded-circle"
                                    src="img/undraw_profile.svg">
                            </a>
                            <!-- Dropdown - User Information -->
                            <div class="dropdown-menu dropdown-menu-right shadow animated--grow-in"
                                aria-labelledby="userDropdown">
                                
                                <a class="dropdown-item" href="logout.php" data-toggle="modal" data-target="#logoutModal">
                                    <i class="fas fa-sign-out-alt fa-sm fa-fw mr-2 text-gray-400"></i>
                                    Logout
                                </a>

                            </div>
                        </li>

                    </ul>

                </nav>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                <div class="container-fluid">

                    <!-- Page Heading -->
                    <div class="d-sm-flex align-items-center justify-content-between mb-4" >
                        <h1 class="h3 mb-0 text-gray-800">Leads Dashboard</h1>
                    </div>
<div class="card shadow mb-4">
                        <div class="card-header ">
                            <h6 class="m-0 font-weight-bold text-primary">Leads</h6>
                        </div>
                        <div class="card-body" style="width:100%">
                            <div class="table-responsive">
                                <table class="table table-bordered"  width="100%" cellspacing="0">
                                    <thead>
                                        <tr>
                                        <th style='width: 200px; word-wrap: break-word;' >Customer Name</th>
                                            <th>Email</th>
                                            <th>Phone</th>
                                            
                                            <th>Product</th>
                                            <th>Message</th>
                                            <th>Received_at</th>
                                            <th>Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <!--<tfoot>-->
                                        
                                    <!--    <tr>-->
                                    <!--        <th>Customer Name</th>-->
                                    <!--        <th>Email</th>-->
                                    <!--        <th>Phone</th>-->
                                    <!--        <th>Country</th>-->
                                    <!--        <th>Product</th>-->
                                    <!--        <th>Status</th>-->
                                    <!--        <th>Action</th>-->
                                    <!--    </tr>-->
                                    <!--</tfoot>-->
                                    <?php  
                                    if (isset($_GET['lus']) && $_GET['lus'] == 1) {
                                        echo'<div class="" role="alert">
                                        <strong>Lead Status</strong> Lead Status Updated.
                                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                          <span aria-hidden="true">&times;</span>
                                        </button>
                                      </div>';
                                    }
                                    ?>
                                    <tbody>
                                    <?php
// Database connection parameters
$host = "localhost";
$username = "collinsg_camgtpu";
$password = "dh+z9*R*V0mR";
$database = "collinsg_camgtpu";

try {
    // Create a PDO database connection
    $pdo = new PDO("mysql:host=$host;dbname=$database", $username, $password);

    // Set the PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // SQL query to retrieve data from the campaignleads table
    $sql = "SELECT * FROM contact ORDER by id DESC LIMIT 500";

    // Prepare the SQL statement
    $stmt = $pdo->prepare($sql);

    // Execute the statement
    $stmt->execute();

    // Fetch all rows as an associative array
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // Loop through the results and do something with the data
    foreach ($results as $row) {
        // Access data using column names (e.g., $row['column_name'])
        // echo "Lead ID: " . $row['id'] . "<br>";
        // echo "Name: " . $row['customername'] . "<br>";
        // echo "Email: " . $row['customeremail'] . "<br>";
        // Add more fields as needed
      
        echo "<tr>
        <td ><a style='display:block;width: 150px; word-wrap: break-word;' href='javascript:void(0);'>" . $row['name'] . "</a></td>
        <td ><a style='display:block;width: 150px; word-wrap: break-word;' href='mailto:" . $row['email'] . "'>" . $row['email'] . "</a></td>
        <td ><a style='display:block;width: 150px; word-wrap: break-word;' href='javascript:void(0);'>" . $row['phone'] . "</a></td>
       
        <td ><a style='display:block;width: 150px; word-wrap: break-word;' href='javascript:void(0);'>" . $row['product'] . "</a></td>
          <td ><a style='display:block;width: 150px; word-wrap: break-word;' href='javascript:void(0);'>" . $row['message'] . "</a></td>
        
        <td style='width: 200px; word-wrap: break-word;'>" . $row['created_at'] . "</td>";
        
        if($row['status']==0){
         echo "<td>Lead Pending </td>";   
            }elseif($row['status']==1){
                echo "<td>Lead in Progress </td>";   
            }elseif($row['status']==2){
                echo "<td>Lead Answered </td>";   
            }
        echo '<td><div class="dropdown show">
        <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
         Update Status
        </a>
      
        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
          <a class="dropdown-item" href="updatestatus.php?id='.$row['id'].'&st=0">Pending</a>
          <a class="dropdown-item" href="updatestatus.php?id='.$row['id'].'&st=1">In Progress</a>
          <a class="dropdown-item" href="updatestatus.php?id='.$row['id'].'&st=2">Answered</a>
          <a class="dropdown-item" href="deletecontact.php?id='.$row['id'].'">Delete leads</a>
        </div>
      </div></td>';
      
      
    echo"</tr>";
    }
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}

// Close the database connection
$pdo = null;
?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                    
                    <!-- Content Row -->
                    
                </div>
                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

            <!-- Footer -->
            <footer class="sticky-footer bg-white">
                <div class="container my-auto">
                    <div class="copyright text-center my-auto">
                        <span>Copyright &copy;  2024</span>
                    </div>
                </div>
            </footer>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="logout.php">Logout</a>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>

    <!-- Page level plugins -->
    <script src="vendor/chart.js/Chart.min.js"></script>

    <!-- Page level custom scripts -->
    <script src="js/demo/chart-area-demo.js"></script>
    <script src="js/demo/chart-pie-demo.js"></script>
    

</body>

</html>