<?php
// profile.php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'config/database.php';
require_once 'config/app.php';
checkAuth();

$conn = getConnection();
$user_id = $_SESSION['user_id'];

// Ambil data user lengkap
$result = $conn->query("
    SELECT u.*, e.full_name, e.nik, e.employee_code, e.phone, e.email, e.photo_path,
           e.profession, e.position_name, e.join_date,
           u2.name as unit_name
    FROM users u
    JOIN employees e ON u.employee_id = e.id
    LEFT JOIN units u2 ON e.unit_id = u2.id
    WHERE u.id = $user_id
");
$user_detail = $result->fetch_assoc();

// Format tanggal
function formatTanggalIndonesia($date) {
    if (!$date) return '-';
    $bulan = [
        1 => 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni',
        'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
    ];
    $tgl = date('j', strtotime($date));
    $bln = $bulan[(int)date('n', strtotime($date))];
    $thn = date('Y', strtotime($date));
    return "$tgl $bln $thn";
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Profil Saya - <?= APP_NAME ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body class="bg-gray-100">
    <div class="max-w-4xl mx-auto mt-10 bg-white rounded-xl shadow-lg overflow-hidden">
        <!-- Header -->
        <div class="bg-gradient-to-r from-blue-600 to-indigo-600 px-6 py-8 text-white">
            <div class="flex items-center gap-6">
                <div class="w-24 h-24 bg-white/20 rounded-full flex items-center justify-center text-4xl font-bold border-4 border-white">
                    <?= strtoupper(substr($user_detail['full_name'], 0, 1)) ?>
                </div>
                <div>
                    <h1 class="text-2xl font-bold"><?= $user_detail['full_name'] ?></h1>
                    <p class="text-blue-100 mt-1"><?= ucfirst($user_detail['role']) ?> • <?= $user_detail['nik'] ?></p>
                    <p class="text-blue-100 text-sm mt-2"><?= $user_detail['position_name'] ?? 'Staf Keperawatan' ?></p>
                </div>
            </div>
        </div>
        
        <!-- Content -->
        <div class="p-6">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Informasi Pribadi -->
                <div class="border rounded-lg p-4">
                    <h2 class="text-lg font-semibold mb-4 flex items-center">
                        <i class="fas fa-user-circle mr-2 text-blue-600"></i>
                        Informasi Pribadi
                    </h2>
                    
                    <div class="space-y-3">
                        <div>
                            <label class="text-sm text-gray-500">Nama Lengkap</label>
                            <p class="font-medium"><?= $user_detail['full_name'] ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">NIK</label>
                            <p class="font-medium"><?= $user_detail['nik'] ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Username</label>
                            <p class="font-medium"><?= $user_detail['username'] ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Email</label>
                            <p class="font-medium"><?= $user_detail['email'] ?? '-' ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Telepon</label>
                            <p class="font-medium"><?= $user_detail['phone'] ?? '-' ?></p>
                        </div>
                    </div>
                </div>
                
                <!-- Informasi Pekerjaan -->
                <div class="border rounded-lg p-4">
                    <h2 class="text-lg font-semibold mb-4 flex items-center">
                        <i class="fas fa-briefcase mr-2 text-green-600"></i>
                        Informasi Pekerjaan
                    </h2>
                    
                    <div class="space-y-3">
                        <div>
                            <label class="text-sm text-gray-500">Unit</label>
                            <p class="font-medium"><?= $user_detail['unit_name'] ?? '-' ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Profesi</label>
                            <p class="font-medium"><?= ucfirst($user_detail['profession'] ?? '-') ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Jabatan</label>
                            <p class="font-medium"><?= $user_detail['position_name'] ?? '-' ?></p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Role Sistem</label>
                            <p class="font-medium">
                                <span class="px-2 py-1 bg-blue-100 text-blue-800 rounded-full text-xs">
                                    <?= ucfirst($user_detail['role']) ?>
                                </span>
                            </p>
                        </div>
                        <div>
                            <label class="text-sm text-gray-500">Tanggal Bergabung</label>
                            <p class="font-medium"><?= formatTanggalIndonesia($user_detail['join_date']) ?></p>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Aktivitas Login -->
            <div class="mt-6 border rounded-lg p-4">
                <h2 class="text-lg font-semibold mb-4 flex items-center">
                    <i class="fas fa-history mr-2 text-purple-600"></i>
                    Aktivitas Login
                </h2>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="text-sm text-gray-500">Terakhir Login</label>
                        <p class="font-medium">
                            <?= $user_detail['last_login_at'] ? date('d/m/Y H:i', strtotime($user_detail['last_login_at'])) : 'Belum pernah' ?>
                        </p>
                    </div>
                    <div>
                        <label class="text-sm text-gray-500">IP Terakhir</label>
                        <p class="font-medium"><?= $user_detail['last_login_ip'] ?? '-' ?></p>
                    </div>
                </div>
            </div>
            
            <!-- Tombol Aksi -->
            <div class="flex justify-end gap-3 mt-6">
                <a href="change_password.php" class="px-6 py-2 bg-yellow-600 text-white rounded-lg hover:bg-yellow-700 transition">
                    <i class="fas fa-key mr-2"></i>Ganti Password
                </a>
                <a href="javascript:history.back()" class="px-6 py-2 border border-gray-300 rounded-lg hover:bg-gray-50 transition">
                    Kembali
                </a>
            </div>
        </div>
    </div>
</body>
</html>