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

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

if($role_id == 1){
$anganwadi=mysqli_query($conn,"
SELECT a.*,v.village_name
FROM village_anganwadi a
LEFT JOIN villages v ON a.village_id=v.id
ORDER BY a.id DESC
");
}else{
$village_id=$_SESSION['village_id'];

$anganwadi=mysqli_query($conn,"
SELECT a.*,v.village_name
FROM village_anganwadi a
LEFT JOIN villages v ON a.village_id=v.id
WHERE a.village_id='$village_id'
ORDER BY a.id DESC
");
}
?>

<style>
.toast{
position:fixed;top:20px;left:50%;
transform:translateX(-50%);
background:#28a745;color:#fff;
padding:10px 20px;border-radius:8px;
display:none;z-index:9999;
}
.modal{
position:fixed;top:0;left:0;
width:100%;height:100%;
background:rgba(0,0,0,0.5);
display:none;justify-content:center;align-items:center;
z-index:9999;
}
.modal-content{
background:#fff;padding:20px;border-radius:10px;
width:90%;max-width:400px;
}
</style>

<div class="toast" id="toastMsg"></div>

<div class="card-box" style="display:flex;justify-content:space-between;">
<h4>Village Anganwadi</h4>
<a href="add_anganwadi.php" class="btn-mobile btn-add"><i class="fa fa-plus"></i></a>
</div>

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

<div class="card-box" id="row_<?php echo $row['id']; ?>">

<?php if($row['image']){ ?>
<img src="../../uploads/anganwadi/<?php echo $row['image']; ?>" class="anganwadi-img">
<?php } ?>

<h5><?php echo $row['anganwadi_name']; ?></h5>
<p>📍 <?php echo $row['village_name']; ?></p>
<p><?php echo $row['location']; ?></p>

<div style="display:flex;gap:10px;">

<button onclick="openEdit(
<?php echo $row['id']; ?>,
'<?php echo addslashes($row['anganwadi_name']); ?>',
'<?php echo addslashes($row['location']); ?>',
'<?php echo addslashes($row['description']); ?>',
'<?php echo $row['image']; ?>'
)" class="btn btn-primary">Edit</button>

<button onclick="deleteRow(<?php echo $row['id']; ?>)" class="btn btn-danger">Delete</button>

</div>

</div>

<?php } ?>

<!-- MODAL -->
<div class="modal" id="editModal">
<div class="modal-content">

<h4>Edit Anganwadi</h4>

<form id="editForm" enctype="multipart/form-data">

<input type="hidden" name="id" id="edit_id">

<input type="text" name="anganwadi_name" id="edit_name" class="form-control">
<input type="text" name="location" id="edit_location" class="form-control">

<input type="file" name="image" id="edit_image" class="form-control">

<img id="preview" style="width:100%;margin-top:10px;display:none;border-radius:8px;">

<textarea name="description" id="edit_description" class="form-control"></textarea>

<br>

<button type="submit" class="btn btn-success">Update</button>
<button type="button" onclick="closeModal()" class="btn btn-secondary">Close</button>

</form>

</div>
</div>

<script>

/* TOAST */
function showToast(msg){
let t=document.getElementById("toastMsg");
t.innerHTML=msg;
t.style.display="block";
setTimeout(()=>t.style.display="none",2000);
}

/* OPEN MODAL */
function openEdit(id,name,location,description,image){

document.getElementById("edit_id").value=id;
document.getElementById("edit_name").value=name;
document.getElementById("edit_location").value=location;
document.getElementById("edit_description").value=description;

let preview=document.getElementById("preview");

if(image){
preview.src="../../uploads/anganwadi/"+image;
preview.style.display="block";
}else{
preview.style.display="none";
}

document.getElementById("editModal").style.display="flex";
}

function closeModal(){
document.getElementById("editModal").style.display="none";
}

/* IMAGE COMPRESS */
let compressedFile=null;

document.getElementById("edit_image").addEventListener("change",function(){

let file=this.files[0];
if(!file) return;

let img=new Image();
img.src=URL.createObjectURL(file);

img.onload=function(){

let canvas=document.createElement("canvas");
let ctx=canvas.getContext("2d");

let maxWidth=800;
let scale=maxWidth/img.width;

canvas.width=maxWidth;
canvas.height=img.height*scale;

ctx.drawImage(img,0,0,canvas.width,canvas.height);

canvas.toBlob(function(blob){
compressedFile=new File([blob],file.name,{type:"image/jpeg"});
});
};

});

/* UPDATE */
document.getElementById("editForm").addEventListener("submit",function(e){

e.preventDefault();

let formData=new FormData(this);

if(compressedFile){
formData.set("image",compressedFile);
}

fetch("update_anganwadi_ajax.php",{method:"POST",body:formData})
.then(res=>res.json())
.then(res=>{
showToast("✅ Updated");
closeModal();
location.reload();
});

});

/* DELETE */
function deleteRow(id){

if(!confirm("Delete this record?")) return;

fetch("delete_anganwadi_ajax.php?id="+id)
.then(res=>res.json())
.then(res=>{
document.getElementById("row_"+id).remove();
showToast("🗑 Deleted");
});

}

</script>

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