<?php include '../config/koneksi.php'; session_start(); if(!isset($_SESSION['admin_id'])){ header('Location: login.php'); exit; } ?>
<?php
if(isset($_GET['hapus'])){
    $id = (int)$_GET['hapus'];
    $q = mysqli_query($conn, "SELECT gambar FROM kamar WHERE id=$id");
    if($d = mysqli_fetch_assoc($q)){
        if($d['gambar'] && file_exists('../assets/'.$d['gambar'])){
            unlink('../assets/'.$d['gambar']);
        }
    }
    // Hapus data terkait di pemesanan dan pembayaran
    mysqli_query($conn, "DELETE FROM pembayaran WHERE id_pemesanan IN (SELECT id FROM pemesanan WHERE id_kamar=$id)");
    mysqli_query($conn, "DELETE FROM pemesanan WHERE id_kamar=$id");
    mysqli_query($conn, "DELETE FROM kamar WHERE id=$id");
    echo '<div class=\'alert\'><span style="vertical-align:middle;display:inline-block;margin-right:8px;"><svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align:middle;"><circle cx="10" cy="10" r="10" fill="#28a745"/><path d="M6 10.5L9 13.5L14 8.5" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></span>Perubahan kamar berhasil disimpan!</div><meta http-equiv=\'refresh\' content=\'1;url=kamar_data.php\'>';
    exit;
}
if(isset($_POST['hapus_semua'])){
    // Hapus data terkait di pemesanan dan pembayaran
    mysqli_query($conn, "DELETE FROM pembayaran WHERE id_pemesanan IN (SELECT id FROM pemesanan)");
    mysqli_query($conn, "DELETE FROM pemesanan");
    mysqli_query($conn, "DELETE FROM kamar");
    echo '<div class=\'alert\'><span style="vertical-align:middle;display:inline-block;margin-right:8px;"><svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align:middle;"><circle cx="10" cy="10" r="10" fill="#28a745"/><path d="M6 10.5L9 13.5L14 8.5" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></span>Semua kamar berhasil dihapus!</div><meta http-equiv=\'refresh\' content=\'1;url=kamar_data.php\'>';
    exit;
}
if(isset($_POST['isi_otomatis'])){
    // Hapus data terkait di pemesanan dan pembayaran
    mysqli_query($conn, "DELETE FROM pembayaran WHERE id_pemesanan IN (SELECT id FROM pemesanan)");
    mysqli_query($conn, "DELETE FROM pemesanan");
    mysqli_query($conn, "DELETE FROM kamar");
    $hargaDeluxe = 1000000;
    $hargaSuperior = 850000;
    for($i=1;$i<=10;$i++){
        mysqli_query($conn, "INSERT INTO kamar (nama, tipe, harga, status) VALUES ('Kamar Deluxe $i', 'Deluxe', $hargaDeluxe, 'tersedia')");
    }
    for($i=1;$i<=10;$i++){
        mysqli_query($conn, "INSERT INTO kamar (nama, tipe, harga, status) VALUES ('Kamar Superior $i', 'Superior', $hargaSuperior, 'tersedia')");
    }
    echo '<div class=\'alert\'><span style="vertical-align:middle;display:inline-block;margin-right:8px;"><svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align:middle;"><circle cx="10" cy="10" r="10" fill="#28a745"/><path d="M6 10.5L9 13.5L14 8.5" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></span>10 kamar Deluxe dan 10 kamar Superior berhasil diisi otomatis!</div><meta http-equiv=\'refresh\' content=\'1;url=kamar_data.php\'>';
    exit;
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Data Kamar - Admin</title>
    <link rel="stylesheet" type="text/css" href="style_admin.css">
    <style>
        .table-responsive { width: 100%; overflow-x: auto; }
        table { width: 100%; border-collapse: collapse; background: #fff; min-width: 700px; }
        th, td { padding: 12px 10px; border-bottom: 1px solid #e0eaff; text-align: center; vertical-align: middle; }
        tr:nth-child(even) { background: #f4f8ff; }
        .btn { margin: 2px 4px; }
    </style>
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'sidebar.php'; ?>
<div class="content">
    <div class="box">
        <?php if(isset($_GET['notif']) && $_GET['notif'] == '1') echo '<div class="alert"><span style="vertical-align:middle;display:inline-block;margin-right:8px;"><svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" style="vertical-align:middle;"><circle cx="10" cy="10" r="10" fill="#28a745"/><path d="M6 10.5L9 13.5L14 8.5" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></span>Perubahan kamar berhasil disimpan!</div>'; ?>
        <h2 style="text-align:center;">Data Kamar</h2>
        <form method="post" style="margin-bottom:16px;text-align:right;display:inline-block;">
            <button type="submit" name="hapus_semua" class="btn btn-danger" onclick="return confirm('Hapus semua kamar?')">Hapus Semua Kamar</button>
        </form>
        <form method="post" style="margin-bottom:16px;text-align:right;display:inline-block;margin-left:8px;">
            <button type="submit" name="isi_otomatis" class="btn">Isi Otomatis 20 Kamar</button>
        </form>
        <div class="table-responsive">
        <table>
            <tr>
                <th>No</th>
                <th>Nama</th>
                <th>Tipe</th>
                <th>Harga</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
            <?php
            $q = mysqli_query($conn, "SELECT * FROM kamar ORDER BY id ASC LIMIT 20");
            $no = 1;
            while($k = mysqli_fetch_assoc($q)){
                echo '<tr>';
                echo '<td>'.$no.'</td>';
                echo '<td>Kamar '.$no.'</td>';
                echo '<td>'.(!empty($k['tipe']) ? htmlspecialchars($k['tipe']) : '-').'</td>';
                echo '<td>'.(!empty($k['harga']) ? 'Rp. '.number_format($k['harga'],0,',','.') : '-').'</td>';
                echo '<td>'.(!empty($k['status']) ? htmlspecialchars($k['status']) : '-').'</td>';
                echo '<td><a href="kamar_edit.php?id='.$k['id'].'" class="btn">Edit</a> <a href="kamar_data.php?hapus='.$k['id'].'" class="btn" onclick="return confirm(\'Hapus kamar ini?\')">Hapus</a></td>';
                echo '</tr>';
                $no++;
            }
            ?>
        </table>
        </div>
    </div>
</div>
<?php include 'footer.php'; ?>
</body>
</html> 