<?php
session_start();
include 'koneksi.php';

if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit();
}

// Ambil data pengaturan jam saat ini dari database
$jamMasuk = '';
$jamPulang = '';
$result = mysqli_query($conn, "SELECT * FROM pengaturan_jam LIMIT 1");
if ($row = mysqli_fetch_assoc($result)) {
    $jamMasuk = $row['jam_masuk'];
    $jamPulang = $row['jam_pulang'];
}

// Simpan perubahan
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $newMasuk = $_POST['jam_masuk'];
    $newPulang = $_POST['jam_pulang'];

    // Update jika sudah ada, insert jika belum
    if (mysqli_num_rows($result) > 0) {
        mysqli_query($conn, "UPDATE pengaturan_jam SET jam_masuk='$newMasuk', jam_pulang='$newPulang'");
    } else {
        mysqli_query($conn, "INSERT INTO pengaturan_jam (jam_masuk, jam_pulang) VALUES ('$newMasuk', '$newPulang')");
    }

    // Reload nilai baru
    $jamMasuk = $newMasuk;
    $jamPulang = $newPulang;
    $success = true;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Set Jam Masuk / Pulang</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <style>
        body {
            min-height: 100vh;
            display: flex;
            font-family: 'Poppins', sans-serif;
            background-color: #f4f6f9;
        }
        .sidebar {
            width: 250px;
            background-color: #1f2d3d;
            color: white;
            display: flex;
            flex-direction: column;
            padding-top: 20px;
        }
        .sidebar a {
            color: #cfd8dc;
            text-decoration: none;
            display: flex;
            align-items: center;
            padding: 12px 20px;
        }
        .sidebar a i {
            margin-right: 12px;
        }
        .sidebar a:hover, .sidebar a.active {
            background-color: #273746;
            color: #ffffff;
        }
        .content {
            flex-grow: 1;
            padding: 30px;
        }
        .card {
            border: none;
            border-radius: 10px;
            box-shadow: 0 3px 10px rgba(0,0,0,0.05);
        }
        .form-control {
            max-width: 300px;
        }
    </style>
</head>
<body>

<div class="sidebar">
    <h4 class="text-center text-white mb-4">ABSENSI RFID</h4>
    <a href="index.php"><i class="bi bi-house-door-fill"></i> Beranda</a>
    <a href="karyawan.php"><i class="bi bi-people-fill"></i> Data Karyawan</a>
    <a href="absensi.php"><i class="bi bi-clock-fill"></i> Absensi</a>
    <a href="rekap.php"><i class="bi bi-card-checklist"></i> Rekap Absensi</a>
    <a href="atur_jam.php" class="active"><i class="bi bi-clock-history"></i> Set Jam Masuk / Pulang</a>
    <div class="mt-auto">
        <hr class="bg-secondary">
        <a href="logout.php"><i class="bi bi-box-arrow-right"></i> Logout</a>
    </div>
</div>

<div class="content">
    <h3 class="mb-4">Pengaturan Jam Masuk dan Pulang</h3>

    <?php if (isset($success)): ?>
        <div class="alert alert-success">Pengaturan jam berhasil diperbarui.</div>
    <?php endif; ?>

    <div class="card p-4">
        <form method="POST">
            <div class="mb-3">
                <label for="jam_masuk" class="form-label">Jam Masuk:</label>
                <input type="time" id="jam_masuk" name="jam_masuk" value="<?= $jamMasuk ?>" class="form-control" required>
            </div>
            <div class="mb-3">
                <label for="jam_pulang" class="form-label">Jam Pulang:</label>
                <input type="time" id="jam_pulang" name="jam_pulang" value="<?= $jamPulang ?>" class="form-control" required>
            </div>
            <button type="submit" class="btn btn-primary"><i class="bi bi-save"></i> Simpan Pengaturan</button>
        </form>
    </div>
</div>

</body>
</html>
