<?php
include "../config/koneksi.php";
include "../middleware/auth.php";
include "../config/komplain_workflow.php";

sikompro_require_role('admin');

$id = (int) $_SESSION['user_id'];
$message = "";
$adminNotifCount = komplain_notification_count($conn, 'admin');

if (isset($_POST['update'])) {
    $username = mysqli_real_escape_string($conn, trim($_POST['username']));
    $email = mysqli_real_escape_string($conn, trim($_POST['email']));
    $passwordSql = "";
    $fotoSql = "";
    $gaji = komplain_parse_money($_POST['gaji_harian'] ?? 0);

    if (trim($_POST['password']) !== '') {
        $hash = mysqli_real_escape_string($conn, password_hash($_POST['password'], PASSWORD_DEFAULT));
        $passwordSql = ", password='{$hash}'";
    }

    if (!empty($_FILES['foto_profil']['name'])) {
        if (!is_dir("../uploads/profil")) {
            mkdir("../uploads/profil", 0777, true);
        }

        $foto = komplain_safe_uploaded_name('profil_' . $id, $_FILES['foto_profil']['name']);

        if (move_uploaded_file($_FILES['foto_profil']['tmp_name'], "../uploads/profil/" . $foto)) {
            $fotoSql = ", foto_profil='" . mysqli_real_escape_string($conn, $foto) . "'";
        }
    }

    mysqli_query($conn, "UPDATE users SET username='{$username}', email='{$email}', gaji_harian='{$gaji}' {$passwordSql} {$fotoSql} WHERE id='{$id}'");
    $_SESSION['username'] = $username;
    $message = "Profil berhasil diperbarui / Profile updated.";
}

$data = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM users WHERE id='{$id}'"));
$bulanIni = date('Y-m-01');
$akhirIni = date('Y-m-t');
$absenGaji = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(id) hari FROM absensi WHERE user_id='{$id}' AND tanggal BETWEEN '{$bulanIni}' AND '{$akhirIni}' AND jam_masuk IS NOT NULL"));
$totalGajiProfil = (int)($absenGaji['hari'] ?? 0) * (float)($data['gaji_harian'] ?? 0);
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#0f2742">
<title>Profil Admin / Profile | 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>
.profile-shell { max-width: 980px; margin: 0 auto; }
.profile-hero { background: linear-gradient(135deg, #fff 0%, #eef5ff 100%); border-radius: 24px; padding: 26px; margin-bottom: 18px; box-shadow: 0 14px 34px rgba(31,45,61,.08); }
.profile-grid { display: grid; grid-template-columns: 280px 1fr; gap: 18px; }
.profile-side, .profile-form { background: #fff; border-radius: 20px; box-shadow: 0 14px 30px rgba(31,45,61,.08); overflow: hidden; }
.profile-side { padding: 24px; text-align: center; }
.profile-avatar { width: 120px; height: 120px; object-fit: cover; border-radius: 50%; border: 4px solid #dbe7f5; box-shadow: 0 10px 24px rgba(79,142,247,.18); }
.profile-photo-picker { display:flex; justify-content:center; margin-bottom:14px; }
.profile-photo-trigger { display:inline-flex; flex-direction:column; align-items:center; gap:10px; cursor:pointer; user-select:none; }
.profile-photo-trigger img { width: 120px; height: 120px; object-fit: cover; border-radius: 50%; border: 4px solid #dbe7f5; box-shadow: 0 10px 24px rgba(79,142,247,.18); transition: transform .18s ease, box-shadow .18s ease; }
.profile-photo-trigger:hover img, .profile-photo-trigger:focus-visible img { transform: scale(1.03); box-shadow: 0 14px 28px rgba(79,142,247,.22); }
.profile-photo-hint { font-size: .82rem; font-weight:700; color:#64748b; }
.profile-photo-input { display:none; }
.profile-badge { display:inline-flex; align-items:center; gap:8px; margin-top:12px; padding:8px 12px; border-radius:999px; background:#eef5ff; color:#2e67d8; font-weight:700; }
.profile-form { padding: 24px; }
.profile-stats { display:grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 12px; margin: 18px 0; }
.profile-stat { padding: 14px; border-radius: 16px; background: linear-gradient(135deg, #f8fbff 0%, #edf5ff 100%); border: 1px solid #dbe7f5; }
.profile-stat strong { display:block; font-size: 1.2rem; color:#1a2c45; }
@media (max-width: 768px) { .profile-grid { grid-template-columns: 1fr; } .profile-hero { padding:18px; border-radius:18px; } .profile-side, .profile-form { border-radius:18px; } }
</style>
</head>
<body class="hold-transition sidebar-mini dashboard-shell">
<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">
<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> Absensi</a></li>
<li class="nav-item"><a href="profil.php" class="nav-link active"><i class="nav-icon fas fa-user-circle"></i> Profil / Profile</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="../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="profile-shell">
<div class="profile-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">Account Settings / Pengaturan Akun</div>
<h3 class="mb-1">Profil Admin</h3>
<p class="mb-0">Kelola foto, identitas, dan password dari satu layar yang nyaman di HP.</p>
</div>
<a href="dashboard.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Kembali / Back</a>
</div>
</div>
<?php if ($message): ?><div class="alert alert-success"><?= htmlspecialchars($message) ?></div><?php endif; ?>
<div class="profile-grid">
<div class="profile-side">
<div class="profile-photo-picker">
<label for="foto_profil" class="profile-photo-trigger" tabindex="0">
<img class="profile-avatar" src="<?= komplain_profile_photo_url($data) ?>" alt="Foto profil / Profile photo">
<span class="profile-photo-hint">Ketuk foto untuk ganti</span>
</label>
</div>
<div class="mt-3 font-weight-bold h5 mb-1"><?= htmlspecialchars($data['username']) ?></div>
<div class="text-muted mb-2"><?= htmlspecialchars(ucfirst($data['role'])) ?></div>
<div class="profile-badge"><i class="fas fa-wallet"></i> Gaji Harian / Daily Pay</div>
<div class="profile-stats">
<div class="profile-stat"><small class="text-muted">Hari Masuk / Days</small><strong><?= (int)($absenGaji['hari'] ?? 0) ?></strong></div>
<div class="profile-stat"><small class="text-muted">Estimasi / Estimate</small><strong>Rp <?= number_format($totalGajiProfil, 0, ',', '.') ?></strong></div>
</div>
</div>
<div class="profile-form">
<form method="post" enctype="multipart/form-data">
<div class="form-group"><label>Foto Profil / Profile Photo</label><input id="foto_profil" type="file" name="foto_profil" class="form-control profile-photo-input" accept="image/*" capture="user"></div>
<div class="form-group"><label>Username</label><input type="text" name="username" class="form-control" value="<?= htmlspecialchars($data['username']) ?>" required></div>
<div class="form-group"><label>Email</label><input type="email" name="email" class="form-control" value="<?= htmlspecialchars((string) $data['email']) ?>"></div>
<div class="form-group"><label>Role</label><input type="text" class="form-control" value="<?= htmlspecialchars($data['role']) ?>" readonly></div>
<div class="form-group"><label>Gaji Harian / Daily Pay</label><input type="text" name="gaji_harian" class="form-control money-input" value="<?= number_format((float) $data['gaji_harian'], 0, ',', '.') ?>"></div>
<div class="alert alert-info">Gaji bulan ini / This month: <b>Rp <?= number_format($totalGajiProfil, 0, ',', '.') ?></b></div>
<div class="form-group"><label>Password Baru / New Password</label><input type="password" name="password" class="form-control" placeholder="Kosongkan jika tidak diganti / Leave blank if unchanged"></div>
<button name="update" class="btn btn-primary"><i class="fas fa-save"></i> Simpan Profil / Save</button>
</form>
</div>
</div>
</div>
</div>
<script>document.querySelectorAll('.money-input').forEach(i=>i.addEventListener('input',()=>{let v=i.value.replace(/\D/g,'');i.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>













