<?php
//echo '<pre>';
//print_r($_REQUEST);
include 'get_user_info.php';
$admin_id='B00001';
if(isset($_REQUEST['approve']) || isset($_REQUEST['decline']) || isset($_REQUEST['accept']) || isset($_REQUEST['failed']) )
{
include 'include/db.php';
$get_id=$_REQUEST['check_box'];  // get id,amount,unique user id by concetination and separated by(-)

/*$user_id=$_REQUEST['user_id'];
$user_name=$_REQUEST['user_name'];
$total_balance=$_REQUEST['total_balance'];    //  array
$request_balance=$_REQUEST['request_balance'];
*/
date_default_timezone_set('Asia/Kolkata');      
$date=date("Y-m-d H:i:s");      //  for 24 hour
 
    //   approved
    if($_REQUEST['approve']=='Approve')
    {
       $flag=0;
        for($i=0;$i<sizeof($get_id);$i++)
        {
         $all_payout_id=explode('-',$get_id[$i]);
          $transaction_id=get_transaction_id();
           $query="update payout_details set payout_accept_status=1,payout_accept_date='$date' where payout_details_id='$all_payout_id[0]'; ";
           $query2="update wallet_balance set total_balance=total_balance+('$all_payout_id[1]'+'$all_payout_id[3]') where balance_user_id='$admin_id'; ";
           $connection=mysqli_query($conn,$query);
           $connection2=mysqli_query($conn,$query2);
           {
                $flag=1;
                $admin_total_balace=get_admin_total_balance();
                $query3="update wallet set pending='1' where transaction_id='$all_payout_id[4]'";
                $query4="insert into admin_wallet_details(wallet_user_id,receiver_amount,cr_dr,closer_amount,wallet_transaction_id,wallet_remarks,wallet_date) 
                        values('$all_payout_id[2]','$all_payout_id[1]'+'$all_payout_id[3]','cr','$admin_total_balace','$all_payout_id[4]','Approved payout request','$date')";
                mysqli_query($conn,$query3);
                mysqli_query($conn,$query4);
           }
        }
        if($flag>0)
        {
            echo '<script> alert("Selected users Accepted successfully"); window.location="show_request_payout.php"; </script>';
        }
    }
   //  declined
    if($_REQUEST['decline']=='Decline')
    {
        $flag=0;
        for($i=0;$i<sizeof($get_id);$i++)
        {
           $all_payout_id=explode('-',$get_id[$i]);
          
           $transaction_id=get_transaction_id();
           $query="update payout_details set payout_accept_status='2',payout_accept_date='$date' where payout_details_id='$all_payout_id[0]'; ";
           $query2="update wallet_balance set total_balance=total_balance+('$all_payout_id[1]'+'$all_payout_id[3]') where balance_user_id='$all_payout_id[2]'; ";
           $connection=mysqli_query($conn,$query);
           $connection2=mysqli_query($conn,$query2);
           if($connection)
           {
                $flag=1;
                $query12="update wallet set pending='2' where transaction_id='$all_payout_id[4]'";
                mysqli_query($conn,$query12);
           }
        }
        if($flag>0)
        {
            echo '<script> alert("Selected users declined successfully"); window.location="show_request_payout.php"; </script>';
        }
    }
    
    //  successful
    else if($_REQUEST['accept']=='Successful')
    {
        $flag=0;
        for($i=0;$i<sizeof($get_id);$i++)
        {
           $all_payout_id=explode('-',$get_id[$i]);
           
           $query="update payout_details set payout_accept_status='3',payout_accept_date='$date' where payout_details_id='$all_payout_id[0]' ";
           $query2="update wallet_balance set total_balance=total_balance-('$all_payout_id[1]') where balance_user_id='$admin_id'; ";
           $connection=mysqli_query($conn,$query);
           $connection2=mysqli_query($conn,$query2);
           if($connection)
           {
               $flag=1;
               $admin_total_balace=get_admin_total_balance();
               $query4="insert into admin_wallet_details(wallet_user_id,receiver_amount,cr_dr,closer_amount,wallet_transaction_id,wallet_remarks,wallet_date) 
                        values('Bank','$all_payout_id[1]','dr','$admin_total_balace','$all_payout_id[4]','Payout request successful','$date')";
               mysqli_query($conn,$query4);
           }
        }
        if($flag>0)
        {
            echo '<script> alert("Selected users payout successfully done"); window.location="payout_pending.php"; </script>';
            
        }
    }
    //  failed
     else if($_REQUEST['failed']=='Failed')
    {
        $flag=0;
        for($i=0;$i<sizeof($get_id);$i++)
        {
           $all_payout_id=explode('-',$get_id[$i]);
           
           $transaction_id=get_transaction_id();
           $query="update payout_details set payout_accept_status='4',payout_accept_date='$date' where payout_details_id='$all_payout_id[0]'; ";
           $query2="update wallet_balance set total_balance=total_balance-('$all_payout_id[1]'+'$all_payout_id[3]') where balance_user_id='$admin_id' ";
           $query3="update wallet_balance set total_balance=total_balance+('$all_payout_id[1]'+'$all_payout_id[3]') where balance_user_id='$all_payout_id[2]' ";
           $connection=mysqli_query($conn,$query);
           $connection2=mysqli_query($conn,$query2);
            $connection2=mysqli_query($conn,$query3);
           if($connection)
           {
                $flag=1;
                $admin_total_balace=get_admin_total_balance();
                $query3="update wallet set pending='2',source='Payout request failed' where transaction_id='$all_payout_id[4]'";
                $query4="insert into admin_wallet_details(wallet_user_id,receiver_amount,cr_dr,closer_amount,wallet_transaction_id,wallet_remarks,wallet_date) 
                        values('$all_payout_id[2]','$all_payout_id[1]'+'$all_payout_id[3]','dr','$admin_total_balace','$all_payout_id[4]','Payout request failed','$date')";
                mysqli_query($conn,$query3);
                mysqli_query($conn,$query4);
           }
        }
         if($flag>0)
        {
            echo '<script> alert("Selected users payout request failed"); window.location="payout_pending.php"; </script>';
        }
    }
    
}


 function get_transaction_id(){
       include 'include/db.php';
       $qr2="select MAX(transaction_id) from wallet LIMIT 1";
        $con2=mysqli_query($conn,$qr2);
        if(mysqli_num_rows($con2)>0)
        {
        	while($rw=mysqli_fetch_array($con2))
        	{
        		 $xyz=$rw['MAX(transaction_id)'];
        		 $after_substr=substr($xyz,2);
        		 $old_unique_id=intval($after_substr);  //  convert string to int
        	}
        	for ($num = $old_unique_id; $num <= $old_unique_id+1; $num++)
            $net_val=sprintf("%'.06d", $num);
            return 'WT'.$net_val;
        }
   }
?>