<?php 
	$header_file=APPPATH.'views/include/header.php';
	require($header_file);
	$sidenav_file=APPPATH.'views/include/sidenav.php';
	require($sidenav_file);	?>
  
    <!-- BEGIN: Content-->
    <div class="app-content content">
      <div class="content-overlay"></div>
      <div class="content-wrapper">
        <div class="content-header row">
          <div class="content-header-left col-md-12 col-12 mb-2 breadcrumb-new">
            <h3 class="content-header-title mb-0 d-inline-block">Report</h3>
            <div class="row breadcrumbs-top d-inline-block">
              <div class="breadcrumb-wrapper col-12">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="#">Home</a>
                  </li>
                  <li class="breadcrumb-item"><a href="#">Report</a>
                  </li>
                  <li class="breadcrumb-item active">Excel Report
                  </li>
                </ol>
              </div>
            </div>
          </div>
          
        </div>
        <div class="content-body"><!-- Basic form layout section start -->
<section id="basic-form-layouts">
	<div class="row justify-content-md-center " ng-app="reportApp">
		<div class="col-md-12" ng-controller="reportCtrl">
			<div class="card">
				
				<div class="card-content collapse show">
					<div class="card-body">
					<form autocomplete="off" method="POST" action="<?php echo site_url('ExcelReport/searchMonthQtyVolume/');?>"> 
					<div class="row">
								<div class="col-md-3">
									<div class="form-group">
										<!--<input class="form-control pickadate datepicker picker__input picker__input--active" type="text" name="fmonth"  id="fmonth" required data-uk-datepicker="{format:'DD-MM-YYYY'}" placeholder="From Date">-->
									<input type="text" name="fmonth" id="fmonth" class="form-control date-picker"><?php /*$a ="";
									$a.="cw";
									echo $a;
									
									
                                    $my_string = "Hello World!";

echo json_encode($my_string);*/
									?>
									</div>
								</div>
								<div class="col-md-3">
									<div class="form-group">
										<!--<input class="form-control pickadate datepicker picker__input picker__input--active" type="text" name="tmonth"  id="tmonth" required data-uk-datepicker="{format:'DD-MM-YYYY'}" placeholder="To Date">-->
										<input type="text" name="tmonth" id="tmonth" class="form-control date-picker">
									</div>
								</div>
								<div class="col-md-2">
									<div class="form-group">
										<select id="branch" name="branch" class="form-control select2" >
											<option value="">Branch</option>
											<?php  foreach($branch as $row)
												{
											?>
												<option value="<?php echo "$row->branch_name"; ?>"><?php echo $row->branch_name; ?></option>
											<?php
												} 
											?>
										</select>
									</div>
								</div>
							
								<div class="col-md-1">
									<div class="form-group">
										<input type="submit" name="submit" class="btn btn-primary " value="Search" ><!--ng-click="fetchQtyVolume()"-->
									</div>
								</div>
							</div>
						</form>
							<!--	<div ng-if="qvpcount!=0">
									<button onclick="method(btn)" class="btn btn-success pull-right">Export to Excel</button>
									</div>
									</br>
									<div ng-if="qvpcount==0">
										<h6>No Data Found</h6>
									</div>
									<div ng-if="qvpcount!=0" id="tresponse" class="table-responsive">
										<table class="table" >
											<thead>
											    <tr>
											        <th colspan="3"></th>
											        <th colspan="{{qvpclcount}}">Sum of Oty</th>
											    </tr>
												<tr>
													<th ng-repeat="key in getKeys(qvp[0])">{{key}}</th>
												</tr>
											</thead>
											<tbody>
												<tr ng-repeat="row in qvp">
													<td ng-repeat="col in getKeys(qvp[0])">{{row[col]}}</td>
												</tr>
											</tbody>
										</table>
										
										<table class="table" id="tests">
											<thead>
											    <tr>
											        <th colspan="{{spvclcount}}">Sum of Final Rate</th>
											    </tr>
												<tr>
													<th ng-repeat="key in getKeys(spv[0])">{{key}}</th>
												</tr>
											</thead>
											<tbody>
												<tr ng-repeat="row in spv">
													<td style="height:46px!important;" ng-repeat="col in getKeys(spv[0])">{{row[col]}}</td>
												</tr>
											</tbody>
										</table>
									</div>-->
								

									 <div id="raw_material"> </div>
							</div>
					</div>
				</div>
			</div>
		</div>
	</div>

</section>
<!-- // Basic form layout section end -->
        </div>
      </div>
    </div>
    <?php //require('include/theme_customizer.php');?>

   <?php 
  $footer_file=APPPATH.'views/include/footer.php';
	require($footer_file);
	?>
	
<script type="text/javascript">
<?php if(isset($_POST['submit'])) {
			
			$start = $month = strtotime($_POST['fmonth']);
			$end = strtotime($_POST['tmonth']);
			
			$diff = abs($end - $start);
			$years = floor($diff / (365*60*60*24));
			$months = floor(($diff - $years * 365*60*60*24)
                                 / (30*60*60*24));
			$colspan = $months+1;
			
			$mstart = $month = strtotime($_POST['fmonth']);
			$mend = strtotime($_POST['tmonth']);
}        
?>   
$(function() {
	var item = [];
	var items = "", item_name = "";
	var fproduct = "";
	var count = 1;
/*	var fmonth = $('#fmonth1').val();
	var tmonth = $('#tmonth1').val();
//	var dt = new Date(fmonth);
     dt.setMonth( dt.getMonth() + 1 );
    //var months = (tmonth.getFullYear() - fmonth.getFullYear()) * 12;
   // var x = monthDiff(new Date(fmonth), new Date(tmonth));
   //var x = monthDiff(new Date(2000, 01), new Date(2000, 02));
  // var months = tmonth.getMonth() - fmonth.getMonth() + (12 * (to.getFullYear() - from.getFullYear()));

//	alert(dt);*/
	$.getJSON('https://gsrgroup.biz/inv_demo_carnival/monthQtyVolume_list.json',function(data) {
		
		
			items += "<table border='1' class='table table-striped table-bordered nowrap dtable'>";
		//	items += "<tr><th>Category Name</th><th>Item Name</th><th>UOM</th><th style='width:100px'>Qty</th><th>Remarks</th></tr>";
		   items += "<tr><th colspan='2'></th><th colspan='<?php echo $colspan;?>'>Sum of Oty</th><th colspan='<?php echo $colspan;?>'>Sum of Final Rate</th></tr><tr><th>Category Name</th><th>Item Name</th><?php while($month <= $end){ $smonth=date('Y-m-d', $month);   $lmonth=date('Y-m-t', $month); ?><th><?php  echo date('M Y', $month);$month = strtotime("+1 month", $month); ?></th><?php } ?><?php while($mstart <= $mend){ $mmonth=date('Y-m-d', $mstart);   $mlmonth=date('Y-m-t', $mstart); ?><th><?php  echo date('M Y', $mstart);$mstart = strtotime("+1 month", $mstart); ?></th><?php } ?></tr>";
			for(i in data.products)
				{
					
					//	$.each(data.products[i].items, function(j,e) {
					items += "<tr>" + "<td>" + data.products[i].category_name + "</td>" + "<td>" +data.products[i].item_name+ "</td>";
				for(j in data.products[i].items)
				{
					items += "<td>" + data.products[i].items[j].qty + "</td>";
				}
				for(j in data.products[i].items)
				{
					items += "<td>" + data.products[i].items[j].grn_grand + "</td>";
				}
				items += "</tr>";
			//}
			}
			count++;
			items += "</table>"; 
			
		//}
		//alert(items);
		$('#raw_material').append(items);
		//$(items).appendTo("#item_table tbody");
	});
});
</script>
<script>
	
	var fetch = angular.module('reportApp',[]);
	fetch.controller('reportCtrl', ['$scope', '$http', '$timeout', function ($scope, $http, $timeout) {
						
		$scope.fetchQtyVolume = function(){
			
			var fmonth = angular.element(document.getElementById("fmonth"));
			window.localStorage["fmonthval"] = fmonth.val();
			var tmonth = angular.element(document.getElementById("tmonth"));
			window.localStorage["tmonthval"] = tmonth.val();
			var branch = angular.element(document.getElementById("branch"));
			window.localStorage["branchval"] = branch.val();			
			//$scope.acr = [];
						
			window.location.reload(true);
		}
		
		$scope.fmonth1 = window.localStorage["fmonthval"];
		$scope.tmonth1 = window.localStorage["tmonthval"];
		$scope.branch = window.localStorage["branchval"];
		//console.log($scope.fmonth1);
		//console.log($scope.tmonth1);
		
		//$scope.qvpcount = 0;
		$http({
			method: 'POST',
				url: '<?php echo base_url(); ?>index.php/ExcelReport/fetchQtyVolume',
				headers: {'Content-Type': 'application/json'},
				data: JSON.stringify({fmonth:$scope.fmonth1,tmonth:$scope.tmonth1,branch:$scope.branch})
			}).then(function successCallback(response) {
			
				// Assign response to users object
				$scope.qvp = response.data;
				$scope.qvpcount = response.data.length;
				if($scope.qvpcount == 3579 || $scope.qvpcount == '')
				{
				    $scope.qvpcount = 0;
				}
				else
				{
				    $scope.qvpcount = response.data.length;
				}
				console.log($scope.qvpcount);
								
				//datatable
				angular.element(document).ready(function() {  
					dTable = $('#user_table')  
					dTable.DataTable({
						dom: 'Bfrtip',
						"searching": true,
						buttons: [
							'csv', 'excel', 'pdf', 'print'
						]
					}); 
					
				});
		
				$scope.getKeys = function(obj) {
				    $scope.qvpclcounts = Object.getOwnPropertyNames(obj).slice(0, -1).length;
				    $scope.qvpclcount = $scope.qvpclcounts - 3;
				    //console.log($scope.qvpclcount - 3);
				    return Object.getOwnPropertyNames(obj).slice(0, -1)
				}
			});
			
			$http({
			method: 'POST',
				url: '<?php echo base_url(); ?>index.php/ExcelReport/fetchPurchaseVolume',
				headers: {'Content-Type': 'application/json'},
				data: JSON.stringify({fmonth:$scope.fmonth1,tmonth:$scope.tmonth1,branch:$scope.branch})
			}).then(function successCallback(response) {
			
				// Assign response to users object
				$scope.spv = response.data;
				$scope.spvcount = response.data.length;
				//console.log($scope.acrcount);
								
				//datatable
				angular.element(document).ready(function() {  
					dTable = $('#user_table1')  
					dTable.DataTable({
						dom: 'Bfrtip',
						"searching": true,
						buttons: [
							'csv', 'excel', 'pdf', 'print'
						]
					}); 
					
				});
		
				$scope.getKeys = function(obj) {
				    $scope.spvclcount = Object.getOwnPropertyNames(obj).slice(0, -1).length;
				    //console.log($scope.spvclcount);
					return Object.getOwnPropertyNames(obj).slice(0, -1)
				}
			});
			window.localStorage.clear();
	}]);
</script>
<script type="text/javascript">
	$(function() {
		$('.date-picker').datepicker( {
			changeMonth: true,
			changeYear: true,
			showButtonPanel: true,
			dateFormat: 'MM yy',
			onClose: function(dateText, inst) { 
				$(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
			}
		});
	});	
</script>
<script>
	
</script>
</body>
</html>