<?php include '../config/koneksi.php'; session_start(); if(!isset($_SESSION['admin_id'])){ header('Location: login.php'); exit; } ?>
<!DOCTYPE html>
<html>
<head>
    <title>Data User - 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;">Data User</h2>
        <table>
            <tr>
                <th>ID</th>
                <th>Nama</th>
                <th>Email</th>
                <th>Alamat</th>
                <th>Telepon</th>
                <th>Action</th>
            </tr>
            <?php
            $q = mysqli_query($conn, "SELECT * FROM user ORDER BY id ASC");
            while($u = mysqli_fetch_assoc($q)){
                echo '<tr>';
                echo '<td>'.$u['id'].'</td>';
                echo '<td>'.htmlspecialchars($u['nama']).'</td>';
                echo '<td>'.htmlspecialchars($u['email']).'</td>';
                echo '<td>'.htmlspecialchars($u['alamat']).'</td>';
                echo '<td>'.htmlspecialchars($u['telepon']).'</td>';
                echo '<td><a href="user_data.php?hapus='.$u['id'].'" class="btn" onclick="return confirm(\'Hapus user ini?\')">Hapus</a></td>';
                echo '</tr>';
            }
            ?>
        </table>
        <?php
        if(isset($_GET['hapus'])){
            $id = (int)$_GET['hapus'];
            // Hapus data terkait di tabel lain
            mysqli_query($conn, "DELETE FROM kontak WHERE id_user=$id");
            mysqli_query($conn, "DELETE FROM pembayaran WHERE id_user=$id");
            mysqli_query($conn, "DELETE FROM pemesanan WHERE id_user=$id");
            // Baru hapus user
            $result = mysqli_query($conn, "DELETE FROM user WHERE id=$id");
            if($result){
                echo '<div class="alert">User berhasil dihapus!</div><meta http-equiv="refresh" content="1;url=user_data.php">';
            } else {
                echo '<div class="alert">Gagal menghapus user. Pastikan data terkait sudah dihapus.</div>';
            }
        }
        ?>
    </div>
</div>
<?php include 'footer.php'; ?>
</body>
</html> 