<?php
session_start();
if (!isset($_SESSION['AccAdminID'])) {
    header('Location: login.html');
    exit();
}

include("connection.php");
$AccID = $_SESSION['AccAdminID'];

// Fetching the user's name
$sql = "SELECT Admin_Name FROM admin WHERE Admin_ID = '$AccID'";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);
$userName = $user['Admin_Name'];

// Handle deletion if delete request is received
if (isset($_POST['delete']) && isset($_POST['Report_ID'])) {
    $reportId = $_POST['Report_ID'];
    $deleteSql = "DELETE FROM report WHERE Report_ID = '$reportId'";
    if ($conn->query($deleteSql) === TRUE) {
        echo "<script>alert('Row deleted successfully');</script>";
        echo "<script>window.location = 'viewFeedback.php'</script>";
        exit();
    } else {
        echo "<script>alert('Error: " . $conn->error . "');</script>";
        echo "<script>window.location = 'viewFeedback.php'</script>";
        exit();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Feedback | UITM BAZAAR</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="style2.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"> 
    <style type="text/css">
        button {
            padding: 10px 20px;
            margin-left: 30px;
        }
        .table-common button:hover {
            background: #e91e63;
        }
        input {
            color: white;
            margin-left: 26%;
            padding: 1rem;
            font: inherit;
            font-size: 18px;
            width: 300px;
            height: 40px;
            background: hsl(231, 14%, 10%);
            border: 2px solid #2b2c37;
            border-radius: 10px;
            position: relative;
            transition: all 0.35s ease-in-out;
        }
        input::placeholder {
            color: #727884;
        }
        input:focus {
            border: 2px solid #de55de;
            box-shadow: 1px 1px 20px #de55de;
        }
        .table-common {
            margin-left: 50px;
            border-collapse: collapse;
        }
        #table-1 {
            border: 1px solid white;
            font-size: 15px;
            padding: 10px;
        }
        #table-1 tr {
            border: 1px solid white;
            font-size: 15px;
            padding: 10px;
        }
        #table-1 td {
            border: 1px solid white;
            font-size: 15px;
            padding: 10px;
        }
        .footer{
            margin-top: 210px;
        }
    </style>
    <script>
        function filterTable() {
            let input = document.getElementById('searchInput');
            let filter = input.value.toUpperCase();
            let table = document.getElementById('table-1');
            let tr = table.getElementsByTagName('tr');
            let noResults = document.getElementById('noResults');
            let hasResults = false;
            for (let i = 1; i < tr.length; i++) {
                let td = tr[i].getElementsByTagName('td')[2];
                if (td) {
                    let txtValue = td.textContent || td.innerText;
                    if (txtValue.toUpperCase().indexOf(filter) > -1) {
                        tr[i].style.display = '';
                        hasResults = true;
                    } else {
                        tr[i].style.display = 'none';
                    }
                }       
            }
            noResults.style.display = hasResults ? 'none' : 'block';
        }
    </script>
</head>
<body>
    <aside class="sidebar">
      <div class="logo">
        <img src="pic/profiles.png" alt="logo">
        <h2><?php echo $userName; ?>'s</h2>
      </div>
      <ul class="links">
        <h4>Main Menu</h4>
        <li>
          <span class="material-symbols-outlined"><img src="pic/home.png"></span>
          <a href="adminmenu.php">Main Menu</a>
        </li>
        <hr>
        <h4>Advanced</h4>
        <li>
          <span class="material-symbols-outlined"><img src="pic/admin.png"></span>
          <a href="newAdmin.php">New Admin Acc</a>
        </li>
        <hr>
        <h4>Account</h4>
        <li>
          <span class="material-symbols-outlined"><img src="pic/profile2.png"></span>
          <a href="adminProfile.php">Profile</a>
        </li>
        <hr>
        <h4>Sign Out</h4>
        <li class="logout-link">
          <span class="material-symbols-outlined"><i class="fa-solid fa-right-from-bracket"></i></span>
          <a href="logoutAdmin.php">Logout</a>
        </li>
      </ul>
    </aside>
    <header class="header">
        <a href="adminmenu.php" class="logo">
        <span><?php echo $userName; ?>'s</span></a>
        
        <i class="fa-solid fa-bars" id="menu-icon"></i>
        
        <nav class="navbar">      
            <a href="report.php">Report</a> 
            <a href="renterList.php">Renter List</a>  
            <a href="viewFeedback.php">Feedback</a>
            <a href="approval.php">Order</a>
            <a href="postEvents.php">Events</a>
        </nav>    
    </header>
    <section class="home" id="home">
        <div class="home-content">
            <br><br>
            <table width="882" border="0">
                <tbody>
                    <tr>
                        <td><h1 style="margin-left: 220px;">FEEDBACK LIST</h1></td>
                    </tr>
                </tbody>
            </table>
            <form method="GET" action="">
                <div class="input-group">
                    <input type="text" class="form-control" id="searchInput" name="search" placeholder="Search by Department" value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>" onkeyup="filterTable()">
                    <button class="btn btn-primary" type="submit">Search</button>
                    <br><br>
                </div>
            </form>
            <table class="table-common" id="table-1" width="882" border="1" cellpadding="1" cellspacing="1">
                <tbody>
                    <tr align="center" style="background-color: purple">
                        <td width="150">Report ID</td>
                        <td width="150">Renter ID</td>
                        <td width="150">Department</td>
                        <td width="300">Feedback</td>
                        <td width="150">Action</td>
                    </tr>
                    <?php
                    $conn = mysqli_connect("localhost", "root", "", "groupproject");
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    }

                    $search = isset($_GET['search']) ? $conn->real_escape_string($_GET['search']) : '';
                    $sql = "SELECT Report_ID, Renter_ID, Department, Feedback FROM report";
                    if ($search) {
                        $sql .= " WHERE Department LIKE '%$search%'";
                    }

                    $result = $conn->query($sql);

                    if ($result->num_rows > 0) {
                        while ($row = $result->fetch_assoc()) {
                            echo "<tr>";
                            echo "<td>" . $row["Report_ID"] . "</td>";
                            echo "<td>" . $row["Renter_ID"] . "</td>";
                            echo "<td>" . $row["Department"] . "</td>";
                            echo "<td>" . $row["Feedback"] . "</td>";
                            echo "<td>
                                    <form method='POST' action='' onsubmit='return confirm(\"Are you sure you want to delete this row?\");'>
                                        <input type='hidden' name='Report_ID' value='" . $row["Report_ID"] . "'>
                                        <button type='submit' name='delete'>Delete</button>
                                    </form>
                                  </td>";
                            echo "</tr>";
                        }
                    } else {
                        echo "<tr><td colspan='5'>No results found</td></tr>";
                    }

                    $conn->close();
                    ?>
                </tbody>
            </table>
        </div>
    </section>
    <footer class="footer" id="footer">
        <p class="copyright">
            NextGen Techne © | All Rights Reserved
        </p>
    </footer>
</body>
</html>
