<?php
@session_start();
class Xhc extends MX_Controller{
	public function __construct(){
		if(!isset($_SESSION["app.gxc_sesion_id"]) || $_SESSION["app.gxc_sesion_id"]==0){
 			header('Location: '.base_url().'main/no_conectado');
 		}
		parent::__construct();
		$this->load->model('maestras/xhc_model');
		$this->load->model('seguridad/xppo_model');
		date_default_timezone_set('America/Lima');
	}

	public function index(){
		if($this->xppo_model->ver($_SESSION['app.gxc_sesion_id'],9)){
			$titulo 			= $_SESSION['app.gxc_sesion_titulo'];
 			$data['titulo']		= 'H/C :. '.$titulo;
 			$datac['titulo']	= $titulo;
 			$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 			$datam['menu']		= 'xhc';
 			$data['menu']		= $this->load->view('main/menu', $datam, TRUE);
 			$data['pie']		= $this->load->view('main/pie', '', TRUE);
			$this->load->view('xhc/listar', $data);
		}else{
			$titulo 			= $_SESSION['app.gxc_sesion_titulo'];
 			$data['titulo']		= $titulo;
 			$datac['titulo']	= $titulo;
 			$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 			$data['menu']		= $this->load->view('main/menu', '', TRUE);
 			$data['pie']		= $this->load->view('main/pie', '', TRUE);
			$this->load->view('main/sin_permiso', $data);
		}
	}

	public function listar(){
		$html 		= '';
		$texto 		= mb_convert_case($this->input->post('texto'), MB_CASE_UPPER, 'utf-8');
		if($texto!=''){
			$ds 		= $this->xhc_model->listar($texto);
			foreach ($ds->result() as $row) {
				$html.= '
					<tr>
						<td>'.$row->hc_id.'</td>
						<td>'.$row->hc_apenombres.'</td>
						<td>'.str_pad($row->hc_dni, 8, '0', STR_PAD_LEFT).'</td>
						<td>'.$row->hc_celular.'</td>
						<td>'.$row->hc_correo.'</td>
						<td>
							<a href="'.base_url('maestras/xhc/ver/'.$row->hc_id).'" class="btn btn-success btn-xs">
								<span class="glyphicon glyphicon-edit"></span> <b>Editar</b>
							</a>
							<a href="'.base_url('movimientos/xan/todo/'.$row->hc_id).'" target="_blank" class="btn btn-primary btn-xs">
								<span class="glyphicon glyphicon-edit"></span> <b>Atenciones</b>
							</a>
							<!--<a href="javascript:fn_xhc_eliminar('.$row->hc_id.', \''.$row->hc_apenombres.'\');" class="btn btn-danger btn-xs">
								<span class="glyphicon glyphicon-trash"></span> <b>Eliminar</b>
							</a>-->
						</td>
					</tr>
				';
			}
		}
		echo json_encode(array($html));
	}

	public function nuevo(){
		if($this->xppo_model->ver($_SESSION['app.gxc_sesion_id'],9)){
			$titulo 			= $_SESSION['app.gxc_sesion_titulo'];
 			$data['titulo']		= 'NUEVA H/C :. '.$titulo;
 			$datac['titulo']	= $titulo;
 			$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 			$datam['menu']		= 'xhc_n';
 			$data['menu']		= $this->load->view('main/menu', $datam, TRUE);
 			$data['pie']		= $this->load->view('main/pie', '', TRUE);
			$this->load->view('xhc/nuevo', $data);
		}else{
			$titulo 			= $_SESSION['app.gxc_sesion_titulo'];
 			$data['titulo']		= $titulo;
 			$datac['titulo']	= $titulo;
 			$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 			$data['menu']		= $this->load->view('main/menu', '', TRUE);
 			$data['pie']		= $this->load->view('main/pie', '', TRUE);
			$this->load->view('main/sin_permiso', $data);
		}
	}

	public function insertar(){
		$apenombres 	= mb_convert_case(trim($this->input->post('txt_apenombres')), MB_CASE_UPPER, 'utf-8');
		$direccion 		= mb_convert_case(trim($this->input->post('txt_direccion')), MB_CASE_UPPER, 'utf-8');
		$dni 			= $this->input->post('txt_dni');
		$celular 		= $this->input->post('txt_celular');
		//$celular2 		= $this->input->post('txt_celular2');
		$correo 		= $this->input->post('txt_correo');
		//$correo2 		= $this->input->post('txt_correo2');
		$telefono 		= $this->input->post('txt_telefono');
		$fechanac 		= $this->util->fecha_ingles($this->input->post('txt_fecha_nac'));
		//$telefono2 		= $this->input->post('txt_telefono2');
		$dsv 			= $this->xhc_model->validar($dni);
		if(!$dsv){
			$data 		= array('hc_apenombres' => $apenombres,
								'hc_direccion'	=> $direccion,
								'hc_dni'		=> $dni,
								'hc_fecha_nac'	=> $fechanac,
								'hc_celular'	=> $celular,
								'hc_correo'		=> $correo,
								'hc_telefono'	=> $telefono);
			$this->xhc_model->insertar($data);
			echo 0;
		}else
			echo 1;
	}

	public function ver($id=0){
		if($id>0){
			if($this->xppo_model->ver($_SESSION['app.gxc_sesion_id'],9)){
				$data['ds']	= $this->xhc_model->ver($id);
				$titulo 			= $_SESSION['app.gxc_sesion_titulo'];
 				$data['titulo']		= 'EDITAR H/C :. '.$titulo;
 				$datac['titulo']	= $titulo;
 				$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 				$datam['menu']		= 'xhc_n';
 				$data['menu']		= $this->load->view('main/menu', $datam, TRUE);
 				$data['pie']		= $this->load->view('main/pie', '', TRUE);
				$this->load->view('xhc/ver', $data);
			}else{
				$titulo 			= $_SESSION['app.gxc_sesion_titulo'];
 				$data['titulo']		= $titulo;
 				$datac['titulo']	= $titulo;
 				$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 				$data['menu']		= $this->load->view('main/menu', '', TRUE);
 				$data['pie']		= $this->load->view('main/pie', '', TRUE);
				$this->load->view('main/sin_permiso', $data);
			}
		}else
			header('Location: '.base_url('main/consola'));
	}

	public function editar(){
		$id 			= $this->input->post('txt_id');
		$apenombres 	= mb_convert_case(trim($this->input->post('txt_apenombres')), MB_CASE_UPPER, 'utf-8');
		$direccion 		= mb_convert_case(trim($this->input->post('txt_direccion')), MB_CASE_UPPER, 'utf-8');
		$dni 			= $this->input->post('txt_dni');
		$fechanac 		= $this->util->fecha_ingles($this->input->post('txt_fecha_nac'));
		$celular 		= $this->input->post('txt_celular');
		//$celular2 		= $this->input->post('txt_celular2');
		$correo 		= $this->input->post('txt_correo');
		//$correo2 		= $this->input->post('txt_correo2');
		$telefono 		= $this->input->post('txt_telefono');
		//$telefono2 		= $this->input->post('txt_telefono2');
			$data 		= array('hc_apenombres' => $apenombres,
								'hc_direccion'	=> $direccion,
								'hc_dni'		=> $dni,
								'hc_fecha_nac'	=> $fechanac,
								'hc_celular'	=> $celular,
								'hc_correo'		=> $correo,
								'hc_telefono'	=> $telefono);
		$this->xhc_model->editar($id, $data);
		echo 0;
	}

	public function combo($id='', $descripcion='', $ids=0){
		$html 	= '';
		$ds 	= $this->xhc_model->listar();
		if($id!=''&&$descripcion!='')
			$html 	.= '<option value="'.$id.'">'.$descripcion.'</option>';
		foreach ($ds->result() as $row) {
			$selected = ($row->hc_id==$ids)?'selected="selected"':'';
			$html 	.= '<option '.$selected.' value="'.$row->hc_id.'">'.str_pad($row->hc_dni, 8, '0', STR_PAD_LEFT).' | '.$row->hc_apenombres.'</option>';
		}
		return $html;
	}

	public function autocomplete(){
		$texto 	= mb_convert_case(trim($this->input->get('term')), MB_CASE_UPPER, 'utf-8');
		$array 	= array();
		$ds 	= $this->xhc_model->listar($texto);
		foreach ($ds->result() as $row) {
			array_push($array, array('id' => $row->hc_id, 'text' => str_pad($row->hc_dni, 8, '0', STR_PAD_LEFT).' | '.$row->hc_apenombres));
		}
		echo json_encode($array);
	}
}