<?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');

}
$customer=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM malls WHERE id=".$_GET['id']));

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
	
	
	$description=$_POST['description'];
	
	$front_picture=$_FILES['image']['name'];
	$views=$_POST['cate_id'];
	if($front_picture!=''){
			$destinationPath= getcwd().'/uploads/malls';
			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/malls/'.$file_name;
		}else{
			$filePath=$customer['image'];
		}
	
	if($error==false)
	{
		$createCustomer=mysqli_query($con,"UPDATE malls SET country_id=".$views.", mall_name='".$description."',image='".$filePath."' WHERE ID=".$_GET['id']);
		
		if($createCustomer){
			$errors['success']='Malls updated successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
	
}
$customer=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM malls WHERE id=".$_GET['id']));

?>
 <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">Edit Malls- </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-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><img src="<?php echo $customer['image'];?>" width="100" height="100">
    </div>
	
   </div>
   <div class="col-md-8">
   
   <div class="form-group">
   <label>Name</label>
	<input type="text" name="description" class="form-control" value="<?php echo $customer['mall_name'];?>"/>
	
   </div>
   </div>
   </div>
    <div class="row">
   
  
   
      
    <div class="col-md-6">
   
   <div class="form-group">
   <label>Country</label>
   	<select name="cate_id" class="form-control" required>
	<?php 
	$countries=mysqli_query($con,"SELECT * FROM countries ");
	if(mysqli_num_rows($countries)>0){
		  while ($cat=mysqli_fetch_array($countries)){
	?>
		<option value="<?php echo $cat['id'];?>" <?php echo ($cat['id']==$customer['country_id'])?'selected':''; ?>><?php echo $cat['name'];?></option>
	<?php } } ?>
	</select>
   </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'; ?>