<?php
include("connection.php");

session_start();
$AccID = $_SESSION['AccID'];
if (!isset($_POST['payment_type'])) {
    echo "Invalid request.";
    exit();
}

$OrderID = $_POST['OrderID'];
$Renter_ID = $_POST['Renter_ID'];
$Place_Num = $_POST['Place_Num'];
$License = $_POST['License'];
$Dates = $_POST['Dates'];
$Pay_AmountTotal = 100;
$payment_type = $_POST['payment_type'];

// Generate unique Transaction_No
$Transaction_No = uniqid('txn');

// Insert data into the payment table
$payDate = date('Y-m-d');

$sql = "INSERT INTO payment (Transaction_No, OrderID, Renter_ID, Pay_Type, Pay_Date, Pay_AmountTotal)
        VALUES ('$Transaction_No', '$OrderID', '$Renter_ID', '$payment_type', '$payDate', '$Pay_AmountTotal')";

if ($conn->query($sql) === TRUE) {
    $paymentId = $Transaction_No;
    $sql = "UPDATE orders SET Pay_Status = 1 WHERE OrderID = '$OrderID'";

    $result = mysqli_query($conn, $sql);
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    exit();
}

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

$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css" type="text/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">
    <title>Receipt | UITM BAZAAR</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 80%;
            margin: 200px auto;
            background-color: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        img{
            margin-left: 20%;
        }
        h1 {
            text-align: left;
        }
        .invoice-details {
            margin: 20px 0;
        }
        .invoice-details div {
            margin-bottom: 10px;
        }
        .invoice-details label {
            color: black;
            font-size: 20px;
            font-weight: bold;
            display: inline-block;
            width: 200px;
        }
        .invoice-details span {
            color: black;
            font-size: 20px;
            font-weight: bold;
            display: inline-block;
            width: 150px;
        }
        .buttons {
            text-align: center;
            margin-top: 20px;
        }
        .buttons button {
            margin: 5px;
            padding: 20px 25px;
            font-size: 2em;
            cursor: pointer;
        }
        .invoice-footer {
            text-align: center;
            margin-top: 20px;
            font-size: 1.5em;
            color: #555;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            color: black;
            font-size: 27px;
            padding: 10px;
            border: 1px solid #ddd;
        }
        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        .invoice-header {
            display: flex;
            margin-bottom: 20px;
        }
        .container-logo {
            height: 200px;
            margin-right: 50px;
        }
        .invoice-title {
            font-size: 2em;
            margin: 0;
        }
    </style>
    <script>
        function printInvoice() {
            window.print();
        }

        function goToNext() {
            window.location.href = "rentermenu.php";
        }
    </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="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>
    <div class="container">
        <div class="invoice-header">
            <table class="table-1" align="left" width="924" height="145" border="0">
                <tr>
                    <td width="150" align="left"><img src="pic/uitm.png" width="100" height="100" alt=""/></td>
                    <td width="500" align="left"><h1>Invoice</h1></td>
                </tr>
            </table>
        </div>
        <div class="invoice-details">
            <div>
                <label>Transaction No:</label>
                <span id="transactionNo"><?php echo $Transaction_No; ?></span>
            </div>
            <div>
                <label>Renter ID:</label>
                <span id="renterId"><?php echo $Renter_ID; ?></span>
            </div>
            <div>
                <label>Pay Type:</label>
                <span id="payType"><?php echo $payment_type; ?></span>
            </div>
            <div>
                <label>Pay Date:</label>
                <span id="payDate"><?php echo $payDate; ?></span>
            </div>
            <div>
                <label>Pay Amount Total:</label>
                <span id="payAmountTotal">RM<?php echo $Pay_AmountTotal; ?></span>
            </div>
            <div>
                <label>Place Number:</label>
                <span id="placeNumber"><?php echo $Place_Num; ?></span>
            </div>
        </div>
        <table>
            <thead>
                <tr>
                    <th>Item</th>
                    <th>Description</th>
                    <th>Quantity</th>
                    <th>Price</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Place Rental</td>
                    <td>Rental of place number <?php echo $Place_Num; ?></td>
                    <td>1</td>
                    <td>RM<?php echo $Pay_AmountTotal; ?></td>
                    <td>RM<?php echo $Pay_AmountTotal; ?></td>
                </tr>
            </tbody>
        </table>
        <div class="buttons">
            <button onclick="printInvoice()">Print</button>
            <button onclick="goToNext()">Next</button>
        </div>
        <div class="invoice-footer">
            <p>Thank you for your business!</p>
            <p>If you have any questions about this invoice, please contact us.</p>
        </div>
    </div>
    <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>
</body>
</html>
