<?php
include '../config/koneksi.php';
session_start();
if(!isset($_SESSION['admin_id'])){
    header('Location: login.php');
    exit;
}

// Proses konfirmasi/penolakan
if(isset($_POST['aksi']) && isset($_POST['id'])){
    $id = (int)$_POST['id'];
    $aksi = $_POST['aksi'];
    if($aksi == 'terima'){
        mysqli_query($conn, "UPDATE pemesanan SET status='lunas' WHERE id=$id");
    } elseif($aksi == 'tolak'){
        mysqli_query($conn, "UPDATE pemesanan SET status='expired' WHERE id=$id");
    }
    echo '<div class="alert">Status pesanan berhasil diupdate!</div><meta http-equiv="refresh" content="1;url=pesanan_konfirmasi.php">';
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Konfirmasi Pesanan - Admin</title>
    <link rel="stylesheet" type="text/css" href="style_admin.css">
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'sidebar.php'; ?>
<div class="content">
    <div class="box">
        <h2 style="text-align:center;">Konfirmasi Pesanan</h2>
        <table>
            <tr>
                <th>ID</th>
                <th>Nama User</th>
                <th>Tipe Kamar</th>
                <th>Harga</th>
                <th>Tanggal Masuk</th>
                <th>Tanggal Keluar</th>
                <th>Catatan</th>
                <th>Bukti Pembayaran</th>
                <th>Status</th>
                <th>Aksi</th>
            </tr>
            <?php
            $q = mysqli_query($conn, "SELECT p.*, u.nama as nama_user, k.tipe, k.harga FROM pemesanan p JOIN user u ON p.id_user=u.id JOIN kamar k ON p.id_kamar=k.id WHERE p.status='pending' ORDER BY p.id DESC");
            while($d = mysqli_fetch_assoc($q)){
                // Ambil bukti pembayaran
                $bukti = '';
                $q_bukti = mysqli_query($conn, "SELECT bukti FROM pembayaran WHERE id_pemesanan=".$d['id']." LIMIT 1");
                if($b = mysqli_fetch_assoc($q_bukti)){
                    $bukti = $b['bukti'];
                }
                echo '<tr>';
                echo '<td>'.$d['id'].'</td>';
                echo '<td>'.htmlspecialchars($d['nama_user']).'</td>';
                echo '<td>'.htmlspecialchars($d['tipe']).'</td>';
                echo '<td>Rp. '.number_format($d['harga'],0,',','.').'</td>';
                echo '<td>'.$d['tanggal_masuk'].'</td>';
                echo '<td>'.$d['tanggal_keluar'].'</td>';
                echo '<td>'.htmlspecialchars($d['catatan']).'</td>';
                echo '<td>';
                if($bukti){
                    echo '<a href="../assets/images/'.htmlspecialchars($bukti).'" target="_blank">Lihat Bukti</a>';
                } else {
                    echo '-';
                }
                echo '</td>';
                echo '<td><span style="color:#ff9800;font-weight:bold;">Menunggu</span></td>';
                echo '<td>';
                echo '<form method="post" style="display:inline;">';
                echo '<input type="hidden" name="id" value="'.$d['id'].'">';
                echo '<button type="submit" name="aksi" value="terima" class="btn">Terima</button> ';
                echo '<button type="submit" name="aksi" value="tolak" class="btn btn-danger" onclick="return confirm(\'Tolak pesanan ini?\')">Tolak</button>';
                echo '</form>';
                echo '</td>';
                echo '</tr>';
            }
            ?>
        </table>
    </div>
</div>
<?php include 'footer.php'; ?>
</body>
</html> 