/*=============================================
VARIABLE LOCAL STORAGE
=============================================*/

if(localStorage.getItem("capturarRango3") != null){

	$("#daterange-btn3 span").html(localStorage.getItem("capturarRango3"));


}else{

	$("#daterange-btn3 span").html('<i class="fa fa-calendar"></i> Rango de fecha')

}

/*=============================================
RANGO DE FECHAS
=============================================*/
$('#daterange-btn3').daterangepicker(
  {
    ranges   : {
      'Hoy'       : [moment(), moment()],
      'Ayer'   : [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
      'Últimos 7 días' : [moment().subtract(6, 'days'), moment()],
      'Últimos 30 días': [moment().subtract(29, 'days'), moment()],
      'Este mes'  : [moment().startOf('month'), moment().endOf('month')],
      'Último mes'  : [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
    },
    startDate: moment(),
    endDate  : moment(),

    locale : {
      format: "DD/MM/YYYY",
		  daysOfWeek: ['Do','Lu','Ma', 'Mi', 'Ju', 'Vi','Sa'],
		  monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
	  }
  },
  function (start, end) {
    $('#daterange-btn3 span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

    var fechaInicial = start.format('YYYY-MM-DD');

    var fechaFinal = end.format('YYYY-MM-DD');

    var capturarRango = $("#daterange-btn3 span").html();
   
   	localStorage.setItem("capturarRango3", capturarRango);

   	window.location = "index.php?ruta=inventarioABC&fechaInicial="+fechaInicial+"&fechaFinal="+fechaFinal;

  }

)

/*=============================================
CANCELAR RANGO DE FECHAS
=============================================*/

$(".daterangepicker.opensright .range_inputs .cancelBtn").on("click", function(){

	localStorage.removeItem("capturarRango3");
	localStorage.clear();
	window.location = "inventarioABC";
})

/*=============================================
CAPTURAR HOY
=============================================*/

$(".daterangepicker.opensright .ranges li").on("click", function(){

	var textoHoy = $(this).attr("data-range-key");

	if(textoHoy == "Hoy"){

    var d = new Date();
    
    var dia = d.getDate();
    var mes = d.getMonth()+1;
    var año = d.getFullYear();

    if(mes < 10){

      var fechaInicial = año+"-0"+mes+"-"+dia;
      var fechaFinal = año+"-0"+mes+"-"+dia;

    }else if(dia < 10){

      var fechaInicial = año+"-"+mes+"-0"+dia;
      var fechaFinal = año+"-"+mes+"-0"+dia;

    }else if(mes < 10 && dia < 10){

      var fechaInicial = año+"-0"+mes+"-0"+dia;
      var fechaFinal = año+"-0"+mes+"-0"+dia;

    }else{

      var fechaInicial = año+"-"+mes+"-"+dia;
        var fechaFinal = año+"-"+mes+"-"+dia;

    } 

    	localStorage.setItem("capturarRango3", "Hoy");

    	window.location = "index.php?ruta=inventarioABC&fechaInicial="+fechaInicial+"&fechaFinal="+fechaFinal;

	}

})

$('.inventarioPro').DataTable({
    "autoWidth": false,
    "deferRender":true,
    "retrieve":true,
    "scrollCollapse": true,
    "paging": false,
    "processing": false,
    "sScrollY": "75vh",
    "select": {
        "style":    'multi',
        "selector": 'td:first-child'
    },
    "stateSave":  true,
    "language": {

            "decimal": "",
            "sEmptyTable": "No hay información",
            "sInfo": "Mostrando _START_ a _END_ de _TOTAL_ Entradas",
            "sInfoEmpty": "Mostrando 0 to 0 of 0 Entradas",
            "sInfoFiltered": "(Filtrado de _MAX_ total entradas)",
            "sInfoPostFix": "",
            "sInfoThousands": ",",
            "sLengthMenu": "Mostrar _MENU_ Entradas",
            "sLoadingRecords": "Cargando...",
            "sProcessing": "Procesando...",
            "sSearch": "Buscar:",
            "sZeroRecords": "Sin resultados encontrados",
            "oPaginate": {
                "sFirst": "Primero",
                "sLast": "Ultimo",
                "sNext": "Siguiente",
                "sPrevious": "Anterior"
            },
            "oAria": {
                "sSortAscending": ": Activar para ordenar la columna de manera ascendete",
                "sSortDescending": ": Activar para ordenar la columna de manera descendete",
            }
        },
        "drawCallback": function() {
          var api = this.api();
          $(api.column(5).footer()).html(
            'TOTAL: S/. '+Math.round(api.column(5, {page: 'current'}).data().sum()*100)/100
          )
        },
        "dom": "Bfrtilp",
                "buttons": [
                    {
                        "extend": "excel",
                        "text": "<i class='fas fa-file-excel'></i>",
                        "titleAttr" : "Exportar a Excel",
                        "className": 'btn btn-success',
                        "filename": function() {
                            return "Inventario ABC"      
                          },
                        "exportOptions": {
                            "columns": ["0","1","2","3","4","5","6"]
                        }
                    },
                    {
                        "extend": "pdfHtml5",
                        "download": "open",
                        "text": "<i class='fas fa-file-pdf'></i>",
                        "titleAttr" : "Exportar a pdf",
                        "className": 'btn btn-danger',
                        "filename" : function() {
                            return "Inventario ABC"      
                          },      
                        "customize": function(doc) {
                            doc.defaultStyle.fontSize = 8; 
                            doc.defaultStyle.alignment = 'justify'
                            
                         
                        },
                         "exportOptions": {
                            "columns": ["0","1","2","3","4","5","6"],
                            "page": 'current'
                        },
                        "footer": true

                    }
                ],
                "createdRow": function(row, data, rowIndex){

                  if(data[6] >= 15.00){
          
                      $('td', row).css({
          
                          'background-color':'#C8958B',
                          'color': 'black',
                          'border-style':'solid',
                          'border-color':'#bdbdbd'
                      });
                  } else if (data[6] < 15.00 && data[6] > 5.00){
                      $('td', row).css({
          
                          'background-color':'#ede59a',
                          'color': 'black',
                          'border-style':'solid',
                          'border-color':'#bdbdbd'
                      });
          
                  } else if (data[6] <= 5.00) {
                      
                      $('td', row).css({
          
                          'background-color':'#559ACA',
                          'color': 'black',
                          'border-style':'solid',
                          'border-color':'#bdbdbd'
                      });
                  }
          
          
              }
        
});

