<?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'];

// Handling form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['submit']) || isset($_POST['reject'])) {
        if (isset($_POST['checkbox'])) {
            $successMessage = "";
            foreach ($_POST['checkbox'] as $orderID) {
                // Process each selected checkbox individually
                if (isset($_POST['submit'])) {
                    $sql = "UPDATE orders SET Approve_Status = 1, Checks_Status = 1 WHERE OrderID = ?";
                    $stmt = $conn->prepare($sql);
                    $stmt->bind_param("s", $orderID);
                    $stmt->execute();
                    $successMessage = "Order(s) had been Approved!";
                } elseif (isset($_POST['reject'])) {
                    $sql = "UPDATE orders SET Approve_Status = 0, Checks_Status = 1 WHERE OrderID = ?";
                    $stmt = $conn->prepare($sql);
                    $stmt->bind_param("s", $orderID);
                    $stmt->execute();
                    $successMessage = "Order(s) had been rejected!";
                }
            }
            // Redirect after processing all selected checkboxes
            echo "<script>alert('$successMessage');</script>";
            echo "<script>window.location = 'approval.php'</script>";
            exit(); // Exit after redirection
        } else {
            echo "<script>alert('Please select at least one checkbox.');</script>";
            echo "<script>window.location = 'approval.php'</script>";
            exit();
        }
    }
}

// Fetch data
$sql = "SELECT * FROM orders WHERE Checks_Status = 0";
$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Approval | 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">
        .table-common {
            width: 882px; /* Adjust the width as needed */
            border-collapse: collapse; /* Add this to collapse the border */
            margin: auto; /* Center the table horizontally */
        }

        .table-common th,
        .table-common td {
            border: 1px solid white; /* Add a single border to each cell */
            padding: 10px 20px;
            font-size: 18px;
        }

        .table-actions input {
            margin-left: 10px;
            background: purple;
            color: white;
            padding: 10px 20px;
        }

        .table-actions input:hover {
            background: #e91e63;
        }
    </style>
</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 align="center">
                            <h1>Approval Site</h1>
                        </td>
                    </tr>
                </tbody>
            </table>
            <form method="post" id="approval-form">
                <table class="table-common" id="table-1">
                    <thead>
                        <tr align="center" bgcolor="purple">
                            <th width="33">NO</th>
                            <th width="232">RENTER ID</th>
                            <th width="157">PLACE NUMBER</th>
                            <th width="82">License</th>
                            <th width="170">DATE</th>
                            <th width="82">APPROVAL</th>
                        </tr>
                    </thead>
                    <tbody id="table-body">
                        <?php
                        if ($result->num_rows > 0) {
                            $no = 1;
                            while ($row = $result->fetch_assoc()) {
                                echo "<tr align='center'>";
                                echo "<td>{$no}</td>";
                                echo "<td>{$row['Renter_ID']}</td>";
                                echo "<td>{$row['Place_Num']}</td>";
                                echo "<td>{$row['License']}</td>";
                                echo "<td>{$row['Dates']}</td>";
                                echo "<td><input type='checkbox' name='checkbox[]' value='{$row['OrderID']}'></td>";
                                echo "</tr>";
                                $no++;
                            }
                        } else {
                            echo "<tr align='center'><td colspan='6'>No Order Yet</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
                <div class="table-actions" style="text-align: right; margin-top: 20px;">
                    <input type="submit" name="submit" id="submit" value="Approve" onclick="return confirmSubmit();">
                    <input type="submit" name="reject" id="reject" value="Reject" onclick="return confirmReject();">
                </div>
            </form>
        </div>
    </section>
    <footer class="footer" id="footer">
        <p class="copyright">
            NextGen Techne © | All Rights Reserved
        </p>
    </footer>
    <script src="script.js"></script>
    <script>
        function confirmSubmit() {
            var checkboxes = document.querySelectorAll('input[type="checkbox"]:checked');
            if (checkboxes.length === 0) {
                alert("Please select at least one checkbox to APPROVE.");
                return false;
            }
            return confirm("Are you sure you want to APPROVE?");
        }

        function confirmReject() {
            var checkboxes = document.querySelectorAll('input[type="checkbox"]:checked');
            if (checkboxes.length === 0) {
                alert("Please select at least one checkbox to reject the order.");
                return false;
            }
            return confirm("SERIOUS AR !!!Are you sure you want to reject ?");
        }
    </script>
</body>

</html>
