<?php 
	/*session_start();*/
	include_once '../../model/conexion.php';

	class RolTarea_model{
		private $param = array();
	    private $con;

	    public function __construct(){
	    	$this->con = new Conexion();
	    }	


	    public function gestionar($param){
	    	$this->param = $param;
	    	switch ($this->param['param_opcion'])
			{
				case 'mostrarRolTarea':
					echo $this->mostrarRolTarea();
					break;
				case 'listarRoles':
					echo $this->listarRoles();
					break;
				case 'listarTareas':
					echo $this->listarTareas();
					break;
				case 'insert':
					echo $this->insert();
					break;	
				case 'obtener':
					echo $this->obtener();
					break;
				case 'update':
					echo $this->update();
					break;		
				case 'eliminar':
					echo $this->eliminar();
					break;				
			}
	    }	

	    private function mostrarRolTarea(){
			$this->con->cerrarAbrir();
			$sql = "call sp_mostrarRolTarea(";
			$sql .= "1,";
	    	$sql .= "1)";

	    	$tabla = "";

			$cont = 0;
			$datos = $this->con->consultaRetorno($sql);           
           	while($fila = mysqli_fetch_array($datos)){
           		$cont = $cont + 1;
            	$tabla .= "<tr>";
                    $tabla .= "<td style=' width: 3%;'>".$cont."</td>";
                    $tabla .= "<td id = 'rol'  style=' width: 15%;'>".$fila["rol"]."</td>";
                    $tabla .= "<td id = 'tarea' style=' width: 15%;'>".$fila["tarea"]."</td>";
                    $tabla .= "<td id = 'activo' style=' width: 3%;'>".$fila["activo"]."</td>";
                    $tabla .= "<td id='editar' style=' width: 8%;'>
                    			<a href='#modalUpdate' data-toggle='modal' class='btn btn-success btn-xs' onclick='obtener(".$fila["idRolTarea"].")'><i class='fa fa-pencil'></i>  </a>

                    			<a href='#modalEliminar' data-toggle='modal' class='btn btn-danger btn-xs' onclick='obtenerId(".$fila["idRolTarea"].")'><i class='fa fa-trash-o'></i> </a>
                    			</th>";
                $tabla .= "</tr>";
            }      
        	return $tabla;
		}
		
		private function listarRoles(){
			$this->con->cerrarAbrir();
			$sql = "call sp_mostrarRoles(";
			$sql .= "1,";
	    	$sql .= "2)";

	    	$combo = "<option value=''>Seleccionar Rol...</option>";

			$datos = $this->con->consultaRetorno($sql);           
           	while($fila = mysqli_fetch_array($datos)){
            	$combo .= "<option value='".$fila["idRol"]."'>".$fila["nombre"]."</option>";                  
            }      
        	return $combo;
		}

		private function listarTareas(){
			$this->con->cerrarAbrir();
			$sql = "call sp_mostrarTareas(";
			$sql .= "1,";
	    	$sql .= "2)";

	    	$combo = "<option value=''>Seleccionar Tarea...</option>";

			$datos = $this->con->consultaRetorno($sql);           
           	while($fila = mysqli_fetch_array($datos)){
            	$combo .= "<option value='".$fila["idTarea"]."'>".$fila["tarea"]."</option>";                  
            }      
        	return $combo;
		}

	    private function insert(){	    	
	    	$this->con->cerrarAbrir();
			$sql = "call sp_insertUpdateRol_Tarea(";
			$sql .= "1, ";
			$sql .= "'".$this->param['param_idRol']."', ";
			$sql .= "'".$this->param['param_idTarea']."', ";
			$sql .= "'".$_SESSION['usuario']."',";
			$sql .= "'".$_SESSION['usuario']."',";
			$sql .= "1,";
			$sql .= "1) ";
			

	    	$datos = $this->con->consultaRetorno($sql);
	    	while ($fila = mysqli_fetch_array($datos)) {
            	$respuesta = $fila['respuesta'];
        	}

        	return $respuesta;
	    }

	    private function obtener(){
			$this->con->cerrarAbrir();
			$sql = "call sp_mostrarRolTarea(";
			$sql .= "".$this->param['param_idRolTarea'].", ";
	    	$sql .= "3)";

			$datos = $this->con->consultaRetorno($sql);           
           	while($fila = mysqli_fetch_array($datos)){
            	return json_encode($fila);
            }      
		}

	    private function update(){	    	
	    	$this->con->cerrarAbrir();
			$sql = "call sp_insertUpdateRol_Tarea(";
			$sql .= "".$this->param['param_idRolTarea'].", ";
			$sql .= "'".$this->param['param_idRol']."', ";
			$sql .= "'".$this->param['param_idTarea']."', ";
			$sql .= "'".$_SESSION['usuario']."',";
			$sql .= "'".$_SESSION['usuario']."',";
			$sql .= "".$this->param['param_activo'].", ";
			$sql .= "2) ";
			

	    	$datos = $this->con->consultaRetorno($sql);
	    	while ($fila = mysqli_fetch_array($datos)) {
            	$respuesta = $fila['respuesta'];
        	}

        	return $respuesta;
	    }	

	    private function eliminar(){
			$this->con->cerrarAbrir();
			$sql = "call sp_eliminarRolTarea(";
			$sql .= "".$this->param['param_idRolTarea'].", ";
	    	$sql .= "1)";	

			$this->con->consultaSimple($sql);  

        	return 1;
		}         		
	}
?>