<?php
// DB connection here, e.g., $conn = new mysqli(...);
$servername = "localhost";
$username = "adventure_db";
$password = "AnjYWAYPkEXhBBsx";
$dbname = "adventure_db";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
if (isset($_POST['PrAction']) && $_POST['PrAction'] === 'BllaAction') {
    ini_set('display_errors', 1); // Hide from user
    ini_set('log_errors', 1);
    
    error_reporting(E_ALL);
    $oid = $_POST['oid'] ?? 0;
    $tid = $_POST['tid'] ?? 0;
    $uploadDir = $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/adventure-republiq/operator/uploads/";
    $fileName = $tid.'_'.basename($_FILES['file']['name']);
    $uploadFilePath = $uploadDir . $fileName;
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath)) {
        // Insert/update DB
     
        $select = $conn->query("SELECT * FROM tour_images WHERE oid='$oid' AND tid='$tid' AND imgid='1'");
        if ($select && $select->num_rows > 0) {
            $row = $select->fetch_assoc();
            $images = $row['tourimages'] ? $row['tourimages'] . ',' . $fileName : $fileName;
            $conn->query("UPDATE tour_images SET tourimages='$images' WHERE oid='$oid' AND tid='$tid' AND imgid=1");
        } else {
            $conn->query("INSERT INTO tour_images (oid,tid,tourimages,imgid) VALUES ('$oid','$tid','$fileName','1')");
        }
        echo "Upload Success";
    } else {
        echo "Upload Failed: " . $_FILES['file']['error'];
    }
    exit;
}
if (isset($_POST['PrAction']) && $_POST['PrAction'] === 'KhurkAction') {
    $oid = $_POST['oid'] ?? 0;
    $tid = $_POST['tid'] ?? 0;

    // Check if file is uploaded
    if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
        echo "Upload Error!";
        exit;
    }

    $uploadDir = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/themes/adventure-republiq/operator/uploads/";

    // Ensure upload directory exists
    if (!is_dir($uploadDir)) {
        mkdir($uploadDir, 0755, true);
    }

    // Sanitize file name
    $originalName = basename($_FILES['file']['name']);
    $fileName = $tid . "_" . preg_replace("/[^A-Za-z0-9\.\-_]/", "", $originalName);
    $uploadFilePath = $uploadDir . $fileName;

    // Move file to upload folder
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath)) {

        // Check for existing record
        $getaddons = $conn->query("SELECT * FROM tour_images WHERE oid = '$oid' AND tid = '$tid' AND imgid = '2'");

        if ($getaddons && $getaddons->num_rows > 0) {
            $fetchAddons = $getaddons->fetch_assoc();
          
            $newtourimages = $fetchAddons['tourimages'] ? $fetchAddons['tourimages'] . ',' . $fileName : $fileName;
           

            $sqlopdt = "UPDATE tour_images 
                        SET tourimages = '$newtourimages' 
                        WHERE oid = '$oid' AND tid = '$tid' AND imgid = '2'";
            $conn->query($sqlopdt);

            echo "Image updated successfully.";
        } else {
            $sql = "INSERT INTO tour_images (oid, tid, tourimages, imgid) 
                    VALUES ('$oid', '$tid', '$fileName', '2')";
            $conn->query($sql);

            echo "Image inserted successfully.";
        }

    } else {
        echo "Failed to upload file.";
    }
}
if (isset($_POST['PrAction']) && $_POST['PrAction'] === 'KalaAction') {


error_reporting(E_ALL);
$oid = $_POST['oid'] ?? 0;
$tid = $_POST['tid'] ?? 0;
    $uploadDir =$_SERVER['DOCUMENT_ROOT']."/wp-content/themes/adventure-republiq/operator/uploads/"; 
     $fileName =$tid.$_FILES['file']['name'];
     $uploadFilePath = $uploadDir.$fileName; 
    
   if(move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath)){ 
     // Insert file information in the database 
       $getaddons=$conn->query("select * from tour_images where oid='$oid' and  tid='$tid' and imgid= '3'");
       $countData= $getaddons->num_rows;
       $fetchAddons=$getaddons -> fetch_assoc();
       if($countData>0)
        {
            $newtourimages = $fetchAddons['tourimages'] ? $fetchAddons['tourimages'] . ',' . $fileName : $fileName;
           
               $sqlopdt = "UPDATE tour_images  SET 
                           tourimages='$newtourimages'
                           WHERE oid='$oid' and  tid='$tid' and imgid= '3'";
                           $conn->query($sqlopdt);
        }
        else
        {
                $sql = "INSERT INTO tour_images (oid,tid,tourimages,imgid) VALUES ('$oid','$tid','$fileName','3')"; 
                $conn->query($sql);
        }
 }
} 


// Optional: process other Actions here (e.g., delete, accommodation, etc.)
?>
