<?php
	include("includes/dbconfig.php");
	$pagetitle = "System User Setup";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	$err= "";
	
	$successsave="";
	
	if(isset($_POST['userfocus']))
	{
		$userfocus = $_POST['userfocus'];
	}
	
	if(!(isset($_POST['savedatabtn'])) && (isset($userfocus) && $userfocus > 0))
	{
		$sysuserdata = DB::queryFirstRow("SELECT * from settings WHERE id=%i", $userfocus);
		
		if(DB::count() == 1)
		{   
			$_POST['userfocus'] = $userfocus;
			$_POST['sname'] = ($sysuserdata['name'] != "") ? $sysuserdata['name'] : "";
			$_POST['scode'] = ($sysuserdata['code'] != "") ? $sysuserdata['code'] : "";
			$_POST['svalue'] = ($sysuserdata['value'] != "") ? $sysuserdata['value'] : "";
			$_POST['sinformation'] = ($sysuserdata['information'] != "") ? $sysuserdata['information'] : "";
			 
		}
	}
	
	if(isset($_POST['savedatabtn']))
	{
		if($_SESSION["connectedusergroup"] == 1)
		{
			if(trim($_POST['sname']) == "")
				$err = $err."Setting Name Required!<br>";
				
			if(trim($_POST['scode']) == "")
				$err = $err."Code is  Required!<br>";
			
			if(trim($_POST['sinformation']) == "")
				$err = $err."Information is  Required!<br>";
			
			 
			 
		}
		else
		{
			$err = $err."Limited Access!<br>";
		}
		
		if($err == "")
		{
			DB::$error_handler = false;
			DB::$throw_exception_on_error = true;
		
			if($userfocus)
			{
				 
				try
				{
					$trxnConnextn = new MeekroDB();
					$nonTrxnConnextn = new MeekroDB();
					
					$trxnConnextn->startTransaction();
					$trxnConnextn->query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
					$trxnConnextn->query("SELECT * FROM settings WHERE id=%i FOR UPDATE", $_POST['userfocus']);
					
					$trxnConnextn->update
					('settings', 
						array
						(
							  
							'name' => (trim($_POST['sname']) != "") ? trim($_POST['sname']) : NULL, 
							'value' => (trim($_POST['svalue']) != "") ? trim($_POST['svalue']) : NULL, 
							'code' => (trim($_POST['scode']) != "") ? trim($_POST['scode']) : NULL, 
							 
							 
							'information' => (trim($_POST['sinformation']) != "") ? trim($_POST['sinformation']) : NULL,
							 
						), 
						"id=%i", $_POST['userfocus']
					);
					
					 
					
					
						$trxnConnextn->commit();
?>
						<script>
							location.href="new_settings.php";
						</script>
<?php
						exit();
					
					
					
				}
				catch(MeekroDBException $e)
				{
					$err = $err."Critical Error Occured!<br>";
					$err = $err.$e->getMessage()."<br>";
					$err = $err.$e->getQuery()."<br>";
					$trxnConnextn->rollback();
					DB::$error_handler = 'meekrodb_error_handler';
					DB::$throw_exception_on_error = false;
				}
			}
		}
	}
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>System Setting</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<div class="card card-primary">
						<?php
							if($_SESSION["connectedusergroup"] == 1)
							{
						?>
						<div class="card-header with-border text-center">
							<h3 class="card-title">Edit System setting</h3>
						</div>
						
						<div class="card-body">
							<?php
								if($err!="")
								{
							?>
							<div class="alert alert-danger alert-dismissable">
								<i class="fa fa-ban-circle"></i>
								<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><br>
								<?php 
									echo $err; 
								?>
							</div>
							<?php
								}
							?>
							
							<?php
								if($successsave!="")
								{
							?>
							<div class="alert alert-success alert-dismissable">
								<i class="fa fa-ok"></i>
								<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
								<?php 
									echo $successsave; 
								?>
							</div>
							<?php
								}
							?>
							<form role="form"  action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
								<INPUT name="userfocus" id="userfocus" type=hidden value="<?php echo isset($_POST['userfocus']) ? $_POST['userfocus'] : 0 ?>">  
							<div class="row">
								<div class="col-sm-8">
									<div class="form-group">
										<label for="sname"> Name</label>
										<input type="text" readonly   name="sname" id="sname" maxlength="40" class="form-control" value="<?php echo isset($_POST['sname']) ? $_POST['sname'] : "" ?>" >
									</div><!-- form-group -->
									
									<div class="form-group">
										<label for="scode">Code</label>
										<input type="number" readonly name="scode" id="scode" maxlength="40" class="form-control" value="<?php echo isset($_POST['scode']) ? $_POST['scode'] : "" ?>" >
									</div><!-- form-group -->
									
									<div class="form-group">
										<label for="svalue">Set percentage %</label>
										<input type="text" name="svalue" id="svalue" maxlength="15" class="form-control" value="<?php echo isset($_POST['svalue']) ? $_POST['svalue'] : "" ?>" >
									</div><!-- form-group -->
									
									<div class="form-group">
										<label for="sinformation">Details</label>
										<input type="text" readonly  name="sinformation" id="sinformation" maxlength="80" class="form-control" value="<?php echo isset($_POST['sinformation']) ? $_POST['sinformation'] : "" ?>" >
									</div><!-- form-group -->
									
										<div class="card-footer">
							
							<div class="btn-list">
								<div class="text-right">
									<a href="new_settings.php" class="btn btn-danger"><i class="fa fa-remove"></i> Cancel</a>
									
									<button type="submit" name="savedatabtn" id="savedatabtn" class="btn btn-success"><i class="fa fa-floppy-disk"></i> Save</button>
								</div>
							</div>
						</div><!-- /.card-footer-->
																		
								</div>
							
								 
									 
									</div>
								</div>
							</div>
						</div><!-- /.card-body -->
						
					
							</form>
						<?php
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="fa fa-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div><!-- /.card -->
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

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

		<?php include ('includes/general_footer.php')?>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
	</body>
</html>