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

sikompro_require_role('pelaksana');

$id = (int) $_SESSION['user_id'];
$message = "";

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.";
}

$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>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Profil Pelaksana | 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/5.15.4/css/all.min.css">
<style>
.profile-page{
  background:
    radial-gradient(circle at top left, var(--sikompro-page-bg-accent, rgba(14,165,233,.18)), transparent 34%),
    radial-gradient(circle at top right, color-mix(in srgb, var(--sikompro-page-bg-accent, rgba(14,165,233,.18)) 70%, transparent), transparent 30%),
    linear-gradient(180deg, var(--sikompro-page-bg-top, #06111f) 0%, var(--sikompro-page-bg-bottom, #0f1f3d) 100%);
  color:var(--sikompro-ink, #eef5ff);
}
.profile-page .content-wrapper{background:transparent !important;color:var(--sikompro-ink, #eef5ff)}
.profile-page .profile-card{
  border:1px solid var(--sikompro-line, rgba(148,163,184,.16));
  border-radius:16px;
  box-shadow:0 12px 28px rgba(31,45,61,.08);
  background:var(--sikompro-surface, rgba(9,17,32,.94));
  color:var(--sikompro-ink, #eef5ff);
}
.profile-page .profile-card .text-muted,
.profile-page .profile-photo-hint,
.profile-page .alert-info{
  color:var(--sikompro-muted, rgba(226,232,240,.8)) !important;
}
.profile-page .alert-info{
  background:var(--sikompro-surface-alt, rgba(15,23,42,.82)) !important;
  border:1px solid var(--sikompro-line, rgba(148,163,184,.2)) !important;
  color:var(--sikompro-ink, #eef5ff) !important;
}
.profile-page .form-control{
  background:var(--sikompro-surface-alt, rgba(15,23,42,.78));
  color:var(--sikompro-ink, #eef5ff);
  border-color:var(--sikompro-line, rgba(148,163,184,.2));
}
.profile-page .profile-photo-picker{display:flex;justify-content:center;margin-bottom:14px}
.profile-page .profile-photo-trigger{display:inline-flex;flex-direction:column;align-items:center;gap:10px;cursor:pointer;user-select:none}
.profile-page .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-page .profile-photo-trigger:hover img,.profile-page .profile-photo-trigger:focus-visible img{transform:scale(1.03);box-shadow:0 14px 28px rgba(79,142,247,.22)}
.profile-page .profile-photo-input{display:none}
</style>
<link rel="stylesheet" href="../assets/sikompro-ux.css?v=20260722.11">
</head>
<body class="hold-transition sidebar-mini layout-fixed dashboard-shell profile-page">
<div class="wrapper">
<aside class="main-sidebar sidebar-dark-primary elevation-4">
<a class="brand-link text-center text-white">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> Absen</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"><i class="nav-icon fas fa-user-cog"></i> Akses User</a></li>
<li class="nav-item"><a href="profil.php" class="nav-link active"><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</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">
<a href="dashboard.php" class="dashboard-back-button mb-3"><i class="fa fa-arrow-left" aria-hidden="true"></i><span>Kembali</span></a>
<div class="card profile-card">
<div class="card-header"><h4 class="mb-0">Profil Pengguna</h4></div>
<div class="card-body">
<?php if ($message): ?><div class="alert alert-success"><?= htmlspecialchars($message) ?></div><?php endif; ?>
<form method="post" enctype="multipart/form-data">
<div class="profile-photo-picker"><label for="foto_profil" class="profile-photo-trigger" tabindex="0"><img src="<?= komplain_profile_photo_url($data) ?>" alt="Foto profil" style="width:100px;height:100px;object-fit:cover;border-radius:50%"><span class="profile-photo-hint">Ketuk foto untuk ganti</span></label></div>
<div class="form-group"><label>Foto Profil</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</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">Hasil gaji bulan ini: <b>Rp <?= number_format($totalGajiProfil, 0, ',', '.') ?></b> dari <?= (int)($absenGaji['hari'] ?? 0) ?> hari masuk.</div>
<div class="form-group"><label>Password Baru</label><input type="password" name="password" class="form-control" placeholder="Kosongkan jika tidak diganti"></div>
<button name="update" class="btn btn-primary"><i class="fas fa-save"></i> Simpan Profil</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>















