<?php

include "../../config/master.php";
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>";

$query = mysqli_query($conn,"
SELECT departments.*, districts.district_name
FROM departments
JOIN districts ON departments.district_id = districts.id
ORDER BY departments.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;">Departments</h4>
<p style="font-size:12px;color:#777;margin:0;">Manage departments</p>
</div>

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

</div>

<!-- LIST -->

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

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

<div class="card-box">

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

<!-- DISTRICT -->
<p style="font-size:12px;color:#666;margin:5px 0;">
📍 <?php echo htmlspecialchars($row['district_name']); ?>
</p>

<!-- ID -->
<p style="font-size:11px;color:#999;">
ID: <?php echo $row['id']; ?>
</p>

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

<a href="edit_department.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>

<a href="delete_department.php?id=<?php echo $row['id']; ?>"
onclick="return confirm('Delete this department?')"
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>

</div>

</div>

<?php } ?>

<?php } else { ?>

<div class="card-box" style="text-align:center;">
No departments 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"; ?>