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

sikompro_require_role('pelaksana');

$userId = (int) $_SESSION['user_id'];
$profileUser = komplain_user_profile($conn, $userId);
$mulai = $_GET['mulai'] ?? date('Y-m-01');
$selesai = $_GET['selesai'] ?? date('Y-m-t');

$mulaiSql = mysqli_real_escape_string($conn, $mulai);
$selesaiSql = mysqli_real_escape_string($conn, $selesai);

$userRow = mysqli_fetch_assoc(mysqli_query($conn, "SELECT username, role, gaji_harian, gaji_lembur_per_jam FROM users WHERE id='{$userId}' LIMIT 1"));
$absenRow = mysqli_fetch_assoc(mysqli_query($conn, "
    SELECT COUNT(a.id) AS hari_masuk
    FROM absensi a
    WHERE a.user_id = '{$userId}'
      AND a.tanggal BETWEEN '{$mulaiSql}' AND '{$selesaiSql}'
      AND a.jam_masuk IS NOT NULL
"));

$hariMasuk = (int) ($absenRow['hari_masuk'] ?? 0);
$gajiHarian = (float) ($userRow['gaji_harian'] ?? 0);
$lemburRow = mysqli_fetch_assoc(mysqli_query($conn, "
    SELECT COALESCE(SUM(CASE
        WHEN a.jam_lembur_mulai IS NOT NULL AND a.jam_lembur_selesai IS NOT NULL
        THEN LEAST(TIMESTAMPDIFF(MINUTE, a.jam_lembur_mulai, a.jam_lembur_selesai), 1440)
        ELSE 0
    END), 0) AS lembur_menit
    FROM absensi a
    WHERE a.user_id = '{$userId}'
      AND a.tanggal BETWEEN '{$mulaiSql}' AND '{$selesaiSql}'
"));
$lemburJam = round((int) ($lemburRow['lembur_menit'] ?? 0) / 60, 2);
$lemburRate = (float) ($userRow['gaji_lembur_per_jam'] ?? 0);
$lemburTotal = $lemburJam * $lemburRate;
$total = ($hariMasuk * $gajiHarian) + $lemburTotal;
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Gaji 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/6.4.0/css/all.min.css">
<style>
.salary-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);
}
.salary-page .wrapper,
.salary-page .content-wrapper{background:transparent !important;color:var(--sikompro-ink, #eef5ff)}
.salary-page .hero,
.salary-page .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)) !important;
  color:var(--sikompro-ink, #eef5ff);
}
.salary-page .hero{
  background:var(--sikompro-surface, rgba(9,17,32,.94)) !important;
  padding:24px;
  margin-bottom:18px;
}
.salary-page .sidebar-profile{text-align:center;padding:14px 8px;color:#fff;border-bottom:1px solid rgba(255,255,255,.12)}
.salary-page .sidebar-profile img{width:76px;height:76px;object-fit:cover;border-radius:50%;border:3px solid rgba(255,255,255,.45)}
.money{font-size:2rem;font-weight:700}
@media (max-width:576px){.salary-page .hero{padding:18px}.salary-page .content-wrapper{padding:12px!important}}
</style>
<link rel="stylesheet" href="../assets/sikompro-ux.css?v=20260722.11">
</head>
<body class="hold-transition sidebar-mini layout-fixed dashboard-shell salary-page">
<div class="wrapper">
<nav class="main-header navbar navbar-expand navbar-white navbar-light">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link" data-widget="pushmenu" href="#"><i class="fas fa-bars"></i></a></li>
</ul>
<ul class="navbar-nav ms-auto align-items-center">
<li class="nav-item">
<button type="button" class="btn btn-outline-light btn-sm dashboard-theme-toggle" data-theme-toggle>
<i data-theme-toggle-icon class="fa fa-adjust"></i>
<span data-theme-toggle-label>Mode Siang</span>
</button>
</li>
</ul>
</nav>
<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><?= htmlspecialchars(ucfirst($profileUser['role'])) ?></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</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 active"><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"><i class="nav-icon fas fa-user-circle"></i> Profil</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>
            <li class="nav-item mt-2 px-3"><button type="button" class="btn sidebar-theme-toggle" data-theme-toggle><i data-theme-toggle-icon class="fa fa-adjust"></i><span data-theme-toggle-label>Mode Siang</span></button></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="hero">
        <div class="d-flex justify-content-between flex-wrap align-items-center" style="gap:10px;">
            <div>
                <h4 class="mb-1">Gaji Pelaksana</h4>
                <p class="mb-0">Ringkasan gaji personal berdasarkan absensi pada periode yang dipilih. Lembur dihitung per jam, maksimal 1 hari.</p>
            </div>
            <div>
                <a href="data_absensi.php" class="btn btn-outline-primary"><i class="fas fa-users"></i> Data Absensi</a>
                <a href="absen.php" class="btn btn-outline-success"><i class="fas fa-camera"></i> Absen</a>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3">
            <div class="card p-3 mb-3">
                <div class="text-muted">Hari Masuk</div>
                <div class="money"><?= $hariMasuk ?></div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card p-3 mb-3">
                <div class="text-muted">Gaji Harian</div>
                <div class="money">Rp <?= number_format($gajiHarian,0,',','.') ?></div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card p-3 mb-3">
                <div class="text-muted">Lembur</div>
                <div class="money"><?= number_format($lemburJam,2,',','.') ?> jam</div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card p-3 mb-3">
                <div class="text-muted">Estimasi Total</div>
                <div class="money">Rp <?= number_format($total,0,',','.') ?></div>
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-body">
            <form method="get" class="row g-2 align-items-end mb-3">
                <label class="me-2">Mulai</label>
                <input type="date" name="mulai" class="form-control mr-2" value="<?= htmlspecialchars($mulai) ?>">
                <label class="me-2">Selesai</label>
                <input type="date" name="selesai" class="form-control mr-2" value="<?= htmlspecialchars($selesai) ?>">
                <button class="btn btn-info"><i class="fas fa-filter"></i> Filter</button>
            </form>
            <p class="mb-0 text-muted">
                User: <b><?= htmlspecialchars((string)($userRow['username'] ?? $_SESSION['username'])) ?></b>
                | Role: <b><?= htmlspecialchars((string)($userRow['role'] ?? $_SESSION['role'])) ?></b>
                | OT Rate: <b>Rp <?= number_format((float)($userRow['gaji_lembur_per_jam'] ?? 0),0,',','.') ?>/jam</b>
            </p>
        </div>
    </div>
</div>
</div>
<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>















