<?php
include "../../config/master.php";
allowRoles([1,5]);

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

$role_id = $_SESSION['role_id'] ?? 0;

/* =========================
   VILLAGE SELECTION
========================= */
if($role_id == 1){
    $village_id = $_GET['village_id'] ?? ($_POST['village_id'] ?? 0);
}else{
    $village_id = $_SESSION['village_id'];
}

/* =========================
   GLOBAL TOGGLE
========================= */
if(isset($_POST['toggle_global']))
{
    $val = isset($_POST['show_special_thanks_home']) ? 1 : 0;

    mysqli_query($conn,"
    UPDATE villages 
    SET show_special_thanks_home='$val'
    WHERE id='$village_id'
    ");
}

/* FETCH GLOBAL VALUE */
$village = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT show_special_thanks_home 
FROM villages WHERE id='$village_id'
"));

/* =========================
   UPDATE ROW (QUICK UPDATE)
========================= */
if(isset($_POST['update_row']))
{
    $id = intval($_POST['id']);
    $show = isset($_POST['show_on_home']) ? 1 : 0;
    $position = $_POST['position'];
    $order = intval($_POST['display_order']);

    mysqli_query($conn,"
    UPDATE special_thanks SET
    show_on_home='$show',
    position='$position',
    display_order='$order'
    WHERE id='$id'
    ");
}

/* =========================
   FETCH DATA
========================= */
if($role_id == 1)
{
    $data = mysqli_query($conn,"
    SELECT st.*, v.village_name 
    FROM special_thanks st
    LEFT JOIN villages v ON st.village_id=v.id
    ORDER BY st.id DESC
    ");
}
else
{
    $data = mysqli_query($conn,"
    SELECT st.*, v.village_name 
    FROM special_thanks st
    LEFT JOIN villages v ON st.village_id=v.id
    WHERE st.village_id='$village_id'
    ORDER BY st.id DESC
    ");
}
?>

<div class="main">

<h2>Special Thanks Management</h2>

<!-- GLOBAL TOGGLE -->
<form method="POST" style="margin-bottom:15px;">
    
    <?php if($role_id == 1){ ?>
        <p style="font-size:12px;color:#666;">Showing All Villages Data</p>
    <?php } ?>

    <label>
        <input type="checkbox" name="show_special_thanks_home" value="1"
        <?php if(!empty($village['show_special_thanks_home'])) echo "checked"; ?>>
        Show Special Thanks on Home Page
    </label>

    <button name="toggle_global" class="btn-save">Save</button>
</form>

<div style="overflow-x:auto;">

<table class="table">

<tr>
<th>Image</th>
<th>Name</th>
<th>Village</th>
<th>Show</th>
<th>Position</th>
<th>Order</th>
<th>Action</th>
</tr>

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

<tr>
<form method="POST">

<td>
<?php if($row['image']){ ?>
<img src="../../uploads/special_thanks/<?php echo $row['image']; ?>" width="50">
<?php } ?>
</td>

<td><?php echo htmlspecialchars($row['name']); ?></td>

<td><?php echo $row['village_name'] ?? '-'; ?></td>

<td>
<input type="checkbox" name="show_on_home"
<?php if($row['show_on_home']) echo "checked"; ?>>
</td>

<td>
<select name="position">
<option value="top" <?php if($row['position']=='top') echo "selected"; ?>>Top</option>
<option value="bottom" <?php if($row['position']=='bottom') echo "selected"; ?>>Bottom</option>
</select>
</td>

<td>
<input type="number" name="display_order" value="<?php echo $row['display_order']; ?>" style="width:60px;">
</td>

<td>

<input type="hidden" name="id" value="<?php echo $row['id']; ?>">

<!-- QUICK UPDATE -->
<button name="update_row" class="btn-save">Update</button>

<!-- EDIT BUTTON (NEW) -->
<a href="add_special_thanks.php?id=<?php echo $row['id']; ?>" class="btn-edit">Edit</a>

<!-- DELETE -->
<a href="delete_special_thanks.php?id=<?php echo $row['id']; ?>" 
class="btn-delete"
onclick="return confirm('Delete?')">Delete</a>

</td>

</form>
</tr>

<?php } ?>

</table>

</div>

</div>

<!-- STYLING -->
<style>
.btn-save{
    background:#28a745;
    color:#fff;
    padding:5px 10px;
    border:none;
    border-radius:4px;
    cursor:pointer;
}

.btn-edit{
    background:#007bff;
    color:#fff;
    padding:5px 10px;
    text-decoration:none;
    border-radius:4px;
    margin-left:5px;
}

.btn-delete{
    background:#dc3545;
    color:#fff;
    padding:5px 10px;
    text-decoration:none;
    border-radius:4px;
    margin-left:5px;
}
</style>

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