<?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') {
    
	
	
	$credits=$_POST['credits'];
	
	
	
	
	if(empty($credits)){
		$error=true;
		$errors['credits']='Please select Credit Value';
	}
	$setting=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM member_topup_settings WHERE id=1"));
	$points=0;
	$voucher=0.00;
	
	if($credits<=1000){
		$points=($setting['one_points']/100)*$credits;
		$voucher=($setting['one_voucher']/100)*$credits;
	}elseif($credits<=50000 && $credits>1000){
		$points=($setting['two_points']/100)*$credits;
		$voucher=($setting['two_voucher']/100)*$credits;
	}elseif($credits<=100000 && $credits>50000){
		$points=($setting['three_points']/100)*$credits;
		$voucher=($setting['three_voucher']/100)*$credits;
	}elseif($credits<=1000000 && $credits>100000){
		$points=($setting['four_points']/100)*$credits;
		$voucher=($setting['four_voucher']/100)*$credits;
	}
	
	
	if($error==false)
	{
		$createCustomer=mysqli_query($con,"INSERT INTO credit_logs (user_id,credits,points,voucher,created_at) VALUES(".$_GET['user_id'].",'".$credits."','".$points."','".$voucher."','".date('Y-m-d H:i:s')."')");
		$user=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM users WHERE id=".$_GET['user_id']));
		$credits=$credits+$user['credits'];
		$voucher=$voucher+$user['vouchers'];
		$points=$points+$user['point_wallet'];
		$updateCustomer=mysqli_query($con,"UPDATE users SET credits='".$credits."',vouchers='".$voucher."',point_wallet='".$points."' WHERE id=".$_GET['user_id']);
		if($createCustomer){
			$errors['success']='Credits added 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 Credits- 
添加积分</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="credits" class="form-control" required>
	<option value="">Select Credits</option>
	
		<option value="100">100</option>
		<option value="500">500</option>
		<option value="1000">1000</option>
		<option value="10000">10000</option>
		<option value="25000">25000</option>
		<option value="50000">50000</option>
	
	</select>
   </div>
   </div>
   
    <div class="col-md-6">
	 
	
   </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'; ?>