<?php
defined("_JEXEC") or die();
jimport("joomla.application.component.model");
jimport("joomla.application.component.helper");
//require_once JPATH_COMPONENT.'/models/correo/class.phpmailer.php';
use Joomla\CMS\Factory;
class ProdModelMProd  
{
    public function getview($viewin)
    {
        $input = Factory::getApplication()->input;
       // $view = ;
    if($viewin !=''){$view = $viewin; }else{$view=$input->get("view");}
        $VPATH = JPATH_COMPONENT . "/views/v" . $view . ".php";
        return $VPATH;
    }
     public function prod($objRequest){
           $input = Factory::getApplication()->input;
           $obj = new \stdClass();
	       $obj->objDatos=$objRequest->objDatos; 
       
        return $obj;
       }
       public function allData($objRequest){
           $input = Factory::getApplication()->input;
           $obj = new \stdClass();
	       $obj->objDatos=$objRequest->objDatos; 
        
        return $obj;
       }
         public function buscar($objRequest){
           $input = Factory::getApplication()->input;
           $obj = new \stdClass();
	       $obj->objDatos=$objRequest->objDatos; 
           $q = $input->get('q', '', 'RAW');
           $idiomaParaFiltrar = $input->get('lang') ?? '*';
           
            $obj->busqueda = $this->busqueda($objRequest,$q,$idiomaParaFiltrar); 
        return $obj;
       }
     public function busqueda($objRequest, $q, $lang = '*') {
    $DATA = $objRequest->objDatos;
    $pdo = new PDO("mysql:host=" . $DATA->server . ";dbname=" . $DATA->dbname . ";charset=utf8mb4", $DATA->dbuser, $DATA->dbpass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $searchTerm = "%" . $q . "%";
    $items = []; 
    $idiomaParaFiltrar = $_POST['lang'] ?? '*';
    $sql = "SELECT 
                c.id, 
                c.title, 
                c.alias, 
                c.metadesc,
                c.language,
                cat.path, 
                MAX(m.id) AS ItemId
            FROM z_content AS c
            INNER JOIN z_categories AS cat ON c.catid = cat.id
            LEFT JOIN z_menu AS m ON TRIM(m.link) = CONCAT('index.php?option=com_content&view=article&id=', c.id)
    AND m.published = 1
    AND m.client_id = 0
    AND (m.language = c.language OR m.language = '*')
            WHERE (c.title LIKE ? OR c.introtext LIKE ? OR c.`fulltext` LIKE ?)
              AND c.state = 1
              AND (c.language = ? OR c.language = '*') -- Filtra por idioma o 'Todos'
            GROUP BY c.id, c.title, c.alias, c.metadesc, c.language, cat.path  ";  

    try {
        $stmt = $pdo->prepare($sql);
        // Pasamos el término 3 veces y el idioma 1 vez
        $stmt->execute([$searchTerm, $searchTerm, $searchTerm, $lang]);
        $items = $stmt->fetchAll(PDO::FETCH_OBJ);
    } catch (PDOException $e) {
        error_log("Error en la consulta: " . $e->getMessage());
    }

    $obj = new \stdClass();
    $obj->items = $items;
    $obj->q = $q;
     $obj->lang = $lang;
    
    return $obj;
}
     public function sitemap($objRequest){
           $input = Factory::getApplication()->input;
           $obj = new \stdClass();
	       $DATA=$objRequest->objDatos; 
           $pdo = new PDO("mysql:host=" . $DATA->server . ";dbname=" . $DATA->dbname . ";charset=utf8mb4", $DATA->dbuser, $DATA->dbpass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
    // 1. Definir la consulta con un marcador (placeholder)
    $sql = "SELECT alias FROM z_content WHERE language = :lang";
    $stmt = $pdo->prepare($sql);

    // 2. Ejecutar pasando el valor del parámetro
    $lang = 'es-ES';
    $stmt->execute(['lang' => $lang]);

    // 3. Obtener los resultados
    $obj->sitemap = $stmt->fetchAll(PDO::FETCH_ASSOC);



} catch (PDOException $e) {
    echo "Error en la consulta: " . $e->getMessage();
}
        return $obj;
       }
     public function getIp($data){
            $ip= $data['ip'];
           switch ($ip) {
    case "86.127.243.178":
     $ok = true;
        break;
        default:
        $ok = true;
 }
 return $ok;

       }
     
}

/********************************

***************************/