<?php
session_start(); // Start session
include 'koneksi.php';

// Ensure the user is logged in and id_pelanggan exists in the session
if (!isset($_SESSION['id_pelanggan'])) {
    header('Location: login.php');
    exit();
}

// Get id_pelanggan from the session
$id_pelanggan = $_SESSION['id_pelanggan'];

// Retrieve patient data from tbl_pelanggan based on id_pelanggan
$result_pelanggan = mysqli_query($koneksi, "SELECT nik, nama_pelanggan FROM tbl_pelanggan WHERE id_pelanggan = '$id_pelanggan'");

if ($result_pelanggan && mysqli_num_rows($result_pelanggan) > 0) {
    // If patient data is found, store in $pelanggan variable
    $pelanggan = mysqli_fetch_assoc($result_pelanggan);
} else {
    // If not found, show error message or redirect
    echo "Data pelanggan tidak ditemukan!";
    exit();
}

// Retrieve laundri options from tbl_laundri
$laundri_result = mysqli_query($koneksi, "SELECT id_laundri, nama_laundri FROM tbl_laundri");

// Ensure the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Retrieve the form data
    $id_laundri = $_POST['id_laundri'];
    $id_kurir = $_POST['id_kurir'];
    $id_jadwal = $_POST['id_jadwal'];  // New field for jadwal
    $tanggal_daftar = $_POST['tanggal_daftar'];
    $jenis = $_POST['jenis'];
    $keterangan = $_POST['keterangan'];

    // Prepare the query to insert registration data including the id_jadwal
    $query = "INSERT INTO tbl_pemesanan (id_pelanggan, id_kurir, id_laundri, id_jadwal, tanggal_daftar, jenis, keterangan, status) 
              VALUES ('$id_pelanggan', '$id_kurir', '$id_laundri', '$id_jadwal', '$tanggal_daftar', '$jenis', '$keterangan', 'Menunggu')";

    // Check if the query was successful
    if (mysqli_query($koneksi, $query)) {
        // Redirect to kurir3.php after successful registration
        header('Location: kurir3.php');
        exit();
    } else {
        echo "Error: " . mysqli_error($koneksi);
    }
}

    
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pemesanan laundri</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f0f0f0;
            font-family: 'Roboto', sans-serif;
        }
        .container {
            margin-top: 50px;
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        h2 {
            text-align: center;
            color: #4CAF50;
        }
        .btn-android {
            background-color: #4CAF50;
            color: white;
            border-radius: 20px;
            width: 100%;
            height: 50px;
            font-size: 18px;
            font-weight: bold;
            text-transform: uppercase;
        }
        .btn-android:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>pemesanan Pelanggan Laundry</h2>
    <form method="POST" action="">
        <!-- NIK yang tidak bisa diedit -->
        <div class="mb-3">
            <label for="nik" class="form-label">NIK pelanggan:</label>
            <input type="text" name="nik" class="form-control" value="<?php echo $pelanggan['nik']; ?>" readonly>
        </div>
        <!-- Nama pelanggan yang tidak bisa diedit -->
        <div class="mb-3">
            <label for="nama_pelanggan" class="form-label">Nama pelanggan:</label>
            <input type="text" name="nama_pelanggan" class="form-control" value="<?php echo $pelanggan['nama_pelanggan']; ?>" readonly>
        </div>

        <!-- Pilihan laundri -->
        <div class="mb-3">
            <label for="laundri" class="form-label">Pilih laundri:</label>
            <select name="id_laundri" id="laundri" class="form-select" required>
                <option value="">--Pilih laundri--</option>
                <?php while ($row = mysqli_fetch_assoc($laundri_result)): ?>
                    <option value="<?php echo $row['id_laundri']; ?>"><?php echo $row['nama_laundri']; ?></option>
                <?php endwhile; ?>
            </select>
        </div>

        <!-- Pilihan kurir -->
        <div class="mb-3">
            <label for="kurir" class="form-label">Pilih kurir:</label>
            <select name="id_kurir" id="kurir" class="form-select" required>
                <option value="">--Pilih kurir--</option>
            </select>
        </div>

        <!-- Jadwal kurir -->
        <div class="mb-3">
            <label for="id_jadwal" class="form-label">Jadwal kurir:</label>
            <div id="jadwal_kurir">
                <!-- Jadwal kurir akan dimuat di sini menggunakan AJAX -->
            </div>
        </div>

        <!-- Pilihan Tanggal -->
        <div class="mb-3">
            <label for="tanggal_daftar" class="form-label">Tanggal Pemesanan:</label>
            <input type="date" name="tanggal_daftar" class="form-control" required>
        </div>

        <!-- Pilihan Rawat -->
    <div class="mb-3">
    <label for="jenis" class="form-label">Jenis pemesanan:</label>
    <select name="jenis" id="jenis" class="form-select" required>
        <option value="">--Pilih pemesanan--</option>
        <option value="cuci kering/strika">cuci kering/Strika</option>
        <option value="cuci kering">cuci kering</option>
    </select>
    </div>

<!-- keterangan yang Diderita -->
    <div class="mb-3">
    <label for="keterangan" class="form-label">Keterangan Pemesanan:</label>
    <textarea name="keterangan" class="form-control" rows="3" placeholder="Tuliskan Keterangan pemesanan..."></textarea>
    </div>



        <!-- Android Style Button -->
        <button type="submit" class="btn btn-android">Daftar</button>
    </form>
</div>

<!-- jQuery and Bootstrap JS for AJAX -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        // Load kurir when laundri is selected
        $('#laundri').change(function() {
            var id_laundri = $(this).val();
            if (id_laundri != '') {
                $.ajax({
                    url: "load_kurir.php",
                    method: "POST",
                    data: {id_laundri: id_laundri},
                    success: function(data) {
                        $('#kurir').html(data);
                    }
                });
            } else {
                $('#kurir').html('<option value="">--Pilih kurir--</option>');
            }
        });

        // Load jadwal kurir when kurir is selected
        $('#kurir').change(function() {
            var id_kurir = $(this).val();
            if (id_kurir != '') {
                $.ajax({
                    url: "load_jadwal.php",
                    method: "POST",
                    data: {id_kurir: id_kurir},
                    success: function(data) {
                        $('#jadwal_kurir').html(data);
                    }
                });
            } else {
                $('#jadwal_kurir').html(''); // Kosongkan jika tidak ada kurir
            }
        });
    });
</script>

</body>
</html>
