<?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') {
    
	$query='SELECT * FROM trendiframe where id=1 ORDER BY id DESC ';

$row = mysqli_query($con,$query);
     
    if (mysqli_num_rows($row) == 1) {
		//$r=mysqli_fetch_array($row)	;
			
		 $r=mysqli_fetch_array($row);
}	
	//$image=$_POST['image'];
	$price=$_POST['price'];
	$faq=$_POST['faq'];
	$benefits=$_POST['benefits'];
	$howitwork=$_POST['howitwork'];
	$about=$_POST['about'];
	
	
	
	//$front_picture=$_FILES['image']['name'];
//	echo $front_picture;
	if( !empty ($_FILES['image']['name'][0])){ 
			mysqli_query($con,"DELETE FROM frame_img WHERE frame_id=".$_GET['id']);
			//echo '<pre>';print_r($_FILES['image']);die;
	foreach($_FILES['image']['tmp_name'] as $key=>$tmp_name) {
		
		$destinationPath= getcwd().'/uploads/trendiframe';
			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/trendiframe/'.$file_name;*/
			
			$file_name=time().'_'.str_replace(' ', '_', $_FILES['image']['name'][$key]);
			
			$tempName=$_FILES['image']['tmp_name'][$key];
			move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			$filePath=ROOT.'/uploads/trendiframe/'.$file_name;
			mysqli_query($con,"INSERT INTO frame_img (frame_id,image,status) VALUES('1','".$filePath."',1)");
		
	}
	}
	/*if($front_picture!=''){
			$destinationPath= getcwd().'/uploads/trendiframe';
			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/trendiframe/'.$file_name;
		}else{
			$filePath=$r['image'];
		}*/
//	echo $filepath; die;
	if($error==false)
	{
			//$deleteEmployee=mysqli_query($con,"DELETE FROM trendiframe");

		$createCustomer=mysqli_query($con,"update trendiframe set price=".$price.",benefits='".$benefits."', faq='".$faq."', howitwork='".$howitwork."', about='".$about."' where id=1");//,status) VALUES('".$price."','".$benefits."','".$faq."','".$howitwork."','".$about."','".$filePath."',1)");
		
		if($createCustomer){
			$errors['success']='Trendiframe modified successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}
$image="";$price="";$about="";$faq="";$benefits="";$howitwork="";
$query='SELECT * FROM trendiframe where id=1 ORDER BY id DESC ';
$query1='SELECT * FROM frame_img where frame_id=1 ORDER BY id DESC ';

$row = mysqli_query($con,$query);
$row1 = mysqli_query($con,$query1);
     
    if (mysqli_num_rows($row) == 1) {
		//$r=mysqli_fetch_array($row)	;
			
		$r=mysqli_fetch_array($row);
		$image=$r['image'];$price=$r['price'];$about=$r['about'];$faq=$r['faq'];$benefits=$r['benefits'];$howitwork=$r['howitwork'];
	 
	}
?>
<style>
.modal{
	z-index: 9999;
	max-height: 55%;
	
}
.modal-content{
	border:none;
}
</style>
 <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</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">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Image</span>
        <input type="file" name="image[]" multiple>
      </div>
   <?php if (mysqli_num_rows($row1) >= 1){ while($r1=mysqli_fetch_array($row1)){?>   
  <img src="<?php echo $r1['image'];?>" width="100" height="100">
   <?php } } ?>
    </div>
    </div>

	
   </div>
    <div class="col-md-6">
	 <div class="form-group">
	<label>Price</label>
	<input type="text" name="price" value="<?php echo $price; ?>">
    </div>
	
   </div>
   </div>
   <!--<div class="row">
   <div class="col-md-6">
	 <div class="form-group">
	<label>Salary</label>
	<input type="text" name="salary">
    </div>
	
   </div>
   </div>-->
   
   <div class="row">
    <div class="col-md-12">
   
   <div class="form-group">
   <label>About</label><br>
	<textarea type="text" name="about" class="form-control"  ><?php echo $about; ?></textarea>
	
   </div>
   </div>
   
   </div>
   
   <div class="row">
    <div class="col-md-12">
   
   <div class="form-group">
   <label>How it Works</label><br>
	<textarea type="text" name="howitwork" class="form-control"  ><?php echo $howitwork; ?></textarea>
	
   </div>
   </div>
   
   </div>
   <div class="row">
    <div class="col-md-12">
   
   <div class="form-group">
   <label>FAQ</label><br>
	<textarea type="text" name="faq" class="form-control"  ><?php echo $faq; ?></textarea>
	
   </div>
   </div>
   
   </div>
   <div class="row">
    <div class="col-md-12">
   
   <div class="form-group">
   <label>Benefits</label><br>
	<textarea type="text" name="benefits" class="form-control"  ><?php echo $benefits; ?></textarea>
	
   </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( 'benefits' );   </script>
<script>   CKEDITOR.replace( 'faq' );   </script>
<script>   CKEDITOR.replace( 'howitwork' );   </script>
<script>   CKEDITOR.replace( 'about' );   </script>
<?php include_once 'includes/footer.php'; ?>
<script type="text/javascript">
$(document).ready(function(){
  $('#last-date').datepicker();
    //$('#last-date').datepicker({ dateFormat: 'yy-mm-dd',onSelect: function(dateText, inst) {$('#last-date').val(dateText);}});
   // $('#last-date').datepicker({ onSelect: function(dateText, inst) {alert(dateText );}});
  });
  </script>