<?php

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

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


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

if($role_id == 1){
    $village_id = $_POST['village_id'] ?? 0;
}else{
    $village_id = $_SESSION['village_id'] ?? 0;
}

// 🔥 FUNCTION: Extract YouTube Video ID (ALL formats supported)
function getYouTubeId($url){
    preg_match(
        "/(youtu\.be\/|v=|\/embed\/|\/shorts\/)([a-zA-Z0-9_-]{11})/",
        $url,
        $matches
    );
    return $matches[2] ?? '';
}

if(isset($_POST['save'])){

    if($role_id == 1){
        $village_id = $_POST['village_id'];
    }

    $title = mysqli_real_escape_string($conn,$_POST['title']);
    $youtube_url = trim($_POST['youtube_url']);

    // 🔥 Extract Video ID
    $video_id = getYouTubeId($youtube_url);

    if(empty($title) || empty($youtube_url) || empty($video_id)){
        echo "<script>alert('Please enter valid Title and YouTube URL');</script>";
    }else{

        // 🔥 Generate Thumbnail
        $thumbnail = "https://img.youtube.com/vi/".$video_id."/0.jpg";

        mysqli_query($conn,"
        INSERT INTO village_videos
        (village_id,title,youtube_url,thumbnail,created_by)
        VALUES
        ('$village_id','$title','$youtube_url','$thumbnail','".$_SESSION['user_id']."')
        ");

        echo "<script>alert('Video Added Successfully');</script>";
    }

}

?>

<div class="main">
<div class="container-fluid">

<h2>Add Video</h2>

<form method="post">

<?php if($role_id == 1){ ?>

<label>District</label>
<select id="district" class="form-control">
<option value="">Select District</option>

<?php
$dq=mysqli_query($conn,"SELECT * FROM districts");
while($d=mysqli_fetch_assoc($dq)){
?>
<option value="<?php echo $d['id']; ?>">
<?php echo $d['district_name']; ?>
</option>
<?php } ?>
</select>

<label>Block</label>
<select id="block" class="form-control">
<option>Select Block</option>
</select>

<label>Village</label>
<select name="village_id" id="village" class="form-control" required>
<option>Select Village</option>
</select>

<?php } ?>

<label>Video Title</label>
<input type="text" name="title" class="form-control" required>

<label>YouTube URL</label>
<input type="text" name="youtube_url" class="form-control" placeholder="Paste YouTube link" required>

<br>

<button type="submit" name="save" class="btn btn-success">
Save Video
</button>

</form>

</div>
</div>

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

<script>

document.addEventListener("DOMContentLoaded", function(){

const district = document.getElementById("district");
const block = document.getElementById("block");
const village = document.getElementById("village");

if(district){

district.addEventListener("change", function(){

fetch("get_blocks.php?district_id="+this.value)
.then(res=>res.text())
.then(data=>{
block.innerHTML=data;
village.innerHTML="<option>Select Village</option>";
});

});

}

if(block){

block.addEventListener("change", function(){

fetch("get_villages.php?block_id="+this.value)
.then(res=>res.text())
.then(data=>{
village.innerHTML=data;
});

});

}

});

</script>