<?php
session_start();

ini_set('session.cookie_httponly', 1);

ini_set('session.cookie_samesite', 'Strict');

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

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

$userId = $_SESSION['user_id'];

include './db.php';

$stmt = $mysqli->prepare("SELECT name, phone FROM users WHERE id = ?");
$stmt->bind_param("i", $userId);
$stmt->execute();
$stmt->bind_result($name, $phone);
$stmt->fetch();
$stmt->close();

if (empty($name)) {
    session_destroy();
    header("Location: login.php");
    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'; img-src 'self' data:; style-src 'self'; script-src 'self';");

?>
<!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">

</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">
			<div style="margin-top: 6rem;">
				<!-- Page Head -->
				<div class="page-head">
					<div class="row">
						<div class="col-sm-6 mb-sm-4 mb-3">
							<h3 class="mb-0">Good Morning, <?php echo htmlspecialchars($name); ?></h3>
							<p class="mb-0">Here’s what’s happening with your store today</p>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xl-12">
						<div class="row">
							<div class="col-xl-4 col-md-6">
							    <div class="card overflow-hidden">
							        <div class="card-header border-0 pb-0 flex-wrap">
									    <h5 class="mb-0">Profile</h5>
									</div>
									<div class="card-body mb-0">
										<div class="redia-date text-center">
											<img src="./images/user.png" width="100">
											<p class="mb-0"><?php echo htmlspecialchars($name); ?></p>
											<span class="mb-0"><?php echo htmlspecialchars($phone); ?></span>
										</div>
									</div>
									<div class="card-footer text-center border-0 pt-0">
										<a href class="btn btn-primary">Top Up Saldo</a>
										<a href class="btn btn-primary">Top Up Saldo</a>
										<a href class="btn btn-primary">Top Up Saldo</a>
									</div>
							    </div>
							</div>
							<div class="col-xl-4 col-md-6">
								<div class="card overflow-hidden">
								    <div class="card-header border-0 pb-0 flex-wrap">
									    <h5 class="mb-0">Select Machine</h5>
									</div>
									<div class="card-body mb-0">
										
									</div>
									<div class="card-footer text-center border-0 pt-0">
										<a href class="btn btn-primary">Top Up Saldo</a>
									</div>
								</div>
							</div>
							<div class="col-xl-4 col-md-6">
								<div class="card">
									<div class="card-body mb-0">
										<div id="redial"></div>
										<div class="redia-date text-center">
											<h4>Balance</h4>
											<p class="mb-0">Lorem ipsum dolor sit amet, consectetur</p>
										</div>
									</div>
									<div class="card-footer text-center border-0 pt-0">
										<a href class="btn btn-primary">Top Up Saldo</a>
									</div>
								</div>
							</div>
						</div>
					</div>
                    <div class="col-xl-12">
                        <div class="card overflow-hidden">
                            <div class="card-header border-0 pb-0 flex-wrap">
                                <h5 class="mb-0">Select Machine</h5>
                            </div>
                            <div class="card-body border-0 pb-0 flex-wrap">
                                <!-- Tabs -->
                                <ul class="nav nav-tabs" id="machineTab" role="tablist">
                                    <li class="nav-item">
                                        <button class="nav-link active" id="washing-tab" data-bs-toggle="tab" data-bs-target="#washing" type="button" role="tab" aria-controls="washing" aria-selected="true">Washing</button>
                                    </li>
                                    <li class="nav-item">
                                        <button class="nav-link" id="drayer-tab" data-bs-toggle="tab" data-bs-target="#drayer" type="button" role="tab" aria-controls="drayer" aria-selected="false">Drayer</button>
                                    </li>
                                </ul>
                    
                                <!-- Tab Content -->
                                <div class="tab-content" id="machineTabContent">
                                    <!-- Washing Machines -->
                                    <div class="tab-pane fade show active" id="washing" role="tabpanel" aria-labelledby="washing-tab">
                                        <div class="row mt-3">
                                            <?php
                                            // Query untuk mesin Washing
                                            $sql = "SELECT id, serialNumber FROM machine WHERE type = 'Washing'";
                                            $result = $conn->query($sql);
                    
                                            // Tampilkan data Washing
                                            if ($result->num_rows > 0) {
                                                while ($row = $result->fetch_assoc()) {
                                                    echo '
                                                    <div class="col-md-3 text-center mb-3">
                                                        <a href="order.php?machine_id=' . $row['serialNumber'] . '">
                                                            <img src="./images/washing.png" alt="Washing Machine" style="width: 100px; height: 100px;">
                                                            <p>' . htmlspecialchars($row['serialNumber']) . '</p>
                                                        </a>
                                                    </div>';
                                                }
                                            } else {
                                                echo '<p>No washing machines available.</p>';
                                            }
                                            ?>
                                        </div>
                                    </div>
                    
                                    <!-- Drayer Machines -->
                                    <div class="tab-pane fade" id="drayer" role="tabpanel" aria-labelledby="drayer-tab">
                                        <div class="row mt-3">
                                            <?php
                                            // Query untuk mesin Drayer
                                            $sql = "SELECT id, serialNumber FROM machine WHERE type = 'Drayer'";
                                            $result = $conn->query($sql);
                    
                                            // Tampilkan data Drayer
                                            if ($result->num_rows > 0) {
                                                while ($row = $result->fetch_assoc()) {
                                                    echo '
                                                    <div class="col-md-3 text-center mb-3">
                                                        <a href="order.php?machine_id=' . $row['serialNumber'] . '">
                                                            <img src="./images/drayer.png" alt="Drayer Machine" style="width: 100px; height: 100px;">
                                                            <p>' . htmlspecialchars($row['serialNumber']) . '</p>
                                                        </a>
                                                    </div>';
                                                }
                                            } else {
                                                echo '<p>No drayer machines available.</p>';
                                            }
                    
                                            // Tutup koneksi
                                            $conn->close();
                                            ?>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
				</div>
			</div>

			<!--**********************************
            Footer start
        ***********************************-->
			<div class="footer">
				<div class="copyright">
					<p>Copyright © Designed &amp; Developed by <a
							href="https://themeforest.net/user/dexignlabs/portfolio" target="_blank">DexignLab</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>