<?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') {
    
	
	
	$fname=$_POST['fname'];
	$password=$_POST['password'];
	$email=$_POST['email'];
	$phone=$_POST['phone'];
	$credit=$_POST['credit'];
	$point=$_POST['point'];
	$voucher=$_POST['voucher'];
	
	
	
	
	
	
	if(empty($fname)){
		$error=true;
		$errors['fname']='Please enter Nickname';
	}
	if(empty($phone)){
		$error=true;
		$errors['phone']='Please enter Phone';
	}
	
	if(empty($password)){
		$error=true;
		$errors['password']='Please enter Password';
	}
	
	
	$checkEmailExists=mysqli_query($con,"SELECT * FROM users WHERE phone='".$phone."'");
	
	if(mysqli_num_rows($checkEmailExists)>0){
		$error=true;
		$errors['phone']='Phone already exists, Please try new one.';
	}
	
	
	if($error==false)
	{
		$createCustomer=mysqli_query($con,"INSERT INTO users (first_name,email,phone,password,credits,vouchers,point_wallet,role,status,created_at) VALUES('".$fname."','".$email."','".$phone."','".$password."','".$credit."','".$voucher."','".$point."','3',1,'".date('Y-m-d H:i:s')."')");
		
		if($createCustomer){
			$errors['success']='Customer created 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">添加用户 - Add User </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">
	<input type="text" name="fname" required class="form-control" placeholder="Nickname" >
	<?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" >
	<?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="password" name="password" class="form-control" placeholder="Login Password" required >
	<?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" 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-4">
   
   <div class="form-group">
	<input type="text" name="credit"  class="form-control" placeholder="Cash Wallet" >
	<?php if($error==true && isset($errors['credit'])){?>
		<p class="error"><?php echo $errors['credit'];?></p>
	<?php } ?>
   </div>
   </div>
   
   <div class="col-md-4">
   
   <div class="form-group">
	<input type="text" name="point"  class="form-control" placeholder="Points Wallet" >
	<?php if($error==true && isset($errors['point'])){?>
		<p class="error"><?php echo $errors['point'];?></p>
	<?php } ?>
   </div>
   </div>
   
   <div class="col-md-4">
  
   <div class="form-group">
	<input type="text" name="voucher"  class="form-control" placeholder="Voucher Wallet" >
	<?php if($error==true && isset($errors['voucher'])){?>
		<p class="error"><?php echo $errors['voucher'];?></p>
	<?php } ?>
   </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'; ?>