<?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') {
    
	
	
	$user_id=$_POST['user_id'];
	$title=$_POST['title'];
	$description=$_POST['description'];
	$url=$_FILES['url']['name'];
	
	
	
	$front_picture=$_FILES['image']['name'];
	
	
	
	if($front_picture!=''){
			$destinationPath= getcwd().'/uploads/ibox';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			$file_name=time().'_'.$_FILES['image']['name'];
			
			$tempName=$_FILES['image']['tmp_name'];
			move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			$filePath=ROOT.'/uploads/ibox/'.$file_name;
		}else{
			$filePath='';
		}
	if($url!=''){
			$destinationPath= getcwd().'/uploads/ibox';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			$file_name_att=time().'_'.$_FILES['url']['name'];
			
			$tempName_att=$_FILES['url']['tmp_name'];
			move_uploaded_file($tempName_att,$destinationPath.'/'.$file_name_att);
			$filePath_att=ROOT.'/uploads/ibox/'.$file_name_att;
		}else{
			$filePath_att='';
		}
	if($error==false)
	{
		$createCustomer=mysqli_query($con,"INSERT INTO notify_to_user (user_id,title,description,url,image,status,created_at) VALUES('".$user_id."','".$title."','".$description."','".$filePath_att."','".$filePath."',1,'".date('Y-m-d')."')");
		
		if($createCustomer){
			$errors['success']='Ibox created successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}
//$category=mysqli_query($con,"SELECT * FROM users WHERE role=3 AND id NOT IN (SELECT user_id FROM notify_to_user) GROUP BY phone ");
$category=mysqli_query($con,"SELECT * FROM users WHERE role=3  GROUP BY phone ");
?>
 <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 Banner</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="user_id" class="form-control" required>
	<option value="">Select User</option>
	<?php if(mysqli_num_rows($category)>0){
		  while ($cat=mysqli_fetch_array($category)){
	?>
		<option value="<?php echo $cat['id'];?>"><?php echo $cat['phone'].' ' . $cat['first_name'];?></option>
	<?php } } ?>
	</select>
	<?php if($error==true && isset($errors['user_id'])){?>
		<p class="error"><?php echo $errors['user_id'];?></p>
	<?php } ?>
   </div>
   </div>
   <div class="col-md-6">
   
   <div class="form-group">
	<input type="text" name="title" required class="form-control" placeholder="Title" >
	<?php if($error==true && isset($errors['title'])){?>
		<p class="error"><?php echo $errors['title'];?></p>
	<?php } ?>
   </div>
   </div>
   
   
   
   </div>
   
   <div class="row">
   
  
   
    <div class="col-md-4">
	 <div class="form-group">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Image</span>
        <input type="file" name="image">
      </div>
      
    </div>
    </div>
	
   </div>
   
    <div class="col-md-8">
   
   <div class="form-group">
   <label>Description</label>
	<textarea type="text" name="description" class="form-control"  ></textarea>
	
   </div>
   </div>
   
   </div>
   <div class="row">
   
  
   
    <div class="col-md-6">
	 <div class="form-group">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Attachment</span>
        <input type="file" name="url">
      </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>
<script>   CKEDITOR.replace('description');   </script>
<?php include_once 'includes/footer.php'; ?>