<?php
@session_start();
class Xpn 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/xpn_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']		= 'PROGRAMACIÓN DE FECHAS :. '.$titulo;
 			$datac['titulo']	= $titulo;
 			$data['xco']		= modules::run('maestras/xco/combo','0',' - TODOS - ');
 			$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 			$datam['menu']		= 'xpn';
 			$data['menu']		= $this->load->view('main/menu', $datam, TRUE);
 			$data['pie']		= $this->load->view('main/pie', '', TRUE);
			$this->load->view('xpn/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(){
		list($mes,$anio)	= explode('/', $this->input->post('mes_anio'));
		$dias 		= cal_days_in_month(CAL_GREGORIAN, ($mes + 0), $anio);
		$xco 		= $this->input->post('xco');
		$desde 		= "{$anio}-{$mes}-01";
		$hasta 		= "{$anio}-{$mes}-{$dias}";
		$ds 		= $this->xpn_model->listar($desde,$hasta,$xco);
		$html 		= '';
		/*<a href="javascript:fn_xpn_eliminar2('.$row->con_id.',\''.$this->util->fecha_espanol($row->prog_fecha).'\',\''.$row->con_descripcion.'\',\''.$row->pers_apenombres.'\');" class="btn btn-danger btn-xs">
							<span class="glyphicon glyphicon-trash"></span>
						</a>*/
		foreach ($ds->result() as $row) {
			$html.= '
				<tr>
					<td>'.$row->con_descripcion.'</td>
					<td>'.$row->pers_apenombres.'</td>
					<td>'.$this->util->fecha_espanol($row->prog_fecha).'</td>
					<td>'.$row->prog_cupos.'</td>
					<td>'.$row->prog_cuposc.'</td>
					<td>
						<a href="javascript:fn_xpn_ver(	'.$row->prog_id.',
														\''.$row->con_descripcion.'\',
														\''.$row->pers_apenombres.'\',
														\''.$this->util->fecha_espanol($row->prog_fecha).'\',
														'.$row->prog_cupos.','.$row->prog_cuposc.');" class="btn btn-success btn-xs">
							<span class="glyphicon glyphicon-edit"></span> <b>Editar</b>
						</a>
					</td>
				</tr>
			';
		}
		echo json_encode(array($html));
	}

	public function programar(){
		if($this->xppo_model->ver($_SESSION['app.gxc_sesion_id'],9)){
			$titulo 			= $_SESSION['app.gxc_sesion_titulo'];
 			$data['titulo']		= 'PROGRAMAR FECHAS :. '.$titulo;
 			$datac['titulo']	= $titulo;
 			$data['xco']		= modules::run('maestras/xco/combo','0',' - SELECCIONAR - ');
 			$data['xpl']		= modules::run('seguridad/xpl/combo','0',' - SELECCIONAR - ');
 			$data['cabecera']	= $this->load->view('main/cabecera', $datac, TRUE);
 			$datam['menu']		= 'xpn';
 			$data['menu']		= $this->load->view('main/menu', $datam, TRUE);
 			$data['pie']		= $this->load->view('main/pie', '', TRUE);
			$this->load->view('xpn/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 listar_nuevo(){
		$xco 					= $this->input->post('xco');
		$xpl 					= $this->input->post('xpl');
		list($mes, $anio)		= explode('/', $this->input->post('mes_anio'));
		$dias 					= cal_days_in_month(CAL_GREGORIAN, ($mes + 0), $anio);
		$th 					= '<th>DOMINGO</th><th>LUNES</th><th>MARTES</th><th>MIERCOLES</th><th>JUEVES</th><th>VIERNES</th><th>SABADO</th>';
		$tr 					= '';
		$td 					= '';
		$dian 					= date('N', strtotime("{$anio}-{$mes}-01"));
		//echo $dian;
		//$d 						= 7 - $dian;
		if($dian<7){
			for ($i=1; $i <= $dian; $i++)
				$td.= '<td>&nbsp;</td>';
		}
		//$dias += $d;
		for ($i2=1; $i2 <= $dias; $i2++) {
			$class 				= 'primary';
			$fecha 				= "{$anio}-{$mes}-".str_pad($i2, 2, '0', STR_PAD_LEFT);
			$funcion 			= 'fn_xpn_insertar(\''.$fecha.'\');';
			$diad 				= $this->util->dia_semana(date('l', strtotime($fecha)), 'es');
			$ds 				= $this->xpn_model->ver($fecha,$xco,$xpl);
			if($ds){
				if($ds->prog_cuposc>0){
					$class 		= 'success';
					$funcion 	= '';
				}else{
					$class 		= 'warning';
					$funcion 	= 'fn_xpn_eliminar('.$ds->prog_id.');';
				}
			}
			$td .= '
					<td>
						<button class="btn btn-'.$class.'" onclick="'.$funcion.'">
							<b>'.str_pad($i2, 2, '0', STR_PAD_LEFT).'</b>
						</button>
					</td>
			';
			if(($i2+$dian)%7==0||$i2==$dias){
				$tr .= '<tr>'.$td.'</tr>';
				$td 	= '';
			}
		}
		$html 	= '<thead><tr>'.$th.'</tr></thead><tbody>'.$tr.'</tbody>';
		echo json_encode(array($html));
	}

	public function insertar(){
		$xco 	= $this->input->post('d1');
		$xpl 	= $this->input->post('d2');
		$fecha 	= $this->input->post('d3');
		$data 	= array('prog_fecha'		=> $fecha,
						'prog_cupos'		=> 40,
						'prog_usuario_reg'	=> $_SESSION['app.gxc_sesion_usuario'],
						'prog_fecha_reg'	=> date('Y-m-d'),
						'prog_hora_reg'		=> date('H:i:s'),
						'con_id'			=> $xco,
						'pers_id'			=> $xpl);
		$this->xpn_model->insertar($data);
		echo 	0;
	}

	public function eliminar(){
		$id 	= $this->input->post('d1');
		$data 	= array('prog_estado'		=> 1,
						'prog_usuario_elim'	=> $_SESSION['app.gxc_sesion_usuario'],
						'prog_fecha_elim'	=> date('Y-m-d'),
						'prog_hora_elim'	=> date('H:i:s'));
		$this->xpn_model->editar($id, $data);
		echo 	0;
	}

	public function editar_cupos(){
		$id 	= $this->input->post('d1');
		$cupos  = $this->input->post('d2');
		$data 	= array('prog_cupos' 		=> $cupos,
						'prog_usuario_edit'	=> $_SESSION['app.gxc_sesion_usuario'],
						'prog_fecha_edit'	=> date('Y-m-d'),
						'prog_hora_edit'	=> date('H:i:s'));
		$this->xpn_model->editar($id, $data);
		echo 	0;
	}

	public function listar_citar(){
		$fecha 		= $this->util->fecha_ingles($this->input->post('fecha'));
		$ds 		= $this->xpn_model->listar($fecha, $fecha);
		$html 		= '';
		$html 		.= '<option value="0"> - SECCIONAR PROGRAMACIÓN - </option>';
		foreach ($ds->result() as $row) {
			/*$html .= '
				<tr>
					<td></td>
					<td></td>
					<td></td>
					<td></td>
				</tr>
			';*/
			$html .= '
				<option value="'.$row->prog_id.'">
					'.$row->con_descripcion.' | '.$row->pers_apenombres.' | '.$this->util->fecha_espanol($row->prog_fecha).' | '.$row->prog_cupos.' | '.$row->prog_cuposc.'
				</option>
			';
		}
		echo $html;
	}
}