<?php
session_start();
include "db.php";

// Giriş kontrolü
if(!isset($_SESSION['user_id'])){
    header("Location: login.php");
    exit;
}

////////////////////////////////////////////////////////////////////////////////
// KULLANICI EKLE
if(isset($_POST['add_user'])){
    $username = $baglan->real_escape_string($_POST['username']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

    $baglan->query("INSERT INTO users (username, password) VALUES ('$username', '$password')");
    header("Location: user_manage.php"); exit;
}

////////////////////////////////////////////////////////////////////////////////
// ŞİFRE GÜNCELLE
if(isset($_POST['update_pass'])){
    $id = intval($_POST["id"]);
    $new_pass = password_hash($_POST['new_password'], PASSWORD_DEFAULT);

    $baglan->query("UPDATE users SET password='$new_pass' WHERE id=$id");
    header("Location: user_manage.php"); exit;
}

////////////////////////////////////////////////////////////////////////////////
// KULLANICI SİL
if(isset($_POST['delete_user'])){
    $id = intval($_POST['id']);

    // Kendi kendini silmeyi engelle
    if($id == $_SESSION['user_id']){
        die("Kendini silemezsin!");
    }

    $baglan->query("DELETE FROM users WHERE id=$id");
    header("Location: user_manage.php"); exit;
}

////////////////////////////////////////////////////////////////////////////////
// TÜM KULLANICILARI ÇEK
$users = $baglan->query("SELECT * FROM users ORDER BY id ASC");

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Kullanıcı Yönetimi</title>

<style>
body{
    background:#0f172a;
    color:white;
    font-family:Arial;
    padding:20px;
}
table{
    width:100%;
    border-collapse:collapse;
    margin-top:20px;
}
th,td{
    border:1px solid #334155;
    padding:10px;
}
input,button{
    padding:8px;
    border-radius:6px;
    border:none;
}
button{
    cursor:pointer;
}
.btn-del{background:#dc2626;color:white;}
.btn-pass{background:#3b82f6;color:white;}
.btn-add{background:#16a34a;color:white;margin-top:20px;}
.box{
    background:#1e293b;
    padding:20px;
    border-radius:10px;
    margin-bottom:20px;
}
</style>
</head>
<body>

<h2>👥 Kullanıcı Yönetimi</h2>

<!-- YENİ KULLANICI EKLE -->
<div class="box">
    <h3>Yeni Kullanıcı Ekle</h3>
    <form method="POST">
        <input type="text" name="username" placeholder="Kullanıcı adı" required>
        <input type="password" name="password" placeholder="Şifre" required>
        <button class="btn-add" name="add_user">Ekle</button>
    </form>
</div>

<!-- KULLANICI TABLOSU -->
<div class="box">
    <h3>Mevcut Kullanıcılar</h3>
    <table>
        <tr>
            <th>ID</th>
            <th>Kullanıcı</th>
            <th>Şifre Değiştir</th>
            <th>Sil</th>
        </tr>

        <?php while($u = $users->fetch_assoc()){ ?>
        <tr>
            <td><?= $u['id'] ?></td>
            <td><?= htmlspecialchars($u['username']) ?></td>

            <td>
                <form method="POST" style="display:flex; gap:10px;">
                    <input type="hidden" name="id" value="<?= $u['id'] ?>">
                    <input type="password" name="new_password" placeholder="Yeni şifre" required>
                    <button class="btn-pass" name="update_pass">Güncelle</button>
                </form>
            </td>

            <td>
                <?php if($u['id'] != $_SESSION['user_id']){ ?>
                <form method="POST">
                    <input type="hidden" name="id" value="<?= $u['id'] ?>">
                    <button name="delete_user" class="btn-del" onclick="return confirm('Bu kullanıcı silinsin mi?');">
                        Sil
                    </button>
                </form>
                <?php } else { echo "❌"; } ?>
            </td>
        </tr>
        <?php } ?>
    </table>
</div>

<a href="index.php" style="color:#38bdf8; font-size:18px;">⬅ Panele Geri Dön</a>

</body>
</html>
