<?php
	include("includes/dbconfig.php");
	$pagetitle = "Add new partner";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	$thebase ='';
	$err= "";
	include("eaglesdb.php");
	$successsave="";
	if(isset($_POST['shopfocus']))
	{
		$shopfocus = $_POST['shopfocus'];
	}
	if(!(isset($_POST['savedatabtn'])) && (isset($shopfocus) && $shopfocus > 0))
	{
		$result_partners = $eagles->query('SELECT  * FROM tickets WHERE id='.$shopfocus.'');
		if ($result_partners->num_rows > 0) {
	    while($shopdata = $result_partners->fetch_assoc()) {    
			$_POST['shopfocus'] = $shopfocus;
			$_POST['shopname'] = ($shopdata['name'] != "") ? $shopdata['name'] : "";
			$_POST['shopcode'] = ($shopdata['card'] != "") ? $shopdata['card'] : "";
			$_POST['phone'] = $shopdata['phone'];
			$_POST['expiry'] = $shopdata['expiry'];
		   }
		}
	}
	if(isset($_POST['savedatabtn']))
	{
		if($_SESSION["connectedusergroup"] == 1)
		{
			if(trim($_POST['shopname']) == "")
				$err = $err."Partner  Name Required!<br>";
			if(trim($_POST['shopcode']) == "")
			{
				$err = $err."Partner address Required!<br>";
			}
			if($err == "")
			{
				$shoplogofile = "";
				if($shopfocus == 0)
				{
					 $recid = 0;
					 $sql = 'INSERT INTO tickets (name, card, phone, expiry)
						VALUES ("'.$_POST['shopname'].'", "'.$_POST['shopcode'].'", '.$_POST['phone'].', "'.$_POST['expiry'].'")';
						if (mysqli_query($eagles, $sql)) {
						  $recid = mysqli_insert_id($eagles);
						}
						$successsave="New Shop Successfully Saved!";
						unset($_POST['shopfocus']);
						unset($_POST['shopname']);
						unset($_POST['shopcode']);
						unset($_POST['rate']); 
						unset($_POST['recversion']);
				}
				else
				{
						 $sql =  
						 'UPDATE tickets  SET name= "'.$_POST['shopname'].'", card= "'.$_POST['shopcode'].'", phone= "'.$_POST['phone'].'", expiry= "'.$_POST['expiry'].'"  WHERE id = '.$_POST['shopfocus'].'';
						if (mysqli_query($eagles, $sql)) {
						  //$eagles->close();
						}
?>
							<script>
								location.href="eagles.php";
							</script>
<?php
							exit();
				}
			}
		}
		else
		{
			$err = $err."Limited Acccess!<br>";
		}
	}
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>New Ticket</h1>
					<ol class="breadcrumb">
					</ol>
				</section>
				<!-- Main content -->
				<section class="content">
					<!-- Default box -->
					  <div class="card">
						<?php
							if($_SESSION["connectedusergroup"] == 1)
							{
						?>
						<div class="card-header with-border text-center">
							<h3 class="card-title">Ticket Set Up Form</h3>
						</div>
						<div class="card-body">
							<?php
								if($err!="")
								{
							?>
							<div class="alert alert-danger alert-dismissable">
								<i class="glyphicon glyphicon-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="glyphicon glyphicon-ok"></i>
								<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
								<?php 
									echo $successsave; 
								?>
							</div>
							<?php
								}
							?>
							<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
								<INPUT name="shopfocus" id="shopfocus" type=hidden value="<?php echo isset($_POST['shopfocus']) ? $_POST['shopfocus'] : 0 ?>">
								<INPUT name="recversion" id="recversion" type=hidden value="<?php echo isset($_POST['recversion']) ? $_POST['recversion'] : 0 ?>">
							<div class="row">
								<div class="col-sm-6">
									<div class="col-sm-8">
										<div class="form-group">
											<label for="shopname">Customer</label>
											<input type="text" name="shopname" id="shopname" maxlength="100" class="form-control" value="<?php echo isset($_POST['shopname']) ? $_POST['shopname'] : "" ?>" >
										</div><!-- form-group -->
										<div class="form-group">
											<label for="shopcode">Identification number</label>
											<input type="text" name="shopcode" id="shopcode"   class="form-control" value="<?php echo isset($_POST['shopcode']) ? $_POST['shopcode'] : "" ?>" >
										</div><!-- form-group -->
										<div class="form-group">
											<label for="phone">Phone Number</label>
											<input type="text" name="phone" id="phone" maxlength="12" class="form-control" value="<?php echo isset($_POST['phone']) ? $_POST['phone'] : "" ?>" >
										</div><!-- form-group -->
										<div class="form-group">
											<label for="expiry">Expiry date</label>
											<input type="date" name="expiry" id="expiry" maxlength="12" class="form-control" value="<?php echo isset($_POST['expiry']) ? $_POST['expiry'] : "" ?>" >
										</div><!-- form-group -->
										<div class="box-footer">
							<div class="btn-list">
							<div class="text-right">
								<a href="shops.php" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i> Cancel</a>
								<button type="submit" name="savedatabtn" id="savedatabtn" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
							</div>
							</div>
						</div><!-- /.box-footer-->
									</div>
							</div>
						</div><!-- /.box-body -->
							</form>
						<?php
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="glyphicon glyphicon-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div><!-- /.box -->
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->
				<?php include ('includes/general_footer.php')?>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/shopsetup.js" type="text/javascript"></script>
	</body>
</html>