<?php

include "../../config/master.php";

/* Only Super Admin */
allowRoles([1]);

include "../layout/header.php";

/* HIDE SIDEBAR */
echo "<style>
.sidebar{display:none !important;}
.topbar{margin-left:0 !important;}
.main{margin-left:0 !important;padding-bottom:80px;}
</style>";

$q=mysqli_query($conn,"
SELECT users.*,roles.role_name,
districts.district_name,
blocks.block_name,
villages.village_name

FROM users

LEFT JOIN roles ON users.role_id=roles.id
LEFT JOIN districts ON users.district_id=districts.id
LEFT JOIN blocks ON users.block_id=blocks.id
LEFT JOIN villages ON users.village_id=villages.id

ORDER BY users.id DESC
");

?>

<div class="app-content">

<!-- HEADER -->
<div class="card-box" style="display:flex;justify-content:space-between;align-items:center;">

<div>
<h4 style="margin:0;">Users</h4>
<p style="font-size:12px;color:#777;margin:0;">Manage system users</p>
</div>

<a href="add_user.php" class="btn-mobile"
style="background:#27ae60;color:#fff;">
<i class="fa fa-plus"></i>
</a>

</div>

<!-- LIST -->

<?php if(mysqli_num_rows($q) > 0){ ?>

<?php while($row=mysqli_fetch_assoc($q)){ ?>

<div class="card-box">

<!-- NAME -->
<h5 style="margin:0;">
<?php echo htmlspecialchars($row['name']); ?>
</h5>

<!-- EMAIL -->
<p style="font-size:12px;color:#666;margin:4px 0;">
📧 <?php echo htmlspecialchars($row['email']); ?>
</p>

<!-- ROLE -->
<p style="font-size:12px;color:#444;margin:4px 0;">
👤 <?php echo htmlspecialchars($row['role_name']); ?>
</p>

<!-- LOCATION -->
<p style="font-size:11px;color:#999;margin:4px 0;">
📍 
<?php echo $row['district_name'] ?: '-'; ?> /
<?php echo $row['block_name'] ?: '-'; ?> /
<?php echo $row['village_name'] ?: '-'; ?>
</p>

<!-- ACTIONS -->
<div style="display:flex;gap:10px;margin-top:10px;">

<a href="edit_user.php?id=<?php echo $row['id']; ?>"
style="
flex:1;
text-align:center;
padding:10px;
background:#3498db;
color:#fff;
border-radius:8px;
text-decoration:none;
">
<i class="fa fa-pen"></i>
</a>

<?php if($row['role_id']!=1){ ?>

<a href="delete_user.php?id=<?php echo $row['id']; ?>"
onclick="return confirm('Delete this user?')"
style="
flex:1;
text-align:center;
padding:10px;
background:#e74c3c;
color:#fff;
border-radius:8px;
text-decoration:none;
">
<i class="fa fa-trash"></i>
</a>

<?php } ?>

</div>

</div>

<?php } ?>

<?php } else { ?>

<div class="card-box" style="text-align:center;">
No users found
</div>

<?php } ?>

</div>

<!-- ===== FOOTER ===== -->

<div class="bottom-nav">
    <a href="../dashboard.php">
        <i class="fa fa-house"></i>
        Home
    </a>

    <a href="../villages/village_data.php">
        <i class="fa fa-map"></i>
        Villages
    </a>

    <a href="../families/view_families.php">
        <i class="fa fa-users"></i>
        Families
    </a>

    <a href="../schemes/view_schemes.php">
        <i class="fa fa-list"></i>
        Schemes
    </a>
</div>

<?php include "../layout/footer.php"; ?>