<?php
/**
* 
*/
class Mpersona extends CI_Model
{
	
	function __construct()
	{
		parent::__construct();
	}

	public function guardar($param){
		$campos = array(
			'dni' => $param['dni'],
			'nombre' => $param['nombre'],
			'appaterno' => $param['appaterno'],
			'apmaterno' => $param['apmaterno'],
			'email' => $param['email'],
			'fecnac' => date('Y-m-d',strtotime(str_replace('/','-',$param['fecnac'])))
		);

		$this->db->insert('persona',$campos);

		return $this->db->insert_id();
	}

	public function actualizarDatos($param){
		$campos = array(
			'nombre' => $param['nombre'],
			'appaterno' => $param['appaterno'],
			'apmaterno' => $param['apmaterno'],
			'email' => $param['email']
		);
		$this->db->where('idPersona',$this->session->userdata('s_idPersona'));
		$this->db->update('persona',$campos);
		
		return 1;
	}

	public function eliminarPersona($idP){
		$campos = array(
			'idPersona' => $idP
			);

		$this->db->delete('persona',$campos);

	}

	// public function getPersonas(){
	// 	$this->db->select('p.idPersona rownum, p.idPersona,p.nombre,p.appaterno app,p.apmaterno,p.dni,c.ciudad,p.email,p.intEstado');
	// 	$this->db->from('persona p');
	// 	$this->db->join('ciudades c','c.idCiudad = p.idCiudad');

	// 	$r = $this->db->get();

	// 	return $r->result();
	// }

	public function getPersonas($start,$length,$search){//server side processing

		$srch = "";
		if ($search) {
			$srch = " WHERE (p.nombre LIKE '%".$search."%' OR 
							p.appaterno LIKE '%".$search."%' OR
							p.apmaterno LIKE '%".$search."%' OR
							p.dni LIKE '%".$search."%' OR
							c.ciudad LIKE '%".$search."%' OR
							p.email LIKE '%".$search."%') ";
		}

		$qnr = "
			SELECT count(1) cant
			FROM persona p
			INNER JOIN ciudades c ON c.idCiudad = p.idCiudad
		".$srch;

		$qnr = $this->db->query($qnr);
		$qnr = $qnr->row();
		$qnr = $qnr->cant;


		$q = "
			SELECT p.idPersona rownum, p.idPersona,p.nombre,p.appaterno app,p.apmaterno,p.dni,c.ciudad,p.email,p.intEstado
			FROM persona p
			INNER JOIN ciudades c ON c.idCiudad = p.idCiudad
			
		".$srch." LIMIT $start,$length ";

		$r = $this->db->query($q);

		$retornar = array(
			'numDataTotal' => $qnr,
			'datos' => $r
			);

		return $retornar;
	}

	public function updPersona($param){
		$campos = array(
			'nombre' => $param['nombre'],
			'appaterno' => $param['appaterno'],
			'apmaterno' => $param['apmaterno'],
			'email' => $param['email']
		);
		$this->db->where('idPersona', $param['idPersona']);
		$this->db->update('persona',$campos);
		
		return 1;
	}
}