<?php

/**
 * NoteModel
 * This is basically a simple CRUD (Create/Read/Update/Delete) demonstration.
 */
class GalleryModel
{
   
    public static function getImages()
    {
        HeaderModel::postMethod();
         
        $database = DatabaseFactory::getFactory()->getConnection();
        
        $sql  = "SELECT `id`";
        $sql .= " FROM `sf_images`";
	
	 $database = DatabaseFactory::getFactory()->getConnection();
	 $query = $database->prepare($sql);
	 $query->execute();
	 $totalData = $query->rowCount();
     $totalFiltered = $totalData;
        
        $data = json_decode(file_get_contents("php://input"));
       

    $limit = 20; // Number of images per page
    
    $offset = ($data->items - 1) * $limit;
    
    $sql = "SELECT * FROM sf_images WHERE 1=1";

    // Fetch images from the database with lazy loading
    if ($data && isset($data->category_id)) {
        
       $sql .= " AND category_id='".$data->category_id."'";
    }
    
    if ($data && isset($data->sub_category_id)) {
        
       $sql .= " AND sub_category_id='".$data->sub_category_id."'";
    }
    
    if($data && isset($data->colour_id)) {
        
         $sql .= " AND colour_id='".$data->colour_id."'";
    }
    
    if($data && isset($data->shap_id)) {
        
         $sql .= " AND shap_id LIKE '%".$data->shap_id."%'";
    }
    
    if($data && isset($data->search)) {
        
         $sql.=" AND ( title LIKE '%".$data->search."%' ";  
         $sql.=" OR title_ar LIKE '%".$data->search."%' ";
         $sql.=" OR description_ar LIKE '%".$data->search."%' ";
	     $sql.=" OR description LIKE '%".$data->search."%')";
         
   
    }
    
$query = $database->prepare($sql);
$query->execute();
$totalFiltered = $query->rowCount();
    
    
    
    $sql .= " ORDER BY id DESC LIMIT $offset,  $limit";
    
    
    
     $stmt = $database->prepare($sql);
     $stmt->execute();
     $images = $stmt->fetchAll();
    
    
     $pagesCount = ceil($totalFiltered / $limit);
   

    // Add lazy loading URL for each image
    foreach ($images as &$image) {
        $image->lazy_load_url = 'https://admin.deco4mural.com/dats/'.$image->file_name;
        $image->status = $image->shap_id;
        unset($image->file_name); // Remove original image URL from the response
        unset($image->shap_id);
        
    }
    
    
            $response['status_code_header'] = 'HTTP/1.1 200 ok';
            $response['message'] = 'Data Fetched Successfully';
            $response['total_data'] = $totalFiltered;
            $response['pageCount'] = $pagesCount;
            $response['limit'] = $limit;
            $response['data'] = $images;
            
            http_response_code(200);
            
            
            echo  json_encode($response);
            return true;


 


       
    }
    
    
    
     public static function getSubImages($image_id)
    {
        HeaderModel::postMethod();
        
        if(!$image_id) {
            
             $response['status_code_header'] = 'HTTP/1.1 404';
            $response['message'] = 'Data Not Found';
           
            $response['data'] = [];
            
            http_response_code(404);
            echo  json_encode($response);
            return false;
        }
         
        $database = DatabaseFactory::getFactory()->getConnection();
     
    $sql = "SELECT file_name FROM sf_subimages WHERE image_id='".$image_id."'";

   
$query = $database->prepare($sql);
$query->execute();

if($query->rowCount()>0) {
    
    $images = $query->fetchAll();
    
     // Add lazy loading URL for each image
    foreach ($images as &$image) {
        $img [] = 'https://admin.deco4mural.com/dats/'.$image->file_name;
        unset($image->file_name); // Remove original image URL from the response
    }

    
    
            $response['status_code_header'] = 'HTTP/1.1 200 ok';
            $response['message'] = 'Data Fetched Successfully';
           
            $response['data'] = $img;
            
            http_response_code(200);
            
            
            echo  json_encode($response);
            return true;
            
}

else {
    
    $response['status_code_header'] = 'HTTP/1.1 404';
            $response['message'] = 'Data Not Found';
           
            $response['data'] = [];
            
            http_response_code(404);
            echo  json_encode($response);
            return false;
    
    
}


 


       
    }
    
    
  
   
}