<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header2.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";

    $cwallet        = sumWall("masuk", "cwallet") - sumWall("keluar", "cwallet");
    $mwallet        = MsumWall("masuk", "mwallet") - MsumWall("keluar", "mwallet");
    $mwallet_keluar = MsumWall("keluar", "mwallet");

    $limit = 100;

    if ($cwallet >= $limit) {
        $link = "request_withdraw.php";
    } else {
        $link = "#";
    }

    $result = mysqli_query($con, "SELECT SUM(amaun) FROM withdraw WHERE userid='".$_SESSION['aidi']."'");
    $withdraw = mysqli_fetch_array($result);
    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Withdrawal</h2>
                    </div>
                </div>
            <hr />
<?
 if (!in_array($_SESSION['role'], ['admin', 'finance', 'cashier'])) {
    
    if ($_SESSION['role'] <> "merchant") {
?>
                <div class="row">

                    <div class="col-md-6 col-sm-6 col-xs-6">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-money-bill-transfer"></i>
                            </span>
                        <div class="text-box" >
                            <p class="main-text"><?= number_format($withdraw[0],2) ?> MYR</p>
                            <p class="text-muted">Total Withdrawal</p>
                        </div>
                        </div>
                    </div>

                    <div class="col-md-6 col-sm-6 col-xs-6">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-usd"></i>
                            </span>
                        <div class="text-box" >
                            <p class="main-text"><?= number_format($cwallet,2) ?> MYR</p>
                            <p class="text-muted">Available Bonus</p>
                        </div>
                        </div>
                    </div>

            </div>
                 <!-- /. ROW  -->
               
<?php
    }    
    else    {
        
?>
                <div class="row">

                    <div class="col-md-6 col-sm-6 col-xs-6">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-money-bill-transfer"></i>
                            </span>
                        <div class="text-box" >
                            <p class="main-text"><?= number_format($mwallet_keluar,2) ?> MYR</p>
                            <p class="text-muted">Total Withdrawal</p>
                        </div>
                        </div>
                    </div>

                    <div class="col-md-6 col-sm-6 col-xs-6">
                        <div class="panel panel-back noti-box">
                            <span class="icon-box bg-primary set-icon">
                                <i class="fa fa-usd"></i>
                            </span>
                        <div class="text-box" >
                            <p class="main-text"><?= number_format($mwallet,2) ?> MYR</p>
                            <p class="text-muted">Available Balance</p>
                        </div>
                        </div>
                    </div>

            </div>
                 <!-- /. ROW  -->    
<?php
        } 
}   

?>
            <hr />
            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                             Withdrawal List
                        </div>
<script>
function checkAll(o) {
    var boxes = document.getElementsByTagName("input");
    for (var x = 0; x < boxes.length; x++) {
        var obj = boxes[x];
        if (obj.type == "checkbox") {
            if (obj.name != "check")
                obj.checked = o.checked;
        }
    }
}
</script>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <form action="check.php" method="post">
                                <table class="table table-striped table-bordered table-hover" id="example">
                                    <thead>
                                        <tr>
                                            <th><input type="checkbox" onclick="javascript:checkAll(this)"/></th>
                                            <th class="text-center">Date</th>

                                            <th>Name</th>
                                            <th>Bank Info</th>
                                            <th class="text-center">Amount</th>
                                            <th class="text-center">Payment Date</th>
                                            <th>Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
<?
$memba = '';

if ($_SESSION['username'] <> "admin") {
  $memba = " WHERE b.id = ".$_SESSION['aidi'];
}


define("S0", "<a class=\"btn btn-info btn-xs\">waiting</a>");
define("S1", "<a class=\"btn btn-success btn-xs\">paid</a>");

    $sql = "SELECT a.id, a.tarikh, b.username, b.nama, b.bank, b.noakaun, a.amaun, a.paydate, a.status FROM withdraw a INNER JOIN ahli b ON a.userid = b.id".$memba;
    $result = mysqli_query($con, $sql);

    while ($row = mysqli_fetch_array($result)) {
?>
                                        <tr class="even">
                                            <td><input type="checkbox" name="wd[]" value="<?= $row[0] ?>" /></td>
                                            <td class="text-center"><?= $row[1] ?></td>
                                            <td><?= $row[3] ?><br><code><?= $row[2] ?></code></td>
                                            <td><?= $row[4] ?><br><?= $row[5] ?></td>
                                            <td class="text-center"><?= $row[6] ?></td>
                                            <td class="text-center"><?= ($row[7] == "0000-00-00 00:00:00" ? '-' : $row[7]) ?></td>
                                            <td><?= constant("S".$row[8])?></td>
                                        </tr>
 <? } ?>
                                    </tbody>
                                </table>
                                <? if ($_SESSION['username'] == "admin") { ?> 
                                <input type="submit" name="paid" value="Paid" />&nbsp;&nbsp;&nbsp;<input type="submit" name="unpaid" value="Unpaid" />
                                <? } ?>

                                </form>
                            </div>

                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>          
    </div>
             <!-- /. PAGE INNER  -->
            </div>
<?
    include "_footer2.php";
}
?>
