<?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();
 }
}
else
{
 header('Location:../login/index.php');
}
?>
<?php
    require('../config/dbcon.php');
    $query = "Select * from control_mst where description='TRUCK'";
    $rs = mysqli_query($connect, $query) or die("Cannot execute query: $query\n");
    while ($row = mysqli_fetch_assoc($rs)) {
    	$srno = $row['srno'];
    }
?>
<html>

<head>
    <title>SSR-Truck Master</title>
    <link rel = "icon" href = "https:../image/ims.png" type = "image/x-icon">
</head>
<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-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">Truck Master</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;">
	<center>
			<tr>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">party_code</label>
					<input type="text" id="id" class="roundedCategory" readonly style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" value="<?php echo $srno; ?>"  oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				<div class="col-xs-4">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_number</label>
					<input type="text" id="truck_number" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required autofocus>
				</div>
				<div class="col-xs-3">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_owner_name</label>
					<input type="text" id="truck_owner_name" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				<div class="col-xs-3">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_remarks</label>
					<input type="text" id="truck_remarks" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				
			</tr>
		</center>
		<center>
			<tr>
			    <div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_address_one</label>
					<input type="text" id="truck_address_one" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
			    <div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_address_two</label>
					<input type="text" id="truck_address_two" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" onfocus="this.select();" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_place</label>
					<input type="text" id="truck_place" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" onfocus="this.select();" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_mobile_number</label>
					<input type="text" id="truck_mobile_number" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_pan_number</label>
					<input type="text" id="truck_pan_number" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" placeholder="" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_bank_name</label>
					<input type="text" id="truck_bank_name" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
			</tr>
		</center>
		<center>
			<tr>
			    <div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_bank_acc_number</label>
					<input type="text" id="truck_bank_acc_number" class="roundedCategory" maxlength="15" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_bank_branch</label>
					<input type="text" id="truck_bank_branch" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_ifsc_code</label>
					<input type="text" id="truck_ifsc_code" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				<div class="col-xs-2">
					<label style="font-size:18px;" class="col-sm-2 control-label">truck_aadhar_no</label>
					<input type="text" id="truck_aadhar_no" class="roundedCategory" style="font-size:18px; width:100%; height: 35px; font-weight:bold; color: #000066;" oninput="this.value = this.value.toUpperCase()" autocomplete="off" required>
				</div>
				
			</tr>
		</center>
		<br><br><br><br><br><br><br><br>
	<div style="margin-top : 5px;min-width: <?php echo $width; ?>; width: auto;" align="center">
		<input type="hidden" style="font-size:18px; font-weight:bold;  color: #000066;" name="user_id" id="user_id" />
		<button type="button" name="action" id="action" style="width : 200px; height: 40px;" class="btn btn-primary">Add</button>
	</div>
	</div>
	<div class="container" style = "min-width: <?php echo $width; ?>; width: auto;">
	    <center>
			<tr>
			    <input style ="margin-left : 0px; width : 100%; height:40px; font-size:20px; font-weight:bold;" name="search_text" id="search_text" type="text" placeholder="search" alt="search">
			<tr>
		</center>
	</div>
	<div style="margin-top : 0px; margin-left : 0px;min-width: <?php echo $width; ?>; width: auto; height:50%; margin-left: 0px; 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>
<?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>
	$(document).keydown(function(objEvent) {
		if (objEvent.keyCode == 9) {
			objEvent.preventDefault();
		}
	})
	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>
    function GetPartyCode(x) {
       $.ajax({
          url: 'getpartycode',
          data: "",
          dataType: 'json',
          success: function(data){
    		$('#id').val(data.srno);		
          } 
        });
    }
	$(document).ready(function() {
		load_data();
        GetPartyCode();
		function load_data(query) {
			$.ajax({
				url: "search.php",
				method: "post",
				data: {
					query: query
				},
				success: function(data) {
					$('#result').html(data);
					$('#user_id').val('');
					$('#truck_number').val('');
					$('#truck_owner_name').val('');
					$('#truck_address_one').val('');
					$('#truck_address_two').val('');
					$('#truck_place').val('');
					$('#truck_mobile_number').val('');
					$('#truck_pan_number').val('');
					$('#truck_bank_name').val('');
					$('#truck_bank_acc_number').val('');
					$('#truck_bank_branch').val('');
					$('#truck_ifsc_code').val('');
					$('#truck_aadhar_no').val('');
					$('#truck_remarks').val('');
					$('#action').text("Add");
				}
			});
		}
		function load_data1(query){
			$.ajax({
				url:"search.php",
				method:"post",
				data:{query:query},
				success:function(data)
				{
					$('#result').html(data);				
				}
			});
		}
		$('#search_text').keyup(function() {
			var search = $(this).val();
			if (search !== '') {
				load_data1(search);
			} else {
				load_data();
				GetPartyCode();
			}
		});
		$(function() {
			$("#truck_owner_name").autocomplete({
				source: function(request, response) {
					$.ajax({
						url: "fetchtruck.php",
						type: 'post',
						dataType: "json",
						data: {
							search: request.term
						},
						success: function(data) {
							response(data);
						}
					});
				},
				select: function(event, ui) {
				    //$('#truck_number').val(ui.item.label);
					$('#truck_owner_name').val(ui.item.truck_owner_name);
					$('#truck_address_one').val(ui.item.truck_address_one);
					$('#truck_address_two').val(ui.item.truck_address_two);
					$('#truck_place').val(ui.item.truck_place);
					$('#truck_mobile_number').val(ui.item.truck_mobile_number);
					$('#truck_pan_number').val(ui.item.truck_pan_number);
					$('#truck_bank_name').val(ui.item.truck_bank_name);
					$('#truck_bank_acc_number').val(ui.item.truck_bank_acc_number);
					$('#truck_bank_branch').val(ui.item.truck_bank_branch);
					$('#truck_ifsc_code').val(ui.item.truck_ifsc_code);
					$('#truck_aadhar_no').val(ui.item.truck_aadhar_no);
					$('#truck_remarks').val(ui.item.truck_remarks);
					document.getElementById("truck_remarks").focus();
					return false;
				}
			});
		});
		$('#action').click(function() {
			var id = $('#id').val();
			var truck_number = $('#truck_number').val();
			var truck_owner_name = $('#truck_owner_name').val();
			var truck_address_one = $('#truck_address_one').val();
			var truck_address_two = $('#truck_address_two').val();
			var truck_place = $('#truck_place').val();
			var truck_mobile_number = $('#truck_mobile_number').val();
			var truck_pan_number = $('#truck_pan_number').val();
			var truck_bank_name = $('#truck_bank_name').val();
			var truck_bank_acc_number = $('#truck_bank_acc_number').val();
			var truck_bank_branch = $('#truck_bank_branch').val();
			var truck_ifsc_code = $('#truck_ifsc_code').val();
			var truck_aadhar_no = $('#truck_aadhar_no').val();
			var truck_remarks = $('#truck_remarks').val();
			var action = $('#action').text();
			if (truck_number != '' && truck_owner_name != '' && truck_address_one != '' && truck_address_two != '' && truck_place != '' && truck_mobile_number != '' && truck_pan_number != '' && truck_bank_name != '' && truck_bank_acc_number != '' && truck_bank_branch != '' && truck_ifsc_code != '' && truck_aadhar_no != '' && truck_remarks != '') {
				$.ajax({
					url: "action.php",
					method: "POST",
					data: {
						truck_number: truck_number,
						truck_owner_name: truck_owner_name,
						truck_address_one: truck_address_one,
						truck_address_two: truck_address_two,
						truck_place: truck_place,
						truck_mobile_number: truck_mobile_number,
						truck_pan_number: truck_pan_number,
						truck_bank_name: truck_bank_name,
						truck_bank_acc_number: truck_bank_acc_number,
						truck_bank_branch: truck_bank_branch,
						truck_ifsc_code: truck_ifsc_code,
						truck_aadhar_no: truck_aadhar_no,
						truck_remarks: truck_remarks,
						id: id,
						action: action
					},
					success: function(data) {
						$('.success').show(1000).html("Record Saved Successfully.").delay(1000).fadeOut(1000);
						load_data();
						GetPartyCode();
					}
				});
			} 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.php",
				method: "POST",
				data: {
					id: id
				},
				dataType: "json",
				success: function(data) {
					$('#action').text("Update");
					$('#user_id').val(id);
					$('#id').val(data.id);
					$('#truck_number').val(data.truck_number);
					$('#truck_owner_name').val(data.truck_owner_name);
					$('#truck_address_one').val(data.truck_address_one);
					$('#truck_address_two').val(data.truck_address_two);
					$('#truck_place').val(data.truck_place);
					$('#truck_mobile_number').val(data.truck_mobile_number);
					$('#truck_pan_number').val(data.truck_pan_number);
					$('#truck_bank_name').val(data.truck_bank_name);
					$('#truck_bank_acc_number').val(data.truck_bank_acc_number);
					$('#truck_bank_branch').val(data.truck_bank_branch);
					$('#truck_ifsc_code').val(data.truck_ifsc_code);
					$('#truck_aadhar_no').val(data.truck_aadhar_no);
					$('#truck_remarks').val(data.truck_remarks);
				}
			})
		});
		$(document).on('click', '.delete', function() {
			var id = $(this).attr("id");
			if (confirm("Are you sure you want to remove this data?")) {
				var action = "Delete";
				$.ajax({
					url: "action.php",
					method: "POST",
					data: {
						id: id,
						action: action
					},
					success: function(data) {
						$('.success').show(1000).html("Record Deleted Successfully.").delay(1000).fadeOut(1000);
						load_data();
					}
				})
			} else {
				return false;
			}
		});
		$(document).ready(function() {
			$('input').on('keypress', function(e) {
				if (e.which == 13) {
					switch ($(this).attr('id')) {
						case 'truck_number':
							if (!document.getElementById("truck_number").value.length == 0) {
								$('#truck_owner_name').focus();
								e.preventDefault();
								break;
							}
						case 'truck_owner_name':
							if (!document.getElementById("truck_owner_name").value.length == 0) {
								$('#truck_address_one').focus();
								e.preventDefault();
								break;
							}
						case 'truck_address_one':
							if (!document.getElementById("truck_address_one").value.length == 0) {
								$('#truck_address_two').focus();
								e.preventDefault();
								break;
							}
						case 'truck_address_two':
							if (!document.getElementById("truck_address_two").value.length == 0) {
								$('#truck_place').focus();
								e.preventDefault();
								break;
							}
						case 'truck_place':
							if (!document.getElementById("truck_place").value.length == 0) {
								$('#truck_mobile_number').focus();
								e.preventDefault();
								break;
							}
						case 'truck_mobile_number':
							if (!document.getElementById("truck_mobile_number").value.length == 0) {
								$('#truck_pan_number').focus();
								e.preventDefault();
								break;
							}
						case 'truck_pan_number':
							if (!document.getElementById("truck_pan_number").value.length == 0) {
								$('#truck_bank_name').focus();
								e.preventDefault();
								break;
							}
						case 'truck_bank_name':
							if (!document.getElementById("truck_bank_name").value.length == 0) {
								$('#truck_bank_acc_number').focus();
								e.preventDefault();
								break;
							}
						case 'truck_bank_acc_number':
							if (!document.getElementById("truck_bank_acc_number").value.length == 0) {
								$('#truck_bank_branch').focus();
								e.preventDefault();
								break;
							}
						case 'truck_bank_branch':
							if (!document.getElementById("truck_bank_branch").value.length == 0) {
								$('#truck_ifsc_code').focus();
								e.preventDefault();
								break;
							}
						case 'truck_ifsc_code':
							if (!document.getElementById("truck_ifsc_code").value.length == 0) {
								$('#truck_aadhar_no').focus();
								e.preventDefault();
								break;
							}
						case 'truck_aadhar_no':
							if (!document.getElementById("truck_aadhar_no").value.length == 0) {
								$('#truck_remarks').focus();
								e.preventDefault();
								break;
							}
						case 'truck_remarks':
							if (!document.getElementById("truck_remarks").value.length == 0) {
								$('#action').focus();
								e.preventDefault();
								break;
							}
					}
				}
			});
		});
	});
</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>