<?php
session_start();
if (!isset($_SESSION['username']) || $_SESSION['role'] !== 'admin') {
    header("Location: index.php");
    exit;
}

// Koneksi database
require_once 'config.php';

// Fungsi untuk mendapatkan pengaturan jam absensi
function getJamAbsensi($conn) {
    $jam_absensi = [
        'jam_masuk' => '07:00',
        'jam_pulang' => '15:00',
        'toleransi_masuk' => '15',
        'toleransi_pulang' => '15',
        'status' => 'aktif'
    ];
    
    // Cek apakah tabel pengaturan jam absensi sudah ada
    $checkTable = mysqli_query($conn, "SHOW TABLES LIKE 'pengaturan_jam'");
    if (mysqli_num_rows($checkTable) == 0) {
        // Buat tabel jika belum ada
        $createTable = "CREATE TABLE IF NOT EXISTS `pengaturan_jam` (
            `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
            `jam_masuk` time NOT NULL DEFAULT '07:00:00',
            `jam_pulang` time NOT NULL DEFAULT '15:00:00',
            `toleransi_masuk` int(11) NOT NULL DEFAULT '15' COMMENT 'dalam menit',
            `toleransi_pulang` int(11) NOT NULL DEFAULT '15' COMMENT 'dalam menit',
            `status` enum('aktif','nonaktif') NOT NULL DEFAULT 'aktif',
            `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
        mysqli_query($conn, $createTable);
        
        // Insert data default
        $insertDefault = "INSERT INTO pengaturan_jam (jam_masuk, jam_pulang, toleransi_masuk, toleransi_pulang) 
                         VALUES ('07:00:00', '15:00:00', 15, 15)";
        mysqli_query($conn, $insertDefault);
    } else {
        // Ambil data dari database
        $query = "SELECT * FROM pengaturan_jam WHERE id = 1";
        $result = mysqli_query($conn, $query);
        if ($result && mysqli_num_rows($result) > 0) {
            $row = mysqli_fetch_assoc($result);
            $jam_absensi = [
                'jam_masuk' => $row['jam_masuk'],
                'jam_pulang' => $row['jam_pulang'],
                'toleransi_masuk' => $row['toleransi_masuk'],
                'toleransi_pulang' => $row['toleransi_pulang'],
                'status' => $row['status']
            ];
        }
    }
    
    return $jam_absensi;
}

// Proses update pengaturan
$message = '';
$message_type = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $jam_masuk = mysqli_real_escape_string($conn, $_POST['jam_masuk']);
    $jam_pulang = mysqli_real_escape_string($conn, $_POST['jam_pulang']);
    $toleransi_masuk = (int)$_POST['toleransi_masuk'];
    $toleransi_pulang = (int)$_POST['toleransi_pulang'];
    $status = mysqli_real_escape_string($conn, $_POST['status']);
    
    // Validasi jam
    if ($jam_masuk >= $jam_pulang) {
        $message = "Jam masuk harus lebih awal dari jam pulang!";
        $message_type = "error";
    } else {
        // Update data
        $query = "UPDATE pengaturan_jam SET 
                  jam_masuk = '$jam_masuk',
                  jam_pulang = '$jam_pulang',
                  toleransi_masuk = $toleransi_masuk,
                  toleransi_pulang = $toleransi_pulang,
                  status = '$status'
                  WHERE id = 1";
        
        if (mysqli_query($conn, $query)) {
            $message = "Pengaturan jam absensi berhasil diperbarui!";
            $message_type = "success";
            
            // Update session untuk refresh
            $_SESSION['jam_absensi'] = [
                'jam_masuk' => $jam_masuk,
                'jam_pulang' => $jam_pulang,
                'toleransi_masuk' => $toleransi_masuk,
                'toleransi_pulang' => $toleransi_pulang,
                'status' => $status
            ];
        } else {
            $message = "Gagal memperbarui pengaturan: " . mysqli_error($conn);
            $message_type = "error";
        }
    }
}

// Ambil data pengaturan
$jam_absensi = getJamAbsensi($conn);
?>

<!DOCTYPE html>
<html lang="id" class="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pengaturan Jam Absensi - Sistem Absensi QR</title>
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary: #6366f1;
            --primary-dark: #4f46e5;
            --primary-light: #8b5cf6;
            --secondary: #ec4899;
            --accent: #f59e0b;
            --success: #10b981;
            --warning: #f59e0b;
            --danger: #ef4444;
            --info: #3b82f6;
            --dark: #1f2937;
            --darker: #111827;
            --light: #f8fafc;
            --gray: #6b7280;
            --gray-light: #e5e7eb;
            --card-bg: #ffffff;
            --card-bg-dark: #374151;
            --text-primary: #1f2937;
            --text-primary-dark: #f9fafb;
            --text-secondary: #6b7280;
            --text-secondary-dark: #d1d5db;
            --border-radius: 16px;
            --shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
            --shadow-hover: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
            --shadow-dark: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.2);
            --gradient-primary: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
            --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        .dark {
            --card-bg: var(--card-bg-dark);
            --text-primary: var(--text-primary-dark);
            --text-secondary: var(--text-secondary-dark);
            --shadow: var(--shadow-dark);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Inter', sans-serif;
            background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
            min-height: 100vh;
            color: var(--text-primary);
            line-height: 1.6;
            transition: var(--transition);
        }

        .dark body {
            background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
        }

        /* Header */
        .header {
            background: var(--gradient-primary);
            color: white;
            padding: 1.5rem 2rem;
            box-shadow: var(--shadow);
            position: relative;
            overflow: hidden;
        }

        .header::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -10%;
            width: 300px;
            height: 300px;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
            border-radius: 50%;
        }

        .header-content {
            position: relative;
            z-index: 2;
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logo-section h1 {
            font-size: 1.75rem;
            font-weight: 700;
            margin-bottom: 0.25rem;
        }

        .logo-section .subtitle {
            font-size: 0.9rem;
            opacity: 0.9;
            font-weight: 400;
        }

        /* Breadcrumb */
        .breadcrumb {
            max-width: 1400px;
            margin: 1rem auto;
            padding: 0 2rem;
        }

        .breadcrumb-content {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            font-size: 0.9rem;
        }

        .breadcrumb-content a {
            color: var(--primary);
            text-decoration: none;
            transition: var(--transition);
        }

        .breadcrumb-content a:hover {
            color: var(--primary-dark);
            text-decoration: underline;
        }

        .breadcrumb-separator {
            color: var(--gray);
        }

        /* Main Content */
        .main-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 2rem;
        }

        /* Card Styles */
        .card {
            background: var(--card-bg);
            border-radius: var(--border-radius);
            box-shadow: var(--shadow);
            padding: 2rem;
            margin-bottom: 2rem;
            border: 1px solid rgba(0,0,0,0.05);
            transition: var(--transition);
        }

        .dark .card {
            border: 1px solid rgba(255,255,255,0.1);
        }

        .card-header {
            display: flex;
            align-items: center;
            gap: 1rem;
            margin-bottom: 2rem;
            padding-bottom: 1rem;
            border-bottom: 2px solid var(--primary);
        }

        .card-icon {
            width: 60px;
            height: 60px;
            border-radius: 12px;
            background: var(--gradient-primary);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.75rem;
            color: white;
        }

        .card-title {
            font-size: 1.5rem;
            font-weight: 600;
            color: var(--text-primary);
        }

        .card-subtitle {
            font-size: 1rem;
            color: var(--text-secondary);
            margin-top: 0.25rem;
        }

        /* Message Styles */
        .message {
            padding: 1rem;
            border-radius: var(--border-radius);
            margin-bottom: 2rem;
            display: flex;
            align-items: center;
            gap: 1rem;
            animation: slideIn 0.3s ease-out;
        }

        .message.success {
            background: rgba(16, 185, 129, 0.1);
            border: 1px solid var(--success);
            color: var(--success);
        }

        .message.error {
            background: rgba(239, 68, 68, 0.1);
            border: 1px solid var(--danger);
            color: var(--danger);
        }

        .message.warning {
            background: rgba(245, 158, 11, 0.1);
            border: 1px solid var(--warning);
            color: var(--warning);
        }

        .message-icon {
            font-size: 1.25rem;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Form Styles */
        .form-group {
            margin-bottom: 1.5rem;
        }

        .form-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 2rem;
            margin-bottom: 1rem;
        }

        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 500;
            color: var(--text-primary);
            font-size: 0.95rem;
        }

        .form-input {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 1px solid var(--gray-light);
            border-radius: 10px;
            font-size: 1rem;
            color: var(--text-primary);
            background: var(--card-bg);
            transition: var(--transition);
        }

        .dark .form-input {
            border-color: rgba(255,255,255,0.1);
            background: rgba(255,255,255,0.05);
        }

        .form-input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
        }

        .form-input[type="time"] {
            font-family: 'Inter', sans-serif;
        }

        .form-input[type="number"] {
            max-width: 150px;
        }

        .form-select {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 1px solid var(--gray-light);
            border-radius: 10px;
            font-size: 1rem;
            color: var(--text-primary);
            background: var(--card-bg);
            transition: var(--transition);
            cursor: pointer;
        }

        .dark .form-select {
            border-color: rgba(255,255,255,0.1);
            background: rgba(255,255,255,0.05);
        }

        .form-select:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
        }

        .form-help {
            font-size: 0.85rem;
            color: var(--text-secondary);
            margin-top: 0.25rem;
            font-style: italic;
        }

        /* Time Display */
        .time-display {
            background: rgba(99, 102, 241, 0.05);
            border-radius: var(--border-radius);
            padding: 1.5rem;
            margin-bottom: 2rem;
            border: 1px solid rgba(99, 102, 241, 0.1);
        }

        .dark .time-display {
            background: rgba(99, 102, 241, 0.1);
        }

        .time-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 1.5rem;
        }

        .time-item {
            text-align: center;
            padding: 1rem;
            background: var(--card-bg);
            border-radius: 10px;
            box-shadow: var(--shadow);
        }

        .time-label {
            font-size: 0.9rem;
            color: var(--text-secondary);
            margin-bottom: 0.5rem;
        }

        .time-value {
            font-size: 1.5rem;
            font-weight: 600;
            color: var(--text-primary);
        }

        .time-status {
            font-size: 0.8rem;
            padding: 0.25rem 0.5rem;
            border-radius: 20px;
            display: inline-block;
            margin-top: 0.5rem;
        }

        .status-aktif {
            background: rgba(16, 185, 129, 0.1);
            color: var(--success);
        }

        .status-nonaktif {
            background: rgba(239, 68, 68, 0.1);
            color: var(--danger);
        }

        /* Button Styles */
        .button-group {
            display: flex;
            gap: 1rem;
            margin-top: 2rem;
            padding-top: 1.5rem;
            border-top: 1px solid rgba(0,0,0,0.1);
        }

        .dark .button-group {
            border-top-color: rgba(255,255,255,0.1);
        }

        .btn {
            padding: 0.75rem 2rem;
            border-radius: 10px;
            font-weight: 500;
            font-size: 1rem;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            border: none;
            text-decoration: none;
        }

        .btn-primary {
            background: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background: var(--primary-dark);
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0,0,0,0.15);
        }

        .btn-secondary {
            background: var(--gray-light);
            color: var(--text-primary);
        }

        .dark .btn-secondary {
            background: rgba(255,255,255,0.1);
        }

        .btn-secondary:hover {
            background: var(--gray);
            color: white;
            transform: translateY(-2px);
        }

        .btn-danger {
            background: var(--danger);
            color: white;
        }

        .btn-danger:hover {
            background: #dc2626;
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(239, 68, 68, 0.15);
        }

        /* Footer */
        .footer {
            background: var(--dark);
            color: white;
            text-align: center;
            padding: 1.5rem;
            margin-top: 3rem;
        }

        .footer-content {
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .version {
            font-size: 0.875rem;
            opacity: 0.8;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 1rem;
                text-align: center;
            }

            .main-container {
                padding: 1rem;
            }

            .card {
                padding: 1.5rem;
            }

            .card-header {
                flex-direction: column;
                text-align: center;
            }

            .card-icon {
                width: 50px;
                height: 50px;
                font-size: 1.5rem;
            }

            .form-row {
                grid-template-columns: 1fr;
                gap: 1rem;
            }

            .time-grid {
                grid-template-columns: 1fr;
            }

            .button-group {
                flex-direction: column;
            }

            .btn {
                width: 100%;
            }

            .footer-content {
                flex-direction: column;
                gap: 1rem;
            }
        }

        @media (max-width: 480px) {
            .header {
                padding: 1rem;
            }

            .main-container {
                padding: 1rem;
            }

            .card {
                padding: 1rem;
            }

            .breadcrumb {
                padding: 0 1rem;
            }
        }

        /* Loading Animation */
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .fade-in {
            animation: fadeIn 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Header -->
    <header class="header">
        <div class="header-content">
            <div class="logo-section">
                <h1>Pengaturan Jam Absensi</h1>
                <div class="subtitle">Atur jadwal dan batas waktu absensi</div>
            </div>
        </div>
    </header>

    <!-- Breadcrumb -->
    <nav class="breadcrumb">
        <div class="breadcrumb-content">
            <a href="dashboard.php"><i class="fas fa-home"></i> Dashboard</a>
            <span class="breadcrumb-separator">/</span>
            <span>Pengaturan Jam Absensi</span>
        </div>
    </nav>

    <!-- Main Content -->
    <div class="main-container">
        <!-- Message -->
        <?php if ($message): ?>
            <div class="message <?php echo $message_type; ?> fade-in">
                <i class="fas fa-<?php echo $message_type == 'success' ? 'check-circle' : 'exclamation-circle'; ?> message-icon"></i>
                <span><?php echo htmlspecialchars($message); ?></span>
            </div>
        <?php endif; ?>

        <!-- Current Settings -->
        <div class="card fade-in">
            <div class="time-display">
                <h3 style="margin-bottom: 1rem; color: var(--text-primary);">Pengaturan Saat Ini</h3>
                <div class="time-grid">
                    <div class="time-item">
                        <div class="time-label">Jam Masuk</div>
                        <div class="time-value"><?php echo date('H:i', strtotime($jam_absensi['jam_masuk'])); ?></div>
                    </div>
                    <div class="time-item">
                        <div class="time-label">Jam Pulang</div>
                        <div class="time-value"><?php echo date('H:i', strtotime($jam_absensi['jam_pulang'])); ?></div>
                    </div>
                    <div class="time-item">
                        <div class="time-label">Toleransi Masuk</div>
                        <div class="time-value"><?php echo $jam_absensi['toleransi_masuk']; ?> menit</div>
                    </div>
                    <div class="time-item">
                        <div class="time-label">Status</div>
                        <div class="time-value"><?php echo ucfirst($jam_absensi['status']); ?></div>
                        <span class="time-status status-<?php echo $jam_absensi['status']; ?>">
                            <?php echo $jam_absensi['status'] == 'aktif' ? 'Aktif' : 'Nonaktif'; ?>
                        </span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Settings Form -->
        <div class="card fade-in">
            <div class="card-header">
                <div class="card-icon">
                    <i class="fas fa-clock"></i>
                </div>
                <div>
                    <h2 class="card-title">Atur Jam Absensi</h2>
                    <p class="card-subtitle">Konfigurasi jadwal dan toleransi waktu absensi</p>
                </div>
            </div>

            <form method="POST" action="">
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label" for="jam_masuk">
                            <i class="fas fa-sign-in-alt"></i> Jam Masuk
                        </label>
                        <input type="time" 
                               id="jam_masuk" 
                               name="jam_masuk" 
                               class="form-input" 
                               value="<?php echo htmlspecialchars($jam_absensi['jam_masuk']); ?>"
                               required>
                        <p class="form-help">Waktu mulai absensi masuk (contoh: 07:00)</p>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="jam_pulang">
                            <i class="fas fa-sign-out-alt"></i> Jam Pulang
                        </label>
                        <input type="time" 
                               id="jam_pulang" 
                               name="jam_pulang" 
                               class="form-input" 
                               value="<?php echo htmlspecialchars($jam_absensi['jam_pulang']); ?>"
                               required>
                        <p class="form-help">Waktu mulai absensi pulang (contoh: 15:00)</p>
                    </div>
                </div>

                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label" for="toleransi_masuk">
                            <i class="fas fa-hourglass-half"></i> Toleransi Keterlambatan Masuk
                        </label>
                        <input type="number" 
                               id="toleransi_masuk" 
                               name="toleransi_masuk" 
                               class="form-input" 
                               value="<?php echo htmlspecialchars($jam_absensi['toleransi_masuk']); ?>"
                               min="0" 
                               max="60" 
                               required>
                        <p class="form-help">Menit toleransi keterlambatan masuk (0-60 menit)</p>
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="toleransi_pulang">
                            <i class="fas fa-hourglass-end"></i> Toleransi Kepulangan
                        </label>
                        <input type="number" 
                               id="toleransi_pulang" 
                               name="toleransi_pulang" 
                               class="form-input" 
                               value="<?php echo htmlspecialchars($jam_absensi['toleransi_pulang']); ?>"
                               min="0" 
                               max="60" 
                               required>
                        <p class="form-help">Menit toleransi kepulangan lebih cepat (0-60 menit)</p>
                    </div>
                </div>

                <div class="form-group">
                    <label class="form-label" for="status">
                        <i class="fas fa-toggle-on"></i> Status Pengaturan
                    </label>
                    <select id="status" name="status" class="form-select" required>
                        <option value="aktif" <?php echo $jam_absensi['status'] == 'aktif' ? 'selected' : ''; ?>>Aktif</option>
                        <option value="nonaktif" <?php echo $jam_absensi['status'] == 'nonaktif' ? 'selected' : ''; ?>>Nonaktif</option>
                    </select>
                    <p class="form-help">Atur apakah pengaturan jam absensi ini aktif atau nonaktif</p>
                </div>

                <div class="button-group">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> Simpan Pengaturan
                    </button>
                    <a href="dashboard.php" class="btn btn-secondary">
                        <i class="fas fa-arrow-left"></i> Kembali ke Dashboard
                    </a>
                    <button type="reset" class="btn btn-secondary">
                        <i class="fas fa-redo"></i> Reset Form
                    </button>
                </div>
            </form>
        </div>

        <!-- Information Card -->
        <div class="card fade-in">
            <div class="card-header">
                <div class="card-icon" style="background: var(--info);">
                    <i class="fas fa-info-circle"></i>
                </div>
                <div>
                    <h2 class="card-title">Informasi Penting</h2>
                    <p class="card-subtitle">Panduan pengaturan jam absensi</p>
                </div>
            </div>
            
            <div style="margin-top: 1.5rem;">
                <h3 style="color: var(--text-primary); margin-bottom: 1rem; font-size: 1.1rem;">
                    <i class="fas fa-lightbulb"></i> Cara Kerja Sistem:
                </h3>
                <ul style="color: var(--text-secondary); padding-left: 1.5rem; line-height: 1.8;">
                    <li><strong>Jam Masuk:</strong> Siswa dapat melakukan absensi masuk mulai dari waktu ini</li>
                    <li><strong>Toleransi Masuk:</strong> Batas waktu maksimal keterlambatan sebelum dianggap terlambat</li>
                    <li><strong>Jam Pulang:</strong> Waktu mulai absensi pulang</li>
                    <li><strong>Toleransi Pulang:</strong> Batas minimal sebelum jam pulang untuk melakukan absensi pulang</li>
                    <li><strong>Status Nonaktif:</strong> Sistem akan menonaktifkan validasi waktu (absensi bisa kapan saja)</li>
                </ul>
                
                <div style="margin-top: 1.5rem; padding: 1rem; background: rgba(245, 158, 11, 0.1); border-radius: 10px; border-left: 4px solid var(--warning);">
                    <p style="color: var(--warning); font-weight: 500;">
                        <i class="fas fa-exclamation-triangle"></i> Perhatian:
                    </p>
                    <p style="color: var(--text-secondary); margin-top: 0.5rem;">
                        Perubahan pengaturan ini akan mempengaruhi semua sistem absensi, termasuk QR scan dan input manual.
                    </p>
                </div>
            </div>
        </div>
    </div>

    <footer class="footer">
        <div class="footer-content">
            <div class="version">Versi Aplikasi: 4.00 | &copy; <?php echo date('Y'); ?> Sistem Absensi QR Modern</div>
            <div class="footer-links">
                <a href="#"><i class="fas fa-question-circle"></i> Bantuan</a>
                <a href="#"><i class="fas fa-shield-alt"></i> Privasi</a>
                <a href="#"><i class="fas fa-file-contract"></i> Kebijakan</a>
            </div>
        </div>
    </footer>

    <script>
        // Theme Toggle
        function toggleTheme() {
            document.documentElement.classList.toggle('dark');
            const icon = document.querySelector('.theme-toggle i');
            if (document.documentElement.classList.contains('dark')) {
                icon.className = 'fas fa-sun';
                localStorage.setItem('theme', 'dark');
            } else {
                icon.className = 'fas fa-moon';
                localStorage.setItem('theme', 'light');
            }
        }

        // Load saved theme
        const savedTheme = localStorage.getItem('theme');
        if (savedTheme === 'dark') {
            document.documentElement.classList.add('dark');
        }

        // Form validation
        document.querySelector('form').addEventListener('submit', function(e) {
            const jamMasuk = document.getElementById('jam_masuk').value;
            const jamPulang = document.getElementById('jam_pulang').value;
            
            if (jamMasuk >= jamPulang) {
                e.preventDefault();
                alert('Jam masuk harus lebih awal dari jam pulang!');
                document.getElementById('jam_masuk').focus();
            }
        });

        // Real-time clock
        function updateClock() {
            const now = new Date();
            const timeString = now.toLocaleTimeString('id-ID', { 
                hour: '2-digit', 
                minute: '2-digit', 
                second: '2-digit' 
            });
            
            const dateString = now.toLocaleDateString('id-ID', {
                weekday: 'long',
                year: 'numeric',
                month: 'long',
                day: 'numeric'
            });
            
            // Update clock display if exists
            const clockElement = document.getElementById('current-time');
            if (clockElement) {
                clockElement.textContent = timeString + ' - ' + dateString;
            }
        }
        
        // Update clock every second
        setInterval(updateClock, 1000);
        updateClock(); // Initial call

        // Auto-hide message after 5 seconds
        const messageElement = document.querySelector('.message');
        if (messageElement) {
            setTimeout(() => {
                messageElement.style.opacity = '0';
                messageElement.style.transform = 'translateY(-10px)';
                setTimeout(() => {
                    messageElement.style.display = 'none';
                }, 300);
            }, 5000);
        }
    </script>
</body>
</html>