<?php
require('../config/dbcon.php');
    $query = "SELECT timeout from timeout_mst";
    $rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
    while ($row = mysqli_fetch_assoc($rs)) {
    	$timeout = $row['timeout'];
    }
    $query = "select screenwidth from width_mst";
    $rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
    while ($row = mysqli_fetch_assoc($rs)) {
    	$width = $row['screenwidth'];
    }
    $width = $width . "px";
    //$width = '2000';
session_start();
if(isset($_SESSION["user"]))
{
 if((time() - $_SESSION['last_time']) > $timeout) // Time in Seconds
 {
 header("location:../login/logout.php");
 }
 else
 {
 $_SESSION['last_time'] = time();
 //echo "<h1 align='center'>Welcome ".$_SESSION["user"]. "</h1>";
 //echo "<h3 align='center'>Automatic Logout after 1 minute of inactive</h3>";
 //echo "<p align='center'><a href='../login/logout.php'>Logout</a></p>";
 //echo "<p align='center'><a href='../main/welcome.php'>Welcome1</a></p>";
 }
}
else
{
 header('Location:../login/index.php');
}
?>
<html>
<head>
	<title>IMS-Purchase Reports</title>
</head>
<script type="text/javascript">
	function preventBack() {
		window.history.forward();
	}
	setTimeout("preventBack()", 0);
	window.onunload = function() {
		null
	};
</script>
<title>Purchase Reports</title>
<link rel = "icon" href = "https:../image/ims.png" type = "image/x-icon">
<script src='../js/jquery-3.1.1.min.js' type='text/javascript'></script>
<script src='../js/jquery-ui.min.js' type='text/javascript'></script>
<link href="../css/style.css" rel="stylesheet" />
<link href="../css/style1.css" rel="stylesheet" />
<link href='../css/jquery-ui.min.css' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="../css/all.css">
<link href="../css/bootstrap.min.css" rel="stylesheet" />
<link href="../css/font-awesome.min.css" rel="stylesheet" />
<link href="../css/styleg.css" rel="stylesheet" />
<script type="text/javascript" src="../js/jquery.placeholder.label.js"></script>
<style>
    label {
        font-size: 15px;
        top: 11px;
        margin-bottom: .5rem;
        margin-left: 5px;
        font-weight:bold;
        display: inline-transparent;
    }
</style>
<body style="min-min-width: <?php echo $width; ?>; width: auto; width: auto;">
    <div style="min-min-width: <?php echo $width; ?>; width: auto; width: auto;">
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary" style="min-width: <?php echo $width; ?>; width: auto; height: 62px; background-color: #007bff; font-size:24px;">
        <input style = "background-color: #007bff; font-size:24px; width:100px; height : 55px; margin-top : px;" type="button" onclick="location.href='../main/index';" value="Home">
        <input style = "background-color: #007bff; font-size:24px; width:200px; height : 55px; margin-top : px;" type="button" onclick="exportToExcel('tblexportData', 'Export-data')" value="Export to Excel">
        <h1 style="color:white; font-size:24px; margin-top: -40px;" class="text-primary" align="center">Purchase Report</h1>
        <h1 style="color:white; font-size:24px; margin-top : -40px;margin-right :20px;" class="text-primary" id="demo2" align="right"></h1>
    </nav>
	<div class="container" style = "min-width: <?php echo $width; ?>; width: auto;">
	<form class="form-inline" method="POST" action="">		
	<center>
		<tr>			
			<label style="font-size:18px; font-weight:bold;">Date [DD/MM/YYY]:</label>
			<input type="name" style="font-size:20px; font-weight:bold; width: 250px; height: 40px;" id="fdate" value="<?php date_default_timezone_set('Asia/Kolkata'); echo date( 'd-m-Y'); ?>" class="roundedCategory" placeholder="Start"  name="fdate"/>
			<!--input type="date" style="font-size:20px; font-weight:bold; width: 250px;" id="fdate" class="roundedCategory" placeholder="Start"  name="date1"/-->
			<label style="font-size:18px; font-weight:bold;">To [DD/MM/YYY]:</label>
			<input type="name" style="font-size:20px; font-weight:bold;  width: 250px; height: 40px;" id="tdate" value="<?php date_default_timezone_set('Asia/Kolkata'); echo date( 'd-m-Y'); ?>" class="roundedCategory" placeholder="End"  name="tdate"/>
			<button type="button" name="action" id="action" style="font-size:20px; font-weight:bold; height : 50px; width : 150px;" class="btn btn-primary">Search</button>
			<button type="button" name="action1" id="action1" style="font-size:20px; font-weight:bold; height : 50px; width : 150px;" class="btn btn-primary">Refresh</button>
		</tr>
	</center>
	<br>
	<center>
		<tr>	
		    <button type="button" name="action2" id="action2" style="font-size:20px; font-weight:bold; height : 50px; width : 250px;" class="btn btn-primary">Supplier Wise Purchase</button>
		    <button type="button" name="action3" id="action3" style="font-size:20px; font-weight:bold; height : 50px; width : 250px;" class="btn btn-primary">Part Wise Purchase</button>
		    <button type="button" name="action4" id="action4" style="font-size:20px; font-weight:bold; height : 50px; width : 250px;" class="btn btn-primary">Segment Wise Purchase</button>
		    <button type="button" name="action4" id="action5" style="font-size:20px; font-weight:bold; height : 50px; width : 250px;" class="btn btn-primary">Part Wise Purchase</button>
		</tr>
	</center>
	</form>
	</div>
	<div style="min-width: <?php echo $width; ?>; width: auto; margin-left : 20px; margin-top : 10px; height: 45px;" class=container">
		<div class="form-group">
		    <center>
    			<div class="input-group">
    			    <input style ="margin-left : 0px; width : 1600px; font-size:20px; font-weight:bold;" name="search_text" id="search_text" type="text" placeholder="search" alt="search">
    			</div>
			</center>
		</div>
	</div>
	<br>
	</div>
	<div style="margin-top : 0px; margin-left : 10px; min-width: <?php echo $width; ?>; width: auto; height:50%; margin-right: 10px; margin-top: 0px; overflow:auto;" id="result" class="table-responsive"  align="center"></div>
	<div style="height: 60px; margin-left:0px; margin-top:0px; font-size:30px;" class="success"></div>
	<div style="height: 60px; margin-left:0px; margin-top:0px; font-size:30px;" class="error"></div>
	</div>
</body>
</html>
<!--script src='js/app.js' type='text/javascript'></script-->
<?php
	$date = new DateTime();
	$current_timestamp = $date->getTimestamp();
?>
<script type="text/javascript">
    $(function () {
        $("#fdate").datepicker({ 
            dateFormat: 'dd-mm-yy' 
        });
        $("#tdate").datepicker({ 
            dateFormat: 'dd-mm-yy' 
        });
    });
</script>
<script>
    $(document).on('keyup', function(e) {
        if (e.key == "Escape"){
          window.location.href = "../main/index"
        }
    });
</script>
<script>
	flag_time = true;
	timer = '';
	setInterval(function(){phpJavascriptClock();},1000);
	
	function phpJavascriptClock()
	{
		if ( flag_time ) {
		timer = <?php echo $current_timestamp;?>*1000;
		}
		var d = new Date(timer);
		months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
		
		month_array = new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'Augest', 'September', 'October', 'November', 'December');
		
		day_array = new Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		
		currentYear = d.getFullYear();
		month = d.getMonth();
		var currentMonth = months[month];
		var currentMonth1 = month_array[month];
		var currentDate = d.getDate();
		currentDate = currentDate < 10 ? '0'+currentDate : currentDate;
		
		var day = d.getDay();
		current_day = day_array[day];
		var hours = d.getHours();
		var minutes = d.getMinutes();
		var seconds = d.getSeconds();
		
		var ampm = hours >= 12 ? 'PM' : 'AM';
		hours = hours % 12;
		hours = hours ? hours : 12; // the hour ’0′ should be ’12′
		minutes = minutes < 10 ? '0'+minutes : minutes;
		seconds = seconds < 10 ? '0'+seconds : seconds;
		var strTime = hours + ':' + minutes+ ':' + seconds + ' ' + ampm;
		timer = timer + 1000;
		//document.getElementById("demo").innerHTML= currentMonth+' ' + currentDate+' , ' + currentYear + ' ' + strTime ;
		
		//document.getElementById("demo1").innerHTML= currentMonth1+' ' + currentDate+' , ' + currentYear + ' ' + strTime ;
		
		document.getElementById("demo2").innerHTML='Date : ' + currentDate+'-' +(month+1)+'-' +currentYear + ' Time : ' + strTime ;
		
		//document.getElementById("demo3").innerHTML= strTime ;
		
		//document.getElementById("demo4").innerHTML= current_day + ' , ' +currentMonth1+' ' + currentDate+' , ' + currentYear + ' ' + strTime ;		
		flag_time = false;
	}

</script>
<script>
	$(document).ready(function() {
		load_data();
		function load_data(query) {
			$.ajax({
				url: "search",
				method: "post",
				data: {
					query: query
				},
				success: function(data) {
					$('#result').html(data);
					$('#segment_name').val('');
				}
			});
		}
		$('#search_text').keyup(function() {
			var search = $(this).val();
			if (search !== '') {
				load_data(search);
			} else {
				load_data();
			}
		});
		
		$('#action').click(function() {
			var date1 = $('#fdate').val();
			var date2 = $('#tdate').val();			
			var action = $('#action').text();
			$actiontext = $('#action').text();
			if (date1 != '' && date2 != ''){
				$.ajax({
					url: "action",
					method: "POST",
					data: {
						date1: date1,
						date2: date2,						
						action: action
					},
					success: function(data) {
						$('#result').html(data);
					}
				});
			}
		});
		
		$('#action1').click(function() {
			var action = $('#action1').text();
			$actiontext = $('#action1').text();			
			$.ajax({
				url: "action1",
				method: "POST",
				data: {						
					action: action
				},
				success: function(data) {
					$('#result').html(data);
				}
			});
			
		});
		
		$('#action2').click(function() {
			var date1 = $('#fdate').val();
			var date2 = $('#tdate').val();			
			var action = $('#action2').text();
			$actiontext = $('#action2').text();
			if (date1 != '' && date2 != ''){
				$.ajax({
					url: "customerwisesales",
					method: "POST",
					data: {
						date1: date1,
						date2: date2,						
						action: action
					},
					success: function(data) {
						$('#result').html(data);
					}
				});
			}
		});
		
	});
</script>
<script type="text/javascript">
	function exportToExcel(tableID, filename = '') {
		var downloadurl;
		var dataFileType = 'application/vnd.ms-excel';
		var tableSelect = document.getElementById(tableID);
		var tableHTMLData = tableSelect.outerHTML.replace(/ /g, '%20');
		filename = filename ? filename + '.xls' : 'export_excel_data.xls';
		downloadurl = document.createElement("a");
		document.body.appendChild(downloadurl);
		if (navigator.msSaveOrOpenBlob) {
			var blob = new Blob(['\ufeff', tableHTMLData], {
				type: dataFileType
			});
			navigator.msSaveOrOpenBlob(blob, filename);
		} else {
			downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;
			downloadurl.download = filename;
			downloadurl.click();
		}
	}
</script>