<?php
session_start();
error_reporting(0);
//include('includes/config.php');
//require_once __DIR__."/loader.php";
include('includes/loader.php');
 // $loadSelect = new data;
 // $loadSelect = $loadSelect->getAllSelect();
if(strlen($_SESSION['alogin'])=="")
    {   
    header("Location: index.php"); 
    }
    else{
    ?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
    	<meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Table View AWS</title>
        <link rel="stylesheet" href="css/bootstrap.min.css" media="screen" >
        <link rel="stylesheet" href="css/font-awesome.min.css" media="screen" >
        <link rel="stylesheet" href="css/animate-css/animate.min.css" media="screen" >
        <link rel="stylesheet" href="css/lobipanel/lobipanel.min.css" media="screen" >
        <link rel="stylesheet" href="css/prism/prism.css" media="screen" > <!-- USED FOR DEMO HELP - YOU CAN REMOVE IT -->
        <link rel="stylesheet" type="text/css" href="js/DataTables/datatables.min.css"/>
        <link rel="stylesheet" href="css/main.css" media="screen" >
        <link rel="stylesheet" href="css/toastr/toastr.min.css" media="screen" >
        <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
        <script src="js/modernizr/modernizr.min.js"></script>
          <style>
        .errorWrap {
            padding: 10px;
            margin: 0 0 20px 0;
            background: #fff;
            border-left: 4px solid #dd3d36;
            -webkit-box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
            box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
        }
        .succWrap{
            padding: 10px;
            margin: 0 0 20px 0;
            background: #fff;
            border-left: 4px solid #5cb85c;
            -webkit-box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
            box-shadow: 0 1px 1px 0 rgba(0,0,0,.1);
        }
        </style>
    </head>
    <body class="top-navbar-fixed">
        <div class="main-wrapper">

            <!-- ========== TOP NAVBAR ========== -->
   <?php include('includes/topbar.php');?> 
            <!-- ========== WRAPPER FOR BOTH SIDEBARS & MAIN CONTENT ========== -->
            <div class="content-wrapper">
                <div class="content-container">
            

                    <div class="main-page">
                        <div class="container-fluid">
                            <div class="row page-title-div">
                                <div class="col-md-6">
                                    <h2 class="title">AWS Data</h2>
                                
                                </div>
                                
                                <!-- /.col-md-6 text-right -->
                            </div>
                            <!-- /.row -->
                            <div class="row breadcrumb-div">
                                <div class="col-md-6">
                                    <ul class="breadcrumb">
            							<li><a href="dashboard.php"><i class="fa fa-home"></i> Home</a></li>
                                        <li> AWS Data</li>
            							<li class="active">Table View</li>
            						</ul>
                                </div>
                             
                            </div>
                            <!-- /.row -->
                        </div>
                        <!-- /.container-fluid -->

                        <section class="section">
                            <div class="container-fluid">
                                
    				                        <form method="post" action="process.php">
                                                <div class="row">
                                                    <!-- Field Start -->
                                                    <div class="col-xs-12 col-md-3">
                                                        <div class="form-group">
                                                            <label for="txtStartDate">Start Date</label>
                                                            <input type="text" name="txtStartDate" class="form-control datepicker" id="txtStartDate"
                                                                placeholder="Start Date" required>
                                                        </div>
                                                    </div>
                                                    <!-- Field Ends -->
                                    
                                                    <!-- Field Start -->
                                                    <div class="col-xs-12 col-md-3">
                                                        <div class="form-group">
                                                            <label for="txtEndDate">End Date</label>
                                                            <input type="text" name="txtEndDate" class="form-control datepicker" id="txtEndDate"
                                                                placeholder="End Date" required>
                                                        </div>
                                                    </div>
                                                    <!-- Field Ends -->
                                    
                                                    <!-- Field Start -->
                                                    <div class="col-xs-12 col-md-3" >
                                                        <div class="form-group">
                                                            <label for="txtInterval">Interval</label>
                                                                <!--<select name="interval" id="interval" style="margin-left:4px;margin-bottom:10px;background: #fff; cursor: pointer; padding: 5px 10px; border: 5px solid #ccc;">-->
                                                                <select class="form-control" name="txtInterval" id="txtInterval">
                                                                    <option value="DEFAULT"> DEFAULT </option>
                                                                    <option value="5 MINUTE"> 5 MINUTE </option>
                                                                    <option value="30 MINUTE"> 30 MINUTE </option></option>
                                                                    <option value="60 MINUTE"> 60 MINUTE </option>
                                                                    <option value="1 DAY"> 1 DAY </option>
                                                                    <option value="1 MONTH"> 1 MONTH </option>
                                                                    <option value="1 YEAR"> 1 YEAR </option>
 
                                                                </select>
                                                        </div>
                                                    </div>
                                                    <!-- Field Ends -->
                                    
                                                    <div class="col-xs-12 col-md-3" style="top:23px;">
                                                        <input type="hidden" name="process" value="1">
                                                        <button type="submit" class="btn btn-primary mt-1"> Download </button>
                                                        <button class="btn btn-primary mt-1" id="view"> View </button>
                                                    </div>
                                                </div>
                                            </form>

    				              
				
                                

                                <div class="row">
                                    <div class="col-md-12">

                                        <div class="panel" style="margin-top:42px;">
                                            <div class="panel-heading">
                                                <div class="panel-title">
                                                    <h5>AWS Table View</h5>
                                                </div>
                                            </div>
                                            <?php if($msg){?>
                                            <div class="alert alert-success left-icon-alert" role="alert">
                                             <strong>Well!</strong><?php echo htmlentities($msg); ?>
                                             </div><?php } 
                                            else if($error){?>
                                                <div class="alert alert-danger left-icon-alert" role="alert">
                                                        <strong>Oh snap!</strong> <?php echo htmlentities($error); ?>
                                                </div>
                                                 <?php } ?>
                                            <div class="panel-body p-20">
                                               

                                                <table id="myexample" class="display table table-striped table-bordered" cellspacing="0" width="100%">
                                                </table>

                                         
                                                <!-- /.col-md-12 -->
                                            </div>
                                        </div>
                                    </div>
                                    <!-- /.col-md-6 -->

                                                               
                                                </div>
                                                <!-- /.col-md-12 -->
                                            </div>
                                        </div>
                                        <!-- /.panel -->
                                    </div>
                                    <!-- /.col-md-6 -->

                                </div>
                                <!-- /.row -->

                            </div>
                            <!-- /.container-fluid -->
                        </section>
                        <!-- /.section -->

                    </div>
                    <!-- /.main-page -->

                    

                </div>
                <!-- /.content-container -->
            </div>
            <!-- /.content-wrapper -->

        </div>
        <!-- /.main-wrapper -->

	<!-- Confirmation dialog-->
        <div class="modal" id="delmodal" tabindex="-1" role="dialog">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title">Confirm Deletion</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">
                <p>Are you sure?</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-primary delpopup" data-dismiss="modal">Confirm</button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div>



        <!-- ========== COMMON JS FILES ========== -->
        <script src="js/jquery/jquery-2.2.4.min.js"></script>
        <script src="js/bootstrap/bootstrap.min.js"></script>
        <script src="js/pace/pace.min.js"></script>
        <script src="js/lobipanel/lobipanel.min.js"></script>
        <script src="js/iscroll/iscroll.js"></script>
        <script src="js/toastr/toastr.min.js"></script>

        <!-- ========== PAGE JS FILES ========== -->
        <script src="js/prism/prism.js"></script>
        <script src="js/DataTables/datatables.min.js"></script>
        
        <script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
	    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>

        <!-- ========== THEME JS ========== -->
        <script src="js/main.js"></script>
        <!--<script src="js/awstable.js"></script>-->
        
        <script>
            $(function($) {
	    	    var awsData = <?php echo json_encode($results); ?>;
		        //console.log("results", awsData);
               
                var starTime = moment().subtract({'days': 5}).format('YYYY-MM-DD hh:mm:ss')
                var stTime = moment().subtract({'days': 5}).format('L')
		        var enTime = moment().add({'days': 1}).format('L')
		        var endTime = moment().add({'days': 1}).format('YYYY-MM-DD hh:mm:ss')
		        
                applyFilter(starTime, endTime);
                
            //     (function worker() {
		          //    applyFilter()
		          //    setTimeout(worker, 60000);
		          //})();
                
             
                // $("#download").click(function(){
                //      repSt = $("#reportstart").val();
                //      repEn = $("#reportend").val();
                //      interval = $("#interval").val();
                //      console.log("rep st", repSt, repEn, interval);
                     
                 
                // }); 
                
                  $("#view").click(function(event){
                    event.preventDefault();
                     repSt = $("#txtStartDate").val();
                     repEn = $("#txtEndDate").val();
                     interval = $("#txtInterval").val();
                     
                    //  repSt = "2021-09-10";
                    //  repEn = "2021-09-17";
                    //  interval = "30 MINUTE";
                     console.log("rep st", repSt, repEn, interval);
                    $.ajax({                                                                                                             	
                      type: "POST",
                      url: 'reportView.php',
                      async: false,
                      data: {start: repSt, end: repEn, interval: interval },
                      success: function(awsData)
                      {
                            var sourceArr = [];
                            $("#myexample").DataTable().destroy();
                           
                          //  console.log("aws1 data", awsData);
                            awsData = JSON.parse(awsData);
                            console.log("aws data", awsData);
                            if(awsData.length > 0) {
                                for (var i = 0; i < awsData.length; i++) {
                                    
                                       tempArr = [];
                                    tempArr.push(awsData[i]["WSmax"]);
                                    tempArr.push(awsData[i]["WSavg"]);
                                    tempArr.push(awsData[i]["WSmin"]);
                                    tempArr.push(awsData[i]["TMPmax"]);
                                    tempArr.push(awsData[i]["TMPavg"]);
                                    tempArr.push(awsData[i]["TMPmin"]);
                                    tempArr.push(awsData[i]["RHmax"]);
                                    tempArr.push(awsData[i]["RHavg"]);
                                    tempArr.push(awsData[i]["RHmin"]);
                                    tempArr.push(awsData[i]["Battery"]);
                                    tempArr.push(awsData[i]["WD"]);
                                    tempArr.push(awsData[i]["BPavg"]);
                                    tempArr.push(awsData[i]["RAINavg"]);
                                    tempArr.push(awsData[i]["SOLARavg"]);
                                     
                                    tempArr.push(awsData[i]["CreationDate"]);
                                   
                                    sourceArr.push(tempArr);
                                }
            	 	        }	
            	 	        
            	 	        createDataTable(sourceArr)
                		   
                      },
                      error: function() {
                            console.log("unsuccessful");
                                                                                         
                      }
                    });
                     
                 
                }); 
            
                $('.datepicker').daterangepicker({
                      singleDatePicker: true,
                    timePicker: true,
                    timePicker24Hour: true,
                    timePickerIncrement: 1,
                    showDropdowns: true,
                    minYear: 1901,
                    maxYear: parseInt(moment().format('YYYY'),10),
                    locale: {
                     format: 'YYYY-MM-DD HH:mm:ss'
                    }
                });
                
                // $('#reportend').daterangepicker({
                //     singleDatePicker: true,
                //     showDropdowns: true,
                //     minYear: 1901,
                //     maxYear: parseInt(moment().format('YYYY'),10)
                // });
                
                function createDataTable(sourceArr) {
                    
                            $('#myexample').DataTable({
                              
            		    	 
                            columns: [
                                          
 
                                            { title: "WS Max" },
                                            { title: "WS Avg" },
                                            { title: "WS Min" },
                                            { title: "Temp Max" },
                                            { title: "Temp Avg" },
                                            { title: "Temp Min" },
                                            { title: "RH Max" },
                                            { title: "RH Avg" },
                                            { title: "RH Min" },
                                            { title: "Battery" },
                                            { title: "WD" },
                                            { title: "BP Avg" },
                                            { title: "Rain" },
                                            { title: "Solar Avg" },
                                            { title: "Creation Date" },
                                ],
                                data: sourceArr,
            			        
                                deferRender: true,
                                //scrollY: 200,
                                scrollCollapse: true,
                                scroller: true,
                                "searching": true,
            			        scrollX: true,
            			        filter: true,
            			         order: [[14, 'desc']],
                                //dom: 'Bfrtip'
                               
                                   
            		
            		        }); //Table end
                }
                
                //  buttons: [
                //                   'excelHtml5', 'pdfHtml5', 'csvHtml5'
                //                 ],
                
                
                function applyFilter(start, end) {
                    //console.log("start and end date", start, end);
                    
                    $.ajax({                                                                                                             	
                      type: "POST",
                      url: 'reportList.php',
                      async: false,
                      data: {start: start, end: end },
                      success: function(awsData)
                      {
                            var sourceArr = [];
                            //$("#myexample").DataTable().destroy();
                            //console.log("aws data", awsData);
                            
                            awsData = JSON.parse(awsData);
                            if(awsData.length > 0) {
                                for (var i = 0; i < awsData.length; i++) {
                                    
                                    tempArr = [];
                                    tempArr.push(awsData[i]["WSmax"]);
                                    tempArr.push(awsData[i]["WSavg"]);
                                    tempArr.push(awsData[i]["WSmin"]);
                                    tempArr.push(awsData[i]["TMPmax"]);
                                    tempArr.push(awsData[i]["TMPavg"]);
                                    tempArr.push(awsData[i]["TMPmin"]);
                                    tempArr.push(awsData[i]["RHmax"]);
                                    tempArr.push(awsData[i]["RHavg"]);
                                    tempArr.push(awsData[i]["RHmin"]);
                                    tempArr.push(awsData[i]["Battery"]);
                                    tempArr.push(awsData[i]["WD"]);
                                    tempArr.push(awsData[i]["BPavg"]);
                                    tempArr.push(awsData[i]["RAINavg"]);
                                    tempArr.push(awsData[i]["SOLARavg"]);
                                     
                                    tempArr.push(awsData[i]["CreationDate"]);
                                   
                                    sourceArr.push(tempArr);
                                }
            	 	        }	
            	 	        
            	 	        createDataTable(sourceArr)
                		   
                      },
                      error: function() {
                            console.log("unsuccessful");
                                                                                         
                      }
                    });
                }
		
	           // var start = moment().subtract(29, 'days').calendar();;
	           // var end = moment().format('L');
	    
	           // function cb(start, end) {
	           //     $("#myexample").DataTable().destroy();
	           //     $('#reportrange span').html(start + ' - ' + end);
	           // }
	    
	           // $('#reportrange').daterangepicker({
	           //     opens: 'left'
        	   //     singleDatePicker: true,
        	   //    // startDate: start,
        	   //    // endDate: end
        	   //    // locale: {
        	   //    //    format: 'MMMM D, YYYY',
        	   //    // },
        	   //    // ranges: {
        	   //    //    'Today': [moment(), moment()],
        	   //    //    'Last 1 hour': [moment().subtract({'hours': 1}), moment()],
        	   //    //    'Last 24 hour': [moment().subtract({'hours': 25}), moment()],
        	   //    //    'Last 12 hour': [moment().subtract({'hours': 13}), moment()],
        
        	   //      //    'Last 7 Days': [moment().subtract(6, 'days'), moment()],
        	   //      //    'Last 30 Days': [moment().subtract(29, 'days'), moment()],
        	   //      //    'This Month': [moment().startOf('month'), moment().endOf('month')],
        	   //      //   'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        	   //     //}
        	   // });
        	    
	            //cb(start, end);
	    
	            var mself = this
	            $('#reportrange').on('apply.daterangepicker', function(ev, picker) {
		            //console.log(picker.startDate.format('YYYY-MM-DD hh:mm:ss'));
		            //console.log(picker.endDate.format('YYYY-MM-DD hh:mm:ss'));
		            var start = picker.startDate.format('L')
		            var end = picker.endDate.format('L')
		            localStorage.setItem("filter", true);

		            applyFilter(start, end) 

	            });
		
		
		    });
       
       </script>
    </body>
</html>
<?php } ?>

