<?php
session_start();
include "control/activity.php";
$dd = new activity();

$userKey = $_REQUEST['userKey'];
$bankholder = $_REQUEST['bankholder'];
$bankname = $_REQUEST['bankname'];
$bankaccount = $_REQUEST['bankaccount'];
$bankifsc = $_REQUEST['bankifsc'];

$am = $_REQUEST['am'];
$pass = $_REQUEST['pass'];

$table = "userdetails";
$condition = " where userKey='$userKey' AND password='$pass'";
$checkUser = $dd->check($table,$condition);
if($checkUser['status'] == "success"){



$table = "admin_calculation";
$condition = null;
$getAdminDetails = $dd->single($table,$condition);



$blnc = $am/100*$getAdminDetails['withdraw_fee'];
$am = $am;
$withAmnt = $am-$blnc;
$withdrawlimit = $getAdminDetails['withdraw_limit'];
if($getAdminDetails['withdraw_limit'] <= $am){

$table = "userdetails";
$value = "wallet=wallet-$am,withdraw_balance=withdraw_balance-$am";
$condition = " userKey='$userKey'";
$result = $dd->updateData($table,$value,$condition);
if($result){
    $date = date("h:i d/m/Y");
    $ref = mt_rand(1111111111,9999999999);
    $table = "balance_withdraw";
    $value = "(`user_key`, `action_date`, `withdraw_type`, `amounnt`,`withdraw_tax`, `refrence_number`, `from_address`,`bankholder`,`bankname`,`bankaccount`,`bankifsc`, `avl_balance`) 
    SELECT '$userKey','$date','INR','$withAmnt','$blnc','$ref','test','$bankholder','$bankname','$bankaccount','$bankifsc',wallet FROM userdetails WHERE userKey='$userKey'";
    $result = $dd->insertData($table,$value);
    if($result)
    {
        print_r(json_encode(array("status" => "success","remark" => "Withdraw Request sending..!!")));
    }
}else{
    print_r(json_encode(array("status" => "failed","remark" => "Sorry Internal issues..!!")));
}

}else{
     print_r(json_encode(array("status" => "failed","remark" => "Sorry Minimum withdraw is $withdrawlimit")));
}
}else{
     print_r(json_encode(array("status" => "failed","remark" => "Sorry Invalid password..!!")));
}


?>