<?php

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

/* Allow Super Admin + Village Admin + Operator */
allowRoles([1,5,6]);

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

/* FETCH DATA */
$query = mysqli_query($conn,"
SELECT s.*, sc.category_name
FROM schemes s
LEFT JOIN scheme_categories sc ON s.category_id = sc.id
ORDER BY s.id DESC
") or die(mysqli_error($conn));

?>

<!-- ===== PAGE HEADER ===== -->

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

<div>
<h4 style="margin:0;">Government Schemes</h4>
<p style="font-size:12px;color:#777;margin:0;">All available schemes</p>
</div>

<?php if($_SESSION['role_id']==1){ ?>
<a href="add_scheme.php" class="btn-mobile btn-add">
    <i class="fa fa-plus"></i>
</a>
<?php } ?>

</div>

<!-- ===== LIST ===== -->

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

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

<div class="card-box" style="padding:12px;">

    <!-- IMAGE -->
    <?php if(!empty($row['image']) && file_exists("../../uploads/schemes/".$row['image'])){ ?>
        <img src="../../uploads/schemes/<?php echo $row['image']; ?>"
        style="width:100%;height:160px;object-fit:cover;border-radius:10px;margin-bottom:10px;">
    <?php } ?>

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

    <!-- CATEGORY -->
    <p style="font-size:12px;color:#666;margin:2px 0;">
        📂 
        <?php
        if(empty($row['category_name'])){
            echo "All";
        }else{
            echo htmlspecialchars($row['category_name']);
        }
        ?>
    </p>

    <!-- DEPARTMENT -->
    <p style="font-size:13px;margin:2px 0;">
        🏢 <?php echo htmlspecialchars($row['department']); ?>
    </p>

    <!-- STATUS -->
    <div style="margin-top:6px;">
        <span class="status-badge <?php echo $row['status']==1 ? 'active' : 'inactive'; ?>">
            <?php echo $row['status']==1 ? 'Active' : 'Disabled'; ?>
        </span>
    </div>

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

        <a href="scheme_detail.php?id=<?php echo $row['id']; ?>" class="icon-btn view">
            <i class="fa fa-eye"></i>
        </a>

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

        <a href="edit_scheme.php?id=<?php echo $row['id']; ?>" class="icon-btn edit">
            <i class="fa fa-pen"></i>
        </a>

        <a href="delete_scheme.php?id=<?php echo $row['id']; ?>"
        onclick="return confirm('Delete this scheme?')"
        class="icon-btn delete">
            <i class="fa fa-trash"></i>
        </a>

        <?php } ?>

    </div>

</div>

<?php } ?>

<?php } else { ?>

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

<?php } ?>

<!-- ===== BOTTOM NAV ===== -->

<div class="bottom-nav">
    <a href="../dashboard.php">
        <i class="fa fa-home"></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="view_schemes.php" class="active">
        <i class="fa fa-list"></i>
        Schemes
    </a>
</div>

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