<?php
ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_samesite', 'Strict');

session_start();

if (!isset($_SESSION['initialized'])) {
    session_regenerate_id(true);
    $_SESSION['initialized'] = true;
}

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// Include DB connection
include './db.php';


$user_id = $_SESSION['user_id']; // Get user ID

// Check if invoice number is provided in the URL
if (isset($_GET['invoice_number'])) {
    $invoice_number = $_GET['invoice_number'];
} else {
    echo "Invoice number not found.";
    exit();
}

// Fetch the transaction details based on the invoice number
$stmt = $conn->prepare("SELECT t.invoice_number, t.total_price, t.status, td.machine_id, td.machine_type, td.price
                        FROM transactions t
                        JOIN transaction_details td ON t.id = td.transaction_id
                        WHERE t.invoice_number = ? AND t.user_id = ?");
$stmt->bind_param("si", $invoice_number, $user_id);
$stmt->execute();
$stmt->bind_result($invoice_number, $total_price, $status, $machine_id, $machine_type, $item_price);

$transaction_items = [];
while ($stmt->fetch()) {
    $transaction_items[] = [
        'machine_id' => htmlspecialchars($machine_id),
        'machine_type' => htmlspecialchars($machine_type),
        'price' => number_format($item_price, 0, ',', '.')
    ];
}
$stmt->close();

// If no transaction is found, redirect to home page or show error
if (empty($transaction_items)) {
    echo "Transaction not found.";
    exit();
}

header("X-Content-Type-Options: nosniff");
header("X-Frame-Options: SAMEORIGIN");
header("X-XSS-Protection: 1; mode=block");
header("Content-Security-Policy: default-src 'self'; font-src 'self' https://fonts.gstatic.com; img-src 'self' data:; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://cdn.jsdelivr.net; script-src 'self' https://cdn.jsdelivr.net 'unsafe-inline';");

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

<head>
	<!-- PAGE TITLE HERE -->
	<title>My Wash - Dashboard</title>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="author" content="DexignLab">
	<!-- MOBILE SPECIFIC -->
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<!-- FAVICONS ICON -->
	<link rel="shortcut icon" href="images/favicon.png">
	<link href="vendor/bootstrap-select/css/bootstrap-select.min.css" rel="stylesheet">

	<!-- STYLE CSS -->
	<link class="main-css" href="css/style.css" rel="stylesheet">
	
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0&icon_names=notifications" />
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

</head>

<body>

	<!--*******************
        Preloader start
    ********************-->
	<div id="preloader">
		<div class="loading-wave">
			<div class="loading-bar"></div>
			<div class="loading-bar"></div>
			<div class="loading-bar"></div>
			<div class="loading-bar"></div>
		</div>
	</div>

	<!--*******************
        Preloader end
    ********************-->

	<!--**********************************
        Main wrapper start
    ***********************************-->
	<div id="main-wrapper" class="show">
        
        <?php include './page/header.php'; ?>
        
		<!--**********************************
            Content body start
        ***********************************-->
		<div class="container">
            <h1>Transaction Completed</h1>
            <p><strong>Invoice Number:</strong> <?php echo $invoice_number; ?></p>
            <p><strong>Status:</strong> <?php echo ucfirst($status); ?></p>
            <p><strong>Total Price:</strong> Rp <?php echo number_format($total_price, 0, ',', '.'); ?></p>
    
            <h3>Items Purchased</h3>
            <ul>
                <?php foreach ($transaction_items as $item): ?>
                    <li>
                        <strong>Machine ID:</strong> <?php echo $item['machine_id']; ?><br>
                        <strong>Type:</strong> <?php echo $item['machine_type']; ?><br>
                        <strong>Price:</strong> Rp <?php echo $item['price']; ?><br><br>
                    </li>
                <?php endforeach; ?>
            </ul>
    
            <p>Your transaction has been successfully completed. Thank you for your purchase!</p>
    
            <a href="dashboard.php" class="btn btn-primary">Go to Home</a>
        </div>
		<!--**********************************
            Footer start
        ***********************************-->
			<div class="footer">
                <div class="copyright">
                    <p>© Designed &amp; Developed by <a href="#">MyWash</a> <span class="current-year">2024</span></p>
                </div>
            </div>
			<!--**********************************
            Footer end
        ***********************************-->

		</div>
		<!--**********************************
        Main wrapper end
    ***********************************-->

		<!--**********************************
        Scripts
    ***********************************-->
		<!-- Required vendors -->
		<script src="vendor/global/global.min.js"></script>
		<script src="vendor/bootstrap-select/js/bootstrap-select.min.js"></script>
		<!-- Dashboard 1 -->
		<script src="./vendor/swiper/js/swiper-bundle.min.js"></script>
		<script src="js/custom.js"></script>
		<script src="js/ic-sidenav-init.js"></script>
</body>

</html>