<?php 
require_once 'config.php';
$error=false;
$errors=array();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   
  
   $user_id=$_POST['user_id'];
   $credits=$_POST['amount'];
   
 $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;
	}
	
	
		$createCustomer=mysqli_query($con,"INSERT INTO credit_logs (user_id,credits,points,voucher,created_at) VALUES(".$user_id.",'".$credits."','".$points."','".$voucher."','".date('Y-m-d H:i:s')."')");
		$user=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM users WHERE id=".$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=".$user_id);
		if($createCustomer){
			$errors['success']='Credits added successfully.';
		}else{
			$error=true;
			$errors['error']='Something went wrong!!!, Please try again.';
		}
	
 	 
	 	echo json_encode($errors);
}