<?php
@session_start();
class Xdo 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/xdo_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']		= $titulo;
 			$datac['titulo']	= $titulo;
 			$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 			$datam['menu']		= 'xdo';
 			$data['menu']		= $this->load->view('main/menu', $datam, TRUE);
 			$data['pie']		= $this->load->view('main/pie', '', TRUE);
			$this->load->view('xdo/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');
		$ds 		= $this->xdo_model->listar($texto);
		foreach ($ds->result() as $row) {
			$html.= '
				<tr>
					<td>'.$row->med_descripcion.'</td>
					<td>
						<a href="javascript:fn_xdo_ver('.$row->med_id.', \''.$row->med_descripcion.'\');" class="btn btn-success">
							<span class="glyphicon glyphicon-edit"></span> <b>Editar</b>
						</a>
						<a href="javascript:fn_xdo_eliminar('.$row->med_id.', \''.$row->med_descripcion.'\');" class="btn btn-danger">
							<span class="glyphicon glyphicon-trash"></span> <b>Eliminar</b>
						</a>
					</td>
				</tr>
			';
		}
		echo json_encode(array($html));
	}

	public function insertar(){
		$descripcion 	= mb_convert_case($this->input->post('d1'), MB_CASE_UPPER, 'utf-8');
		$dsv 			= $this->xdo_model->validar($descripcion);
		if(!$dsv){
			$data 		= array('med_descripcion' => $descripcion);
			$this->xdo_model->insertar($data);
			echo 0;
		}else
			echo 1;
	}

	public function editar(){
		$id 			= $this->input->post('d1');
		$descripcion 	= mb_convert_case($this->input->post('d2'), MB_CASE_UPPER, 'utf-8');
		$data 			= array('med_descripcion' => $descripcion);
		$this->xdo_model->editar($id, $data);
		echo 0;
	}

	public function eliminar(){
		$id 	= $this->input->post('d1');
		$data 	= array('med_estado' => 1);
		$this->xdo_model->editar($id, $data);
	}

	public function combo($id='', $descripcion='', $ids=0){
		$html 	= '';
		$ds 	= $this->xdo_model->listar();
		if($id!=''&&$descripcion!='')
			$html 	.= '<option value="'.$id.'">'.$descripcion.'</option>';
		foreach ($ds->result() as $row) {
			$selected = ($row->con_id==$ids)?'selected="selected"':'';
			$html 	.= '<option '.$selected.' value="'.$row->med_id.'">'.$row->med_descripcion.'</option>';
		}
		return $html;
	}

	public function autocomplete(){
		$texto 	= mb_convert_case(trim($this->input->get('term')), MB_CASE_UPPER, 'utf-8');
		$array 	= array();
		$ds 	= $this->xdo_model->listar($texto);
		foreach ($ds->result() as $row) {
			array_push($array, array('id' => $row->diag_codigo, 'text' => $row->diag_codigo.' | '.$row->diag_descripcion));
		}
		echo json_encode($array);
	}
}