<?php
session_start();
require_once __DIR__ . '/../config.php';

// Check if user is logged in
if (!isset($_SESSION['token'])) {
    header('Location: login.php');
    exit;
}

$token = $_SESSION['token'];
$user_data = $_SESSION['user_data'] ?? [];

// Get user profile data
$profile_result = api_get_profil($token);
$profile_data = $profile_result['success'] ? $profile_result['data'] : [];

// Get today's attendance
$presensi_today_result = api_get_presensi_today($token);
$presensi_today = $presensi_today_result['success'] ? $presensi_today_result['data'] : [];

// Extract user info from API response
$pegawai = $profile_data['pegawai'] ?? [];
$user_name = $pegawai['nama_lengkap'] ?? $user_data['nama'] ?? 'User';
$user_jabatan = $pegawai['jabatan']['nama_jabatan'] ?? $user_data['jabatan'] ?? 'Karyawan';
$user_id = $pegawai['nip'] ?? $user_data['id'] ?? '';
$unit_kerja = $pegawai['unit_kerja']['nama_unit_kerja'] ?? 'Unit Kerja';
$kantor = $pegawai['kantor']['nama_kantor'] ?? 'Kantor Pusat';
$alamat_kantor = $pegawai['kantor']['alamat_kantor'] ?? '';
$kode_kantor = $pegawai['kantor']['kode_kantor'] ?? '';

// Extract attendance info from presensi_today
$presensi_data_today = $presensi_today['data'] ?? [];
$masuk_time = $presensi_data_today['jam_masuk'] ?? null;
$pulang_time = $presensi_data_today['jam_pulang'] ?? null;

// Extract work schedule info from profile
$jadwal = $pegawai['jadwal'] ?? [];
$jam_masuk = $jadwal['masuk'] ?? '08:00';
$jam_pulang = $jadwal['pulang'] ?? '17:00';
$jam_istirahat = $jadwal['istirahat'] ?? '12:00';
$toleransi_masuk = $jadwal['toleransi_masuk'] ?? '15';

// Helper function untuk format tanggal Indonesia
function formatTanggalIndonesia($format = 'l, d F Y')
{
    $hari = ['Sunday' => 'Minggu', 'Monday' => 'Senin', 'Tuesday' => 'Selasa', 'Wednesday' => 'Rabu', 'Thursday' => 'Kamis', 'Friday' => 'Jumat', 'Saturday' => 'Sabtu'];
    $bulan = ['January' => 'Januari', 'February' => 'Februari', 'March' => 'Maret', 'April' => 'April', 'May' => 'Mei', 'June' => 'Juni', 'July' => 'Juli', 'August' => 'Agustus', 'September' => 'September', 'October' => 'Oktober', 'November' => 'November', 'December' => 'Desember'];
    $tanggal_inggris = date($format);
    $tanggal_indonesia = str_replace(array_keys($hari), array_values($hari), str_replace(array_keys($bulan), array_values($bulan), $tanggal_inggris));
    return $tanggal_indonesia;
}
?>
<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Sistem Presensi Bank BIJ</title>
    <link rel="stylesheet" href="<?php echo asset('css/tailwind-essential.css'); ?>">
</head>

<body class="bg-gradient-to-br from-blue-900 via-blue-800 to-blue-700 min-h-screen">
    <div class="container mx-auto px-4 py-8">
        <!-- Header -->
        <div class="bg-white rounded-lg shadow-lg p-6 mb-6">
            <div class="flex justify-between items-center">
                <div>
                    <h1 class="text-2xl font-bold text-gray-800">Dashboard Presensi</h1>
                    <p class="text-gray-600">Bank BIJ</p>
                </div>
                <div class="text-right">
                    <p class="text-lg font-semibold text-gray-800"><?php echo htmlspecialchars($user_name); ?></p>
                    <p class="text-gray-600"><?php echo htmlspecialchars($user_jabatan); ?></p>
                    <p class="text-sm text-gray-500"><?php echo htmlspecialchars($user_id); ?></p>
                </div>
            </div>
        </div>

        <!-- Informasi Jadwal Kerja -->
        <div class="bg-white rounded-lg shadow-lg p-6 mb-6">
            <h2 class="text-xl font-bold text-gray-800 mb-4">Informasi Jadwal Kerja</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <p class="text-sm text-gray-500">Jadwal Kerja:</p>
                    <p class="text-lg font-semibold text-gray-800"><?php echo formatTanggalIndonesia(); ?></p>
                    <p class="text-gray-700"><?php echo $jam_masuk; ?> - <?php echo $jam_pulang; ?></p>
                    <p class="text-sm text-gray-600">Istirahat: <?php echo $jam_istirahat; ?></p>
                </div>
                <div>
                    <p class="text-sm text-gray-500">Lokasi Kerja:</p>
                    <p class="text-lg font-semibold text-gray-800"><?php echo htmlspecialchars($unit_kerja); ?></p>
                    <?php if (!empty($alamat_kantor)): ?>
                        <p class="text-sm text-gray-600"><?php echo htmlspecialchars($alamat_kantor); ?></p>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Status Kehadiran Hari Ini -->
        <div class="bg-white rounded-lg shadow-lg p-6 mb-6">
            <h2 class="text-xl font-bold text-gray-800 mb-4">Status Kehadiran Hari Ini</h2>
            <div class="grid grid-cols-2 gap-4">
                <div class="text-center p-4 border rounded-lg <?php echo $masuk_time ? 'bg-green-50 border-green-200' : 'bg-gray-50 border-gray-200'; ?>">
                    <p class="text-sm text-gray-500">Masuk</p>
                    <p class="text-xl font-bold text-gray-800"><?php echo $masuk_time ?: '00:00'; ?></p>
                    <p class="text-sm text-gray-500"><?php echo $masuk_time ? '(sudah rekam)' : '(belum rekam)'; ?></p>
                </div>
                <div class="text-center p-4 border rounded-lg <?php echo $pulang_time ? 'bg-green-50 border-green-200' : 'bg-gray-50 border-gray-200'; ?>">
                    <p class="text-sm text-gray-500">Pulang</p>
                    <p class="text-xl font-bold text-gray-800"><?php echo $pulang_time ?: '00:00'; ?></p>
                    <p class="text-sm text-gray-500"><?php echo $pulang_time ? '(sudah rekam)' : '(belum rekam)'; ?></p>
                </div>
            </div>
        </div>

        <!-- Actions -->
        <div class="bg-white rounded-lg shadow-lg p-6">
            <h2 class="text-xl font-bold text-gray-800 mb-4">Aksi Presensi</h2>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <a href="rekam.php" class="bg-blue-600 text-white px-6 py-3 rounded-lg text-center hover:bg-blue-700 transition">
                    <i class="fas fa-clock mr-2"></i>Rekam Presensi
                </a>
                <a href="dashboard.php" class="bg-green-600 text-white px-6 py-3 rounded-lg text-center hover:bg-green-700 transition">
                    <i class="fas fa-chart-bar mr-2"></i>Lihat Dashboard
                </a>
                <a href="logout.php" class="bg-red-600 text-white px-6 py-3 rounded-lg text-center hover:bg-red-700 transition">
                    <i class="fas fa-sign-out-alt mr-2"></i>Logout
                </a>
            </div>
        </div>
    </div>
</body>

</html>