<?php
use application\models\MyModel;
use application\entities\hclinica\UsuarioEnt;

//namespace proyectos\application\models\hclinica;

class UsuarioModel extends MyModel
{
    public function __construct()
    {
        parent::__construct();
        $this->db_server=$this->conexion;
        $this->table="usuario";
        $this->idTable="idUsuario";
        $this->camposEncrypt=array("user","pass");
    }
    public function guardarUsuario($usuario)
    {
        $this->guardarObjectoEncrypt($usuario);
        //echo $this->cadEncrypt($usuario->user);
//         $this->db_server->set('user', $this->cadEncrypt($usuario->user),FALSE);
//         $this->db_server->set('pass', $this->cadEncrypt($usuario->pass),FALSE);
//         $this->db_server->set('status', 1);
        //$this->db_server->insert($this->table);
        //$data=array("user"=>"mi usuario","pass"=>$this->db_server->escape_str($this->cadEncrypt("123")),"status"=>1);
        //$sql=$this->db_server->set($data)->get_compiled_insert($this->table);
        //echo $sql;
        
        //$this->db_server->query("INSERT INTO {$this->table}(user,pass,status) VALUES(".$this->cadEncrypt("David").",".$this->cadEncrypt("password").",1)");
    }
    public function getArrayUsuarioByLogin($login)
    {
        $this->db_server->select("idUsuario,".$this->getStmtDecrypt(array("user","pass")).",idPersona,status");
        $this->db_server->from($this->table);
        $this->db_server->where($this->cadDecrypt("user"),$login);
        $this->db_server->where("status",1);
        
        $res=$this->db_server->get();
        return $res->row_array();
    }
    public function getUsuarioById($idUsuario,$assoc=false)
    {
        $this->db_server->select("idUsuario,".$this->getStmtDecrypt(array("login","pass","idEmpresa")).",idPersona,idSucursal,status");
        $this->db_server->from($this->table);
        $this->db_server->where("idUsuario",$idUsuario);
        $this->db_server->where("status",1);
        $res=$this->db_server->get();
        if($assoc){
            return $res->row_array();
        }else{
            return current($res->custom_result_object(UsuarioEnt::tipo()));
        }
    }
    public function getItemUsuario($idUsuario)
    {
        $this->db_server->select("idUsuario,".$this->getStmtDecrypt(array("login","pass","idEmpresa")).",usuario.idPersona,idSucursal,".$this->getStmtDecrypt($this->camposEncrypt(),"persona"));
        $this->db_server->from($this->table);
        $this->db_server->join("persona","usuario.idPersona=persona.idPersona","inner");
        $this->db_server->where("idUsuario",$idUsuario);
        $res=$this->db_server->get();
        return $res->row_array();
    }
    private function camposEncrypt()
    {
        return array("rfc","nombre","apa","ama","tipo","telefono","celular","email");
    }
    public function getListUsuario()
    {
        $this->db_server->select("idUsuario,".$this->getStmtDecrypt(array("user","pass")));
        $this->db_server->from($this->table);
        $this->db_server->join("persona","usuario.idPersona=persona.idPersona","inner");
        $this->db_server->where("status",1);
        $res=$this->db_server->get();
        return $res->result_array();
    }
    /*public function getListUsuario($assoc=false)
    {
        $sql="SELECT * FROM {$this->table} WHERE status=?";
        if($assoc){
            return (array)$this->getListRowsAssoc($sql,array(1));
        }else{
            return (array)$this->getListaObjetos($sql, array(1), UsuarioEnt::tipo());
        }
        $this->db_server->select("idUsuario,".$this->getStmtDecrypt(array("login","pass")).",".$this->getStmtDecrypt($this->camposEncrypt(),"persona"));
        $this->db_server->from($this->table);
        $this->db_server->join("persona","usuario.idPersona=persona.idPersona","inner");
        $this->db_server->where("status",1);
        $res=$this->db_server->get();
        return $res->result_array();
        
    }*/
}

