<?php 

date_default_timezone_set('America/Lima');

require_once ("../../config/Cado.php");
require_once ("../canchapos/cCanchapos.php");
$oCanchapos = new cCanchapos();

require_once("../formatos/formato.php");

// echo $_POST['cancha_id'];

$default_date = date('Y-m-d');
$default_scrolltime = date('H:00:00');

//echo $default_scrolltime;


?>

 
		
        
          <div class="col-md-12">
            <div class="card card-primary">
              <div class="card-body p-0">
              <div class="table-responsive" >
        
                <div id="calendar"></div>
              </div>
              </div>
             
            </div>
           
          </div>
         
       


<script type="text/javascript">



function separarcampo_ventaform(idf){
		
	
		$('#div_canchapos_info_dialog').modal();
	
		$.ajax({
			type: "POST",
			url: "../canchapos/canchapos_ventaform.php",
			async:true,
			dataType: "html",                      
			data: ({
				action: 'insertar_venta',
				sepcan_id:	idf,
				cancha_id : '<?php echo $_POST['cancha_id']; ?>'
			}),
			beforeSend: function() {
				var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
					
				$('.div_canchapos_info').html(div); 
			},
			success: function(html){
				$('.div_canchapos_info').html(html);	
			},
			complete: function(){
			}
		});
	}

function separarcampo_info(idf){
		
	
		$('#div_canchapos_info_dialog').modal();
	
		$.ajax({
			type: "POST",
			url: "../canchapos/canchapos_info.php",
			async:true,
			dataType: "html",                      
			data: ({
				action: 'editar',
				sepcan_id:	idf,
				cancha_id : '<?php echo $_POST['cancha_id']; ?>'
			}),
			beforeSend: function() {
				var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
					
				$('.div_canchapos_info').html(div); 
			},
			success: function(html){
				$('.div_canchapos_info').html(html);	
			},
			complete: function(){
			}
		});
	}

function eliminarEvento(id){
		
	//alert(id);
		//$('#div_canchapos_info_dialog').modal();
	if(confirm("Realmente desea anular la reserva.  Continuar?")){
		
		 $.ajax({
			type: "POST",
			url: "../canchapos/canchapos_reg.php",
			async:true,
			dataType: "html",                      
			data: ({
				action: 'eliminar',
				sepcan_id:	id,
			}),
			beforeSend: function() {
				//var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
					
				// $('.div_canchapos_info').html(div); 
			},
			success: function(html){
				//$('.div_canchapos_info').html(html);	
				
				canchapos_form_calendar('<?php echo $_POST['cancha_id']?>');
				
			},
			complete: function(){
			}
		}); 
		}
	}



 $(function () {

  var date = new Date();
  var d = date.getDate();
  var m = date.getMonth();
  var y = date.getFullYear();
  

  var calendarEl = document.getElementById("calendar");
  
  var calendar = new FullCalendar.Calendar(calendarEl, {
    plugins: ["interaction", "dayGrid", "timeGrid"],
	header: {
      left: "next,today",
      center: "title",
      right: "timeGridWeek,timeGridDay"
    },
    editable: false,
	scrollTime : '<?php echo $default_scrolltime; ?>',
	defaultDate: '<?php echo $default_date; ?>',
    events:[
      <?php 
		$dts= $oCanchapos->mostrarTodos_separaciones($_POST['cancha_id']);
		$rows = mysqli_num_rows($dts);
		if($rows>0){
			
		while($dt = mysqli_fetch_array($dts)){
			$sepcan_est = '';
			$tel = '';
			$mon = '';
			$fi = $dt['tb_separacioncancha_horaini'];
			$ff = $dt['tb_separacioncancha_horafin'];
			$cli_nom = $dt['tb_cliente_nom'];
			$cli_tel = $dt['tb_cliente_tel'];
			$sepcan_id = $dt['tb_separacioncancha_id'];
			$sepcan_est = $dt['tb_separacioncancha_est'];
			$sepcan_mon = $dt['tb_separacioncancha_ade'];
			
			$nom = substr($cli_nom,0,20);
			
			if($cli_tel!=""){
				$tel = ' | '.$cli_tel;
			}
			if($sepcan_mon>0){
				$mon = ' | S/ '.formato_money($sepcan_mon);
			}
			
			
			if($sepcan_est==1){
				$col = '#80CBC4';
				$text = '#000';
			}if($sepcan_est==2){
				$col = '#4F4F4F';
				$text = '#fff';
			}
			
			
		?>
		 {
			title: '<?php echo $nom.$tel.$mon; ?> ',
			start: '<?php echo $fi; ?>',
			end: '<?php echo $ff; ?>',
			id:'<?php echo $sepcan_id; ?>',
			color:'<?php echo $col; ?>',
			textColor : '<?php echo $text; ?>',
			groupId : '<?php echo $sepcan_est; ?>',
		},
		<?php 
		}
		mysqli_free_result($dts);	
		}
		
	  ?>
	  
	  
	  
    ],
	
		
	eventRender: function(info) {
   
		id = info.event.id;
		est = info.event.groupId;
   
		const time = $(info.el).find('.fc-time');
		
		const title = $(info.el).find('.fc-title');
		if(est==1){
			time.html(time.text()+'<div class="btn btn-success btn-xs btn-flat "  onclick="separarcampo_info('+id+');" ><i class="fas fa-edit"></i></div>');
	
			title.html(title.text()+'<div class="btn btn-warning btn-xs btn-flat "  onclick="separarcampo_ventaform('+id+');" ><i class="fas fa-wallet"></i></div> '+
			' <div class="btn btn-danger btn-xs btn-flat "  onclick="eliminarEvento('+id+');" ><i class="fas fa-times"></i></div> '+
			' <div class="btn btn-success btn-xs btn-flat "  onclick="enviar_whatsap_reserva('+id+');" ><i class="fab fa-whatsapp"></i></div> ');
		}else if(est==2){
			time.html(time.text()+'<div class="btn btn-primary btn-xs btn-flat "  onclick="separarcampo_info('+id+');" ><i class="fas fa-eye"></i></div>');
	
			title.html(title.text()+'');
		}
		
	}
  });

  calendar.render();
  
  
  $("html, body").animate({scrollTop:400 },800);
  

  });

</script>
