<?php
include "config/init.php";
//include "system/bank/bank-index.php";
include "template/header.php";
//Main Header
include "template/main-header.php";
//Left side column. contains the logo and sidebar -->
include "template/main-sidebar.php";

if(isset($_POST['creditCustomer'])){
    $sender = html_filter($dbc,$_POST['sender']);
    $receiver = html_filter($dbc,$_POST['receiver']);
    $amount = html_filter($dbc,$_POST['amount']);
	$senderData = get_customer_by_id($dbc,$sender);
    $receiverData = get_customer_by_id($dbc,$receiver);
	$rate = get_conversion_rate($dbc,$senderData['countryCode'],$receiverData['countryCode']);
	$cov_amount = $amount * $rate;


    if(!empty($sender) AND !empty($receiver) AND !empty($amount)){

 
        //if($senderData['countryCode'] == $receiverData['countryCode']){
            if(redraw_funds($dbc,$sender,$amount,$senderData['countryCode'])){
                if(add_funds($dbc,$receiver,$cov_amount,$receiverData['countryCode'])){
                    $transactionID = genTransactionID();
                    if(add_main_transaction($dbc,$receiverData,$senderData,'MT','',$transactionID,'COMPLETED','0',$amount,$cov_amount,'PC','')){
                        $result = "<p class='alert alert-success'>Transaction Done Successful!</p>";
                        activity_log($dbc,"Transaction made with a transaction ID {$transactionID}","TRANSACTION ACTIVITY");
						
						if($senderData['countryCode'] != $receiverData['countryCode']){
                           (!transcription($dbc,$customerData,$global_hubtel_Acc,"MM-PUSH",$cov_amount,"",$transaction_uid)) ? Response::output(500, 'Internal System Error') : '';
                          }
						
                    }else{
                        $result = "<p class='alert alert-danger'>Transaction Error</p>";
                    }
                }else{
                    $result = "<p class='alert alert-danger'>Transaction Error2</p>";
                }
            } else{
                $result = "<p class='alert alert-danger'>Transaction Error1</p>";
            }
        //} else{
            //$result = "<p class='alert alert-danger'>Transaction can't be done between different Countries</p>";
        //}
    } else {
        $result = "<p class='alert alert-danger'>All fields required</p>";
    }

}

?>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Virtual Wallet Reversal
        </h1>
    </section>

    <!-- Main content -->
    <section class="content">
        <?php
            if(isset($result)){echo $result;}
        ?>
        <div class="row">
            <div class="col-md-4">
                <div class="box box-default">
                    <!--<div class="box-header with-border">Perform Transaction.</div>-->
                    <div class="box-body">
                        <form method="post" role="form" onsubmit="return confirm('Are you sure you want to do this transaction');">
                            <div class="form-group">
                                <label for="findCustomer">Sender Customer</label>
                                <select class="form-control" required name="sender" id="findCustomer">
                                    <option value="">Find Customer</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="findCustomer2"> Recipient Customer</label>
                                <select class="form-control" required name="receiver" id="findCustomer2">
                                    <option value="">Find Customer</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="amount">Amount</label>
                                <input type="number" minlength="1" name="amount" id="amount" required step="any" class="form-control">
                            </div>
                            <input type="submit" class="btn btn-primary" name="creditCustomer" value="Reverse">
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-md-8">

            </div>
        </div>
    </section>
    <!-- /.content -->
</div>
<!-- /.content-wrapper -->

<?php include "template/footer.php"?>


