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

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

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

// Fetching records from the place table for the current user
$sql_places = "SELECT * FROM orders WHERE Renter_ID = '$AccID' ";
$result_places = mysqli_query($conn, $sql_places);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['delete'])) {
        foreach ($_POST['delete'] as $orderID) {
            $sql = "DELETE FROM orders WHERE OrderID = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("s", $orderID);
            $stmt->execute();
        }
        echo "<script>alert('Order had been deleted.');</script>";
        echo "<script>window.location = 'payment.php'</script>";
        exit();
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Payment | 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: 1050px; 
         height: 95px; 
         border: 1; 
         border-collapse: collapse;
         align: center; 
         cellpadding: 1; 
         cellspacing: 1;
      }
      .table-common tr{
        padding: 10px 20px;
        border: 1px solid white;
      }
      .table-common thead tr{
         text-align: center; 
         background-color: purple;
      }
      .table-common td{
        border: 1px solid white;
        font-size: 18px;
        padding: 10px 20px;
      }
      tbody td h1{
        text-align: center;
        margin-left: 100px;
      }
      .table-actions input{
        margin-left: 10px;
        background: purple;
        color: white;
        padding: 10px 20px;
      }
      .table-actions input:hover{
        background: #e91e63;
      }
      button{
        padding: 10px 15px;
        margin-left: 7px;
        background: white;
      }

      button: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="rentermenu.php">Main Menu</a>
        </li>
        <hr>
        <h4>Advanced</h4>
        <li>
          <span class="material-symbols-outlined"><img src="pic/receipt.png"></span>
          <a href="selectReceipt.php">Receipt</a>
        </li>
        <hr>
        <h4>Account</h4>
        <li>
          <span class="material-symbols-outlined"><img src="pic/profile2.png"></span>
          <a href="renterProfile.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="logout.php">Logout</a>
        </li>
      </ul>
    </aside>
    <header class="header">
        <a href="rentermenu.php" class="logo">Welcome
        <span><?php echo $userName; ?>'s</span></a>

        <i class="fa-solid fa-bars" id="menu-icon"></i>

        <nav class="navbar">
            <a href="rentPlace.php">Rent Place</a>
            <a href="sawEvents.php">Event</a>
            <a href="payment.php">Payment</a>
            <a href="sentFeedback.php">Sent Feedback</a>   
        </nav>      
    </header>
    <section class="home" id="home">
        <div class="home-content">
            <br><br>
            <table width="882" border="0">
                <tbody>
                    <tr>
                        <td><h1>PAYMENT SITE</h1></td>
                    </tr>
                </tbody>
            </table>
            <table class="table-common" id="table-1" width="1402" height="152" border="1" align="center" cellpadding="1" cellspacing="1">
                <thead>
                    <tr>
                        <td width="33">NO</td>
                        <td width="150">RENTER ID</td>
                        <td width="82">PLACE NUMBER</td>
                        <td width="82">PEDDLING LICENSE</td>
                        <td width="180">DATE</td>
                        <td width="82">APPROVAL</td>
                        <td width="250">ACTION</td> 
                        <td width="100">PAY</td> 
                    </tr>
                </thead>
                <tbody>
                    <?php
                        if ($result_places->num_rows > 0) {
                            $no = 1;
                            while ($row = $result_places->fetch_assoc()) {
                                $isApproved = $row['Approve_Status'] == 1;
                                $isChecks = $row['Checks_Status'] == 0;
                                $isPay = $row['Pay_Status'] == 1;
                                if ($isApproved OR $isChecks) {
                                    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>" . ($row['Approve_Status'] ? '<i class="fa fa-check"></i>' : 'Pending') . "</td>";
                                    echo "<td>";
                                    echo "<form action='payment.php' method='POST' style='display:inline;'>";
                                    echo "<input type='hidden' name='OrderID' value='" . $row['OrderID'] . "'>";
                                    echo "<input type='hidden' name='Renter_ID' value='" . $row['Renter_ID'] . "'>";
                                    echo "<input type='hidden' name='Place_Num' value='" . $row['Place_Num'] . "'>";
                                    echo "<input type='hidden' name='License' value='" . $row['License'] . "'>";
                                    echo "<input type='hidden' name='Dates' value='" . $row['Dates'] . "'>";
                                    echo "<input type='hidden' name='delete[]' value='" . $row['OrderID'] . "'>";
                                    echo "<button type='submit' name='deleteButton' onclick='confirmCancel(event)'" . ($row['Approve_Status'] && !$isPay ? '' : ' disabled') . ">Cancel</button>";
                                    echo "</form>";
                                    echo "<form action='payment2.php' method='POST' style='display:inline;'>";
                                    echo "<input type='hidden' name='OrderID' value='" . $row['OrderID'] . "'>";
                                    echo "<input type='hidden' name='Renter_ID' value='" . $row['Renter_ID'] . "'>";
                                    echo "<input type='hidden' name='Place_Num' value='" . $row['Place_Num'] . "'>";
                                    echo "<input type='hidden' name='License' value='" . $row['License'] . "'>";
                                    echo "<input type='hidden' name='Dates' value='" . $row['Dates'] . "'>";
                                    echo "<button type='submit'" . ($row['Approve_Status'] && !$isPay ? '' : ' disabled') . ">Pay</button>";
                                    echo "</form>";
                                    echo "</td>";
                                    echo "<td>" . ($row['Pay_Status'] ? '<i class="fa fa-check"></i>' : '❌') . "</td>";
                                    echo "</tr>";
                                    $no++;
                                } else {
                                    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>" . ($row['Approve_Status'] ? '' : '❌') . "</td>";
                                    echo "<td>";
                                    echo "<form action='payment.php' method='POST' onsubmit='return confirm(\"Are you sure you want to delete this row?\");'>";
                                    echo "<input type='hidden' name='delete[]' value='" . $row['OrderID'] . "'>";
                                    echo "<button type='delete' name='deleteButton'" . ($row['Approve_Status'] ? 'disabled' : '') . ">Delete</button>";
                                    echo "</form>";
                                    echo "</td>";
                                    echo "<td>" . ($row['Pay_Status'] ? '<i class="fa fa-check"></i>' : 'Reject') . "</td>";
                                    echo "</tr>";
                                    $no++;
                                }
                            }
                        } else {
                            echo "<tr align='center'><td colspan='8'>No Order Being Made</td></tr>";
                        }
                        ?>

                </tbody>
            </table>
        </div>
    </section>
    <h1 style="font-size: 15px; position: bottom; margin: -4.375rem 0 5rem 21.875rem; text-align: center; width: 800px;">YOU HAVE TO WAIT FOR ADMIN TO APPROVE YOUR ORDER, IF ✔ IN APPROVAL THAT MEANS YOUR ORDER HAD BEEN APPROVED, IF ❌ IN APPROVAL THAT MEANS YOUR ORDER HAD BEEN REJECTED</h1>
    <footer class="footer" id="footer">
        <div class="social">
            <a onclick="showPopup()"><i class="fa-solid fa-phone"></i></a>
            <a onclick="showPopup2()"><i class="fa-solid fa-envelope"></i></a>
        </div>
        <div id="popup" class="popup">
                <h2>This is Our No Phone!</h2><br>
                <p>016-430 6562</p>
                <button class="btns" onclick="closePopup()">Close</button>
        </div>

        <div id="popup2" class="popup">
                <h2>This is Our Email!</h2><br>
                <p>uitm@student.uitm.edu.my</p>
                <button class="btns" onclick="closePopup2()">Close</button>
        </div>
        <ul class="list">
            <li>
                <a href="sentFeedback.php">Rate Us</a>
            </li>

            <li>
                <a href="sawEvents.php">Event</a>
            </li>

            <li>
                <a href="renterProfile.php">Profile</a>
            </li>

            <li>
                <a href="https://bendahari.uitm.edu.my/index.php/component/contact/category/55-cawangan-negeri-perak-kampus-tapah">Website</a>
            </li>
        </ul>
        <p class="copyright">
            © NextGen Techne | All Rights Reserved
        </p>
    </footer>
    <script src="script.js"></script>
    <script type="text/javascript">
        function confirmCancel(event) {
            if (!confirm("Are you sure you want to cancel this order?")) {
                event.preventDefault();
            }
        }
    </script>
</body>
</html>
