<?php 
require_once("db.php");
class fapi {    
    public function API(){
        header('Content-Type: application/JSON');
        $method = $_SERVER['REQUEST_METHOD'];
        $data="";
        
        switch ($method) {
        case 'GET'://consulta
            switch($_GET["pet"]){
                case "productos":
                    $data2 = (empty($_GET["data2"]))?null:$_GET["data2"];
                    $data= self::productos($_GET["act"],$_GET["data"], $data2);
                break;
            }            
        break;     
        default://metodo NO soportado
            $data= 'METODO NO SOPORTADO';
        break;
        }
        
        $data= json_encode($data);
        echo $data;
    }
    
    private function productos($act,$data,$subcat){
        
        try {
            if(isset($act) && isset($data)){
                if($act === "cat"){
                    if ($data === "all") {
                        // $subcat = "INDUCCION DE CRECIMIENTO";
                        $pdo= new dbCon();
                        $db= $pdo->connect();
                        $st= $db->prepare('SELECT * FROM productos WHERE subcategoria = :subcat');
                        
                        $st->bindParam(":subcat", $subcat, PDO::PARAM_STR);
                        $st->execute();  
                        while($row= $st->fetch(PDO::FETCH_ASSOC)){
                            $datos[]= $row;
                        }                        
                    } else if ($data === "nutrientes"){
                       $pdo= new dbCon();
                       $db= $pdo->connect();
                       $st= $db->prepare("SELECT subcategoria FROM productos WHERE categoria = 'NUTRIENTES' AND  status = 1 GROUP BY subcategoria ORDER BY subcategoria ASC");
                       
                       $st->execute();  
                       while($row= $st->fetch(PDO::FETCH_ASSOC)){
                        $datos[]= $row;
                        }                                        
                    } else if ($data === "organicos"){
                       $pdo= new dbCon();
                       $db= $pdo->connect();
                       $st= $db->prepare("SELECT subcategoria FROM productos WHERE categoria = 'DIVISION ORGANICA' AND  status = 1 GROUP BY subcategoria ORDER BY subcategoria ASC");
                       
                       $st->execute();  
                       while($row= $st->fetch(PDO::FETCH_ASSOC)){
                        $datos[]= $row;
                        }                            
                    }  else {
                       $pdo= new dbCon();
                       $db= $pdo->connect();
                       $st= $db->prepare("SELECT * FROM productos WHERE id_producto = :producto LIMIT 1");
                       $st->bindParam(":producto", $data, PDO::PARAM_STR);
                       $st->execute();  
                       while($row= $st->fetch(PDO::FETCH_ASSOC)){
                        $datos[]= $row;
                        }                            
                    }                
                }else if($act === "nutrientes"){
                    if ($data === "all") {
                        $pdo= new dbCon();
                        $db= $pdo->connect();
                        $st= $db->prepare("SELECT * FROM productos WHERE categoria like '%NUTRIENTES%' AND  status = 1 ORDER BY orden ASC");
                        // $st= $db->prepare("SELECT * FROM productos WHERE status = 1 ");
                        $st->execute();  
                        while($row= $st->fetch(PDO::FETCH_ASSOC)){
                            $datos[]= $row;
                        }                        
                    } else {
                       $pdo= new dbCon();
                       $db= $pdo->connect();
                       $st= $db->prepare("SELECT * FROM productos WHERE producto = '".$data."' LIMIT 1");
                       $st->execute();  
                       while($row= $st->fetch(PDO::FETCH_ASSOC)){
                        $datos[]= $row;
                        }                            
                    }                
                }else if($act === "organicos"){
                    if ($data === "all") {
                        $pdo= new dbCon();
                        $db= $pdo->connect();
                        $st= $db->prepare("SELECT * FROM productos WHERE categoria = 'DIVISION ORGANICA' AND  status = 1 ORDER BY producto ASC");
                        // $st= $db->prepare("SELECT * FROM productos WHERE status = 1 ");
                        $st->execute();  
                        while($row= $st->fetch(PDO::FETCH_ASSOC)){
                            $datos[]= $row;
                        }                        
                    } else {
                       $pdo= new dbCon();
                       $db= $pdo->connect();
                       $st= $db->prepare("SELECT * FROM productos WHERE producto = '".$data."' LIMIT 1");
                       $st->execute();  
                       while($row= $st->fetch(PDO::FETCH_ASSOC)){
                        $datos[]= $row;
                        }                            
                    }                
                }else if($act === "SAMSUNG"){
                    if ($data === "all") {
                        $pdo= new dbCon();
                        $db= $pdo->connect();
                        $st= $db->prepare("SELECT * FROM productos WHERE marca = 'Samsung Probes' ORDER BY finsert");
                        $st->execute();  
                        while($row= $st->fetch(PDO::FETCH_ASSOC)){
                            $datos[]= $row;
                        }                        
                    } else {
                       $pdo= new dbCon();
                       $db= $pdo->connect();
                       $st= $db->prepare("SELECT * FROM productos WHERE producto = '".$data."' LIMIT 1");
                       $st->execute();  
                       while($row= $st->fetch(PDO::FETCH_ASSOC)){
                        $datos[]= $row;
                        }                            
                    }                
                }
            }else{$datos = "La consulta solicitada no es correcta.";}
        } catch (PDOException $e) {
                // echo 'Error (0x00001): ' . $e->getMessage();
            $datos= "0 datos disponibles";
        }
        return $datos;
    }// end GE
}//end class
?>