<?php
include 'koneksi.php';

if (isset($_GET['id_pesanan'])) {
    $id_pesanan = $_GET['id_pesanan'];
    
    // Ambil data pesanan berdasarkan ID
    $query = "SELECT p.id_pesanan, s.nama_servis, s.harga 
              FROM tbl_pesanan p
              JOIN tbl_servis s ON p.id_servis = s.id_servis
              WHERE p.id_pesanan = '$id_pesanan'";
    $result = mysqli_query($koneksi, $query);
    
    if ($result) {
        $order = mysqli_fetch_assoc($result);
    } else {
        // Handle error jika tidak ada hasil
        die("Pesanan tidak ditemukan.");
    }
}

// Proses pembayaran
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $jumlah_bayar = $_POST['jumlah_bayar'];

    // Pastikan jumlah bayar cukup
    if ($jumlah_bayar >= $order['harga']) {
        // Pembayaran cukup, update status pesanan
        $update_query = "UPDATE tbl_pesanan SET status = 'Sudah Dibayar' WHERE id_pesanan = '$id_pesanan'";

        if (mysqli_query($koneksi, $update_query)) {
            $status_message = "Pembayaran berhasil! Status pesanan Anda telah diperbarui.";
        } else {
            $status_message = "Terjadi kesalahan saat memperbarui status pesanan. Silakan coba lagi.";
        }
    } else {
        $status_message = "Jumlah pembayaran kurang dari harga pesanan.";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pembayaran Pesanan</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f7f7f7;
            margin: 0;
            padding: 0;
            color: #333;
        }

        .container {
            width: 80%;
            margin: 30px auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
        }

        h2 {
            color: #4CAF50;
            text-align: center;
            margin-bottom: 20px;
        }

        .servis-details {
            background-color: #e9f7e9;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 8px;
            box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
        }

        .servis-details p {
            font-size: 16px;
            color: #333;
        }

        .payment-form {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        .payment-form input[type="number"] {
            padding: 12px 20px;
            font-size: 16px;
            margin-bottom: 20px;
            width: 50%;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-sizing: border-box;
            transition: 0.3s ease-in-out;
        }

        .payment-form input[type="number"]:focus {
            border-color: #4CAF50;
            outline: none;
        }

        .payment-form button {
            background-color: #4CAF50;
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 18px;
            cursor: pointer;
            transition: background-color 0.3s;
            width: 60%;
        }

        .payment-form button:hover {
            background-color: #45a049;
        }

        .status-message {
            text-align: center;
            margin-top: 20px;
            font-size: 18px;
            font-weight: bold;
        }

        .status-success {
            color: #4CAF50;
        }

        .status-error {
            color: #e74c3c;
        }

        .back-link {
            text-align: center;
            margin-top: 20px;
        }

        .back-link a {
            text-decoration: none;
            font-size: 16px;
            color: #4CAF50;
            border: 2px solid #4CAF50;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        .back-link a:hover {
            background-color: #4CAF50;
            color: white;
        }
    </style>
</head>
<body>

<div class="container">
    <h2>Pembayaran Pesanan</h2>
    
    <div class="servis-details">
        <p><strong>Servis:</strong> <?= $order['nama_servis']; ?></p>
        <p><strong>Harga:</strong> Rp <?= number_format($order['harga'], 0, ',', '.'); ?></p>
    </div>

    <h3>Masukkan Jumlah Pembayaran</h3>

    <div class="payment-form">
        <form action="pembayaran.php?id_pesanan=<?= $id_pesanan; ?>" method="POST">
            <label for="jumlah_bayar">Jumlah Pembayaran:</label>
            <input type="number" name="jumlah_bayar" id="jumlah_bayar" min="0" required placeholder="Masukkan jumlah pembayaran">
            <button type="submit">Bayar Sekarang</button>
        </form>
    </div>

    <!-- Menampilkan status pembayaran -->
    <?php if (isset($status_message)) { ?>
        <div class="status-message <?= isset($status_success) ? 'status-success' : 'status-error' ?>">
            <?= $status_message; ?>
        </div>
    <?php } ?>

    <div class="back-link">
        <a href="home_pelanggan.php">Kembali ke Home</a>
    </div>
</div>

</body>
</html>
