<?php include('header.php');
	error_reporting(0);
	$report = "";
?>
			<?php
							if(isset($_GET['id'])){
								$aid=$_GET['id'];
								
								
						$selectpost="select * from admin where id='$aid'";
						$run = $conn->query($selectpost);
						while($row = $run->fetch_assoc())
						{
							
							
										$id=$row['id'];
										$fn=$row['fullname'];
										$un=$row['username'];
										$p=$row['password'];
										$e=$row['email'];
										$c=$row['contact'];
							
						}
						$caption = "Edit Admin";
							}else{
								$caption = "Add Admin";
							}
		?>

    <!-- MENU SECTION END-->
    <div class="content-wrapper">
        <div class="container" >
<?php
		
			if(isset($_POST['add'])){
		
				$fn = $_POST['fname'];
				$un = $_POST['un'];
				$pw = $_POST['pw'];
				$cp = $_POST['cpw'];
				$ph = $_POST['con'];
				$em = $_POST['em'];
				
					
						$dt = date("Y-m-d h:i:s");
				
				if(!isset($error) && $pw==$cp) {

				
					$sql="INSERT INTO admin(fullname, username, password, email, contact, cDate)values('$fn', '$un', '$cp', '$em', '$ph', '$dt')";
					
					$result=mysqli_query($conn,$sql);
										
					if ($result) {
						
						 $report = "<span style='color: green; font-weight: bold;'>[Admin Added Successfully!]</span>";	
						 
					} else {
						//echo mysqli_error('$conn');
						$report = "<span style='color: red; font-weight: bold;'>[Something went wrong, try again later...]</span>";
					}
				} 


		}
		
		
		
		?>
          
            <div class="row" style="margin-top:30px">
                 <div class="col-md-5">
                        <div class="">
                        <div class="" style="border-bottom:4px solid white;color:white;font-size:25px;font-family:Agency FB">
                          <?php echo $caption; ?>
                        </div>
                        </div>
                        </div>
				
				
				                   <div class="col-md-6">
                        <div class="panel panel-default">
                        <div class="panel-heading" style="background-color:#1D2D40;color:white;font-size:20px;font-family:Agency FB">
                          Complete All Fields <?php echo $report; ?>
                        </div>
                        <div class="panel-body">
						<?php
							if(isset($_GET['id'])){
								$aid=$_GET['id'];
							?>
							<form action="" method="post">
					   
							  <div class="form-group">
								<label for="exampleInputEmail1">Full Name</label>
								<br>
								<input type="text" name="fname" class="form-control" value="<?php echo $fn;?>" placeholder="Full Name" />
							  </div>

							  <div class="form-group">
								<label for="exampleInputEmail1">Username</label>
								<br>
								<input type="text" name="un" class="form-control" value="<?php echo $un;?>" placeholder="Username" />
							  </div>  

							  <div class="form-group">
								<label for="exampleInputPassword1">Password</label>
								<input type="password" name="pw" class="form-control" value="<?php echo $p;?>" placeholder="Password" />
							  </div>

							  <div class="form-group">
								<label for="exampleInputPassword1">Confirm Password</label>
								<input type="password" name="cpw" class="form-control" value="<?php echo $p;?>" placeholder="Confirm Password" />
							  </div>

							  <div class="form-group">
								<label for="exampleInputPassword1">Contact Number</label>
								<input type="text" class="form-control" name="con" value="<?php echo $c;?>" placeholder="Contact Number" />
							  </div>
							  
									  <div class="form-group">
								<label for="exampleInputPassword1">Email Address</label>
								<input type="email" class="form-control" name="em" value="<?php echo $e;?>" placeholder="Email" />
							  </div>
							  
							  <input type="submit" class="btn btn-primary" style="width:100%;padding:10px;" value="Update Admin" name="update">
							</form>

						<?php
								
							}else{
						?>
							<form action="" method="post">
					   
							  <div class="form-group">
								<label for="exampleInputEmail1">Full Name</label>
								<br>
								<input type="text" name="fname" class="form-control" placeholder="Full Name" />
							  </div>

							  <div class="form-group">
								<label for="exampleInputEmail1">Username</label>
								<br>
								<input type="text" name="un" class="form-control" placeholder="Username" />
							  </div>  

							  <div class="form-group">
								<label for="exampleInputPassword1">Password</label>
								<input type="password" name="pw" class="form-control" placeholder="Password" />
							  </div>

							  <div class="form-group">
								<label for="exampleInputPassword1">Confirm Password</label>
								<input type="password" name="cpw" class="form-control" placeholder="Confirm Password" />
							  </div>

							  <div class="form-group">
								<label for="exampleInputPassword1">Contact Number</label>
								<input type="text" class="form-control" name="con" placeholder="Contact Number" />
							  </div>
							  
									  <div class="form-group">
								<label for="exampleInputPassword1">Email Address</label>
								<input type="email" class="form-control" name="em" placeholder="Email" />
							  </div>
							  
							  <input type="submit" class="btn btn-primary" style="width:100%;padding:10px;" value="Add Admin" name="add">
							</form>
						<?php

							}
							?>
                            </div>
                            </div>
                    </div>
					
				<?php 
					if(isset($_POST['update'])){
		
				$fn = $_POST['fname'];
				$un = $_POST['un'];
				$pw = $_POST['pw'];
				$cp = $_POST['cpw'];
				$ph = $_POST['con'];
				$em = $_POST['em'];
				
					
						$dt = date("Y-m-d h:i:s");
				
				if(!isset($error) && $pw==$cp) {

				
					$up="update admin set fullname='$fn', username='$un', password='$cp', email='$em', contact='$ph', cDate='$dt' where id='$aid'";
					
					$result=mysqli_query($conn,$up);
										
					if ($result) {
						
						 echo '<script>alert("Admin Updated Successfully!")</script>';	
						 echo '<script>window.location="admins.php";</script>';	
					} else {
						echo mysqli_error('$conn');
						//$error[] = "Something went wrong, try again later...";	
					}
				} 


		}
				?>
            </div>
           
          
        </div>
    </div>
    <!-- CONTENT-WRAPPER SECTION END-->
    <footer style="background-color:#071A30">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    Copyright &copy; <?php echo date("Y"); ?> | ALL RIGHTS RESERVED. 
                </div>

            </div>
        </div>
    </footer>
    <!-- FOOTER SECTION END-->
    <!-- JAVASCRIPT AT THE BOTTOM TO REDUCE THE LOADING TIME  -->
    <!-- CORE JQUERY SCRIPTS -->
    <script src="assets/js/jquery-1.11.1.js"></script>
    <!-- BOOTSTRAP SCRIPTS  -->
    <script src="assets/js/bootstrap.js"></script>
</body>
</html>
