<?php
session_start();
require_once 'includes/header.php';
require_once 'includes/config.php';
$error=false;
$errors=array();
// avoid user arriving to this page without logging in
if(!isset($_SESSION['user_logged_in']) && $_SESSION['user_logged_in']!=TRUE )
{
	header('Location:login.php');

}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
	//$store=$_POST['store'];
	
	$group_id=$_POST['group_id'];
	
	
	
	
	if(empty($group_id)){
		$error=true;
		$errors['group_id']='Please select group name';
	}
	//$front_picture=$_FILES['front']['name'];
	//$back_picture=$_FILES['back']['name'];
	
	if($error==false)
	{
		
		
		foreach($_FILES["image"]["tmp_name"] as $key=>$tmp_name) {
			
			$destinationPath= getcwd().'/uploads/popup';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			$file_name=time().'_'.$_FILES['image']['name'][$key];
			
			$tempName=$_FILES['image']['tmp_name'][$key];
			move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			$filePath=ROOT.'/uploads/popup/'.$file_name;
			mysqli_query($con,"INSERT INTO popupgroup_images (group_id,image) VALUES('".$group_id."','".$filePath."')");
			
		}
		
		
		if($createEmployee){
			$errors['success']='Image created successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}



//$cities=mysqli_query($con,"SELECT * FROM cities  ORDER BY id ASC ");
$category=mysqli_query($con,"SELECT * FROM popupgroups ");
?>
<div class="content">
        <div class="container-fluid">
          <div class="row">
            <div class="col-md-12">
              <div class="card">
                <div class="card-header card-header-primary">
                  <h4 class="card-title">Add popup image</h4>
                  <p class="card-category"></p>
                </div>
                <div class="card-body">

<?php

if (isset($error) && $error==false && isset($errors['success'])) {
	echo '<div class="alert alert-success" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> '.$errors['success'].'</div>';
} 

if (isset($error) && $error==true && isset($errors['error'])) {
	echo '<div class="alert alert-danger" role="alert" id="success_message">Success <i class="glyphicon glyphicon-thumbs-up"></i> '.$errors['error'].'</div>';
} 

?>

  
  
   <form role="form" method="post" enctype="multipart/form-data">
   <div class="box-body">
   
  
   
   <div class="row">
   
   
   <div class="col-md-6">
  
   <div class="form-group">
	<select name="group_id" class="form-control" required>
	<option value="">Select Group</option>
	<?php if(mysqli_num_rows($category)>0){
		  while ($cat=mysqli_fetch_array($category)){
	?>
		<option value="<?php echo $cat['id'];?>"><?php echo $cat['group_name'];?></option>
	<?php } } ?>
	</select>
	<?php if($error==true && isset($errors['group_id'])){?>
		<p class="error"><?php echo $errors['group_id'];?></p>
	<?php } ?>
   </div>
   </div>
   
    <div class="col-md-6">
	 <div class="form-group">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Images</span>
        <input multiple type="file" name="image[]">
      </div>
      
    </div>
    </div>
	
   </div>
   
  
   </div>
   
  
   
   <div class="box-footer">
		<button type="submit" class="btn btn-primary">Save</button>
   </div>
			  
   </form>
  </div>
  </div>
  </div>
  </div>
  </div>
  </div>
<?php include_once 'includes/footer.php'; ?>