<?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(T_BANK_RECEIPTS, $page);
}

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

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

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

$total_credits_rev = $db->where('approved',1)->where('mode','track')->getValue(T_BANK_RECEIPTS,'SUM(price)');
$total_members_rev = $db->where('approved',1)->where('mode','membership')->getValue(T_BANK_RECEIPTS,'SUM(price)');
?>
<style>
    .setting-avatar {
        width: 30px;
        margin-right: 10px;
    }
</style>
<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 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 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 Tracks 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);
                                ?>

                        <?php if($wo['mediafilelist']->user){?>
                        <tr class="user-list" id="ReceiptID_<?php echo $wo['mediafilelist']->id?>" data-receipt-id="<?php echo $wo['mediafilelist']->id?>" style="width: 25%; float: left;margin-top: 20px;">
                            <td style="border: 0px;text-align: center;display: block;">
                                <a href="<?php echo $wo['site_url'] . '/@' . $wo['mediafilelist']->user->username; ?>" target="_blank">
                                    <img src="<?php echo $wo['mediafilelist']->user->avatar;?>" class="setting-avatar" alt="<?php echo $wo['mediafilelist']->user->username;?> Profile Picture">
                                    <?php echo $wo['mediafilelist']->user->username; ?>
                                </a>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <span style="color:#607D8B;font-size: 11px;font-weight: bold;" class=" ajax-time" title="<?php echo date('c', strtotime($wo['mediafilelist']->created_at)); ?>">
            <?php echo Time_Elapsed_String(strtotime($wo['mediafilelist']->created_at));?>
        </span>
                                <hr>
                                <a href="<?php echo GetMedia($wo['mediafilelist']->receipt_file); ?>" target="_blank" >
                                    <img src="<?php echo GetMedia($wo['mediafilelist']->receipt_file); ?>" class="setting-avatar" style="min-width: 197px;object-fit: cover;height: 250px;">
                                </a>
                                <div class="clearfix"></div>
                                <p><b><?php echo $wo['mediafilelist']->mode;?></b></p>
                                <p>
                                    <?php echo $wo['mediafilelist']->description;?> =
                                    <b><?php echo '$' . $wo['mediafilelist']->price;?></b>
                                </p>
                                <?php if((int)$wo['mediafilelist']->approved == 0){?>
                                <div class="row">
                                    <div class="col-md-6">
                                        <button  style="float: right;" onclick="Wo_DeleteReceipt(<?php echo $wo['mediafilelist']->id?>,<?php echo $wo['mediafilelist']->user_id?>,'<?php echo $wo['mediafilelist']->receipt_file?>');" type="button" class="btn btn-danger waves-effect waves-light" style="margin: 0 auto;display: block;">DELETE</button>
                                    </div>
                                    <div class="col-md-6">
                                        <button style="float: left;" onclick="Wo_ApproveReceipt(<?php echo $wo['mediafilelist']->id?>,'<?php echo $wo['mediafilelist']->receipt_file?>');" type="button" class="btn btn-success waves-effect waves-light" style="margin: 0 auto;display: block;">APPROVE</button>
                                    </div>
                                </div>
                                <?php } else{ ?>
                                <b style="color:darkgreen;">Approved <?php echo Time_Elapsed_String($wo['mediafilelist']->approved_at);}?></b>
                            </td>
                        </tr>
                        <?php } ?>

                                <?php
                                //echo 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 LoadAdminLinkSettings('bank-receipts?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 LoadAdminLinkSettings('bank-receipts?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 LoadAdminLinkSettings('bank-receipts?page-id=' . ($i)); ?>" class="waves-effect">
                                                <?php echo $i ?>
                                            </a>
                                        </li>

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

                                <?php if ($db->totalPages > $page) { ?>
                                    <li>
                                        <a href="<?php echo LoadAdminLinkSettings('bank-receipts?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 LoadAdminLinkSettings('bank-receipts?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 createMainSession();?>';

        $.get('{{CONFIG ajax_url}}/ap/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();
            });
            window.location.reload();
        });
    }

    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 createMainSession();?>';
        $('#ReceiptID_' + receipt_id).fadeOut(300, function() {
            $(this).remove();
        });
        $.get('{{CONFIG ajax_url}}/ap/approve_receipt',{receipt_id: receipt_id, receipt_file: photo_file, hash_id: hash_id})
        .done(function( data ) {
            //window.location = window.location.href;
        });
        window.location.reload();
    }

</script>