<?php

class Painel_model extends CI_Model {
    
    private $tb_emais = 'nwl_emails';
    private $tb_campanha = 'nwl_campanha_email';
    private $tb_log_email = 'nwl_log_emails';
    private $tb_desinscricao = 'nwl_desinscricao';
    private $tb_local_cadastro = 'nwl_local_cadastro';
    
   
    function get_arquivos($perfil=null)
    {
        /*if($perfil!=null && $perfil > 1)
            $this->db->where('tipo', 'Externo');*/
        
        $this->db->order_by('id_arquivo','desc');
        $query = $this->db->get('nwl_arquivo');
	
		if($query->num_rows() > 0)
		    return $query->result();
		return null;
        
        
    }
    
    function get_campanhas($perfil=null)
    {
        /*if($perfil!=null && $perfil > 1)
            $this->db->where('tipo', 'Externo');*/
        
        $this->db->order_by('id_campanha','asc');
        $query = $this->db->get('nwl_campanha_email');
	
		if($query->num_rows() > 0)
		    return $query->result();
		return null;
        
        
    }
    
    function select_qnt_status()
    {
        $return = array(0,0,0);
        $sql = "SELECT COUNT(id) qnt, `status` FROM nwl_emails GROUP BY `status`";
        $query =  $this->db->query($sql);
        if($query->num_rows() > 0)
        {
            $result = $query->result();
            foreach($result as $row)
            {
                if ($row->status == 0) {
                    $return[0] = $row->qnt;
                } elseif ($row->status == 1) {
                    $return[1] = $row->qnt;
                } elseif ($row->status == 2) {
                    $return[2] = $row->qnt;
                }
            }
        }
        return $return;
    }
    
    function select_emails()
	{  
		$query = $this->db->get( $this->tb_emais );
	
		if($query->num_rows() > 0)
		    return $query->result();
		return null;
	
	}
	
	function get_config_tb($perfil = null)
	{
	    /*if($perfil!=null && $perfil > 1)
            $this->db->where('tipo', 'Externo');*/
	    
	    $query = $this->db->get('nwl_config_tb_rel');
	
		if($query->num_rows() > 0)
		    return $query->result();
		return null;
	}
	
	function get_config_listas()
	{
	    $query = $this->db->get('nwl_config_listas');
	
		if($query->num_rows() > 0)
		    return $query->result();
		return null;
	}
	
    function get_emais($tb)
    {
        $this->db->select($tb->campo);
        $this->db->distinct($tb->campo);
        $this->db->where($tb->campo.' !=', null);
        $query = $this->db->get( $tb->tabela );
	
		if($query->num_rows() > 0)
		    return $query->result();
		return null;
    }
    
    
	function get_listas($tb)
    {
        $query = $this->db->get( $tb->tabela );
	
		if($query->num_rows() > 0)
		    return $query->result();
		return null;
    }
    
    
	function get_sql_lista($lista, $perfil = null)
    {
        
        
        
        /*if($lista->tipo == 'Externo')
        {
            $query = $this->db->query($lista->sql);
            if($query->num_rows() > 0)
            return $query->result();
            return null;
        }*/
        
        /*if($perfil!=null && $perfil > 1 )
        return null;*/
        if(!empty($lista->sql)){
            $query = $this->db->query($lista->sql);
                if($query->num_rows() > 0)
                return $query->result();
                
        }
        return null;
        
	    
    }
	
	
    
}