<?php
include "config/init.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($_SESSION['role'] != 'SYSTEMADMIN'){
    header("location:index.php");
    die();
}

/**
$total_sent ="0.00";
$num_sent =0;
$total_received ="0.00";
$country_bal  ="0.00";
$num_received =0;
$total_commission ="0.00";
$total_pg_commission ="0.00";
$total_fees ="0.00";
$total_unredeemed_tokens ="0.00";
moneyFormat2(
 */
if(isset($_POST['RECONCILE'])){
    if(!empty($_POST['country']) && !empty($_POST['supertype']) && !empty($_POST['accounttype'])){

        //$result ="TEST";
        $sql=sprintf("call get_total_transactions('%s','%s','%s')",$_POST['country'],$_POST['supertype'],$_POST['accounttype']);
        $summary = mysqli_query($dbc,$sql);


        $row  = mysqli_fetch_assoc($summary);

        $total_sent1 = $row['total_sent'];
        $total_received1 = $row['total_received'];
        $country_bal1  = moneyFormat2($total_received1 - $total_sent1);
        $total_sent1 = moneyFormat2($row['total_sent']);
        $total_received1 = moneyFormat2($row['total_received']);
        $num_sent1 = $row['num_sent'];
        //$total_received1 = $row['total_received'];
        $num_received1 = $row['num_received'];
        $total_commission1 = moneyFormat2($row['total_commission']);
        $total_pg_commission1 = moneyFormat2($row['total_pg_commission']);
        $total_fees1 = moneyFormat2($row['total_fees']);
        $total_unredeemed_tokens1 = moneyFormat2($row['total_unredeemed_tokens']);



    }

}



?>

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

        <h1>
            Country Reconciliation
        </h1>

        <?php if(isset($result)){echo $result;}?>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-md-3">
                <div class="box box-primary">
                    <div class="box-header with-border">Select Reconciliation Parameters</div>
                    <div class="box-body">
                        <form role="form" method="post">

                            <div class="form-group">
                                <label for="country">Country</label>
                                <select class="form-control" required name="country" id="country">
                                    <option value="">Select country...</option>
                                    <?php

                                    $countries = get_system_country($dbc);
                                    foreach ($countries as $value ){
                                        echo"<option ".($_POST['country']==$value['countryCode']?' selected':'')." value='{$value['countryCode']}'>{$value['countryName']}</option>";
                                    }

                                    ?>
                                </select>
                            </div>


                            <div class="form-group">
                                <label for="accounttype">Account Type</label>
                                <select class="form-control staff_block" required name="accounttype" id="accounttype">
                                    <option value="">Select Account Type...</option>
                                    <option value="4">SUPERAGENT</option>
                                    <option value="3">MERCHANT</option>
                                    <option value="2">AGENT</option>
                                    <option value="1">INDIVIDUAL</option>
                                    <option value="ALL">ALL</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="supertype">Super Type</label>
                                <select class="form-control" required name="supertype" id="supertype">
                                    <option value="">Select Super Type...</option>
                                    <option value="A">A</option>
                                    <option value="B">B</option>
                                    <option value="B0">B0</option>
                                    <option value="B1">B1</option>
                                    <option value="B2">B2</option>
                                    <option value="D">D</option>
                                    <option value="E">E</option>
                                    <option value="F">F</option>
                                    <option value="G">G</option>
                                    <option value="ALL">ALL</option>
                                </select>
                            </div>


                            <div class="form-group">
                                <button type="submit" name="RECONCILE" class="btn btn-primary">Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <div class="col-md-9">
                <div class="row">
                    <div class="col-md-12">
                        <div class="small-box bg-aqua">
                            <div class="inner">
                                <h4><b>Actual Transactions Summary<?=!isset($_POST['sender'])?" ":" For ".$_POST['sender']?></b></h4>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_sent1)?"0.00":$total_sent1;?></h3>
                                <p><b>Total Sent</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_received1)?"0.00":$total_received1;?></h3>
                                <p>Total Received</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-blue">
                            <div class="inner">
                                <h3><?=!isset($country_bal1)?"0.00":$country_bal1;?></h3>
                                <p>Account Balance</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_fees1)?"0.00":$total_fees1;?></h3>
                                <p>Total Fees</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_commission1)?"0.00":$total_commission1?></h3>
                                <p>Agent Commissions</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-green">
                            <div class="inner">
                                <h3><?=!isset($total_pg_commission1)?"0.00":$total_pg_commission1;?></h3>
                                <p>PayGlobal Commission</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($num_sent1)?"0":$num_sent1;?></h3>
                                <p>Sent Transactions #</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($num_received1)?"0":$num_received1;?></h3>
                                <p>Received Transactions #</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-red">
                            <div class="inner">
                                <h3><?=!isset($total_unredeemed_tokens1)?"0.00":$total_unredeemed_tokens1;?></h3>
                                <p>Unredeemed Tokens</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                </div>


                <div class="row">
                    <div class="col-md-12">
                        <div class="small-box bg-aqua-gradient">
                            <div class="inner">
                                <h4><b>System Transactions Summary</b></h4>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_debit)?"0.00":$total_debit;?></h3>
                                <p><b>Total Sent</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_credit)?"0.00":$total_credit;?></h3>
                                <p>Total Received</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-blue">
                            <div class="inner">
                                <h3><?=!isset($total_balance)?"0.00":$total_balance;?></h3>
                                <p>Account Balance</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_fees1)?"0.00":$total_fees1;?></h3>
                                <p>Total Fees</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($total_commission1)?"0.00":$total_commission1?></h3>
                                <p>Agent Commissions</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-green">
                            <div class="inner">
                                <h3><?=!isset($total_pg_commission1)?"0.00":$total_pg_commission1;?></h3>
                                <p>PayGlobal Commission</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($num_sent1)?"0":$num_sent1;?></h3>
                                <p>Sent Transactions #</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-yellow">
                            <div class="inner">
                                <h3><?=!isset($num_received1)?"0":$num_received1;?></h3>
                                <p>Received Transactions #</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>

                    <div class="col-md-4">
                        <div class="small-box bg-red">
                            <div class="inner">
                                <h3><?=!isset($total_unredeemed_tokens1)?"0.00":$total_unredeemed_tokens1;?></h3>
                                <p>Unredeemed Tokens</p>
                            </div>
                            <div class="icon">
                                <i class="fa fa-money"></i>
                            </div>
                        </div>

                    </div>
                </div>

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

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


