<?php
$path="../../";
include("../../map.php");
$cpath="../../"; 
include("../../scripts.php");
include("../../secure.php");

if($_GET["q"]==""){
	$sql=mysqli_query($conn,"select * from ".$_SESSION['lang']." where id=".$_GET["id"]."");
	$rs=mysqli_fetch_assoc($sql);
	$risim=$rs["t1"];
}else{
	$risim=$_GET["q"];
}

function searchImagesOnPexels($keyword){
    $apiKey = "HKnBYC6ckwO0SYifayYWkBEUowRSWIIBIPETYUBLju0QwBFvbmnaHK0F";

    $url = "https://api.pexels.com/v1/search?"
         . http_build_query([
            "query"       => $keyword,
            "orientation" => "landscape",
            "per_page"    => 8
         ]);

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => [
            "Authorization: ".$apiKey
        ],
        CURLOPT_TIMEOUT => 30
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($response, true);

    $images = [];
    if(!empty($data['photos'])){
        foreach($data['photos'] as $photo){
            // 🔑 EK YOK – DÜZ JPG
            $images[] = $photo['src']['original'];
        }
    }

    return [
        'query'  => $keyword,
        'images' => $images
    ];
}

$result = searchImagesOnPexels($risim);

if(!empty($result['images'])){
    echo '<div class="container"><div class="row">';
    foreach($result['images'] as $image){
        $img = htmlspecialchars($image);

        echo '<div class="col-xs-6 col-md-3 col-item">';
        echo '<a href="?ne='.$_GET["ne"].'&islem='.$_GET["islem"].'&id='.$_GET["id"].'&durum=addimagewgoogle&src='.$img.'" 
              class="btn btn-warning"
              style="position:absolute; right:10px; top:10px; border:0; font-size:28px; padding:0">
              <i class="fa fa-plus-square" style="font-size:24px"></i></a>';

        echo '<a href="?ne='.$_GET["ne"].'&islem='.$_GET["islem"].'&id='.$_GET["id"].'&durum=addimagewgoogle&src='.$img.'">
              <img src="'.$img.'" style="max-width:100%; height:100%; object-fit:cover;"></a>';
        echo '</div>';
    }
    echo '</div></div>';
}else{
    echo 'Resim bulunamadı.';
}
?>
