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

sikompro_require_role('teknisi');

$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);

$q = mysqli_query($conn, "
    SELECT a.*
    FROM absensi a
    WHERE a.user_id = '{$userId}'
      AND a.tanggal BETWEEN '{$mulaiSql}' AND '{$selesaiSql}'
    ORDER BY a.tanggal DESC, a.id DESC
");
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Data Absensi Teknisi | 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>
.sidebar-profile{text-align:center;padding:14px 8px;color:#fff;border-bottom:1px solid rgba(255,255,255,.12)}
.sidebar-profile img{width:76px;height:76px;object-fit:cover;border-radius:50%;border:3px solid rgba(255,255,255,.45)}
.thumb{width:72px;height:72px;object-fit:cover;border-radius:10px}
@media (max-width:576px){.content-wrapper{padding:12px!important}.thumb{width:58px;height:58px}}
</style>
<link rel="stylesheet" href="../assets/sikompro-ux.css?v=20260722.11">
</head>
<body class="hold-transition sidebar-mini dashboard-shell">
<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="teknisi.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="teknisi.php" class="nav-link"><i class="nav-icon fas fa-home"></i> Dashboard</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 active"><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"><i class="nav-icon fas fa-user-circle"></i> Profil</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">
    <div class="page-hero utility-page-hero">
        <div class="d-flex justify-content-between flex-wrap align-items-center" style="gap:10px;">
            <div>
                <h4 class="mb-1">Data Absensi Teknisi</h4>
                <p class="mb-0">Riwayat absen masuk, pulang, dan lembur untuk akun Anda.</p>
            </div>
            <div>
                <a href="absen.php" class="btn btn-outline-primary"><i class="fas fa-camera"></i> Absen</a>
                <a href="gaji.php" class="btn btn-outline-success"><i class="fas fa-money-bill"></i> Gaji</a>
            </div>
        </div>
    </div>
    <div class="card utility-page-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" value="<?= htmlspecialchars($mulai) ?>">
                <label class="me-2">Selesai</label>
                <input type="date" name="selesai" class="form-control" value="<?= htmlspecialchars($selesai) ?>">
                <button class="btn btn-info"><i class="fas fa-filter"></i> Filter</button>
            </form>
            <div class="table-responsive complaint-table-wrap">
                <table class="table table-bordered table-striped align-middle">
                    <thead>
                        <tr>
                            <th>Tanggal</th>
                            <th>Masuk</th>
                            <th>Pulang</th>
                            <th>Lembur Mulai</th>
                            <th>Lembur Selesai</th>
                            <th>Durasi Lembur</th>
                            <th>Status</th>
                            <th>Lokasi</th>
                            <th>Keterangan Lembur</th>
                            <th>Foto Masuk</th>
                            <th>Foto Pulang</th>
                            <th>Foto Lembur</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while($d = mysqli_fetch_assoc($q)): ?>
                        <tr>
                            <td><?= htmlspecialchars($d['tanggal']) ?></td>
                            <td><?= htmlspecialchars((string)($d['jam_masuk'] ?? '-')) ?></td>
                            <td><?= htmlspecialchars((string)($d['jam_pulang'] ?? '-')) ?></td>
                            <td><?= htmlspecialchars((string)($d['jam_lembur_mulai'] ?? '-')) ?></td>
                            <td><?= htmlspecialchars((string)($d['jam_lembur_selesai'] ?? '-')) ?></td>
                            <td><?= htmlspecialchars(komplain_absensi_overtime_label($d)) ?></td>
                            <td><span class="badge badge-info"><?= htmlspecialchars((string)($d['status'] ?? '-')) ?></span></td>
                            <td><?= htmlspecialchars(komplain_absensi_location_label($d)) ?></td>
                            <td><?= nl2br(htmlspecialchars((string)($d['keterangan_lembur'] ?? '-'))) ?></td>
                            <td>
                                <?php if(!empty($d['foto'])): ?>
                                    <img class="thumb" src="../uploads/absen/<?= rawurlencode($d['foto']) ?>" alt="Foto masuk">
                                <?php else: ?>
                                    -
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if(!empty($d['foto_pulang'])): ?>
                                    <img class="thumb" src="../uploads/absen/<?= rawurlencode($d['foto_pulang']) ?>" alt="Foto pulang">
                                <?php else: ?>
                                    -
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if(!empty($d['foto_lembur'])): ?>
                                    <img class="thumb" src="../uploads/absen/<?= rawurlencode($d['foto_lembur']) ?>" alt="Foto lembur">
                                <?php else: ?>
                                    -
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        </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>















