<?php
include "../config/koneksi.php";
include "../middleware/auth.php";
include "../config/komplain_workflow.php";
sikompro_require_role('admin');
$profileUser = komplain_user_profile($conn, (int) $_SESSION['user_id']);
$adminNotifCount = komplain_notification_count($conn, 'admin');

if (isset($_POST['tambah_user'])) {
    $username = mysqli_real_escape_string($conn, trim($_POST['username'] ?? ''));
    $email = mysqli_real_escape_string($conn, trim($_POST['email'] ?? ''));
    $noHp = mysqli_real_escape_string($conn, sikompro_normalize_phone(trim($_POST['no_hp'] ?? '')));
    $role = mysqli_real_escape_string($conn, $_POST['role'] ?? '');
    $gaji = komplain_parse_money($_POST['gaji_harian'] ?? 0);
    $lembur = komplain_parse_money($_POST['gaji_lembur_per_jam'] ?? 0);
    $lemburTotal = komplain_parse_money($_POST['gaji_lembur_total'] ?? 0);
    $password = password_hash($_POST['password'] ?? '123456', PASSWORD_DEFAULT);
    $passwordSql = mysqli_real_escape_string($conn, $password);
    if ($username !== '' && in_array($role, ['admin','pelaksana','teknisi'], true)) {
        mysqli_query($conn, "INSERT INTO users(username,email,no_hp,password,role,gaji_harian,gaji_lembur_per_jam,gaji_lembur_total) VALUES('{$username}','{$email}','{$noHp}','{$passwordSql}','{$role}','{$gaji}','{$lembur}','{$lemburTotal}')");
    }
    header("Location: users.php?msg=tambah");
    exit;
}

if (isset($_POST['edit_user'])) {
    $id = (int) $_POST['user_id'];
    $username = mysqli_real_escape_string($conn, trim($_POST['username'] ?? ''));
    $email = mysqli_real_escape_string($conn, trim($_POST['email'] ?? ''));
    $noHp = mysqli_real_escape_string($conn, sikompro_normalize_phone(trim($_POST['no_hp'] ?? '')));
    $role = mysqli_real_escape_string($conn, $_POST['role'] ?? '');
    $gaji = komplain_parse_money($_POST['gaji_harian'] ?? 0);
    $lembur = komplain_parse_money($_POST['gaji_lembur_per_jam'] ?? 0);
    $lemburTotal = komplain_parse_money($_POST['gaji_lembur_total'] ?? 0);
    $passwordSql = '';
    if (trim($_POST['password'] ?? '') !== '') {
        $passwordSql = ", password='" . mysqli_real_escape_string($conn, password_hash($_POST['password'], PASSWORD_DEFAULT)) . "'";
    }
    mysqli_query($conn, "UPDATE users SET username='{$username}', email='{$email}', no_hp='{$noHp}', role='{$role}', gaji_harian='{$gaji}', gaji_lembur_per_jam='{$lembur}', gaji_lembur_total='{$lemburTotal}' {$passwordSql} WHERE id='{$id}'");
    header("Location: users.php?msg=edit");
    exit;
}

if (isset($_POST['hapus_user'])) {
    $id = (int) $_POST['user_id'];
    if ($id !== (int) $_SESSION['user_id']) {
        mysqli_query($conn, "DELETE FROM users WHERE id='{$id}'");
    }
    header("Location: users.php?msg=hapus");
    exit;
}

$users = mysqli_query($conn, "SELECT * FROM users ORDER BY FIELD(role,'admin','pelaksana','teknisi'), username");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Akses User / User Access | PT. ALVINDO</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="../assets/sikompro-ux.css?v=20260722.11">
<style>
.user-page .user-shell { max-width: 1200px; margin: 0 auto; }
.user-page .user-hero {
    background: linear-gradient(
        135deg,
        var(--sikompro-surface, rgba(9,17,32,.94)) 0%,
        var(--sikompro-surface-alt, rgba(15,23,42,.86)) 100%
    );
    border-radius: 24px;
    padding: 26px;
    margin-bottom: 18px;
    box-shadow: 0 14px 34px rgba(31,45,61,.08);
    border: 1px solid var(--sikompro-line, rgba(148,163,184,.16));
}
.user-page .user-card { border:0; border-radius:18px; box-shadow:0 12px 28px rgba(31,45,61,.08); overflow:hidden; background:var(--sikompro-surface, rgba(9,17,32,.94)); color:var(--sikompro-ink, #eef5ff); }
.money-input { text-align:right; }
.table td { vertical-align: middle; }
.user-page .bg-light {
    background: var(--sikompro-surface-alt, rgba(15,23,42,.82)) !important;
    color: var(--sikompro-ink, #eef5ff) !important;
    border-color: var(--sikompro-line, rgba(148,163,184,.2)) !important;
}
@media (max-width:768px){ .user-page .user-hero{padding:18px;border-radius:18px;} .card-body{padding:14px;} }
</style>
</head>
<body class="hold-transition sidebar-mini dashboard-shell user-page">
<div class="wrapper">
<aside class="main-sidebar sidebar-dark-primary elevation-4">
<a href="dashboard.php" class="brand-link text-center">PT. ALVINDO</a>
<div class="sidebar"><div class="sidebar-profile"><img src="<?= komplain_profile_photo_url($profileUser) ?>" alt="Foto profil"><div class="mt-2 font-weight-bold"><?= htmlspecialchars($profileUser['username']) ?></div><small>Admin</small></div>
<ul class="nav nav-pills nav-sidebar flex-column">
<li class="nav-item"><a href="dashboard.php" class="nav-link"><i class="nav-icon fas fa-home"></i> Dashboard</a></li>
<li class="nav-item"><a href="data_komplain.php?filter=total" class="nav-link"><i class="nav-icon fas fa-table"></i> Data Komplain</a></li>
<li class="nav-item"><a href="absen.php" class="nav-link"><i class="nav-icon fas fa-user-check"></i> Absen Saya</a></li>
<li class="nav-item"><a href="data_absensi.php" class="nav-link"><i class="nav-icon fas fa-users"></i> Data Absensi</a></li>
<li class="nav-item"><a href="gaji.php" class="nav-link"><i class="nav-icon fas fa-money-bill"></i> Gaji</a></li>
<li class="nav-item"><a href="users.php" class="nav-link active"><i class="nav-icon fas fa-user-cog"></i> Akses User / Access</a></li>
<li class="nav-item"><a href="profil.php" class="nav-link"><i class="nav-icon fas fa-user-circle"></i> Profil Pengguna</a></li>
<li class="nav-item"><a href="notif.php" class="nav-link"><i class="nav-icon fas fa-bell"></i> Notifikasi <span class="badge bg-warning text-dark float-end"><?= $adminNotifCount ?></span></a></li>
<li class="nav-item"><a href="input_komplain_admin.php" class="nav-link"><i class="nav-icon fas fa-plus"></i> Input Komplain</a></li>
<li class="nav-item"><a href="cetak_pdf.php" class="nav-link"><i class="nav-icon fas fa-print"></i> Cetak PDF</a></li>
<li class="nav-item"><a href="../middleware/logout.php" class="nav-link"><i class="nav-icon fas fa-sign-out-alt"></i> Logout</a></li>
</ul></div>
</aside>
<div class="content-wrapper p-3">
<div class="user-shell">
<a href="dashboard.php" class="dashboard-back-button mb-3"><i class="fas fa-arrow-left"></i><span>Kembali</span></a>
<div class="user-hero">
<div class="d-flex justify-content-between flex-wrap align-items-center" style="gap:12px;">
<div>
<div class="text-uppercase small text-muted font-weight-bold">User Management / Manajemen User</div>
<h3 class="mb-1">Data Akses User Semua Role</h3>
<p class="mb-0">Tambah, edit, dan hapus user untuk admin, pelaksana, dan teknisi dalam tampilan yang lebih bersih.</p>
</div>
<a href="dashboard.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Dashboard</a>
</div>
</div>
<?php if(isset($_GET['msg'])): ?><div class="alert alert-success">Data user berhasil diproses / User updated.</div><?php endif; ?>
<div class="card user-card mb-3"><div class="card-header bg-primary text-white"><h5 class="mb-0">Tambah User / Add User</h5></div><div class="card-body"><form method="post" class="row">
<div class="col-md-2 mb-2"><input name="username" class="form-control" placeholder="Username" required></div>
<div class="col-md-2 mb-2"><input name="email" type="email" class="form-control" placeholder="Email"></div>
<div class="col-md-2 mb-2"><input name="no_hp" class="form-control" placeholder="No. HP"></div>
<div class="col-md-2 mb-2"><input name="password" type="password" class="form-control" placeholder="Password" required></div>
<div class="col-md-2 mb-2"><select name="role" class="form-control" required><option value="">Role</option><option value="admin">Admin</option><option value="pelaksana">Pelaksana</option><option value="teknisi">Teknisi</option></select></div>
<div class="col-md-2 mb-2"><input name="gaji_harian" class="form-control money-input" placeholder="Gaji harian"></div>
<div class="col-md-2 mb-2"><input name="gaji_lembur_per_jam" class="form-control money-input" placeholder="Gaji lembur / jam"></div>
<div class="col-md-2 mb-2"><input name="gaji_lembur_total" class="form-control money-input" placeholder="Gaji lembur total"></div>
<div class="col-md-2 mb-2"><button name="tambah_user" class="btn btn-success w-100"><i class="fas fa-plus"></i> Tambah</button></div>
</form></div></div>
<div class="card user-card"><div class="card-header"><h5 class="mb-0">Daftar User / User List</h5></div><div class="card-body table-responsive"><table class="table table-bordered table-striped"><thead><tr><th>Username</th><th>Email</th><th>No. HP</th><th>Role</th><th>Gaji Harian</th><th>Gaji Lembur / Jam</th><th>Gaji Lembur Total</th><th>Password Baru</th><th>Aksi</th></tr></thead><tbody><?php while($u=mysqli_fetch_assoc($users)): ?><tr><form method="post"><td><input name="username" class="form-control" value="<?= htmlspecialchars($u['username']) ?>"></td><td><input name="email" type="email" class="form-control" value="<?= htmlspecialchars((string)($u['email'] ?? '')) ?>"></td><td><input name="no_hp" class="form-control" value="<?= htmlspecialchars((string)($u['no_hp'] ?? '')) ?>"></td><td><select name="role" class="form-control"><?php foreach(['admin','pelaksana','teknisi'] as $r): ?><option value="<?= $r ?>" <?= $u['role']===$r?'selected':'' ?>><?= ucfirst($r) ?></option><?php endforeach; ?></select></td><td><input name="gaji_harian" class="form-control money-input" value="<?= number_format((float)($u['gaji_harian'] ?? 0),0,',','.') ?>"></td><td><input name="gaji_lembur_per_jam" class="form-control money-input" value="<?= number_format((float)($u['gaji_lembur_per_jam'] ?? 0),0,',','.') ?>"></td><td><input name="gaji_lembur_total" class="form-control money-input" value="<?= number_format((float)($u['gaji_lembur_total'] ?? 0),0,',','.') ?>"></td><td><input name="password" type="password" class="form-control" placeholder="Kosongkan jika tetap"></td><td><input type="hidden" name="user_id" value="<?= (int)$u['id'] ?>"><button name="edit_user" class="btn btn-outline-warning btn-sm"><i class="fas fa-save"></i></button> <button name="hapus_user" class="btn btn-outline-danger btn-sm" onclick="return confirm('Hapus user ini?')" <?= (int)$u['id']===(int)$_SESSION['user_id']?'disabled':'' ?>><i class="fas fa-trash"></i></button></td></form></tr><?php endwhile; ?></tbody></table></div></div>
</div></div>
<script>document.querySelectorAll('.money-input').forEach(input=>input.addEventListener('input',()=>{let v=input.value.replace(/\D/g,'');input.value=v.replace(/\B(?=(\d{3})+(?!\d))/g,'.');}));</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
<script src="../assets/bootstrap5-compat.js"></script><script src="../assets/sikompro-theme.js?v=20260722.11"></script>
<?php include '../partials/mobile_nav.php'; ?>
</body></html>












