<?php
$page_title = 'Lista de Programacion';
require_once('../../includes/load.php');
// Checkin What level user has permission to view this page
page_require_level(5);
?>
<?php include_once('../../layouts/header.php'); ?>
<div class="row">
 <div class="col-md-12">
   <?php echo display_msg($msg); ?>
 </div>
<div class="col-md-16">
  <div class="panel panel-default">
    <div class="panel-heading clearfix">
        <strong>
          <span class="glyphicon glyphicon-th-list"></span>
          <span>Listado de Programas</span> 
        </strong>
        <center>
         <form class="form-inline" method="POST">
              <label class="from-addon"> Rango de Fechas:</label>
            <div class="panel-heading clearfix ">             
                  <input type="date" id="toDate" name="toDate" class="form-control" data-plugin="datepicker" placeholder="To Date" onchange="selectDate()">
                  <label>A:</label>
                  <input type="date" id="fromDate" name="fromDate" class="form-control" data-plugin="datepicker" placeholder="From Date" onchange="selectDate()">                        
                  <button type="button" class="btn btn-default " onclick="setTimeout(function(){location.reload();}, 3);">
                    <span class="glyphicon glyphicon-repeat"></span> 
                  </button>
            </div>
              
          </form>
       </center>
        <div class="pull-right">
            <a href="programacion.php" class="btn btn-primary" id="programa"><i class="glyphicon glyphicon-plus"></i>Programa</a>
            <a href="../reportes/granjasprograma.php" class="btn btn-success"><i class="fi fi-sr-farm"></i> rptGranjas </a>
        </div>
    </div>
    <div class="col-12">          
                <div class="panel-body">
                <table class="table table-bordered table-striped"  id="mytable">
                    <thead>
                        <tr>
                            <th style="text-align: center; padding-right: 10px;">Id</th>
                            <th style="text-align: center; padding-right: 10px;">Area</th>
                            <th style="text-align: center; padding-right: 10px;">Registro</th>
                            <th style="text-align: center; padding-right: 10px;">Campaña</th>
                            <th style="text-align: center; padding-right: 10px;">Granja</th>
                            <th style="text-align: center; padding-right: 10px;">Destino</th>
                            <th style="text-align: center; padding-right: 10px;">Servicio</th>
                            <th style="text-align: center; padding-right: 10px;">Inicio</th>
                            <th style="text-align: center; padding-right: 10px;">Entrega</th>
                            <th style="text-align: center; padding-right: 10px;">Estado</th>
                            <th style="text-align: center; padding-right: 10px;">Opciones</th>
                        </tr>
                    </thead>
                </table>
            </div>
    </div>
  </div>
</div>
</div>
<?php include_once('../../layouts/footer.php'); ?>
<script>
$( document ).ready(function() {
var user_level = '<?= remove_junk($user['user_level'])?>';
var table = $('#mytable').dataTable({
          responsive: true,
          deferRender: true,
          columnDefs: [{
                "defaultContent": "-",
                "targets": "_all"
              }],
          dom: '<"row"<"col-sm-12 col-md-4"l><"col-sm-12 col-md-4"<"dt-buttons btn-group flex-wrap"B>><"col-sm-12 col-md-4"f>>t<"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>',
              buttons: [
                'copyHtml5',
                'excelHtml5',
                'csvHtml5',
             ],
         "autoWidth": false,
         "bProcessing": true,
         "aLengthMenu": [[7, 10, 25, 50, -1], [7, 10, 25, 50, "All"]],
         "language":{
            "lengthMenu": "Mostrar _MENU_ registros por pagina",
            "info": "Mostrando pagina _PAGE_ de _PAGES_",
            "infoEmpty": "No hay registros disponibles",
            "infoFiltered": "(filtrada de _MAX_ registros)",
            "loadingRecords": "Cargando...",
            "processing":     "Procesando...",
            "search": "Buscar:",
            "zeroRecords":    "No se encontraron registros coincidentes",
            "paginate": {
                "next":       "Siguiente",
                "previous":   "Anterior"
            },  
         },
         "sAjaxSource": "data/data.php",
          "bPaginate":true,
          "iDisplayLength": 7,
          "order": [[0, "ASC"]],
         "aoColumns": [
                { mData:'id'},
                { mData:'NombreAreaGerenciaOperaciones'}, 
                { mData:'ProFechaRegistro'},
                { mData:'Campana'},
                { mData:'GNombre'},
                { mData:'TipoGranja'}, 
                { mData:'TSNombre'}, 
                { mData:'ProFechaInicioLimpieza'},
                { mData:'ProFechaInicioRecepcion'}, 
                { mData:'EstadoPrograma'}, 
                { 
                   "render": function (data, type, full, row) {
                        if (user_level === '4'  || user_level === '5') {
                            return '<center><a style="margin-right: 3pt" href="view_programa.php?id='+ full.id +'" class="btn btn-xs btn-warning"  title="Ver"><span class="glyphicon glyphicon-eye-open"></span></a></center>';
                        }
                        if (full['EstadoPrograma'] === 'Programado') {
                            return '<center><a style="margin-right: 3pt" href="../recurso/recursos.php?id='+ full.id +'" class="btn btn-xs btn-success" title="Agregar Recursos"><span class="glyphicon glyphicon-plus"></span></a>    <a style="margin-right: 3pt" href="edit_programacion.php?id='+ full.id +'" class="btn btn-xs btn-danger confirmEditar"  title="Editar"><span class="glyphicon glyphicon-pencil"></span></a></center>'; 

                        }
                        else{
                            return '<center><a style="margin-right: 3pt" href="view_programa.php?id='+ full.id +'" class="btn btn-xs btn-warning"  title="Ver"><span class="glyphicon glyphicon-eye-open"></span></a></center>';
                        }
                                                    
                   }

                }    
                           
        ]


}); 

});

function selectDate() {
  let table = $('#mytable').DataTable();
  table.draw();
}
$(document).ready(function () {
    $.fn.dataTable.ext.search.push( function( settings, data, dataIndex ) { 
      let toDate = $('#toDate').val();
      let fromDate = $('#fromDate').val();
      let formatedToDate = toDate ? new Date(moment(toDate, "YYYY-MM-DD")) : '';
      let formatedFromDate = fromDate ? new Date(moment(fromDate, "YYYY-MM-DD")) : '';
      let dateField = data[7];
      let formatedDateField = dateField ? new Date(moment(dateField, "YYYY-MM-DD")) : '';
      if(formatedToDate && formatedFromDate && formatedDateField) {
        return formatedToDate.getTime() <= formatedDateField.getTime() && formatedDateField.getTime() <= formatedFromDate.getTime()
      } else if(formatedToDate && !formatedFromDate && formatedDateField) {
        return formatedToDate.getTime() <= formatedDateField.getTime()
      } else if(!formatedToDate && formatedFromDate && formatedDateField) {
        return formatedDateField.getTime() <= formatedFromDate.getTime()
      } else if(!formatedToDate && !formatedFromDate) {
        return true
      }
      return false; 
    });
  });

//VISUALIZAR CAMPOS DEPENDIENDO AL USUARIO QUE ENTRA 
 var programa = document.getElementById("programa");

 var user_level = '<?= remove_junk($user['user_level'])?>';

 if ( user_level === '2' || user_level === '4') {
// O bien, para esconderlo
programa.style.display = "none";
 }
</script>