<?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 users WHERE id=".$_GET['id']));
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
	
	$fname=$_POST['fname'];
	$password=$_POST['password'];
	$email=$_POST['email'];
	$phone=$_POST['phone'];
	$address=$_POST['address'];
	$country_id=$_POST['country_id'];
	$website=$_POST['website'];
	
	$countries=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM countries WHERE id=".$country_id));
	if(!empty($_POST['whatsapp_no'])){
		$whatsapp_no=$countries['phone_code'].$_POST['whatsapp_no'];
		$whatsapp_no="'".$whatsapp_no."'";
	}else{
		$whatsapp_no='NULL';
	}
	
	
	$about_merchant=$_POST['about_merchant'];
	$front_picture=$_FILES['image']['name'];
	$banner=$_FILES['banner']['name'];
	if(empty($fname)){
		$error=true;
		$errors['fname']='Please enter Nickname';
	}
	if(empty($phone)){
		$error=true;
		$errors['phone']='Please enter Phone';
	}
	if(!empty($front_picture)){
			$destinationPath= getcwd().'/uploads/merchant';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			$file_name=time().'_'.str_replace(' ', '_', $_FILES['image']['name']);
			
			$tempName=$_FILES['image']['tmp_name'];
			move_uploaded_file($tempName,$destinationPath.'/'.$file_name);
			$filePath=ROOT.'/uploads/merchant/'.$file_name;
		}else{
			$filePath=$customer['profile_pic'];
		}
		
		if($banner!=''){
			$destinationPath= getcwd().'/uploads/merchant';
			if(!file_exists($destinationPath)){
				mkdir($destinationPath,0777,true);
			}
			$file_name1=time().'_'.str_replace(' ', '_', $_FILES['banner']['name']);
			
			$tempName=$_FILES['banner']['tmp_name'];
			move_uploaded_file($tempName,$destinationPath.'/'.$file_name1);
			$filePath1=ROOT.'/uploads/merchant/'.$file_name1;
		}else{
			$filePath1=$customer['banner'];
		}
	
	if($error==false)
	{
		
		$updateCustomer=mysqli_query($con,"UPDATE users SET first_name='".$fname."',password='".$password."',email='".$email."',phone='".$phone."',about_merchant='".$about_merchant."',address='".$address."',whatsapp_no=$whatsapp_no,profile_pic='".$filePath."',banner='".$filePath1."',country_id=".$country_id.",website='".$website."' WHERE id=".$_GET['id']);
		
		if($updateCustomer){
			$errors['success']='Merchant updated successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	}
	
}


?>




  <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 Merchant</h4>
                  
                </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">
	<input type="text" name="fname"  class="form-control" placeholder="Nickname" value="<?php echo $customer['first_name'];?>" >
	<?php if($error==true && isset($errors['fname'])){?>
		<p class="error"><?php echo $errors['fname'];?></p>
	<?php } ?>
   </div>
   </div>
   
   <div class="col-md-6">
  
   <div class="form-group">
	<input type="number" name="phone" required class="form-control" placeholder="Phone Number" value="<?php echo $customer['phone'];?>" >
	<?php if($error==true && isset($errors['phone'])){?>
		<p class="error"><?php echo $errors['phone'];?></p>
	<?php } ?>
   </div>
   </div>
   
   </div>
   
   <div class="row">
   
   <div class="col-md-6">
  
   <div class="form-group">
	<input type="text" name="whatsapp_no" class="form-control" placeholder="whatsapp no" value="<?php echo $customer['whatsapp_no'];?>"  >
	<?php if($error==true && isset($errors['whatsapp_no'])){?>
		<p class="error"><?php echo $errors['whatsapp_no'];?></p>
	<?php } ?>
   </div>
   </div>
   
   <div class="col-md-6">
  
     <div class="form-group">
   
		<select name="country_id" class="form-control" >
	<?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 class="row">
   
   <div class="col-md-6">
  
   <div class="form-group">
	<input type="password" name="password" class="form-control" placeholder="Login Password" value="<?php echo $customer['password'];?>"  >
	<?php if($error==true && isset($errors['password'])){?>
		<p class="error"><?php echo $errors['password'];?></p>
	<?php } ?>
   </div>
   </div>
   
   <div class="col-md-6">
  
   <div class="form-group">
	<input type="text" name="email" value="<?php echo $customer['email'];?>" required class="form-control" placeholder="Email " >
	<?php if($error==true && isset($errors['email'])){?>
		<p class="error"><?php echo $errors['email'];?></p>
	<?php } ?>
   </div>
   </div>

   </div>
   
    <div class="row">
   
   <div class="col-md-6">
   
   <div class="form-group">
	<input type="text" name="website" required class="form-control" placeholder="Website " value="<?php echo $customer['website'];?>" >
	
   </div>
   </div>
   
    <div class="col-md-6">
	
	
   </div>
   
   </div>
   <div class="row">
   
   <div class="col-md-6">
   
   <div class="form-group">
	<textarea type="text" name="address"  class="form-control" placeholder="Address" ><?php echo $customer['address'];?></textarea>
	
   </div>
   </div>
   
    <div class="col-md-6">
	 <div class="form-group">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Logo</span>
        <input type="file" name="image">
      </div>
      
    </div><img src="<?php echo $customer['profile_pic'];?>" width="100" height="100">
    </div>
	
   </div>
   
   </div>
   <div class="row">
   
   <div class="col-md-6">
   
   <div class="form-group">
	<textarea type="text" name="about_merchant"  class="form-control" placeholder="About Merchant" ><?php echo $customer['about_merchant'];?></textarea>
	
   </div>
   </div>
   
    <div class="col-md-6">
	   <div class="form-group">
	<div class="file-field input-field">
      <div class="btn btn-primary">
        <span>Banner</span>
        <input type="file" name="banner">
      </div>
      
    </div><img src="<?php echo $customer['banner'];?>" width="100" height="100">
    </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( 'about_merchant' );   </script>

<?php include_once 'includes/footer.php'; ?>