<?php
@session_start();
class Xaen 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('movimientos/xaen_model');
		$this->load->model('seguridad/xppo_model');
		date_default_timezone_set('America/Lima');
	}

	public function insertar_listar_cart(){
		$cod 	= $this->input->post('d2');
		$desc 	= $this->input->post('d3');
		$indi 	= mb_convert_case(trim($this->input->post('d4')), MB_CASE_UPPER, 'utf-8');
		if($cod!='0')
			$_SESSION['app.gxc_sesion_xaen'][$cod] 	= array('cod' => $cod, 'desc' => $desc, 'indi' => $indi);
		$array 	= $_SESSION['app.gxc_sesion_xaen'];
		$html 	= '';
		foreach ($array as $row) {
			$html.= '
				<tr>
					<td>'.$row['desc'].'</td>
					<td>'.$row['indi'].'</td>
					<td>
						<a href="javascript:fn_xaen_eliminar('.$row['cod'].',\''.$row['desc'].'\');" 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 insertar_listar(){
		$xan 	= $this->input->post('d1');
		$xdo 	= $this->input->post('d2');
		$indi 	= mb_convert_case(trim($this->input->post('d4')), MB_CASE_UPPER, 'utf-8');
		/*$data 	= array('diag_codigo' 	=> $xdo,
						'ate_id'		=> $xan);*/
		if($xdo!='0')
			$this->xaen_model->insertar($xdo, $xan, $indi);
		$ds 	= $this->xaen_model->listar($xan);
		$html 	= '';
		foreach ($ds->result() as $row) {
			$html.= '
				<tr>
					<td>'.$row->exa_descripcion.'</td>
					<td>'.$row->aex_indicacion.'</td>
					<td>
						<a href="javascript:fn_xaen_eliminar(\''.$row->aex_id.'\',\''.$row->exa_descripcion.'\');" 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 listar_cart(){
		$array 	= $_SESSION['app.gxc_sesion_xaen'];
		$html 	= '';
		foreach ($array as $row) {
			$html.= '
				<tr>
					<td>'.$row['desc'].'</td>
					<td>'.$row['indi'].'</td>
					<td>
						<a href="javascript:fn_xaen_eliminar('.$row['cod'].',\''.$row['desc'].'\');" 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 listar(){
		$xan 	= $this->input->post('d1');
		$ds 	= $this->xaen_model->listar($xan);
		$html 	= '';
		foreach ($ds->result() as $row) {
			$html.= '
				<tr>
					<td>'.$row->exa_descripcion.'</td>
					<td>'.$row->aex_indicacion.'</td>
					<td>
						<a href="javascript:fn_xaen_eliminar(\''.$row->aex_id.'\',\''.$row->exa_descripcion.'\');" 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 insertar_cart(){
		$cod 	= $this->input->post('d1');
		$desc 	= $this->input->post('d4');
		$indi 	= mb_convert_case(trim($this->input->post('d3')), MB_CASE_UPPER, 'utf-8');

		$_SESSION['app.gxc_sesion_xaen'][$cod] 	= array('cod' => $cod, 'desc' => $desc, 'cant' => $cant, 'indi' => $indi);
		echo 	0;
	}

	public function insertar(){
		$xdo 	= $this->input->post('d1');
		$xan 	= $this->input->post('d2');
		$indi 	= mb_convert_case(trim($this->input->post('d3')), MB_CASE_UPPER, 'utf-8');
		/*$data 	= array('diag_codigo' 	=> $xdo,
						'ate_id'		=> $xan);*/
		$this->xaen_model->insertar($xdo, $xan, $indi);
		echo 	0;
	}

	public function eliminar_cart(){
		$cod 	= $this->input->post('d1');
		unset($_SESSION['app.gxc_sesion_xaen'][$cod]);
		echo 	0;
	}

	public function eliminar(){
		$id 	= $this->input->post('d1');
		$this->xaen_model->eliminar($id);
		echo 	0;
	}
}