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

// Pastikan hanya Super User yang bisa akses
if (!isset($_SESSION['role']) || $_SESSION['role'] != 'super_user') {
    header("Location: index.php");
    exit;
}

// Tambah user
if (isset($_POST['add_user'])) {
    $username   = trim($_POST['username']);
    $password   = trim($_POST['password']);
    $role       = $_POST['role'];
    $full_name  = trim($_POST['full_name']);

    if ($username && $password && $role) {
        // SIMPAN PASSWORD TANPA HASH
        $stmt = $koneksi->prepare("INSERT INTO users (username, password, role, full_name) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss", $username, $password, $role, $full_name);
        $stmt->execute();
        $stmt->close();
    }
    header("Location: manage_users.php");
    exit;
}

// Hapus user
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    $koneksi->query("DELETE FROM users WHERE id_user=$id");
    header("Location: manage_users.php");
    exit;
}

// Edit user
if (isset($_POST['edit_user'])) {
    $id         = intval($_POST['id_user']);
    $username   = trim($_POST['username']);
    $role       = $_POST['role'];
    $full_name  = trim($_POST['full_name']);
    $password   = trim($_POST['password']);

    if (!empty($password)) {
        // UPDATE PASSWORD TANPA HASH
        $stmt = $koneksi->prepare("UPDATE users SET username=?, password=?, role=?, full_name=? WHERE id_user=?");
        $stmt->bind_param("ssssi", $username, $password, $role, $full_name, $id);
    } else {
        $stmt = $koneksi->prepare("UPDATE users SET username=?, role=?, full_name=? WHERE id_user=?");
        $stmt->bind_param("sssi", $username, $role, $full_name, $id);
    }
    $stmt->execute();
    $stmt->close();
    header("Location: manage_users.php");
    exit;
}

// Ambil semua user
$users = $koneksi->query("SELECT * FROM users ORDER BY id_user ASC");
?>

<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow-sm">
  <div class="container-fluid">
    <a class="navbar-brand fw-bold" href="#">
      <i class="fas fa-users-cog me-2"></i>Manajemen User
    </a>

    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarMenu">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse justify-content-end" id="navbarMenu">
      <ul class="navbar-nav mb-2 mb-lg-0">
        <li class="nav-item me-3">
          <span class="nav-link text-white">
            <i class="fas fa-user-circle me-1"></i> 
            <?= htmlspecialchars($_SESSION['full_name'] ?? 'Super User') ?>
          </span>
        </li>
        <li class="nav-item">
          <a class="btn btn-light btn-sm" href="logout.php">
            <i class="fas fa-sign-out-alt me-1"></i> Logout
          </a>
        </li>
      </ul>
    </div>
  </div>
</nav>

  
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Manajemen User - Finance System</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>

<body class="bg-light">

<div class="container py-4">
    <div class="card shadow">
        <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
            <h4 class="mb-0">Manajemen User</h4>
            <button class="btn btn-light btn-sm" data-bs-toggle="modal" data-bs-target="#addUserModal">Hatama Utilizador</button>
        </div>
        <div class="card-body">
  <table id="userTable" class="table table-bordered table-striped align-middle">
    <thead class="table-dark text-center">
      <tr>
        <th>ID</th>
        <th>Username</th>
        <th>Naran kompletu</th>
        <th>Role</th>
        <th width="150">Aksi</th>
      </tr>
    </thead>
    <tbody>
      <?php while ($row = $users->fetch_assoc()) { ?>
      <tr>
        <td class="text-center"><?= $row['id_user'] ?></td>
        <td><?= htmlspecialchars($row['username']) ?></td>
        <td><?= htmlspecialchars($row['full_name']) ?></td>
        <td class="text-center">
          <span class="badge bg-<?=
$row['role'] == 'super_user' ? 'danger' :
($row['role'] == 'director' ? 'success' :
($row['role'] == 'dct' ? 'warning text-dark' :
($row['role'] == 'dfo' ? 'warning text-dark' :
($row['role'] == 'arsip' ? 'info text-dark' :
($row['role'] == 'admin' ? 'secondary' : 'dark')))))
          ?>">
            <?= strtoupper($row['role']) ?>
          </span>
        </td>
        <td class="text-center">
          <?php if ($row['role'] != 'super_user') { ?>
            <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#editUserModal<?= $row['id_user'] ?>">
              <i class="fas fa-edit"></i>
            </button>
            <a href="?delete=<?= $row['id_user'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Ita-boot iha serteza katak ita-boot hakarak atu hamoos utilizad�r ida-nee?')">
              <i class="fas fa-trash-alt"></i>
            </a>
          <?php } else { ?>
            <span class="text-muted">-</span>
          <?php } ?>
        </td>
      </tr>
      <?php } ?>
    </tbody>
  </table>
</div>


                    <!-- Modal Edit -->
                    <div class="modal fade" id="editUserModal<?= $row['id_user'] ?>" tabindex="-1">
                        <div class="modal-dialog">
                            <form method="POST" class="modal-content">
                                <div class="modal-header bg-primary text-white">
                                    <h5 class="modal-title">Hadi'a Utilizador sira</h5>
                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                </div>
                                <div class="modal-body">
                                    <input type="hidden" name="id_user" value="<?= $row['id_user'] ?>">
                                    <div class="mb-3">
                                        <label>Username</label>
                                        <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($row['username']) ?>" required>
                                    </div>
                                    <div class="mb-3">
                                        <label>Password (husik mamuk se la troka)</label>
                                        <input type="password" name="password" class="form-control">
                                    </div>
                                    <div class="mb-3">
                                        <label>Naran kompletu</label>
                                        <input type="text" name="full_name" class="form-control" value="<?= htmlspecialchars($row['full_name']) ?>">
                                    </div>
                                    <div class="mb-3">
                                        <label>Role</label>
                                        <select name="role" class="form-select" required>
                                            <option value="admin" <?= $row['role']=='admin'?'selected':'' ?>>Admin</option>
                                            <option value="dfo" <?= $row['role']=='dfo'?'selected':'' ?>>DFO</option>
                                            <option value="dct" <?= $row['role']=='dct'?'selected':'' ?>>DCT</option>
          
                                            <option value="arsip" <?= $row['role']=='arsip'?'selected':'' ?>>Arsip</option>
                                            <option value="director" <?= $row['role']=='director'?'selected':'' ?>>Director</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" name="edit_user" class="btn btn-primary">Salva</button>
                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Taka</button>
                                </div>
                            </form>
                        </div>
                    </div>
                
                </tbody>
            </table>
        </div>
    </div>
</div>



<?php
// Ulangi hasil query untuk modal edit
$users = $koneksi->query("SELECT * FROM users ORDER BY id_user ASC");
while ($row = $users->fetch_assoc()) {
?>
<div class="modal fade" id="editUserModal<?= $row['id_user'] ?>" tabindex="-1">
  <div class="modal-dialog">
    <form method="POST" class="modal-content">
      <div class="modal-header bg-primary text-white">
        <h5 class="modal-title">Edita Utilizador sira</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
      </div>
      <div class="modal-body">
        <input type="hidden" name="id_user" value="<?= $row['id_user'] ?>">
        <div class="mb-3">
          <label>Username</label>
          <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($row['username']) ?>" required>
        </div>
        <div class="mb-3">
          <label>Password (husik mamuk se la troka)</label>
          <input type="password" name="password" class="form-control">
        </div>
        <div class="mb-3">
          <label>Naran kompletu</label>
          <input type="text" name="full_name" class="form-control" value="<?= htmlspecialchars($row['full_name']) ?>">
        </div>
        <div class="mb-3">
          <label>Role</label>
          <select name="role" class="form-select" required>
            <option value="admin" <?= $row['role']=='admin'?'selected':'' ?>>admin</option>
            <option value="dfo" <?= $row['role']=='dfo'?'selected':'' ?>>DFO</option>
            <option value="dct" <?= $row['role']=='dct'?'selected':'' ?>>DCT</option>
            
            <option value="arsip" <?= $row['role']=='arsip'?'selected':'' ?>>Arsip</option>
            <option value="director" <?= $row['role']=='director'?'selected':'' ?>>Director</option>
          </select>
        </div>
      </div>
      <div class="modal-footer">
        <button type="submit" name="edit_user" class="btn btn-primary">Salva</button>
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Taka</button>
      </div>
    </form>
  </div>
</div>
<?php } ?>

<!-- Modal Tambah User -->
<div class="modal fade" id="addUserModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title">Hatama Utilizador Foun</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label>Username</label>
                    <input type="text" name="username" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label>Naran kompletu</label>
                    <input type="text" name="full_name" class="form-control">
                </div>
                <div class="mb-3">
                    <label>Role</label>
                    <select name="role" class="form-select" required>
                        <option value="admin">Admin</option>
                        <option value="dfo">DFO</option>
                        <option value="dct">DCT</option>
                        
                        <option value="arsip">Arsip</option>
                        <option value="director">Director</option>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button type="submit" name="add_user" class="btn btn-success">Plus</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Taka</button>
            </div>
        </form>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>
<script>
$(document).ready(function() {
    $('#userTable').DataTable({
        "pageLength": 10,
        "language": {
            "search": "Ba buka:",
            "lengthMenu": "Eventu _MENU_ data",
            "info": "Hatudu _START_ - _END_ husi _TOTAL_ data",
            "paginate": {"previous": "Antes", "next": "Tuir mai"}
        }
    });
});
</script>
</body>
</html>
