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

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../login.php"); exit;
}

$query = "SELECT u.*, p.nama_posyandu 
          FROM users u 
          LEFT JOIN posyandu p ON u.id_posyandu = p.id_posyandu 
          ORDER BY u.role ASC, u.nama_lengkap ASC";
$users = $pdo->query($query)->fetchAll();
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manajemen User</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <?php include 'sidebar.php'; ?>

    <div class="main-content">
        <div class="container-fluid">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h4 class="fw-bold mb-0">Manajemen User</h4>
                <a href="user_add.php" class="btn btn-primary shadow-sm"><i class="fa fa-plus me-1"></i> Tambah</a>
            </div>

            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="px-3">User & Role</th>
                                    <th>Penempatan / NIK</th>
                                    <th class="text-center">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($users as $u): ?>
                                <tr>
                                    <td class="px-3">
                                        <div class="fw-bold text-dark"><?= $u['nama_lengkap'] ?></div>
                                        <small class="text-muted">@<?= $u['username'] ?> • </small>
                                        <span class="badge <?= $u['role'] == 'admin' ? 'bg-primary' : ($u['role'] == 'kader' ? 'bg-success' : 'bg-info') ?> font-monospace">
                                            <?= strtoupper($u['role']) ?>
                                        </span>
                                    </td>
                                    <td>
                                        <?php 
                                            if($u['role'] == 'kader') echo $u['nama_posyandu'] ?: '-';
                                            elseif($u['role'] == 'ibu') echo "NIK: " . ($u['nik_ibu'] ?: '-');
                                            else echo '<span class="text-muted italic">Akses Global</span>';
                                        ?>
                                    </td>
                                    <td class="text-center">
                                        <div class="btn-group">
                                            <a href="user_edit.php?id=<?= $u['id_user'] ?>" class="btn btn-sm btn-outline-warning border-0"><i class="fa fa-edit fa-lg"></i></a>
                                            <a href="user_delete.php?id=<?= $u['id_user'] ?>" class="btn btn-sm btn-outline-danger border-0" onclick="return confirm('Hapus user ini?')"><i class="fa fa-trash fa-lg"></i></a>
                                        </div>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>