<!-- FILE: tambahpengguna.php -->
<?php
include 'koneksi.php';

if (isset($_POST['tambah'])) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $nohp = $_POST['nohp'];
    $role = $_POST['role'];

    // Cek username sudah ada atau belum
    $cek_username = "SELECT * FROM users WHERE username = '$username'";
    $result_username = mysqli_query($conn, $cek_username);

    // Cek email sudah digunakan atau belum
    $cek_email = "SELECT * FROM users WHERE email = '$email'";
    $result_email = mysqli_query($conn, $cek_email);

    // Cek no hp sudah digunakan atau belum
    $cek_nohp = "SELECT * FROM users WHERE nohp = '$nohp'";
    $result_nohp = mysqli_query($conn, $cek_nohp);

    if (mysqli_num_rows($result_username) > 0) {
        echo "<script>alert('Username sudah digunakan! Gunakan username lain.'); window.location='pengguna.php';</script>";
    } elseif (mysqli_num_rows($result_email) > 0) {
        echo "<script>alert('Email sudah digunakan! Gunakan email lain.'); window.location='pengguna.php';</script>";
    } elseif (mysqli_num_rows($result_nohp) > 0) {
        echo "<script>alert('Nomor HP sudah digunakan! Gunakan nomor HP lain.'); window.location='pengguna.php';</script>";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "<script>alert('Format email tidak valid!'); window.location='pengguna.php';</script>";
    } elseif (strlen($password) < 6) {
        echo "<script>alert('Password minimal 6 karakter!'); window.location='pengguna.php';</script>";
    } else {
        // Hash password untuk keamanan (gunakan password_hash jika ingin lebih aman)
        // $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        // Untuk contoh ini, password disimpan plain text (tidak disarankan untuk production)
        
        $query = "INSERT INTO users (username, email, password, nohp, role) 
                  VALUES ('$username', '$email', '$password', '$nohp', '$role')";
        
        if (mysqli_query($conn, $query)) {
            echo "<script>alert('Pengguna berhasil ditambahkan!'); window.location='pengguna.php';</script>";
        } else {
            echo "<script>alert('Gagal menambahkan pengguna.'); window.location='pengguna.php';</script>";
        }
    }
}
?>

<!-- FILE: editpengguna.php -->
<?php
include 'koneksi.php';

if (isset($_POST['edit'])) {
    $id_user = $_POST['id_user'];
    $username = $_POST['username'];
    $email = $_POST['email'];
    $nohp = $_POST['nohp'];
    $role = $_POST['role'];

    // Cek username sudah digunakan oleh pengguna lain
    $cek_username = "SELECT * FROM users WHERE username = '$username' AND id_user != '$id_user'";
    $result_username = mysqli_query($conn, $cek_username);

    // Cek email sudah digunakan oleh pengguna lain
    $cek_email = "SELECT * FROM users WHERE email = '$email' AND id_user != '$id_user'";
    $result_email = mysqli_query($conn, $cek_email);

    // Cek no hp sudah digunakan oleh pengguna lain
    $cek_nohp = "SELECT * FROM users WHERE nohp = '$nohp' AND id_user != '$id_user'";
    $result_nohp = mysqli_query($conn, $cek_nohp);

    if (mysqli_num_rows($result_username) > 0) {
        echo "<script>alert('Username sudah digunakan oleh pengguna lain!'); window.location='pengguna.php';</script>";
    } elseif (mysqli_num_rows($result_email) > 0) {
        echo "<script>alert('Email sudah digunakan oleh pengguna lain!'); window.location='pengguna.php';</script>";
    } elseif (mysqli_num_rows($result_nohp) > 0) {
        echo "<script>alert('Nomor HP sudah digunakan oleh pengguna lain!'); window.location='pengguna.php';</script>";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "<script>alert('Format email tidak valid!'); window.location='pengguna.php';</script>";
    } else {
        // Password tidak diubah saat edit
        $query = "UPDATE users SET 
                  username = '$username',
                  email = '$email',
                  nohp = '$nohp',
                  role = '$role'
                  WHERE id_user = '$id_user'";
        
        if (mysqli_query($conn, $query)) {
            echo "<script>alert('Data pengguna berhasil diperbarui!'); window.location='pengguna.php';</script>";
        } else {
            echo "<script>alert('Gagal memperbarui data pengguna.'); window.location='pengguna.php';</script>";
        }
    }
}
?>