<?php

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

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

/* DELETE VIDEO */
if(isset($_GET['delete'])){

    $id = intval($_GET['delete']);

    $check = mysqli_fetch_assoc(mysqli_query($conn,"
        SELECT * FROM village_videos WHERE id='$id'
    "));

    if($check){

        if($role_id != 1 && $check['village_id'] != $_SESSION['village_id']){
            echo "<script>alert('Unauthorized Action');</script>";
        }else{

            mysqli_query($conn,"DELETE FROM village_videos WHERE id='$id'");

            echo "<script>
            alert('Video Deleted Successfully');
            window.location='video_list.php';
            </script>";
            exit;
        }

    }else{
        echo "<script>alert('Video not found');</script>";
    }
}

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>";

/* FETCH DATA */
if($role_id == 1){

$videos=mysqli_query($conn,"
SELECT v.*,vi.village_name
FROM village_videos v
LEFT JOIN villages vi ON v.village_id=vi.id
ORDER BY v.id DESC
");

}else{

$village_id=$_SESSION['village_id'];

$videos=mysqli_query($conn,"
SELECT v.*,vi.village_name
FROM village_videos v
LEFT JOIN villages vi ON v.village_id=vi.id
WHERE v.village_id='$village_id'
ORDER BY v.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;">Village Videos</h4>
<p style="font-size:12px;color:#777;margin:0;">YouTube video gallery</p>
</div>

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

</div>

<!-- LIST -->

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

<?php while($row=mysqli_fetch_assoc($videos)){

preg_match(
"/(youtu\.be\/|v=|\/embed\/|\/shorts\/)([a-zA-Z0-9_-]{11})/",
$row['youtube_url'],
$matches
);

$video_id = $matches[2] ?? '';

?>

<div class="card-box">

<!-- VIDEO -->
<?php if($video_id){ ?>
<iframe 
width="100%" 
height="180"
src="https://www.youtube.com/embed/<?php echo $video_id; ?>"
style="border-radius:10px;margin-bottom:10px;"
frameborder="0" allowfullscreen>
</iframe>
<?php } ?>

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

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

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

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

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