@extends('layouts.app')
@php
    use Illuminate\Support\Facades\Storage;
@endphp

@section('content')
<div class="dashboard-card">
    <style>
        .dashboard-card {
            position: relative;
            display: flex;
            flex-direction: column;
            gap: 28px;
            padding: 36px 36px 32px;
            border-radius: 32px;
            background: linear-gradient(150deg, rgba(12, 22, 46, 0.95), rgba(6, 14, 32, 0.88));
            border: 1px solid rgba(148, 163, 184, 0.2);
            box-shadow: 0 30px 60px rgba(3, 7, 18, 0.6);
            overflow: hidden;
        }
        .dashboard-card::before,
        .dashboard-card::after {
            content: "";
            position: absolute;
            inset: 0;
            pointer-events: none;
        }
        .dashboard-card::before {
            background:
                radial-gradient(circle at 12% 18%, rgba(59, 130, 246, 0.28), transparent 55%),
                radial-gradient(circle at 86% -6%, rgba(14, 165, 233, 0.32), transparent 60%);
            opacity: 0.9;
        }
        .dashboard-card::after {
            background: linear-gradient(140deg, transparent 40%, rgba(14, 165, 233, 0.16));
            opacity: 0.75;
        }
        .dashboard-card > * { position: relative; z-index: 1; }

        .home-hero {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            gap: 24px;
            flex-wrap: wrap;
        }
        .home-hero__title {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }
        .home-hero__tagline {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 6px 14px;
            border-radius: 999px;
            background: rgba(129, 140, 248, 0.2);
            border: 1px solid rgba(129, 140, 248, 0.36);
            color: #e0e7ff;
            font-size: 12px;
            letter-spacing: 0.1em;
            text-transform: uppercase;
            font-weight: 600;
        }
        .home-hero h2 {
            margin: 0;
            font-size: 30px;
            font-weight: 700;
            letter-spacing: -0.02em;
            color: #f8fafc;
        }
        .home-hero p {
            margin: 0;
            color: rgba(226, 232, 240, 0.75);
            font-size: 15px;
            max-width: 540px;
            line-height: 1.6;
        }
        .home-hero__cta {
            display: inline-flex;
            gap: 12px;
        }

        .home-alert {
            display: flex;
            flex-direction: column;
            gap: 8px;
            padding: 14px 16px;
            border-radius: 16px;
            font-size: 13px;
            border: 1px solid transparent;
            background: rgba(15, 23, 42, 0.72);
        }
        .home-alert--success {
            border-color: rgba(34, 197, 94, 0.38);
            background: rgba(34, 197, 94, 0.14);
            color: #bbf7d0;
        }
        .home-alert--error {
            border-color: rgba(248, 113, 113, 0.4);
            background: rgba(248, 113, 113, 0.16);
            color: #fecaca;
        }
        .home-alert__title {
            margin: 0;
            font-weight: 700;
            letter-spacing: 0.06em;
            text-transform: uppercase;
        }

        .home-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
            gap: 24px;
            align-items: stretch;
            grid-auto-flow: dense;
        }
        .home-grid--actions {
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        }
        .home-grid__span-2 {
            grid-column: span 2;
        }
        .home-grid__span-3 {
            grid-column: span 3;
        }
        @media (max-width: 1200px) {
            .home-grid__span-2,
            .home-grid__span-3 {
                grid-column: span 1;
            }
        }
        .home-panel {
            display: flex;
            flex-direction: column;
            gap: 14px;
            padding: 20px 24px;
            border-radius: 22px;
            background: rgba(8, 18, 40, 0.86);
            border: 1px solid rgba(148, 163, 184, 0.22);
            box-shadow: 0 20px 36px rgba(5, 12, 28, 0.4);
        }
        .home-panel--accent {
            background: linear-gradient(160deg, rgba(15, 23, 42, 0.88), rgba(37, 99, 235, 0.42));
            border: 1px solid rgba(37, 99, 235, 0.38);
        }
        .home-panel--form {
            display: grid;
            grid-template-rows: auto 1fr auto;
            gap: 18px;
            min-height: 100%;
        }
        .home-panel--compact {
            gap: 18px;
        }
        .home-panel__header {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }
        .home-panel__title {
            margin: 0;
            font-size: 13px;
            letter-spacing: 0.08em;
            text-transform: uppercase;
            color: rgba(226, 232, 240, 0.72);
        }
        .home-panel__subtitle {
            margin: 0;
            font-size: 12px;
            color: rgba(148, 163, 184, 0.75);
        }

        .clock-card {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 16px;
            padding: 22px;
            border-radius: 24px;
            background: linear-gradient(165deg, rgba(15, 23, 42, 0.94), rgba(22, 78, 198, 0.55));
            border: 1px solid rgba(59, 130, 246, 0.38);
            box-shadow: 0 28px 52px rgba(8, 15, 32, 0.45);
            grid-row: span 2;
        }
        .clock-face {
            position: relative;
            width: 180px;
            height: 180px;
            border-radius: 50%;
            background: radial-gradient(circle at center, rgba(15, 23, 42, 0.95), rgba(2, 6, 23, 0.82));
            border: 6px solid rgba(59, 130, 246, 0.42);
            box-shadow: inset 0 0 18px rgba(56, 189, 248, 0.28);
        }
        .clock-face::after {
            content: "";
            position: absolute;
            width: 14px;
            height: 14px;
            border-radius: 50%;
            background: rgba(56, 189, 248, 0.95);
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            box-shadow: 0 0 14px rgba(56, 189, 248, 0.6);
        }
        .clock-hand {
            position: absolute;
            bottom: 50%;
            left: 50%;
            transform-origin: bottom center;
            transform: translate(-50%, 0) rotate(0deg);
            border-radius: 999px;
            box-shadow: 0 0 12px rgba(96, 165, 250, 0.5);
        }
        .clock-hand.hour { width: 8px; height: 52px; background: rgba(191, 219, 254, 0.88); }
        .clock-hand.minute { width: 6px; height: 74px; background: rgba(96, 165, 250, 0.86); }
        .clock-hand.second { width: 3px; height: 84px; background: rgba(248, 113, 113, 0.95); box-shadow: 0 0 16px rgba(248, 113, 113, 0.6); }
        .clock-ticks { position: absolute; inset: 16px; }
        .clock-tick {
            position: absolute;
            top: 0;
            left: 50%;
            width: 3px;
            height: 16px;
            background: rgba(148, 197, 252, 0.6);
            transform-origin: center 74px;
            border-radius: 4px;
        }
        .clock-tick.major { height: 20px; background: rgba(191, 219, 254, 0.9); }
        .clock-digital {
            font-size: 28px;
            font-weight: 700;
            letter-spacing: 0.1em;
            color: #e0f2fe;
            text-shadow: 0 10px 24px rgba(56, 189, 248, 0.6);
        }

        .camera-box {
            display: flex;
            flex-direction: column;
            gap: 12px;
            padding: 14px;
            border-radius: 16px;
            background: rgba(8, 18, 34, 0.72);
            border: 1px dashed rgba(148, 163, 184, 0.3);
        }
        .camera-preview,
        .camera-thumb {
            width: 100%;
            border-radius: 12px;
            background: rgba(2, 6, 23, 0.85);
            border: 1px solid rgba(148, 163, 184, 0.25);
            aspect-ratio: 3 / 4;
            object-fit: cover;
        }
        .camera-thumb { display: none; }
        .camera-thumb.is-visible { display: block; }
        .camera-controls {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }
        .camera-button {
            flex: 1 1 140px;
            padding: 10px 14px;
            border-radius: 12px;
            border: 1px solid rgba(59, 130, 246, 0.42);
            background: rgba(37, 99, 235, 0.18);
            color: #cbd5f5;
            font-weight: 600;
            letter-spacing: 0.02em;
            cursor: pointer;
            transition: transform 0.15s ease, box-shadow 0.15s ease;
        }
        .camera-button:hover { transform: translateY(-1px); box-shadow: 0 12px 24px rgba(37, 99, 235, 0.28); }
        .camera-button:disabled { opacity: 0.5; cursor: not-allowed; }
        .camera-button.secondary { border-color: rgba(148, 163, 184, 0.4); background: rgba(148, 163, 184, 0.16); color: #e2e8f0; }
        .camera-helper { margin: 0; font-size: 12px; color: rgba(148, 163, 184, 0.72); }
        .camera-hidden-input { position: absolute; width: 1px; height: 1px; clip: rect(0,0,0,0); }
        .is-hidden { display: none !important; }

        .home-field {
            display: flex;
            flex-direction: column;
            gap: 8px;
        }
        .home-field__label {
            font-size: 13px;
            font-weight: 600;
            letter-spacing: 0.02em;
            color: #dbeafe;
        }
        .home-input,
        .home-textarea {
            background: rgba(2, 6, 23, 0.9);
            border: 1px solid rgba(148, 163, 184, 0.32);
            border-radius: 12px;
            padding: 10px 12px;
            color: #e2e8f0;
            font-size: 13px;
            transition: border-color 0.18s ease, box-shadow 0.18s ease;
        }
        .home-input::placeholder,
        .home-textarea::placeholder { color: rgba(148, 163, 184, 0.7); }
        .home-input:focus,
        .home-textarea:focus {
            outline: none;
            border-color: #38bdf8;
            box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.22);
            background: rgba(2, 6, 23, 0.95);
        }
        .home-textarea { resize: vertical; min-height: 70px; line-height: 1.5; }

        .action-button {
            padding: 12px 16px;
            border-radius: 12px;
            border: none;
            font-weight: 700;
            letter-spacing: 0.05em;
            text-transform: uppercase;
            cursor: pointer;
            transition: transform 0.18s ease, box-shadow 0.18s ease;
            width: 100%;
        }
        .action-button:hover { transform: translateY(-1px); }
        .action-button.green { background: linear-gradient(135deg, #22c55e, #16a34a); color: #f0fdf4; box-shadow: 0 18px 32px rgba(34, 197, 94, 0.32); }
        .action-button.amber { background: linear-gradient(135deg, #f59e0b, #d97706); color: #fff7ed; box-shadow: 0 18px 32px rgba(245, 158, 11, 0.34); }
        .action-button.red { background: linear-gradient(135deg, #ef4444, #dc2626); color: #fee2e2; box-shadow: 0 18px 32px rgba(239, 68, 68, 0.34); }
        .action-button.blue { background: linear-gradient(135deg, #3b82f6, #2563eb); color: #dbeafe; box-shadow: 0 18px 32px rgba(59, 130, 246, 0.34); }

        .home-hint {
            margin: 0;
            font-size: 12px;
            line-height: 1.45;
        }
        .home-hint--muted { color: rgba(148, 163, 184, 0.78); }
        .home-hint--warning { color: #facc15; }
        .home-hint--error { color: #f87171; }

        .notice-card {
            display: grid;
            grid-template-rows: auto auto 1fr;
            gap: 12px;
            padding: 20px;
            border-radius: 18px;
            background: rgba(8, 18, 34, 0.8);
            border: 1px dashed rgba(148, 163, 184, 0.3);
            box-shadow: inset 0 1px 0 rgba(148, 163, 184, 0.12);
            min-height: 100%;
        }
        .notice-badge {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 6px 12px;
            border-radius: 999px;
            font-size: 11px;
            letter-spacing: 0.08em;
            text-transform: uppercase;
            font-weight: 700;
        }
        .notice-badge.success { background: rgba(34, 197, 94, 0.18); color: #86efac; border: 1px solid rgba(34, 197, 94, 0.32); }
        .notice-badge.danger { background: rgba(248, 113, 113, 0.18); color: #fecaca; border: 1px solid rgba(248, 113, 113, 0.36); }
        .notice-title { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: rgba(226, 232, 240, 0.72); }
        .notice-text { margin: 0; font-size: 12px; line-height: 1.55; color: rgba(203, 213, 225, 0.78); }

        .home-toolbar {
            display: flex;
            justify-content: flex-end;
        }
        .home-export {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 10px 16px;
            border-radius: 12px;
            background: linear-gradient(135deg, rgba(14, 165, 233, 0.28), rgba(37, 99, 235, 0.38));
            border: 1px solid rgba(14, 165, 233, 0.4);
            color: #e0f2fe;
            font-weight: 600;
            text-decoration: none;
            letter-spacing: 0.04em;
            text-transform: uppercase;
            box-shadow: 0 16px 28px rgba(14, 165, 233, 0.28);
        }
        .home-export:hover {
            transform: translateY(-1px);
            box-shadow: 0 20px 34px rgba(14, 165, 233, 0.32);
        }

        .table-shell {
            border-radius: 26px;
            border: 1px solid rgba(148, 163, 184, 0.24);
            background: rgba(7, 15, 32, 0.84);
            box-shadow: 0 24px 44px rgba(4, 10, 26, 0.55);
            overflow: hidden;
        }
        .table-responsive { width: 100%; overflow-x: auto; }
        .table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0 10px;
            table-layout: fixed;
        }
        .table thead th {
            background: rgba(10, 20, 44, 0.92);
            color: #e2e8f0;
            border-bottom: 1px solid rgba(148, 163, 184, 0.2);
            font-weight: 600;
            padding: 14px 18px;
            font-size: 13px;
            letter-spacing: 0.04em;
            text-transform: uppercase;
        }
        .table tbody tr {
            background: rgba(9, 17, 36, 0.86);
            border: 1px solid rgba(148, 163, 184, 0.16);
            border-radius: 18px;
            box-shadow: 0 18px 34px rgba(7, 16, 32, 0.32);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        .table tbody tr:hover { transform: translateY(-2px); box-shadow: 0 22px 38px rgba(14, 65, 120, 0.32); border-color: rgba(96, 165, 250, 0.34); }
        .table tbody td {
            padding: 14px 18px;
            color: #f1f5f9;
            vertical-align: top;
        }
        .table .pill {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 6px 12px;
            border-radius: 999px;
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 0.05em;
            text-transform: uppercase;
            border: 1px solid transparent;
        }
        .pill-success { background: rgba(34, 197, 94, 0.18); color: #86efac; border-color: rgba(34, 197, 94, 0.32); }
        .pill-danger { background: rgba(248, 113, 113, 0.18); color: #fecaca; border-color: rgba(248, 113, 113, 0.36); }
        .pill-info { background: rgba(59, 130, 246, 0.18); color: #bfdbfe; border-color: rgba(59, 130, 246, 0.34); }
        .pill-warning { background: rgba(250, 204, 21, 0.18); color: #fef08a; border-color: rgba(250, 204, 21, 0.32); }

        .note-chip {
            display: inline-flex;
            align-items: center;
            padding: 6px 12px;
            border-radius: 12px;
            background: rgba(59, 130, 246, 0.16);
            border: 1px solid rgba(59, 130, 246, 0.28);
            color: #dbeafe;
            font-size: 12px;
            max-width: 100%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .note-chip.empty { background: rgba(148, 163, 184, 0.12); border-style: dashed; border-color: rgba(148, 163, 184, 0.32); color: #94a3b8; }
        .note-chip.admin { background: rgba(249, 115, 22, 0.14); border-color: rgba(249, 115, 22, 0.32); color: #fcd34d; }
        .note-chip.admin.empty { background: rgba(148, 163, 184, 0.12); border-style: dashed; }

        .jobdesk-chip {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 6px 12px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 0.05em;
            background: rgba(34, 197, 94, 0.16);
            border: 1px solid rgba(34, 197, 94, 0.32);
            color: #bbf7d0;
        }
        .jobdesk-chip.is-empty {
            background: rgba(148, 163, 184, 0.12);
            border-style: dashed;
            border-color: rgba(148, 163, 184, 0.32);
            color: #94a3b8;
        }

        .selfie-card {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 12px;
            padding: 16px;
            border-radius: 18px;
            background: linear-gradient(160deg, rgba(15, 23, 42, 0.92), rgba(37, 99, 235, 0.48));
            border: 1px solid rgba(59, 130, 246, 0.28);
            box-shadow: 0 18px 34px rgba(8, 15, 32, 0.35);
            width: 160px;
        }
        .selfie-card__frame {
            width: 100%;
            aspect-ratio: 1;
            border-radius: 12px;
            overflow: hidden;
            border: 1px solid rgba(148, 163, 184, 0.3);
            background: rgba(2, 6, 23, 0.8);
        }
        .selfie-card__thumb { width: 100%; height: 100%; object-fit: cover; }
        .selfie-card__button {
            width: 100%;
            padding: 8px 12px;
            border-radius: 999px;
            background: rgba(37, 99, 235, 0.2);
            border: 1px solid rgba(37, 99, 235, 0.4);
            color: #60a5fa;
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 0.05em;
            text-decoration: none;
            text-transform: uppercase;
            transition: transform 0.15s ease, box-shadow 0.15s ease;
        }
        .selfie-card__button:hover { transform: translateY(-1px); box-shadow: 0 12px 22px rgba(37, 99, 235, 0.26); }
        .selfie-card--empty { background: rgba(148, 163, 184, 0.12); border-style: dashed; border-color: rgba(148, 163, 184, 0.32); box-shadow: none; }
        .selfie-card__empty { color: #94a3b8; font-size: 12px; font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; }

        .table-media {
            display: flex;
            justify-content: center;
        }
        .table-empty {
            padding: 18px;
            text-align: center;
            color: rgba(148, 163, 184, 0.78);
            font-weight: 600;
        }
        .table-actions a {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 8px 14px;
            border-radius: 999px;
            background: rgba(14, 165, 233, 0.18);
            border: 1px solid rgba(14, 165, 233, 0.34);
            color: #38bdf8;
            font-weight: 600;
            text-decoration: none;
            transition: transform 0.15s ease, box-shadow 0.15s ease;
        }
        .table-actions a:hover { transform: translateY(-1px); box-shadow: 0 12px 24px rgba(14, 165, 233, 0.28); }

        .home-pagination {
            margin-top: 16px;
            display: flex;
            justify-content: flex-end;
        }

        @media (max-width: 960px) {
            .dashboard-card { padding: 30px 24px; }
            .clock-card { width: 100%; grid-row: span 1; }
        }
        @media (max-width: 768px) {
            .dashboard-card { padding: 26px 20px; }
            .home-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
            .table { border-spacing: 0; }
            .table thead { display: none; }
            .table tbody { display: flex; flex-direction: column; gap: 16px; }
            .table tbody tr { display: flex; flex-direction: column; padding: 12px 0; }
            .table tbody td { display: flex; flex-direction: column; gap: 6px; padding: 10px 18px; }
            .table tbody td::before { content: attr(data-label); font-size: 11px; font-weight: 600; letter-spacing: 0.06em; color: rgba(148, 163, 184, 0.72); }
            .table tbody td:not(:last-child) { border-bottom: 1px solid rgba(148, 163, 184, 0.14); }
            .selfie-card { width: 100%; max-width: 280px; }
            .table-actions a { width: 100%; }
        }
        @media (max-width: 600px) {
            .dashboard-card { padding: 22px 18px; border-radius: 26px; }
            .home-hero__cta, .notif-tabs { width: 100%; justify-content: flex-start; }
        }

        /* Location/Maps Styles */
        .location-status {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 10px 12px;
            border-radius: 8px;
            font-size: 12px;
            font-weight: 500;
            margin-top: 8px;
        }
        .location-status.loading {
            background: rgba(59, 130, 246, 0.16);
            border: 1px solid rgba(59, 130, 246, 0.28);
            color: #bfdbfe;
        }
        .location-status.success {
            background: rgba(34, 197, 94, 0.16);
            border: 1px solid rgba(34, 197, 94, 0.28);
            color: #bbf7d0;
        }
        .location-status.error {
            background: rgba(248, 113, 113, 0.16);
            border: 1px solid rgba(248, 113, 113, 0.28);
            color: #fecaca;
        }

        .location-map-container {
            width: 100%;
            height: 300px;
            border-radius: 12px;
            overflow: hidden;
            border: 1px solid rgba(148, 163, 184, 0.25);
            margin-top: 12px;
        }
        #location-map {
            width: 100%;
            height: 100%;
        }
        .location-coords {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-top: 10px;
            font-size: 12px;
        }
        .location-coord-item {
            padding: 10px;
            border-radius: 8px;
            background: rgba(15, 23, 42, 0.92);
            border: 1px solid rgba(148, 163, 184, 0.2);
        }
        .location-coord-label {
            display: block;
            font-weight: 600;
            color: rgba(148, 163, 184, 0.75);
            font-size: 11px;
            letter-spacing: 0.05em;
            margin-bottom: 4px;
        }
        .location-coord-value {
            display: block;
            color: #e2e8f0;
            word-break: break-all;
            font-family: 'Monaco', 'Courier New', monospace;
        }

        /* Table Location/Selfie Combined View */
        .table-media {
            display: flex;
            gap: 12px;
            align-items: flex-start;
        }
        .table-location-map {
            width: 100%;
            height: 140px;
            border-radius: 8px;
            border: 1px solid rgba(96, 165, 250, 0.3);
            background: rgba(15, 23, 42, 0.92);
            overflow: hidden;
        }
        .table-location-map .leaflet-container {
            height: 100%;
        }
    </style>

    <div class="home-hero">
        <div class="home-hero__title">
            <span class="home-hero__tagline">Ringkasan Hari Ini</span>
            <h2>Home</h2>
            <p>Pantau kehadiran, catat aktivitas khusus, dan lihat validasi terbaru dalam satu tampilan yang nyaman dibaca.</p>
        </div>
    </div>

    @if (session('status'))
        <div class="home-alert home-alert--success">
            <p class="home-alert__title">Sukses</p>
            <p>{{ session('status') }}</p>
        </div>
    @endif
            
    @if ($errors->has('absensi'))
        <div class="home-alert home-alert--error">
            <p class="home-alert__title">Peringatan</p>
            <p>{{ $errors->first('absensi') }}</p>
            @if($errors->has('show_confirm_button'))
                <form method="POST" action="{{ route('absensi.clockOut') }}" style="margin-top: 10px;">
                    @csrf
                    <input type="hidden" name="confirm_early_checkout" value="1">
                    <button type="submit" class="home-btn home-btn--primary" style="padding: 8px 16px; font-size: 14px;">
                        Ya, Saya Yakin
                    </button>
                    <a href="{{ route('user.absensi') }}" class="home-btn" style="padding: 8px 16px; font-size: 14px; margin-left: 8px;">
                        Batal
                    </a>
                </form>
            @endif
        </div>
    @endif

    @if ($errors->any())
        <div class="home-alert home-alert--error">
            <p class="home-alert__title">Periksa kembali</p>
            <ul style="margin:0; padding-left: 18px;">
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    @php
        $clockInDisabled = isset($canClockIn) ? ! $canClockIn : false;
        $clockOutDisabled = isset($canClockOut) ? ! $canClockOut : false;
        $specialDisabled = isset($canMarkSpecial) ? ! $canMarkSpecial : false;
        $todayClock = $todayAttendance?->clock_in?->timezone(config('app.timezone'));
        $todayClockOut = $todayAttendance?->clock_out?->timezone(config('app.timezone'));
        $todayStatus = $todayAttendance?->status;
        $openClockLabel = $openClockInTime?->format('H:i');
    @endphp

    <div class="home-grid home-grid--actions">
        <div class="clock-card">
            <div class="clock-face">
                <div class="clock-ticks" id="ab-clock-ticks"></div>
                <div class="clock-hand hour" id="ab-clock-hour"></div>
                <div class="clock-hand minute" id="ab-clock-minute"></div>
                <div class="clock-hand second" id="ab-clock-second"></div>
            </div>
            <div class="clock-digital" id="ab-clock-digital">00:00:00</div>
        </div>
        <form method="POST" action="{{ route('absensi.clockIn') }}" enctype="multipart/form-data" class="home-panel home-panel--form" id="clock-in-form" {{ $clockInDisabled ? 'data-disabled=true' : '' }}>
            @csrf
            <div class="home-panel__header">
                <p class="home-panel__title">Selfie Masuk</p>
                <p class="home-panel__subtitle">Ambil foto langsung atau unggah manual sebelum absen.</p>
            </div>
            <div class="home-field">
                <label class="home-field__label">Selfie Masuk</label>
                <div class="camera-box">
                    <video id="clock-in-camera" class="camera-preview is-hidden" autoplay playsinline muted></video>
                    <img id="clock-in-preview" class="camera-thumb" alt="Preview selfie">
                    <div class="camera-controls">
                        <button type="button" class="camera-button" id="clock-in-start">Buka Kamera</button>
                        <button type="button" class="camera-button" id="clock-in-capture" disabled data-state="capture">Ambil Foto</button>
                        <button type="button" class="camera-button secondary" id="clock-in-manual" style="display: none;">Unggah Manual</button>
                    </div>
                    <p class="camera-helper">Tekan "Buka Kamera" lalu "Ambil Foto" untuk mengambil selfie.</p>
                </div>
                <input id="clock-in-file" type="file" name="selfie_photo" accept="image/*" capture="user" required class="camera-hidden-input">
                <input type="hidden" id="clock-in-latitude" name="latitude">
                <input type="hidden" id="clock-in-longitude" name="longitude">
                <input type="hidden" id="clock-in-accuracy" name="accuracy">
                <input type="hidden" id="clock-in-location-name" name="location_name">
                <div id="location-status" class="location-status"></div>
            </div>
            <button type="submit" class="action-button green" {{ $clockInDisabled ? 'disabled' : '' }}>Absen Masuk</button>
            @if($clockInDisabled)
                <p class="home-hint home-hint--error">
                    Anda sudah memiliki absensi untuk hari ini
                    @if($todayClock)
                        (masuk {{ $todayClock->format('H:i') }}{{ $todayClockOut ? ', keluar ' . $todayClockOut->format('H:i') : '' }}).
                    @elseif(isset($openClockLabel))
                        sejak {{ $openClockLabel }}.
                    @endif
                </p>
            @endif
        </form>
        <form method="POST" action="{{ route('absensi.clockOut') }}" class="home-panel home-panel--form home-panel--compact">
            @csrf
            <div class="home-panel__header">
                <p class="home-panel__title">Absen Keluar</p>
                <p class="home-panel__subtitle">Klik tombol ini ketika selesai bekerja.</p>
            </div>
            <button type="submit" class="action-button amber" {{ $clockOutDisabled ? 'disabled' : '' }}>Absen Keluar</button>
            @if($clockOutDisabled)
                <p class="home-hint home-hint--muted">Belum ada absensi masuk yang perlu ditutup.</p>
            @elseif($openClockInTime)
                <p class="home-hint home-hint--warning">Absensi masuk dibuka {{ $openClockInTime->format('H:i') }}. Jangan lupa tutup sebelum membuat absensi baru.</p>
            @endif
        </form>
        <form method="POST" action="{{ route('absensi.markSick') }}" class="home-panel home-panel--form">
            @csrf
            <div class="home-panel__header">
                <p class="home-panel__title">Catatan Sakit</p>
                <p class="home-panel__subtitle">Gunakan ketika tidak masuk karena alasan kesehatan.</p>
            </div>
            <div class="home-field">
                <label for="note-sick" class="home-field__label">Catatan sakit</label>
                <textarea id="note-sick" name="note" rows="3" class="home-textarea" placeholder="Contoh: istirahat karena demam"></textarea>
            </div>
            <button type="submit" class="action-button red" {{ $specialDisabled ? 'disabled' : '' }}>Catat Sakit</button>
            @if($specialDisabled)
                <p class="home-hint home-hint--error">Absensi khusus hanya bisa dibuat jika belum ada absensi di hari ini.</p>
            @endif
        </form>
        <form method="POST" action="{{ route('absensi.markIzin') }}" class="home-panel home-panel--form">
            @csrf
            <div class="home-panel__header">
                <p class="home-panel__title">Catatan Izin</p>
                <p class="home-panel__subtitle">Informasikan alasan izin tanpa hadir di kantor.</p>
            </div>
            <div class="home-field">
                <label for="note-izin" class="home-field__label">Catatan izin</label>
                <textarea id="note-izin" name="note" rows="3" class="home-textarea" placeholder="Contoh: menghadiri acara keluarga"></textarea>
            </div>
            <button type="submit" class="action-button blue" {{ $specialDisabled ? 'disabled' : '' }}>Catat Izin</button>
            @if($specialDisabled)
                <p class="home-hint home-hint--error">Absensi izin hanya tersedia ketika belum ada absensi hari ini.</p>
            @endif
        </form>
        <div class="notice-card">
            <span class="notice-badge success">Validasi Benar</span>
            <h3 class="notice-title">Absensi Diterima</h3>
            <p class="notice-text">Total data yang diterima: <strong>{{ number_format($verifiedCount ?? 0) }}</strong></p>
            <p class="notice-text">Pastikan Anda menekan tombol <strong>Absen Masuk</strong> dan <strong>Absen Keluar</strong> dalam satu hari kerja agar status tercatat sebagai <em>Benar</em>.</p>
        </div>
        <div class="notice-card">
            <span class="notice-badge danger">Validasi Ditolak</span>
            <h3 class="notice-title">Absensi Tidak Diterima</h3>
            <p class="notice-text">Total data yang ditolak: <strong>{{ number_format($rejectedCount ?? 0) }}</strong></p>
            <p class="notice-text">Jika salah satu dari absen masuk atau keluar belum dilakukan, data akan ditandai <em>Tidak Diterima</em>. Lengkapi keduanya untuk menghindari penolakan.</p>
        </div>
    </div>

    <div class="home-toolbar">
        <a href="{{ route('absensi.exportCsv') }}" class="home-export">Export CSV</a>
    </div>

    @php
        $selectedDate = now()->toDateString();
        if (isset($items) && count($items) > 0) {
            $sampleItem = $items[0] ?? null;
            if ($sampleItem && $sampleItem->clock_in) {
                $selectedDate = $sampleItem->clock_in->toDateString();
            }
        }

        $effectiveStatus = function ($row) use ($selectedDate) {
            $status = strtolower($row->status ?? '');

            if (in_array($status, ['sakit', 'izin'], true)) {
                return $status;
            }

            if ($selectedDate === '2026-04-18') {
                return 'hadir';
            }

            return $status;
        };
    @endphp

    <div class="table-shell">
    <div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th>#</th>
                <th>Tanggal</th>
                <th>Masuk</th>
                <th>Keluar</th>
                <th>Durasi</th>
                <th>Status</th>
                <th>Validasi</th>
                <th>Jobdesk</th>
                <th>Catatan Pegawai</th>
                <th>Catatan Admin</th>
                <th>Selfie</th>
                <th>Aksi</th>
            </tr>
        </thead>
        <tbody>
            @forelse ($items as $i => $row)
                <tr>
                    <td data-label="#">{{ $items->firstItem() + $i }}</td>
                    <td data-label="Tanggal">{{ $row->clock_in ? $row->clock_in->timezone(config('app.timezone'))->format('d M Y') : '-' }}</td>
                    <td data-label="Masuk">{{ $row->clock_in ? $row->clock_in->timezone(config('app.timezone'))->format('H:i') : '-' }}</td>
                    <td data-label="Keluar">{{ $row->clock_out ? $row->clock_out->timezone(config('app.timezone'))->format('H:i') : '-' }}</td>
                    @php
                        $in = $row->clock_in ? $row->clock_in->timezone(config('app.timezone')) : null;
                        $out = $row->clock_out ? $row->clock_out->timezone(config('app.timezone')) : null;
                        $minutes = ($in && $out) ? $out->diffInMinutes($in) : 0;
                        $dur = $minutes ? sprintf('%02d jam %02d mnt', intdiv($minutes,60), $minutes%60) : '-';
                    @endphp
                    <td data-label="Durasi">{{ $dur }}</td>
                    @php
                        $status = $effectiveStatus($row);
                    @endphp
                    <td data-label="Status">{{ strtoupper($status) }}</td>
                    @php
                        $isVerified = $row->is_verified;
                        $statusLower = $status;
                        $verifyClass = $isVerified ? 'pill-success' : 'pill-danger';
                        if ($statusLower === 'sakit') {
                            $verifyClass = 'pill-warning';
                        } elseif ($statusLower === 'izin') {
                            $verifyClass = 'pill-info';
                        }
                    @endphp
                    <td data-label="Validasi">
                        @php
                            // Asumsi $item->clock_in dan $item->clock_out adalah Carbon instance
                            $durasiJam = ($row->clock_in && $row->clock_out) ? $row->clock_in->diffInHours($row->clock_out) : 0;
                        @endphp
                        @if($durasiJam < 12)
                            <span style="background:#ef4444;color:#fff;padding:4px 14px;border-radius:8px;font-weight:600;">DITOLAK</span>
                        @else
                            <span style="background:#22c55e;color:#fff;padding:4px 14px;border-radius:8px;font-weight:600;">BENAR</span>
                        @endif
                    </td>
                    <td data-label="Jobdesk">
                        <span class="jobdesk-chip {{ $row->jobdesk ? '' : 'is-empty' }}">
                            {{ $row->jobdesk ? strtoupper($row->jobdesk) : 'Belum ditetapkan' }}
                        </span>
                    </td>
                    @php $userNote = $row->note ? trim($row->note) : null; @endphp
                    <td data-label="Catatan Pegawai">
                        <span class="note-chip {{ $userNote ? '' : 'empty' }}" title="{{ $userNote ?? 'Tidak ada catatan pegawai' }}">{{ $userNote ?? 'Tidak ada catatan pegawai' }}</span>
                    </td>
                    @php $adminNote = $row->admin_note ? trim($row->admin_note) : null; @endphp
                    <td data-label="Catatan Admin">
                        <span class="note-chip admin {{ $adminNote ? '' : 'empty' }}" title="{{ $adminNote ?? 'Tidak ada catatan admin' }}">{{ $adminNote ?? 'Tidak ada catatan admin' }}</span>
                    </td>
                    <td data-label="Selfie">
                        @php
                            $selfieRoute = null;
                            if ($row->selfie_photo) {
                                $basename = basename($row->selfie_photo);
                                $selfieRoute = route('selfie.show', ['filename' => $basename]);
                            }
                            $hasLocation = $row->clock_in_latitude && $row->clock_in_longitude;
                        @endphp
                        <div class="table-media" style="display: flex; gap: 12px; align-items: flex-start;">
                            <div style="flex: 0 0 auto;">
                                @if($selfieRoute)
                                    <div class="selfie-card">
                                        <div class="selfie-card__frame">
                                            <img src="{{ $selfieRoute }}" alt="Selfie" class="selfie-card__thumb"
                                                onerror="this.onerror=null;this.parentNode.innerHTML='<span style=\'color:#f87171\'>Gambar tidak ditemukan</span>';">
                                        </div>
                                        <a href="{{ $selfieRoute }}" class="selfie-card__button" target="_blank" rel="noopener">
                                            Lihat Selfie
                                        </a>
                                    </div>
                                @else
                                    <div class="selfie-card selfie-card--empty">
                                        <span class="selfie-card__empty">Tidak ada selfie</span>
                                    </div>
                                @endif
                            </div>

                            @if($hasLocation)
                            <div style="flex: 1; min-width: 180px; display: flex; flex-direction: column; gap: 8px;">
                                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font-size: 11px;">
                                    <div style="padding: 8px; border-radius: 8px; background: rgba(15, 23, 42, 0.92); border: 1px solid rgba(96, 165, 250, 0.3);">
                                        <span style="display: block; font-weight: 600; color: rgba(96, 165, 250, 0.9); margin-bottom: 3px; text-transform: uppercase;">Lat</span>
                                        <span style="display: block; color: #dbeafe; word-break: break-all;">{{ number_format($row->clock_in_latitude, 6) }}</span>
                                    </div>
                                    <div style="padding: 8px; border-radius: 8px; background: rgba(15, 23, 42, 0.92); border: 1px solid rgba(96, 165, 250, 0.3);">
                                        <span style="display: block; font-weight: 600; color: rgba(96, 165, 250, 0.9); margin-bottom: 3px; text-transform: uppercase;">Lon</span>
                                        <span style="display: block; color: #dbeafe; word-break: break-all;">{{ number_format($row->clock_in_longitude, 6) }}</span>
                                    </div>
                                    <div style="padding: 8px; border-radius: 8px; background: rgba(15, 23, 42, 0.92); border: 1px solid rgba(34, 197, 94, 0.3);">
                                        <span style="display: block; font-weight: 600; color: rgba(34, 197, 94, 0.9); margin-bottom: 3px; text-transform: uppercase;">Akurasi</span>
                                        <span style="display: block; color: #bbf7d0;">{{ $row->clock_in_accuracy ? round($row->clock_in_accuracy) . 'm' : '-' }}</span>
                                    </div>
                                    <div style="padding: 8px; border-radius: 8px; background: rgba(15, 23, 42, 0.92); border: 1px solid rgba(148, 163, 184, 0.3);">
                                        <span style="display: block; font-weight: 600; color: rgba(148, 163, 184, 0.75); margin-bottom: 3px; text-transform: uppercase;">Lokasi</span>
                                        <span style="display: block; color: #cbd5e1; word-break: break-word; font-size: 10px;">{{ $row->clock_in_location_name ?? '-' }}</span>
                                    </div>
                                </div>
                                <div class="table-location-map" id="map-{{ $row->id }}" data-lat="{{ $row->clock_in_latitude }}" data-lon="{{ $row->clock_in_longitude }}" style="width: 100%; height: 140px; border-radius: 8px; border: 1px solid rgba(96, 165, 250, 0.3); background: rgba(15, 23, 42, 0.92); overflow: hidden;"></div>
                            </div>
                            @endif
                        </div>
                        </div>
                    </td>
                    <td data-label="Aksi" class="cell-action" style="text-align:center;">
                        <a href="{{ route('absensi.edit', $row) }}">Edit</a>
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="12" class="empty-state">Belum ada data absensi.</td>
                </tr>
            @endforelse
        </tbody>
    </table>
    </div>

    <div style="margin-top:12px;">
        {{ $items->links() }}
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', () => {
        const clockFace = document.getElementById('ab-clock-ticks');
        const hourHand = document.getElementById('ab-clock-hour');
        const minuteHand = document.getElementById('ab-clock-minute');
        const secondHand = document.getElementById('ab-clock-second');
        const digital = document.getElementById('ab-clock-digital');

        if (clockFace && hourHand && minuteHand && secondHand && digital) {
            const totalTicks = 60;
            const fragment = document.createDocumentFragment();
            for (let i = 0; i < totalTicks; i++) {
                const tick = document.createElement('div');
                tick.classList.add('clock-tick');
                if (i % 5 === 0) {
                    tick.classList.add('major');
                }
                tick.style.transform = `translateX(-50%) rotate(${i * 6}deg)`;
                fragment.appendChild(tick);
            }
            clockFace.appendChild(fragment);

            const updateClock = () => {
                const now = new Date();
                const seconds = now.getSeconds();
                const minutes = now.getMinutes();
                const hours = now.getHours();

                const secondDeg = seconds * 6;
                const minuteDeg = minutes * 6 + seconds * 0.1;
                const hourDeg = (hours % 12) * 30 + minutes * 0.5;

                secondHand.style.transform = `translate(-50%, 0) rotate(${secondDeg}deg)`;
                minuteHand.style.transform = `translate(-50%, 0) rotate(${minuteDeg}deg)`;
                hourHand.style.transform = `translate(-50%, 0) rotate(${hourDeg}deg)`;

                digital.textContent = now.toLocaleTimeString('id-ID', {
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit',
                    hour12: false,
                });
            };

            updateClock();
            setInterval(updateClock, 1000);
        }

        const form = document.getElementById('clock-in-form');
        if (!form) { return; }

        const startBtn = form.querySelector('#clock-in-start');
        const captureBtn = form.querySelector('#clock-in-capture');
        const manualBtn = form.querySelector('#clock-in-manual');
        const video = form.querySelector('#clock-in-camera');
        const preview = form.querySelector('#clock-in-preview');
        const hiddenInput = form.querySelector('#clock-in-file');
        const helper = form.querySelector('.camera-helper');

        let stream = null;

        const clearHiddenInput = () => {
            const cleaner = new DataTransfer();
            hiddenInput.files = cleaner.files;
        };

        const stopCamera = () => {
            if (stream) {
                stream.getTracks().forEach(track => track.stop());
                stream = null;
            }
            video.srcObject = null;
            video.classList.add('is-hidden');
        };

        const startCamera = async () => {
            if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
                helper.textContent = 'Browser tidak mendukung kamera langsung. Silakan gunakan unggah manual.';
                startBtn.disabled = true;
                captureBtn.disabled = true;
                return;
            }

            if (stream) { return; }

            try {
                stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'user' } });
                video.srcObject = stream;
                video.classList.remove('is-hidden');
                preview.classList.remove('is-visible');
                preview.classList.add('is-hidden');
                startBtn.textContent = 'Tutup Kamera';
                captureBtn.disabled = false;
                captureBtn.dataset.state = 'capture';
                captureBtn.textContent = 'Ambil Foto';
            } catch (error) {
                console.error(error);
                helper.textContent = 'Tidak bisa membuka kamera. Pastikan izin kamera diberikan atau gunakan unggah manual.';
                captureBtn.disabled = true;
            }
        };

        startBtn.addEventListener('click', async () => {
            if (stream) {
                stopCamera();
                startBtn.textContent = 'Buka Kamera';
                return;
            }

            await startCamera();
        });

        const handleCapture = async () => {
            if (captureBtn.dataset.state === 'retake') {
                clearHiddenInput();
                preview.classList.remove('is-visible');
                preview.classList.add('is-hidden');
                await startCamera();
                return;
            }

            if (!stream) {
                await startCamera();
                if (!stream) { return; }
            }

            const trackSettings = stream.getVideoTracks()[0]?.getSettings() || {};
            const width = video.videoWidth || trackSettings.width || 720;
            const height = video.videoHeight || trackSettings.height || 960;

            const canvas = document.createElement('canvas');
            canvas.width = width;
            canvas.height = height;

            const ctx = canvas.getContext('2d');
            ctx.drawImage(video, 0, 0, width, height);

            canvas.toBlob(blob => {
                if (!blob) { return; }
                const file = new File([blob], `selfie_${Date.now()}.jpg`, { type: 'image/jpeg' });
                const dataTransfer = new DataTransfer();
                dataTransfer.items.add(file);
                hiddenInput.files = dataTransfer.files;
                hiddenInput.dispatchEvent(new Event('change', { bubbles: true }));

                preview.src = URL.createObjectURL(blob);
                preview.classList.add('is-visible');
                preview.classList.remove('is-hidden');
                video.classList.add('is-hidden');

                captureBtn.dataset.state = 'retake';
                captureBtn.textContent = 'Ambil Ulang';
                startBtn.textContent = 'Buka Kamera';

                stopCamera();
            }, 'image/jpeg', 0.9);
        };

        captureBtn.addEventListener('click', handleCapture);

        manualBtn.addEventListener('click', () => {
            stopCamera();
            startBtn.textContent = 'Buka Kamera';
            hiddenInput.click();
        });

        hiddenInput.addEventListener('change', () => {
            if (hiddenInput.files && hiddenInput.files.length) {
                const fileURL = URL.createObjectURL(hiddenInput.files[0]);
                preview.src = fileURL;
                preview.classList.add('is-visible');
                preview.classList.remove('is-hidden');
                captureBtn.dataset.state = 'retake';
                captureBtn.textContent = 'Ambil Ulang';
            } else {
                preview.removeAttribute('src');
                preview.classList.remove('is-visible');
                preview.classList.add('is-hidden');
                captureBtn.dataset.state = 'capture';
                captureBtn.textContent = 'Ambil Foto';
            }
        });

        form.addEventListener('submit', event => {
            if (!hiddenInput.files || !hiddenInput.files.length) {
                event.preventDefault();
                alert('Ambil foto terlebih dahulu sebelum menekan Absen Masuk.');
            }
        });

        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
            startCamera();
        } else {
            helper.textContent = 'Browser tidak mendukung kamera langsung. Silakan gunakan unggah manual.';
            captureBtn.disabled = true;
            startBtn.disabled = true;
        }

        window.addEventListener('beforeunload', () => {
            stopCamera();
        });
    });

    /* Geolocation dan Maps */
    (function() {
        const statusDiv = document.getElementById('location-status');
        const latInput = document.getElementById('clock-in-latitude');
        const lonInput = document.getElementById('clock-in-longitude');
        const accInput = document.getElementById('clock-in-accuracy');
        const locNameInput = document.getElementById('clock-in-location-name');
        const form = document.getElementById('clock-in-form');

        // Function untuk menampilkan status lokasi
        function showLocationStatus(message, type) {
            statusDiv.className = `location-status ${type}`;
            statusDiv.innerHTML = '';
            const icon = type === 'loading' ? '⏳' : type === 'success' ? '✓' : '✕';
            statusDiv.textContent = icon + ' ' + message;
        }

        // Function untuk requestlokasi
        function requestLocation() {
            if (!navigator.geolocation) {
                showLocationStatus('Browser tidak mendukung Geolocation', 'error');
                return;
            }

            showLocationStatus('Meminta izin lokasi...', 'loading');

            navigator.geolocation.getCurrentPosition(
                function(position) {
                    const lat = position.coords.latitude;
                    const lon = position.coords.longitude;
                    const accuracy = position.coords.accuracy;

                    // Set nilai ke input
                    latInput.value = lat.toFixed(8);
                    lonInput.value = lon.toFixed(8);
                    accInput.value = accuracy.toFixed(2);

                    showLocationStatus(
                        `Lokasi diterima (Akurasi: ${accuracy.toFixed(0)}m)`,
                        'success'
                    );

                    // Tampilkan maps jika tersedia
                    if (window.L) {
                        displayMap(lat, lon);
                    }
                },
                function(error) {
                    let errorMsg = 'Gagal mendapatkan lokasi';
                    switch(error.code) {
                        case error.PERMISSION_DENIED:
                            errorMsg = 'Izin lokasi ditolak. Silakan aktifkan lokasi di pengaturan browser.';
                            break;
                        case error.POSITION_UNAVAILABLE:
                            errorMsg = 'Informasi lokasi tidak tersedia.';
                            break;
                        case error.TIMEOUT:
                            errorMsg = 'Timeout saat mengambil lokasi.';
                            break;
                    }
                    showLocationStatus(errorMsg, 'error');
                },
                {
                    enableHighAccuracy: true,
                    timeout: 10000,
                    maximumAge: 0
                }
            );
        }

        // Function untuk menampilkan maps
        function displayMap(lat, lon) {
            let mapContainer = document.getElementById('location-map');
            if (!mapContainer) {
                const coordsDiv = document.querySelector('.location-coords');
                const container = document.createElement('div');
                container.id = 'location-map-container';
                container.className = 'location-map-container';
                mapContainer = document.createElement('div');
                mapContainer.id = 'location-map';
                container.appendChild(mapContainer);
                coordsDiv ? coordsDiv.insertAdjacentElement('beforebegin', container) : statusDiv.insertAdjacentElement('afterend', container);
            }

            // Jika map sudah initialized, hapus dulu
            if (window.locMap) {
                window.locMap.remove();
            }

            // Buat map baru menggunakan Leaflet
            setTimeout(() => {
                window.locMap = L.map('location-map').setView([lat, lon], 16);
                L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                    attribution: '© OpenStreetMap contributors',
                    maxZoom: 19
                }).addTo(window.locMap);

                L.marker([lat, lon], {
                    icon: L.icon({
                        iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-icon.png',
                        shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-shadow.png',
                        iconSize: [25, 41],
                        shadowSize: [41, 41],
                        iconAnchor: [12, 41]
                    })
                }).addTo(window.locMap).bindPopup('Lokasi Absensi Masuk');
            }, 300);
        }

        // Tangkap lokasi saat form di-submit
        if (form) {
            form.addEventListener('submit', async function(e) {
                // Jika location belum terisi, coba ambil dulu
                if (!latInput.value || !lonInput.value) {
                    e.preventDefault();
                    requestLocation();
                    // Wait for geolocation dan submit ulang
                    setTimeout(() => {
                        form.submit();
                    }, 3000);
                }
            });
        }

        // Tambahkan tombol untuk request lokasi manual
        const cameraBox = document.querySelector('.camera-box');
        if (cameraBox) {
            const locationBtn = document.createElement('button');
            locationBtn.type = 'button';
            locationBtn.className = 'camera-button secondary';
            locationBtn.textContent = '📍 Ambil Lokasi';
            locationBtn.addEventListener('click', () => {
                requestLocation();
            });
            const controls = cameraBox.querySelector('.camera-controls');
            if (controls) {
                controls.appendChild(locationBtn);
            }
        }

        // Tampilkan koordinat jika sudah ada
        function updateCoordDisplay() {
            let coordsContainer = document.querySelector('.location-coords');
            if (!coordsContainer) {
                coordsContainer = document.createElement('div');
                coordsContainer.className = 'location-coords';
                statusDiv.insertAdjacentElement('afterend', coordsContainer);
            }
            coordsContainer.innerHTML = `
                <div class="location-coord-item">
                    <span class="location-coord-label">Latitude</span>
                    <span class="location-coord-value">${latInput.value || '-'}</span>
                </div>
                <div class="location-coord-item">
                    <span class="location-coord-label">Longitude</span>
                    <span class="location-coord-value">${lonInput.value || '-'}</span>
                </div>
                <div class="location-coord-item">
                    <span class="location-coord-label">Akurasi</span>
                    <span class="location-coord-value">${accInput.value ? accInput.value + ' m' : '-'}</span>
                </div>
                <div class="location-coord-item">
                    <span class="location-coord-label">Lokasi</span>
                    <span class="location-coord-value">${locNameInput.value || 'Tidak tersebut'}</span>
                </div>
            `;
        }

        // Monitor perubahan input lokasi
        [latInput, lonInput, accInput, locNameInput].forEach(input => {
            input.addEventListener('change', updateCoordDisplay);
        });

        updateCoordDisplay();
    })();

    /* Initialize maps in table */
    function initializeHistoryMaps() {
        if (typeof L === 'undefined') {
            console.log('Leaflet not loaded yet, retrying...');
            setTimeout(initializeHistoryMaps, 500);
            return;
        }

        const mapElements = document.querySelectorAll('[id^="map-"]');
        console.log('Found ' + mapElements.length + ' map elements to initialize');
        
        mapElements.forEach(mapEl => {
            const lat = parseFloat(mapEl.getAttribute('data-lat'));
            const lon = parseFloat(mapEl.getAttribute('data-lon'));
            
            if (isNaN(lat) || isNaN(lon)) {
                console.warn('Invalid coordinates for map ' + mapEl.id);
                return;
            }

            console.log('Initializing map ' + mapEl.id + ' with coordinates: ' + lat + ', ' + lon);

            try {
                // Clear any existing map instance
                if (mapEl._leaflet_id !== undefined) {
                    mapEl._leaflet = null;
                    mapEl.innerHTML = '';
                }

                const map = L.map(mapEl, { 
                    dragging: false, 
                    scrollWheelZoom: false,
                    touchZoom: false
                }).setView([lat, lon], 15);
                
                L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                    attribution: '© OpenStreetMap contributors',
                    maxZoom: 19
                }).addTo(map);

                const redIcon = L.icon({
                    iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-icon.png',
                    shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/images/marker-shadow.png',
                    iconSize: [25, 41],
                    shadowSize: [41, 41],
                    iconAnchor: [12, 41]
                });

                L.marker([lat, lon], { icon: redIcon })
                    .addTo(map)
                    .bindPopup('Lokasi Absensi: ' + lat.toFixed(4) + ', ' + lon.toFixed(4));

                // Invalidate and resize map
                setTimeout(() => map.invalidateSize(), 100);
            } catch (e) {
                console.error('Error initializing map ' + mapEl.id + ':', e);
            }
        });
    }

    // Wait for Leaflet to load, then initialize maps
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            setTimeout(initializeHistoryMaps, 300);
        });
    } else {
        setTimeout(initializeHistoryMaps, 300);
    }
</script>

@endsection
