<?php
    require_once 'Categoria.php';
    require_once RUTA_APP.'Conexion.php';
    class CategoriaDatos{
        public static function getCategoriaById(int $id):?Categoria{
            $retorno = null;
            $con = getConnection();
            $query = "SELECT id, CRM, Nombre, Posicion, Visible,Contraido FROM dialvox_crm_categoria WHERE id = '".limpia($id)."'";
            $result = sv_mysqli_query($con,$query);
            if($row = sv_mysqli_fetch_array($result)){
                $retorno = new Categoria($row["id"], CRMDatos::getCRMById($row["CRM"]), $row["Nombre"], $row["Posicion"], $row["Contraido"], $row["Visible"]);
            }
            sv_mysqli_close($con);
            return $retorno;
        }
        public static function addCategoria(Categoria $categoria):int{
            $retorno = 0;
            $con = getConnection();
            $query = "INSERT INTO dialvox_crm_categoria(CRM, Nombre, Posicion, Visible, Contraido) VALUES ('".limpia($categoria->getCRM()->getId())."','".limpia($categoria->getNombre())."','".limpia($categoria->getPosicion())."','".limpia($categoria->getVisible())."','".limpia($categoria->getContraido())."')";
            if($result = sv_mysqli_query($con, $query)){
                $retorno = mysqli_insert_id($con);
            }
            sv_mysqli_close($con);
            return $retorno;
        }
        public static function updateCategoria(Categoria $categoria):int{
            $retorno = 0;
            $con = getConnection();
            $query = "UPDATE dialvox_crm_categoria SET Contraido = '".limpia($categoria->getContraido())."',CRM='".limpia($categoria->getCRM()->getId())."',Nombre='".limpia($categoria->getNombre())."',Posicion='".limpia($categoria->getPosicion())."',Visible='".limpia($categoria->getVisible())."' WHERE id = '".limpia($categoria->getId())."'";
            if($result = sv_mysqli_query($con,$query)){
                $retorno = 1;
            }
            sv_mysqli_close($con);
            return $retorno;
        }
        public static function deleteCategoria(Categoria $categoria):int{
            $retorno = 0;
            $con = getConnection();
            $query = "UPDATE dialvox_crm_categoria SET Visible = FALSE WHERE id = '".limpia($categoria->getId())."'";
            if($result = sv_mysqli_query($con,$query)){
                $retorno = 1;
            }
            sv_mysqli_close($con);
            return $retorno;
        }
        public static function getCategoriasByCRM(CRM $crm):array{
            $retorno = array();
            $con = getConnection();
            $query = "SELECT id FROM dialvox_crm_categoria WHERE CRM = '".limpia($crm->getId())."' AND Visible = TRUE ORDER BY Posicion";
            if($result = sv_mysqli_query($con,$query)){
                while($row = sv_mysqli_fetch_array($result)){
                    $retorno[] = self::getCategoriaById($row["id"]);
                }
            }
            sv_mysqli_close($con);             
            return $retorno;
        }
        public static function listCategoriasByCRM(CRM $crm):array{
            $retorno = array();
            $con = getConnection();
            $query = "SELECT id,Nombre FROM dialvox_crm_categoria WHERE CRM = '".limpia($crm->getId())."' AND Visible = TRUE";
            if($result = sv_mysqli_query($con,$query)){
                while($row = sv_mysqli_fetch_array($result)){
                    $retorno[] = new Categoria($row["id"],$crm,$row["Nombre"],-1,false,true);
                }
            }
            sv_mysqli_close($con);             
            return $retorno;
        }
        public static function countCategoriasByCRM(CRM $crm):int{
            $retorno = 0;
            return $retorno;
        }
    }
?>