@include('led/led_header')
<style>
    .fancy.fancy-grid{
        overflow-x:auto;
        }
</style>

<div class="container" style='width:95%'>

    <div class="row">
    <div class="col-md-4 h3 text-left">
		@traducir('Reporte de actividad de usuarios','Reporte de actividad de usuarios')
	</div>
	<div class="col-md-8 text-center">
		<form name="frmAU" id="frmAU" method="post" class="form-signin">
            {{ csrf_field() }}
			<div class="col-md-7"></div>
			<div class="col-md-2">
			  <label>@traducir('Fecha desde','Fecha desde')</label>
			  <input name="fecha_desde" type="text" id="fecha_desde_au" data-date-format="dd/mm/yyyy" class="fecha form-control readonly text-center" data-date-end-date="0d" data-date-start-date="-3y">
			</div>
			<div class="col-md-2">
				<label>@traducir('Fecha hasta','Fecha hasta')</label>
				<input name="fecha_hasta" type="text" id="fecha_hasta_au" data-date-format="dd/mm/yyyy" class="fecha form-control readonly text-center" data-date-end-date="0d" data-date-start-date="-3y">
			</div>
			<div class="col-md-1">
			  <label>&nbsp;</label>
				<button type="button" class="btn btn-info btn-fidelitas buscar"><i class='fas fa-search'></i>&nbsp;&nbsp;@traducir('Buscar','Buscar')</button>
			</div>
		</form>
	</div>
	</div>
</div>

    <div class="row" id="loading-AU" style="display:block;">
        <div class="col-md-2 col-md-offset-5">
            <img src="/images/loading.gif" alt="" class="img img-responsive">
        </div>
    </div>
    
    <div class="col-md-12 text-center" >
        <div class="col-md-12" id="reportesAU"></div>
	</div>	

<div class="modal fullscreen-modal  fade" id="modal_excel" tabindex="-1" data-backdrop="static" data-keyboard="false">
  <div class="modal-dialog modal_excel" >
    <div class="modal-content" style="overflow-y: auto;">
      <div id="detalle_excel" class="modal-body">
        <div class="col-md-12 text-center">
      <label>@traducir('exportando', 'Exportando... Por favor aguarde un momento.')</label>
    </div>
    <div class="col-md-12 text-center">
      <img style="float:none;" src="/images/loading.gif" class="loading"/>
    </div>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript" src="{{ URL::asset('/js/fancy/fancy.min.js') }}?v={{time()}}"> </script>


<script>    
$( document ).ready(function() {  
    //Fancy.MODULESDIR = '/js/fancy/';
    $('#loading-AU').hide();
    $("#reportesAU").html("");
    var date_desde=$('#fecha_desde_au');
    var options={
      format: 'dd/mm/yyyy',
      todayHighlight: true,
      autoclose: true,
      language: "es",
      //startDate: '-2m', //pato
      endDate: '+0d',
      showButtonPanel: true
    };
    date_desde.datepicker(options); 
    date_desde.datepicker("setDate",'-30d');
    dateDefaultView($('#fecha_desde_au'),'dd/mm/yyyy');

    var date_hasta=$('#fecha_hasta_au');
    var options={
      format: 'dd/mm/yyyy',
      todayHighlight: true,
      autoclose: true,
      language: "es",
      //startDate: '-2m', //pato
      endDate: '+0d',
      showButtonPanel: true
    };
    date_hasta.datepicker(options);
    date_hasta.datepicker("setDate",'0d');
    dateDefaultView($('#fecha_hasta_au'),'dd/mm/yyyy');

    var fancyAU;
    actualizarFancyAU(); 

    function actualizarFancyAU() {
          $('#loading-AU').hide();
          $("#reportesAU").html("");
          var boton_excel = '@traducir("Exportar a excel", "Exportar a excel")'; 
          var registros = '@traducir("Registros", "Registros")';  
          var pagina = '@traducir("Pagina", "Página")'; 
          var de = '@traducir("de", "de")'; 

          var colAU = [
            {
                type: "select",
                width: 70,
                align: "center",
                cellAlign: "center",
                hidden: true,
                sortable: true,
                locked: false
            },
            {
                type: "date",
                format : {read:'Y-m-d H:i',write:'d/m/Y H:i'},
                title: '@traducir("Fecha","Fecha")',
                index: "horario_log",
                width: 100,
                cellAlign: "center",
                sortable: true,
                resizable: true,
                locked: true,
                align: "center"
            },
            {
                type: "string",
                title: '@traducir("Contratista","Contratista")',
                index: "datos_contratista",
                flex: 1,
                cellAlign: "left",
                sortable: true,
                resizable: true,
                 locked: true,
                filter : {header:true},
                align: "left"
            },
            {
                type: "string",
                title: '@traducir("Servicio","Servicio")',
                index: "nombre_tipo_contratista_1",
                flex: 1,
                cellAlign: "left",
                sortable: true,
                resizable: true,
                 locked: true,
                filter : {header:true},
                align: "left"
            },
            {
                type: "string",
                title: '@traducir("Subcontratista","Subcontratista")',
                index: "datos_subcontratista",
                flex: 1,
                cellAlign: "left",
                sortable: true,
                resizable: true,
                 locked: true,
                filter : {header:true},
                align: "left"
            },
                  {
                type: "string",
                title: '@traducir("Recurso","Recurso")',
                index: "datos_recurso",
                flex: 1,
                cellAlign: "left",
                sortable: true,
                resizable: true,
                 locked: true,
                filter : {header:true},
                align: "left"
            },
           
            { type : 'string' , 
              title : '@traducir("Usuario","Usuario")',
              index : 'usuario' ,
              width: 120,
              cellAlign : 'left',
              sortable : true ,
              resizable : true ,    
              filter : {header:true} ,
              align : 'left' ,
              locked : true  
            },
            { type : 'string' , 
              title : '@traducir("Tipo operación","Tipo operación")',
              index : 'tipo_log' ,
              flex : 1, 
              cellAlign : 'left',
              sortable : true ,
              resizable : true ,
              filter : {header:true} ,
              align : 'left' ,
              locked : true 
            },
           
        ];

        $('#loading-AU').show();
        if (ajax) {
            ajax.abort();
        }
        $.ajax({
            method: "post",
            url: "/imperium/reportes-actividad-usuarios-data",
            data: $("#frmAU").serialize(),
            beforeSend: function () {
                $("#loading-AU").show();
                $("#reportesAU").html("");
            },
            success: function (response) {
                var data = JSON.parse(response);
                var values = data.data;
                var hay = data.hay;
                if (hay=='NO')
                {
                    $('#loading-AU').hide();
                    $("#reportesAU").html("<div class='col-md-12 h4 text-center'>"+'@traducir("No existen datos para las fechas solicitadas","No existen datos para las fechas solicitadas")'+"</div>");
                    return; 
                }
                /*
                else 
                {
                    $('#loading-AU').hide();
                    $("#reportesAU").html("<div class='col-md-12 h4 text-center'>Existen datos para las fechas solicitadas</div>");
                    return; 
                }
                */
               
                var tipos = data.tipos;
                var tipos_list = new Array();
                  tipos.map(function(tipo, i) {
                      tipos_list.push({nombre_tipo:tipo});
                  });

                  var logs = data.logs;
                  var logs_list = new Array();
                  logs.map(function(tipo, i) {
                      logs_list.push({nombre_tipo:tipo});
                  });

         

                var col = colAU[3];
                  col.type = 'combo';
                  col.multiSelect = true;
                  col.itemCheckBox = true;
                  col.minListWidth = 100;
                  col.data = tipos_list;
                  col.displayKey = 'nombre_tipo';
                  col.valueKey = 'nombre_tipo';
                  col.render = function (o) {
                      o.value = $.trim(o.value);
                      return o;
                  }
                  colAU[3] = col;



                var col = colAU[7];
                  col.type = 'combo';
                  col.multiSelect = true;
                  col.itemCheckBox = true;
                  col.minListWidth = 100;
                  col.data = logs_list;
                  col.displayKey = 'nombre_tipo';
                  col.valueKey = 'nombre_tipo';
                  col.render = function (o) {
                      o.value = $.trim(o.value);
                      return o;
                  }
                  colAU[7] = col;


                $('#loading-AU').hide();
                fancyAU = new FancyGrid({
                    renderTo: "reportesAU",
                    width: window.innerWidth - (window.innerWidth * .05),
                    height: window.innerHeight - (window.innerHeight * .20),
                    columnLines: true,
                    theme: {
                        name: "gray",
                    },
                    lang: {
                        paging: {
                            info: registros + ' [0] - [1] '+ de +' [2]',
                            page: pagina,
                            of: de + ' [0]'
                        },
                    },        
                    // Excel 
                    exporter: true,
                    subTBar: [
                     {
                      text: "<button style='background-color:#20a464; width:150px; margin-top: 0px;' type = 'button' class= 'form-control botones-paginador'><i class='fas fa-file-excel'></i>&nbsp;&nbsp;" + boton_excel + "</button>",
                      handler: function() {
                        //$('#modal_excel').modal('show');
                        this.exportToExcel({
                          fileName: 'Actividad',
                          all: true,
                          header: true
                        });
                        //$('#modal_excel').modal('hide');
                        }
                      },
                    ],

                    selModel: { type : 'rows', memory : true },
                    paging : true,
                    paging : {
                        pageSize: 20,
                        pageSizeData: [10,20,50],
                        barType: 'tbar'
                    },
                    data: values,
                    defaults: {
                        sortable: true,
                        resizable: true},
                    columns: colAU,
                    scrollable: true,
                    cellHeight: 26,
                    //Habilita el agregado de css en todas las celdas de un fila cuando se hace mouseover.
                    trackOver: true,      
                    events : [{
                                init: function (g) { 
                                    $('#reportesAU .fancy-bar-button').removeAttr("style");

                                    $('#reportesAU .fancy').removeClass('fancy-panel-shadow');
                                    $('#reportesAU .fancy-panel-tbar').css('border-bottom', '0px');
                                    $('#reportesAU .fancy-panel-tbar .fancy-bar-left-scroller').remove(); 
                                    $('#reportesAU .fancy-panel-tbar .fancy-bar-right-scroller').remove(); 
                                },
                                scope: {}
                                }, {
                                cellclick : function(fancyAU, obj) {},
                                },
                            ],
                });

                $("#loading-AU").hide();
                $("#reportesAU").show();
            },
        });
    }
    
    $(window).resize(function () {
        if (fancyAU != null) {
            fancyAU.setWidth(window.innerWidth - (window.innerWidth * .05),);
            fancyAU.setHeight(window.innerHeight - (window.innerHeight * .20));
            fancyAU.update();
        }
    });

    $('.buscar').on('click', function() {
        actualizarFancyAU();
    });

    $('.fecha').on('change', function() {
      $("#loading-AU").hide();
      $("#reportesAU").html("");
    });

})

</script>
@include('led/led_footer')
