<?php include("auth.php");
require("conn.php");
session_start();

$cid =$_SESSION['v_code'];

function getWallet()
{
  require("./conn.php");
  $cid=$_SESSION['v_code'];
  $sql="select wallet from tbl_vendor where v_code='$cid'";
  $smt=mysqli_query($conn,$sql);
				 
  $rs=mysqli_fetch_assoc($smt);
  return($rs['wallet']);
}


require "./conn.php";
 $action=$_POST['action'];
 if($action=="withdraw-bank")
  {

		if(!empty($_POST['amount'])&&!empty($_POST['bank'])&&!empty($_POST['accountholname'])&&!empty($_POST['accountnumber'])&&!empty($_POST['ifsc']))
		{
			
			$amount=$_POST['amount'];
			$bank_name=$_POST['bank'];
			$account_holder_name=$_POST['accountholname'];
			$account_no=$_POST['accountnumber'];
			$ifsc_code=$_POST['ifsc'];
			
			$cid=$_SESSION['v_code'];
			$wallet=getWallet();
			$wdl_amt=$wallet;
			
		    if($wdl_amt-$amount>=0 && $amount<=10000)
			{
		
			$sql="INSERT INTO  tbl_wallet_withdraw(cid,amount,bank_name,account_holder_name,account_no,ifsc_code,pan_no,description,trans_date,status)VALUES('$cid','$amount','$bank_name','$account_holder_name','$account_no','$ifsc_code','$pan_no','$description',CURDATE(),'Under Process')";
			$res=mysqli_query($conn,$sql);
			   if($res)
			   {	
		        
		            $sql1="update  tbl_vendor set wallet=wallet-$amount where v_code='$cid'";	
					mysqli_query($conn,$sql1);
				
		            echo "Withdrawal request sent Successfully.";
			    }
    			else
    			{
    				echo "Transection failed,Please check and try again.";
    			}
			}	
			else
			{
			 echo "Amountshound be less then Rs. 10000 /-  and more then your wallet balance.".$wdl_amt.$amount;
			 
			}
			
			
			
		}
		else
		{
			 echo "Please Complete Form";
		}
  }
  
 
		
?>