<?php
$page           = (!empty($_GET['page-id']) && is_numeric($_GET['page-id'])) ? Secure($_GET['page-id']) : 1;
$filter_keyword = (!empty($_GET['query'])) ? Secure($_GET['query']) : '';
$db->pageLimit  = 20;

if(isset($_GET['approved'])){
    $db->where('approved',1);
}else{
    $db->where('approved',0);
}


if (!empty($filter_keyword)) {
    $sql   = "(
    `username`     LIKE '%$filter_keyword%' OR 
    `email`        LIKE '%$filter_keyword%' OR 
    `first_name`   LIKE '%$filter_keyword%' OR 
    `ip_address`   LIKE '%$filter_keyword%' OR 
    `phone_number` LIKE '%$filter_keyword%' OR 
    `last_name`    LIKE '%$filter_keyword%'
  )";

    $mediafiles = $db->orderBy('approved_at', 'DESC')->orderBy('id', 'DESC')->objectbuilder()->paginate('bank_receipts', $page);
}

else {
    $mediafiles = $db->objectbuilder()->orderBy('approved_at', 'DESC')->orderBy('id', 'DESC')->paginate('bank_receipts', $page);
}

if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
    header("Location: " . Wo_LoadAdminLinkSettings('bank-receipts'));
    exit();
}

$approved_count = $db->where('approved',1)->getValue('bank_receipts','count(id)');
$disapproved_count = $db->where('approved',0)->getValue('bank_receipts','count(id)');

$total_credits_rev = $db->where('approved',1)->where('mode','credits')->getValue('bank_receipts','SUM(price)');
$total_members_rev = $db->where('approved',1)->where('mode','membership')->getValue('bank_receipts','SUM(price)');
?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Earnings > Manage bank receipts</h2>
    </div>

    <div class="row clearfix">
        <div class="col-lg-3 col-md-3">
            <a href="<?php echo Wo_LoadAdminLinkSettings('bank-receipts');?>?approved=true">
            <div class="info-box bg-light-green hover-expand-effect">
                <div class="icon">
                    <i class="material-icons">check</i>
                </div>
                <div class="content">
                    <div class="text">Approved</div>
                    <div class="number count-to" data-from="0" data-to="<?php echo $approved_count; ?>" data-speed="1000" data-fresh-interval="20"></div>
                </div>
            </div>
            </a>
        </div>
        <div class="col-lg-3 col-md-3 ">
            <a href="<?php echo Wo_LoadAdminLinkSettings('bank-receipts');?>">
            <div class="info-box bg-red hover-expand-effect">
                <div class="icon">
                    <i class="material-icons">clear</i>
                </div>
                <div class="content">
                    <div class="text">Disapproved</div>
                    <div class="number count-to" data-from="0" data-to="<?php echo $disapproved_count; ?>" data-speed="1000" data-fresh-interval="20"></div>
                </div>
            </div>
            </a>
        </div>
        <div class="col-lg-3 col-md-3 ">
            <a href="#">
                <div class="info-box bg-light-green hover-expand-effect">
                    <div class="icon">
                        <i class="material-icons">attach_money</i>
                    </div>
                    <div class="content">
                        <div class="text">Total Credits Revenue</div>
                        <div class="number count-to" data-from="0" data-to="<?php echo $total_credits_rev; ?>" data-speed="1000" data-fresh-interval="20"></div>
                    </div>
                </div>
            </a>
        </div>
        <div class="col-lg-3 col-md-3 ">
            <a href="#">
                <div class="info-box bg-orange  hover-expand-effect">
                    <div class="icon">
                        <i class="material-icons">monetization_on</i>
                    </div>
                    <div class="content">
                        <div class="text">Total Memberships Revenue</div>
                        <div class="number count-to" data-from="0" data-to="<?php echo $total_members_rev; ?>" data-speed="1000" data-fresh-interval="20"></div>
                    </div>
                </div>
            </a>
        </div>
    </div>

    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage bank receipts</h2>
                </div>
                <div class="clearfix"></div>
                <div class="table-responsive1">
                    <table class="table table-bordered table-striped table-hover">
                        <tbody>
                        <?php
                        if( count($mediafiles) === 0 ){
                            echo '<tr><td style="padding: 200px;text-align: center;">No data available in table</td></tr>';
                        }else {
                            foreach ($mediafiles as $mediafilelist) {
                                $wo['mediafilelist'] = $mediafilelist;
                                $wo['mediafilelist']->user = userData($mediafilelist->user_id);
                                echo Wo_LoadAdminPage('bank-receipts/list');
                            }
                        }
                        ?>
                        </tbody>
                    </table>
                </div>
                <div class="wo-admincp-feturepager" style="width: 98%;margin: 0 auto;">
                    <div class="pull-left">
                        <span>
                          <?php echo "Showing $page out of " . $db->totalPages; ?>
                        </span>
                    </div>
                    <div class="pull-right">
                        <nav>
                            <ul class="pagination">
                                <li>
                                    <a href="<?php echo Wo_LoadAdminLinkSettings('manage-photos?page-id=1'); ?>" class="waves-effect" title='First Page'>
                                        <i class="material-icons">first_page</i>
                                    </a>
                                </li>
                                <?php if ($page > 1) {  ?>
                                    <li>
                                        <a href="<?php echo Wo_LoadAdminLinkSettings('manage-photos?page-id=' . ($page - 1)); ?>" class="waves-effect" title='Previous Page'>
                                            <i class="material-icons">chevron_left</i>
                                        </a>
                                    </li>
                                <?php  } ?>

                                <?php
                                $nums       = 0;
                                $nums_pages = ($page > 4) ? ($page - 4) : $page;

                                for ($i=$nums_pages; $i <= $db->totalPages; $i++) {
                                    if ($nums < 20) {
                                        ?>
                                        <li class="<?php echo ($page == $i) ? 'active' : ''; ?>">
                                            <a href="<?php echo Wo_LoadAdminLinkSettings('manage-photos?page-id=' . ($i)); ?>" class="waves-effect">
                                                <?php echo $i ?>
                                            </a>
                                        </li>

                                    <?php } $nums++; }?>

                                <?php if ($db->totalPages > $page) { ?>
                                    <li>
                                        <a href="<?php echo Wo_LoadAdminLinkSettings('manage-photos?page-id=' . ($page + 1)); ?>" class="waves-effect" title="Next Page">
                                            <i class="material-icons">chevron_right</i>
                                        </a>
                                    </li>
                                <?php } ?>
                                <li>
                                    <a href="<?php echo Wo_LoadAdminLinkSettings('manage-photos?page-id=' . ($db->totalPages)); ?>" class="waves-effect" title='Last Page'>
                                        <i class="material-icons">last_page</i>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
</div>
<script>

    function Wo_DeleteReceipt(receipt_id,user_id,photo_file) {
        if (confirm("Are you sure you want to delete this receipt?") == false) {
            return false;
        }
        hash_id = '<?php echo Wo_CreateSession();?>';

        $.get(Wo_Ajax_Requests_File(),{f:'admin_setting', s:'delete_receipt', receipt_id: receipt_id, user_id: user_id, receipt_file: photo_file, hash_id: hash_id})
        .done(function( data ) {
            $('#ReceiptID_' + receipt_id).fadeOut(300, function() {
                $(this).remove();
            });
        });
    }

    function Wo_ApproveReceipt(receipt_id,photo_file) {
        if (confirm("Are you sure you want to approve this receipt?") == false) {
            return false;
        }
        hash_id = '<?php echo Wo_CreateSession();?>';
        $('#ReceiptID_' + receipt_id).fadeOut(300, function() {
            $(this).remove();
        });
        $.get(Wo_Ajax_Requests_File(),{f:'admin_setting', s:'approve_receipt', receipt_id: receipt_id, receipt_file: photo_file, hash_id: hash_id})
        .done(function( data ) {
            window.location = window.location.href;
        });

    }

</script>