<?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 partners WHERE img= '.$shopfocus.''); 
		if ($result_partners->num_rows > 0) {
	    while($shopdata = $result_partners->fetch_assoc()) {    
			$_POST['shopfocus'] = $shopfocus;
			$_POST['shopname'] = ($shopdata['pname'] != "") ? $shopdata['pname'] : "";
			$_POST['shopcode'] = ($shopdata['address'] != "") ? $shopdata['address'] : "";
			$_POST['rate'] = $shopdata['rate'];
			$_POST['currentlogo'] = ($shopdata['image_name'] != "") ? $shopdata['image_name'] : ""; 
		   }
		}
	}
	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 partners (pname, address, rate)
						VALUES ("'.$_POST['shopname'].'", "'.$_POST['shopcode'].'", '.$_POST['rate'].')';
						if (mysqli_query($eagles, $sql)) {
						  $recid = mysqli_insert_id($eagles);
						}
						if($_FILES['file']['name'])
						{
							if($_FILES["file"]["error"] == 0)
							{
								$temp = explode(".", $_FILES["file"]["name"]);
								$extension = end($temp);
								$shoplogofile = $recid . "." . $extension;
								move_uploaded_file($_FILES["file"]["tmp_name"], "../eagles/partners/" . $recid . "." . $extension);
							}
						}
						if($shoplogofile != "")
						{
									 $sql =  
						 "UPDATE partners  SET image_name = '".$shoplogofile."' WHERE img = ".$recid."";
						if (mysqli_query($eagles, $sql)) {
						}
						}
						$successsave="New Shop Successfully Saved!";
						unset($_POST['shopfocus']);
						unset($_POST['shopname']);
						unset($_POST['shopcode']);
						unset($_POST['rate']); 
						unset($_POST['recversion']);
				}
				else
				{
						 $sql =  
						 "UPDATE partners  SET pname= '".$_POST['shopname']."', address= '".$_POST['shopcode']."', rate= ".$_POST['rate']." WHERE img = ".$_POST['shopfocus']."";
						if (mysqli_query($eagles, $sql)) {
						 
						}
							if($_FILES['file']['name'])
							{
								if($_FILES["file"]["error"] == 0)
								{
									$temp = explode(".", $_FILES["file"]["name"]);
									$extension = end($temp);
									$shoplogofile = $_POST['shopfocus'] . "." . $extension;
									if(file_exists("../eagles/partners/" . $_POST['currentlogo']))
										unlink("../eagles/partners/" . $_POST['currentlogo']);
									move_uploaded_file($_FILES["file"]["tmp_name"], "../eagles/partners/" . $_POST['shopfocus'] . "." . $extension);
								}
							}
							if($shoplogofile != "")
							{
								 $result_partners = $eagles->query('UPDATE partners SET image_name = "'.$shoplogofile.'" WHERE img='.$_POST['shopfocus'].''); 
							 
							}
?>
							<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>Partner  Setup</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">Partner Entry 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">Shop Name</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">Address</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="rate">Rate</label>
											<input type="number" name="rate" id="rate" maxlength="2" class="form-control" value="<?php echo isset($_POST['rate']) ? $_POST['rate'] : "" ?>" >
										</div><!-- form-group -->
									</div>
									<INPUT name="currentlogo" id="currentlogo" type=hidden value="<?php echo isset($_POST['currentlogo']) ? $_POST['currentlogo'] : "" ?>">
									<div class="col-sm-4">
										<div class="row col-sm-12">
											<label >Partner Poster</label>
											<div style="height: 100px width: 100px">
												<img class="img-thumbnail" id="userphoto" src="<?php echo (isset($_POST['currentlogo']) && $_POST['currentlogo'] != "") ? "../eagles/partners/" . $_POST['currentlogo'] : "images/shoplogos/noimage.gif" ?>" alt="..." style="width: 100%;max-height: 100%">
											</div>
										</div>
										<div class="row">	
											<div class="form-group">
												<div class="col-sm-12">
													<input name="file" type="file" id="file" accept="image/jpg,image/png,image/jpeg,image/gif"/>
												</div>
											</div><!-- form-group -->
										</div>
									</div>
							</div>
						</div><!-- /.box-body -->
						<div class="box-footer">
							<div class="btn-list">
							<div class="text-right">
								<a href="eagles.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-->
							</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>