<?php
session_start();
include 'include/db.php';
include 'include/constant.php';
include 'include/functions.php';
include 'get_user_info.php';


if(isset($_REQUEST['btn_request_payout']) && (isset($_SESSION['payout_security']) && isset($_REQUEST['payout_security']) && $_SESSION['payout_security'] == $_REQUEST['payout_security']))
{

	$bank = get_bank_details($got_unique_id);
	if(!empty($bank) && $bank['ac'] !='' && $bank['adhar'] !='' && $bank['pan'] !='' && $bank['ifsc'] !='')
	{
		$id=$_REQUEST['take_user_id'];
		$amount=isset($_REQUEST['request_payout_money']) ? $_REQUEST['request_payout_money'] : 0;;
		$payout_type=isset($_REQUEST['payout_type']) ? $_REQUEST['payout_type'] : 1;
		$fees_amount=isset($_REQUEST['fees_amount']) ? $_REQUEST['fees_amount'] : 0;
		$total_deductable_amount = $amount+$fees_amount;
		$transaction_id = get_unique_id('py'); // payout
		
		if(isset($total_balance) && (int)$total_balance >= $total_deductable_amount)
		{
		
			$query="insert into payout_details(payout_user_id,payout_details_type,payout_amount,fee_amount,payout_transaction_id)
					values('$id','$payout_type','$amount','$fees_amount','$transaction_id')";
			$connection=mysqli_query($conn,$query);
			if($connection)
			{
				//  Payout amoutn send to admin
				wallet($wallet = array('sender_id'=> $id, 'receiver_id'=>  ADMIN_ID, 'amount'=> $total_deductable_amount));

				$wallet_arr = array(
					'sender_id'			=>	$id,
					'receiver_id'		=>	ADMIN_ID,
					'amount'			=>	$total_deductable_amount,
					'respective_user'	=>	$id,
					'transaction_type'	=>	3,  //  Payout
					'remarks'			=>  'Payout request send to admin',
					'status'			=>	1,
					'transaction_id'	=>  get_unique_id('t')
				);
				add_wallet_history($wallet_arr);
				
				echo '<script> alert("Payout request send successfully to admin"); window.location="user_payout_details.php" </script>';
			}
			else
			{
				 echo '<script> alert("Sorry, something went wrong"); window.location="request_payout.php" </script>';
			}
		}
		else
		{
			echo '<script> alert("Insufficient wallet balance to request payout"); window.location="request_payout.php" </script>';
		}
	}
	else
	{
		echo '<script> alert("Please fill bank account details to payout amount"); window.location="edit_list_user.php?id='.$got_unique_id.'#account_details" </script>';
	}
}
else
{
	echo '<script> alert("Sorry, something went wrong"); window.location="request_payout.php" </script>';
}
 

?>