<?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']);
$notifCount = komplain_notification_count($conn, 'admin');

if (isset($_POST['hapus_absen'])) {
    $id = (int) $_POST['absen_id'];
    mysqli_query($conn, "DELETE FROM absensi WHERE id='{$id}'");
    header("Location: data_absensi.php?msg=hapus");
    exit;
}

if (isset($_POST['edit_absen'])) {
    $id = (int) $_POST['absen_id'];
    $tanggal = mysqli_real_escape_string($conn, $_POST['tanggal'] ?? date('Y-m-d'));
    $jamMasuk = trim((string) ($_POST['jam_masuk'] ?? ''));
    $jamPulang = trim((string) ($_POST['jam_pulang'] ?? ''));
    $status = mysqli_real_escape_string($conn, $_POST['status'] ?? '');
    $jamMasukSql = $jamMasuk !== '' ? "'" . mysqli_real_escape_string($conn, str_replace('T', ' ', $jamMasuk)) . ":00'" : "NULL";
    $jamPulangSql = $jamPulang !== '' ? "'" . mysqli_real_escape_string($conn, str_replace('T', ' ', $jamPulang)) . ":00'" : "NULL";
    mysqli_query($conn, "UPDATE absensi SET tanggal='{$tanggal}', jam_masuk={$jamMasukSql}, jam_pulang={$jamPulangSql}, status='{$status}' WHERE id='{$id}'");
    header("Location: data_absensi.php?msg=edit");
    exit;
}

$mulai = $_GET['mulai'] ?? date('Y-m-01');
$selesai = $_GET['selesai'] ?? date('Y-m-t');
$roleFilter = $_GET['role'] ?? '';
$mulaiSql = mysqli_real_escape_string($conn, $mulai);
$selesaiSql = mysqli_real_escape_string($conn, $selesai);
$roleSql = in_array($roleFilter, ['admin','pelaksana','teknisi'], true) ? " AND u.role='" . mysqli_real_escape_string($conn, $roleFilter) . "'" : "";
$q = mysqli_query($conn, "SELECT a.*, u.username FROM absensi a JOIN users u ON a.user_id=u.id WHERE a.tanggal BETWEEN '{$mulaiSql}' AND '{$selesaiSql}' {$roleSql} 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 | 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>body,.content-wrapper{background:#f4f7fb}.main-sidebar{background:linear-gradient(180deg,#223047,#1d293d)}.hero,.card{border:0;border-radius:16px;box-shadow:0 12px 28px rgba(31,45,61,.08)}.hero{background:linear-gradient(135deg,#fff,#eaf4ff);padding:24px;margin-bottom:18px}.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:70px;height:70px;object-fit:cover;border-radius:10px}.table td{vertical-align:middle}.table-responsive{-webkit-overflow-scrolling:touch}.form-inline{gap:10px}.form-inline .form-control,.form-inline .btn{border-radius:12px}.form-inline .form-control{min-width:160px}@media (max-width:768px){.hero{padding:16px}.form-inline{display:flex;flex-wrap:wrap;align-items:stretch}.form-inline .form-control,.form-inline .btn{width:100%;margin-right:0 !important}.table{font-size:.88rem}.thumb{width:54px;height:54px}}@media (max-width:576px){.content-wrapper{padding:12px !important}.hero{border-radius:14px}.card-body{padding:12px}.thumb{width:48px;height:48px}}</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="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 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="notif.php" class="nav-link"><i class="nav-icon fas fa-bell"></i> Notifikasi <span class="badge bg-warning text-dark float-end"><?= $notifCount ?></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><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"><div><h4 class="mb-1">Data Absensi Semua User / All Attendance</h4><p class="mb-0">Edit, hapus, filter, dan cetak data absensi / manage, filter, and print attendance records.</p></div><div><a href="absen.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Absen Saya / My Attendance</a> <a target="_blank" class="btn btn-primary" href="cetak_absensi.php?mulai=<?= urlencode($mulai) ?>&selesai=<?= urlencode($selesai) ?>&role=<?= urlencode($roleFilter) ?>"><i class="fas fa-file-pdf"></i> Cetak PDF / Print</a></div></div></div>
<?php if(isset($_GET['msg'])): ?><div class="alert alert-success">Data absensi berhasil diproses.</div><?php endif; ?>
<div class="card"><div class="card-body"><form method="get" class="row g-2 align-items-end mb-3"><label class="me-2">Mulai / Start</label><input type="date" name="mulai" class="form-control mr-2" value="<?= htmlspecialchars($mulai) ?>"><label class="me-2">Selesai / End</label><input type="date" name="selesai" class="form-control mr-2" value="<?= htmlspecialchars($selesai) ?>"><select name="role" class="form-control mr-2"><option value="">Semua Role / All</option><?php foreach(['admin','pelaksana','teknisi'] as $r): ?><option value="<?= $r ?>" <?= $roleFilter===$r?'selected':'' ?>><?= ucfirst($r) ?></option><?php endforeach; ?></select><button class="btn btn-info"><i class="fas fa-filter"></i> Filter</button></form>
<div class="table-responsive"><table class="table table-bordered table-striped"><thead><tr><th>User</th><th>Role</th><th>Tanggal</th><th>Masuk</th><th>Pulang</th><th>Lembur</th><th>Status</th><th>Lokasi</th><th>Foto Masuk</th><th>Foto Pulang</th><th>Foto Lembur</th><th>Aksi</th></tr></thead><tbody><?php while($d=mysqli_fetch_assoc($q)): $jm=$d['jam_masuk']?date('Y-m-d\TH:i',strtotime($d['jam_masuk'])):''; $jp=$d['jam_pulang']?date('Y-m-d\TH:i',strtotime($d['jam_pulang'])):''; ?><tr><form method="post"><td><?= htmlspecialchars($d['username']) ?></td><td><?= htmlspecialchars($d['role'] ?? '-') ?></td><td><input type="date" name="tanggal" class="form-control" value="<?= htmlspecialchars($d['tanggal']) ?>"></td><td><input type="datetime-local" name="jam_masuk" class="form-control" value="<?= htmlspecialchars($jm) ?>"></td><td><input type="datetime-local" name="jam_pulang" class="form-control" value="<?= htmlspecialchars($jp) ?>"></td><td><small><?= htmlspecialchars(komplain_absensi_overtime_label($d)) ?></small><div class="text-muted small"><?= htmlspecialchars((string)($d['keterangan_lembur'] ?? '-')) ?></div></td><td><input type="text" name="status" class="form-control" value="<?= htmlspecialchars((string)$d['status']) ?>"></td><td><?= htmlspecialchars(komplain_absensi_location_label($d)) ?></td><td><?php if($d['foto']): ?><img class="thumb" src="../uploads/absen/<?= rawurlencode($d['foto']) ?>"><?php else: ?>-<?php endif; ?></td><td><?php if(!empty($d['foto_pulang'])): ?><img class="thumb" src="../uploads/absen/<?= rawurlencode($d['foto_pulang']) ?>"><?php else: ?>-<?php endif; ?></td><td><?php if(!empty($d['foto_lembur'])): ?><img class="thumb" src="../uploads/absen/<?= rawurlencode($d['foto_lembur']) ?>"><?php else: ?>-<?php endif; ?></td><td><input type="hidden" name="absen_id" value="<?= (int)$d['id'] ?>"><button name="edit_absen" class="btn btn-outline-warning btn-sm"><i class="fas fa-save"></i></button> <button name="hapus_absen" class="btn btn-outline-danger btn-sm" onclick="return confirm('Hapus data absensi ini?')"><i class="fas fa-trash"></i></button></td></form></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>











