<?php
/**
 * Halaman OPAC Daftar Pustakawan
 * Path: plugins/multi_opac_path/pages/librarian.php
 */

if (!defined('INDEX_AUTH')) {
    die("Direct access not allowed");
}

if (!function_exists('getLibrariansByLibrary')) {
    require_once dirname(__DIR__) . '/src/helpers.php';
}

$currentLib   = $GLOBALS['current_library'] ?? null;
$currentLibId = $currentLib['library_id'] ?? $_SESSION['multi_opac']['library_id'] ?? null;
$libraryName  = $currentLib['library_name'] ?? 'Perpustakaan';

$librarians = getLibrariansByLibrary($currentLibId, 50);

// Helper sederhana untuk konversi user_type angka/string ke Teks Jabatan
function getUserTypeName($type) {
    if ($type == 1 || $type == '1') {
        return 'Librarian';
    } elseif ($type == 2 || $type == '2') {
        return 'Library Staff';
    } elseif (!empty($type) && !is_numeric($type)) {
        return ucfirst($type);
    }
    return 'Librarian'; // Fallback default
}
?>

<div class="container my-2">
    <div class="section-header mb-4 border-bottom pb-2">
        <h2 class="h4 font-weight-bold text-primary">
            <i class="fa-solid fa-user-tie me-2"></i><?= __('Pustakawan & Staf') ?>
        </h2>
        <p class="text-muted mb-0 small">
            <?= __('Daftar pengelola perpustakaan di ') . htmlspecialchars($libraryName) ?>
        </p>
    </div>

    <?php if (!empty($librarians) && count($librarians) > 0): ?>
        <div class="row g-4">
            <?php foreach ($librarians as $staff): ?>
                <?php
                $avatarDir     = 'images/persons/';
                $defaultAvatar = 'plugins/multi_opac_path/assets/images/default_avatar.png';
                
                $avatarUrl = (!empty($staff['user_image']) && file_exists($avatarDir . $staff['user_image'])) 
                    ? $avatarDir . $staff['user_image'] 
                    : $defaultAvatar;
                
                $displayName  = !empty($staff['realname']) ? $staff['realname'] : $staff['username'];
                $userTypeName = getUserTypeName($staff['user_type'] ?? null);
                ?>
                <div class="col-sm-6 col-md-4 col-lg-3">
                    <div class="card h-100 p-3 text-center d-flex flex-column align-items-center justify-content-between shadow-sm border-0 rounded-3" style="background: #fff;">
                        <div class="w-100">
                            <!-- Foto Staf -->
                            <div class="mb-3 mx-auto overflow-hidden rounded-circle shadow-sm" style="width: 90px; height: 90px;">
                                <img src="<?= $avatarUrl ?>" 
                                     alt="<?= htmlspecialchars($displayName) ?>" 
                                     class="w-100 h-100" 
                                     style="object-fit: cover;"
                                     onerror="this.onerror=null; this.src='<?= $defaultAvatar ?>';">
                            </div>

                            <!-- Nama Lengkap (realname) -->
                            <h5 class="h6 font-weight-bold mb-1 text-dark">
                                <?= htmlspecialchars($displayName) ?>
                            </h5>

                            <!-- Jabatan (user_type) -->
                            <span class="badge bg-light text-primary border rounded-pill px-3 py-1 small">
                                <i class="fa-solid fa-id-badge me-1"></i><?= htmlspecialchars($userTypeName) ?>
                            </span>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    <?php else: ?>
        <div class="card p-5 text-center my-4 border-0 shadow-sm rounded-3">
            <i class="fa-solid fa-user-slash fa-3x text-muted mb-3"></i>
            <h5 class="text-muted"><?= __('Belum ada data pustakawan yang ditampilkan.') ?></h5>
            <p class="text-muted small mb-0"><?= __('Informasi pustakawan untuk perpustakaan ini belum diset oleh administrator.') ?></p>
        </div>
    <?php endif; ?>
</div>