<?php

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

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


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

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

// 🔥 FETCH EXISTING DATA
$video = mysqli_fetch_assoc(mysqli_query($conn,"
SELECT * FROM village_videos WHERE id='$id'
"));

// सुरक्षा check
if(!$video){
    echo "<script>alert('Video not found'); window.location='videos.php';</script>";
    exit;
}

// 🔒 Village restriction for village admin
if($role_id != 1 && $video['village_id'] != $_SESSION['village_id']){
    echo "<script>alert('Unauthorized access'); window.location='video_list.php';</script>";
    exit;
}

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

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

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

    $video_id = getYouTubeId($youtube_url);

    if(empty($title) || empty($youtube_url) || empty($video_id)){
        echo "<script>alert('Enter valid details');</script>";
    }else{

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

        mysqli_query($conn,"
        UPDATE village_videos SET
        village_id='$village_id',
        title='$title',
        youtube_url='$youtube_url',
        thumbnail='$thumbnail'
        WHERE id='$id'
        ");

        echo "<script>alert('Video Updated Successfully'); window.location='videos.php';</script>";
    }

}

?>

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

<h2>Edit 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 value="<?php echo $video['village_id']; ?>">Selected Village</option>
</select>

<?php } ?>

<label>Video Title</label>
<input type="text" name="title" class="form-control" value="<?php echo $video['title']; ?>" required>

<label>YouTube URL</label>
<input type="text" name="youtube_url" class="form-control" value="<?php echo $video['youtube_url']; ?>" required>

<br>

<button type="submit" name="update" class="btn btn-success">
Update 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;
});

});

}

if(block){

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

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

});

}

});

</script>