<?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'];
    }
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-Supplier Discount Master</title>
<script type="text/javascript">
	function preventBack() {
		window.history.forward();
	}
	setTimeout("preventBack()", 0);
	window.onunload = function() {
		null
	};
</script>
<title>Supplier Discount Master</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>
</head>
<body style="min-width: <?php echo $width; ?>; width: auto;">
<div style="min-width: <?php echo $width; ?>; 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; 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">Supplier Discount</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; margin-left: 0px; margin-top: -20px;">
    	<center>
    		<tr>
    			<input size="50" type="hidden" name="disc_srno" class="form-control inputstl" readonly id="disc_srno" value="" autocomplete = "off" required>
				<div class="col-xs-2">
				    <label style="font-size:18px;" class=" col-sm-2 control-label">supplier_code</label>
					<input type="text" id="supplier_code" class="roundedCategory" readonly style="font-size:18px; width:100%; height: 40px; font-weight:bold; color: #000066;" value="" autocomplete="off" required>
				</div>
				<div class="col-xs-3">
				    <label style="font-size:18px;" class=" col-sm-2 control-label">supplier_name</label>
					<input type="text" id="supplier_name" class="roundedCategory" readonly style="font-size:18px; width:100%; height: 40px; font-weight:bold; color: #000066;" value="" autocomplete="off" required>
				</div>
				<div class="col-xs-3">
				    <label style="font-size:18px;" class=" col-sm-2 control-label">supplier_place</label>
					<input type="text" id="supplier_place" class="roundedCategory" readonly style="font-size:18px; width:100%; height: 40px; font-weight:bold; color: #000066;" value="" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
				    <label style="font-size:18px;" class=" col-sm-2 control-label">segment</label>
					<input type="text" id="segment" class="roundedCategory" readonly style="font-size:18px; width:100%; height: 40px; font-weight:bold; color: #000066;" value="" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class=" col-sm-2 control-label">disc_per</label>
					<input type="text" id="disc_per" class="roundedCategory" style="font-size:18px; width:100%; height: 40px; font-weight:bold; color: #000066;" value=""onfocus="this.select();" autocomplete="off" required>
				</div>
    		</tr>
    	</center>	
    </div>
    
	<div style="margin-top : -10px;min-width: <?php echo $width; ?>; width: auto;" align="center">
		<input type="hidden" name="user_id" id="user_id" />
		<input style = "background-color: #007bff; height : 40px; margin-top : 0px;  margin-left : 0px;" type="button" name="action" id="action" value="Update">
	</div>
	<div style="min-width: <?php echo $width; ?>; width: auto; margin-left : 0px; margin-top : 10px; height: 45px;" class=container">
		<div class="form-group">
		    <center>
    			<div class="input-group">
    			    <input style ="margin-left : 0px; width : 1800px; height: 40px; font-size:20px; font-weight:bold;" name="search_text" id="search_text" type="text" placeholder="search" alt="search">
    			</div>
			</center>
		</div>
	</div>	
	<br>
	<div style="margin-top : 0px; margin-left : 10px;min-width: <?php echo $width; ?>; width: auto; margin-right : 20px; font-size:20px; margin-left: 0px; margin-top: 0px;" id="result" class="table-responsive"></div>
	</div>
</body>
</html>
<?php
	$date = new DateTime();
	$current_timestamp = $date->getTimestamp();
?>
<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("demo2").innerHTML='Date : ' + currentDate+'-' +(month+1)+'-' +currentYear + ' Time : ' + strTime ;
		flag_time = false;
	}

</script>
<script>
$(document).ready(function(){
	
	$(document).keydown(function(objEvent) {
		if (objEvent.keyCode == 9) {  //tab pressed
			objEvent.preventDefault(); // stops its action
		}
	})	
	load_data();
	function load_data(query)
	{
		$.ajax({
			url:"search",
			method:"post",
			data:{query:query},
			success:function(data)
			{
				$('#result').html(data);
				document.getElementById('supplier_code').value = "";
				document.getElementById('supplier_name').value = "";
				document.getElementById('supplier_place').value = "";
				document.getElementById('segment').value = "";
				document.getElementById('disc_per').value = "";
				$('#search_text').focus();
			}
		});
	}	
	$('#search_text').keyup(function(){
		var search = $(this).val();
		if(search != '')
		{
			load_data(search);
		}
		else
		{
			load_data();			
		}
	});	
	$('#action').click(function() {
		var supplier_code = $('#supplier_code').val();
		var supplier_name = $('#supplier_name').val();
		var supplier_place = $('#supplier_place').val();
		var segment = $('#segment').val();
		var disc_per = $('#disc_per').val();		
		var id = $('#user_id').val();
		var action = $('#action').text();
		$actiontext = $('#action').text();
		if (supplier_code != '' && supplier_name != '' && supplier_place != '' && segment != '' && disc_per != ''){
			$.ajax({
				url: "action",
				method: "POST",
				data: {
					supplier_code: supplier_code,
					supplier_name: supplier_name,
					supplier_place: supplier_place,
					segment: segment,
					disc_per: disc_per,					
					id: id,
					action: action
				},
				success: function(data) {
					$('.success').show(1000).html("Record Saved Successfully.").delay(1000).fadeOut(1000);
					load_data();
				}
			});
		} else {
			$('.success').show(1000).html("All Fields are Required").delay(1000).fadeOut(1000);
		}
	});

	$(document).on('click', '.update', function() {
		var id = $(this).attr("id");
		$.ajax({
			url: "fetch",
			method: "POST",
			data: {
				id: id
			},
			dataType: "json",
			success: function(data) {
				$('#action').text("Update");
				$('#user_id').val(id);
				$('#supplier_code').val(data.supplier_code);
				$('#supplier_name').val(data.supplier_name);					
				$('#supplier_place').val(data.supplier_place);
				$('#segment').val(data.segment);
				$('#disc_per').val(data.disc_per);
				$('#disc_per').focus();
			}
		})
	});		
	//To check the input is not empty and send the cursor to next input.
	$(document).ready(function() {
		$('input').on('keypress', function(e) {
			if (e.which == 13) {
				switch ($(this).attr('id')) {
					case 'disc_per':
					if (!document.getElementById("disc_per").value.length == 0) {
						$('#action').focus();
						e.preventDefault();
						break;
					}																																				
				}
			}
		});
	});
});
</script>
<script>
function myFunction() {
  var str = document.getElementById("search_text").innerHTML; 
  var res = str.replace("*", "%");
  document.getElementById("demo").innerHTML = res;
}
</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');

		// Specify file name
		filename = filename ? filename + '.xls' : 'export_excel_data.xls';

		// Create download link element
		downloadurl = document.createElement("a");

		document.body.appendChild(downloadurl);

		if (navigator.msSaveOrOpenBlob) {
			var blob = new Blob(['\ufeff', tableHTMLData], {
				type: dataFileType
			});
			navigator.msSaveOrOpenBlob(blob, filename);
		} else {
			// Create a link to the file
			downloadurl.href = 'data:' + dataFileType + ', ' + tableHTMLData;

			// Setting the file name
			downloadurl.download = filename;

			//triggering the function
			downloadurl.click();
		}
	}
</script>