<?php
require_once __DIR__ . '/../../config/session.php';
requireAdmin();
require_once __DIR__ . '/../../config/config.php';
require_once __DIR__ . '/../../functions/helper.php';
require_once __DIR__ . '/../../functions/csrf.php';

$db = getDB();


$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$offset = ($page - 1) * $limit;

$total_rows = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
$total_pages = ceil($total_rows / $limit);

$stmt = $db->query("SELECT * FROM users ORDER BY role ASC, created_at DESC LIMIT $limit OFFSET $offset");
$users = $stmt->fetchAll();

require_once '../../includes/header_admin.php';
?>

<div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4 mb-6">
    <div>
        <h1 class="text-2xl font-bold text-gray-800">Manajemen Pengguna</h1>
        <p class="text-gray-500 text-sm mt-1">Kelola akses akun admin dan pasien (ibu hamil).</p>
    </div>
    <div>
        <a href="tambah.php" class="inline-flex items-center justify-center px-4 py-2 bg-primary hover:bg-primary-dark text-white font-medium rounded-lg transition-colors shadow-sm focus:ring-2 focus:ring-offset-2 focus:ring-primary">
            <i class="fas fa-user-plus mr-2"></i> Tambah User
        </a>
    </div>
</div>

<?php $csrfDelete = generateCSRFToken(); ?>

<div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50/50">
        <h3 class="font-bold text-gray-800">Daftar Akun Pengguna</h3>
    </div>
    <div class="overflow-x-auto">
        <table class="w-full text-sm text-left whitespace-nowrap">
            <thead class="text-xs text-gray-500 uppercase bg-gray-50 border-b border-gray-100">
                <tr>
                    <th class="px-6 py-4 font-semibold">User Info</th>
                    <th class="px-6 py-4 font-semibold text-center">Role Akses</th>
                    <th class="px-6 py-4 font-semibold text-center">Status</th>
                    <th class="px-6 py-4 font-semibold text-center">Aksi</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                <?php foreach($users as $u): ?>
                    <tr class="hover:bg-gray-50 transition-colors">
                        <td class="px-6 py-4">
                            <div class="flex items-center gap-3">
                                <div class="w-10 h-10 rounded-full flex items-center justify-center text-white <?= $u['role'] === 'admin' ? 'bg-gradient-to-r from-primary to-primary-dark' : 'bg-sky-400' ?>">
                                    <span class="font-bold text-lg"><?= strtoupper(substr($u['nama_lengkap'], 0, 1)) ?></span>
                                </div>
                                <div>
                                    <div class="font-bold text-gray-900"><?= escape($u['nama_lengkap']) ?></div>
                                    <div class="text-xs text-gray-500 font-mono mt-0.5"><i class="fas fa-fingerprint text-gray-400 mr-1"></i> <?= escape($u['username']) ?></div>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4 text-center">
                            <?php if($u['role'] === 'admin'): ?>
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded border border-primary/30 bg-primary/10 text-primary font-bold text-xs uppercase tracking-wider">
                                    Admin / Bidan
                                </span>
                            <?php else: ?>
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded border border-sky-200 bg-sky-50 text-sky-600 font-bold text-xs uppercase tracking-wider">
                                    User / Pasien
                                </span>
                            <?php endif; ?>
                        </td>
                        <td class="px-6 py-4 text-center">
                            <?php if($u['is_active']): ?>
                                <span class="text-sky-500"><i class="fas fa-check-circle"></i></span>
                            <?php else: ?>
                                <span class="text-red-500"><i class="fas fa-times-circle"></i></span>
                            <?php endif; ?>
                        </td>
                        <td class="px-6 py-4 text-center space-x-1 whitespace-nowrap">
                            <a href="edit.php?id=<?= $u['id'] ?>" class="inline-flex items-center justify-center w-8 h-8 rounded-lg bg-yellow-50 text-yellow-600 hover:bg-yellow-500 hover:text-white transition-colors" title="Edit Data & Password">
                                <i class="fas fa-edit"></i>
                            </a>
                            <?php if ($u['id'] != ($_SESSION['user_id'] ?? 0)): ?>
                            <a href="hapus.php?id=<?= $u['id'] ?>&token=<?= urlencode($csrfDelete) ?>" onclick="return confirm('Yakin ingin menghapus user &lt;<?= escape(addslashes($u['nama_lengkap'])) ?>&gt;? Tindakan ini tidak dapat dibatalkan.')" class="inline-flex items-center justify-center w-8 h-8 rounded-lg bg-red-50 text-red-600 hover:bg-red-500 hover:text-white transition-colors" title="Hapus User">
                                <i class="fas fa-trash-alt"></i>
                            </a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?= renderPagination($total_pages, $page, './', $_GET) ?>
</div>

<?php require_once '../../includes/footer_admin.php'; ?>

