<?php session_start(); error_reporting(0); ?>
<?php if(!isset($_SESSION['j_admin']) && !isset($_SESSION['j_admin'])){ 
    $url = urlencode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"); header('location:login/?url='.$url.'');  
} ?>
<!doctype html>
<html lang="en"> 
<head>
	<?php
    require_once("j_conn/url.php");
    require_once("j_conn/dbc.php");
    require_once("j_incs/functions.php");
    $page_name="$company - $company_motto";
    $page_desc="$company_desc";
    $page_img="".$dir."j_files/j_img/logo/logo-meta.png";
    $page_author="$company";
    $page_keywords="".$company.", $page_name, $page_desc, ".$company_motto.", $company_desc";
    $page_url="".$dir."";
    require_once("j_incs/metas.php");
    require_once("j_incs/scripts-top.php");
    ?>
</head>
<body>
    <div class="app">
        <div class="layout">
            <?php 
            require_once("j_incs/header.php");
            require_once("j_incs/nav.php");
            ?>
            <!-- Page Container START -->
            <div class="page-container">
                
                <!-- Content Wrapper START -->
                <div class="main-content">
                    <div style="margin-bottom:10px;">
                        <?php 
                        $sql="SELECT * FROM j_slides WHERE j_deleted < 1 AND j_status > 0 AND j_category='Home Top Ad' order by rand() limit 1";
                        $result = runSQL($sql);
                        $sum = mysqli_num_rows($result);
                        if($sum>0){ 
                        while($row =mysqli_fetch_array($result)){
                        if(!empty($row['j_img'])){
                            //$image = "background-image: url('".$dir."j_files/j_img/categories/".$row['j_img']."');";
                            $image = "".$dir."j_files/j_img/ads/".$row['j_img']."";
                            $text = $row['j_title'];
                            $link = $row['j_link'];
                        }else{
                            $image = '';
                        }
                        ?>
                        <?php if(!empty($row['j_link'])){ ?><a target="_blank" style="" href="<?php echo $link; ?>">  <?php } ?>
                        
                        <div class="f_c_box c_img" id="imgg1" url="<?php echo $image; ?>"></div>
                        <div id="m_imgg1"></div>
                                                
                        <?php if(!empty($row['j_link'])){ ?> </a>  <?php } ?>
                        <?php } ?>
                        <?php } ?>

                    </div>
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    <?php
                    if(isset($_SESSION['j_admin'])){
                    $sql4="SELECT * from j_users WHERE j_deleted < 1 AND j_status < 1"; //check 
                    $result4 = runSQL($sql4);
                    $students = mysqli_num_rows($result4);
                    //
                    $sql4="SELECT sum(j_amount) as total from j_pays WHERE 
                    j_paid > 0 AND j_kind='TOP-UP' AND j_deleted < 1
                    || j_paid > 0 AND j_kind='A-Credit' AND j_deleted < 1
                    "; //check 
                    $result4 = runSQL($sql4);
                    $data = mysqli_fetch_array($result4);
                    $deposits = $data['total'];
                    //
                    $sql4="SELECT sum(j_wallet) as total from j_users WHERE j_deleted < 1"; //check 
                    $result4 = runSQL($sql4);
                    $data = mysqli_fetch_array($result4);
                    $users_bal = $data['total'];
                    //
                    $profit = $deposits-$users_bal;
                    /*
                    $sql4="SELECT * from j_courses WHERE j_deleted < 1 AND j_status > 0"; //check 
                    $result4 = runSQL($sql4);
                    $materials = mysqli_num_rows($result4); 
                    */
                    //
                    ?>
                    <div class="row">
                        <div class="col-3"> 
                            <p>
                                <b>Revenue</b> <i class="fas fa-angle-right"></i>
                            </p>
                        </div>
                        
                        <div class="col-4" style="display:none"> 
                            <div class="dropup">
                                <span style="cursor:pointer" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <b id="chooseCurrency">NGN</b>
                                </span>
                                <div class="dropdown-menu">
                                    <a class="dropdown-item currency" href="javascript:void(0)">
                                        BTC
                                    </a>
                                    <div class="dropdown-divider"></div>
                                    <a class="dropdown-item currency" href="javascript:void(0)">
                                        ETH
                                    </a>
                                </div>
                            </div>

                        </div>
                        
                        
                        <div class="col-4"> 
                            <div class="dropup">
                                <span style="cursor:pointer" class="dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <b id="choosePeriod">All Time</b>
                                </span>
                                <div class="dropdown-menu">
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        Choose
                                    </a>
                                    <div class="dropdown-divider"></div> 
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        Today
                                    </a>
                                    <div class="dropdown-divider"></div> 
                                    
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        Yesterday
                                    </a>
                                    <div class="dropdown-divider"></div> 
                                    
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        This Week
                                    </a>
                                    <div class="dropdown-divider"></div> 
                                    
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        Last Week
                                    </a>
                                    <div class="dropdown-divider"></div> 
                                    
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        This Month
                                    </a>
                                    <div class="dropdown-divider"></div> 
                                    
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        Last Month
                                    </a>
                                    <div class="dropdown-divider"></div> 

                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        This Year
                                    </a>
                                    <div class="dropdown-divider"></div> 
                                    
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        Last Year
                                    </a>
                                    
                                    <div class="dropdown-divider"></div> 
                                    
                                    <a class="dropdown-item period" href="javascript:void(0)">
                                        All Time
                                    </a>
                                    
                                   
                                </div>
                            </div>

                        </div>
                    </div>
                    
                    <div id="period_r"></div> <?php //display revenue result here ?>
                   
                    <?php } ?> 
                    
                    
                    
                    <div class="row">
                        
                        
                        <div class="col-md-12 col-lg-8">
                            
								
							<?PHP 
							/*
							<ul class="nav nav-tabs nav-justified" id="myTabJustified" role="tablist">
								<li class="nav-item">
									<a class="nav-link active" id="home-tab-justified" data-toggle="tab" href="#home-justified" role="tab" aria-controls="home-justified" aria-selected="true">Properties</a>
								</li>
								<li class="nav-item">
									<a class="nav-link" id="profile-tab-justified" data-toggle="tab" href="#profile-justified" role="tab" aria-controls="profile-justified" aria-selected="false">Wallet</a>
								</li>
								<li class="nav-item">
									<a class="nav-link" id="contact-tab-justified" data-toggle="tab" href="#contact-justified" role="tab" aria-controls="contact-justified" aria-selected="false">TRADING</a>
								</li>
							</ul>
							<div class="tab-content m-t-15" id="myTabContentJustified">
								<div class="tab-pane fade show active" id="home-justified" role="tabpanel" aria-labelledby="home-tab-justified">
									We have properties for sale, rent and lease. 
									View Properties now.
								</div>
								<div class="tab-pane fade" id="profile-justified" role="tabpanel" aria-labelledby="profile-tab-justified">
									You can
								</div>
								<div class="tab-pane fade" id="contact-justified" role="tabpanel" aria-labelledby="contact-tab-justified">
									...
								</div>
							</div>
							*/
							?>




                            
                            <?php 
                                if(isset($_SESSION['j_admin'])){
                                    require_once("dashboard-admin.php");
                                }else{
                                    require_once("dashboard-user.php");
                                }
                            ?>

                            
                        </div>
                        
                        
                        
                        <div class="col-md-12 col-lg-4">
                             <?php require_once("j_incs/adverts-right.php"); ?>
                        </div>
                    </div>
                </div>
                <!-- Content Wrapper END -->
                <?php
                require_once("j_incs/footer.php");
                ?>
            </div>
            <!-- Page Container END -->

        </div>
    </div>
    <?php
    require_once("j_incs/scripts-bottom.php");
    ?>
    <script>
	
		$("[name=wallet_type]").change(function () {
			$("[name=amount]").val('');
			$("[name=amount_usd]").val('');
			var  wallet_type_id = $("[name=wallet_type]");
			var  addressInfo_id = $(".addressInfo");
			var wallet_type_val = wallet_type_id.val();
			if(wallet_type_val !==''){
				addressInfo_id.html(wallet_type_val);
			}
		});
		$("[name=amount_usd]").keyup(function () {
			var  amount_crypto = $("[name=amount]");
			var  amount_usd = $("[name=amount_usd]");
			var  currency = $("[name=wallet_type]").val();
			var  amount_crypto_val = amount_crypto.val();
			var  amount_usd_val = amount_usd.val();
			var  rateResult = $("[name=amount]");
			if(currency !=='' && amount_usd !==''){
				amount_crypto.val('***');
				amount_crypto.attr('disabled','');
				$.post('<?php echo $dir; ?>wallet-topup/rate-usd.php', 
				{
					currency:currency,
					amount_crypto_val:amount_crypto_val,
					amount_usd_val:amount_usd_val,
				},function(data){
					if(data){
						rateResult.html(data);
						amount_crypto.removeAttr('disabled'); 
					}else{
						amount_crypto.val('');
						amount_crypto.removeAttr('disabled'); 
					}
				});
			}
		});
		
		
		
		$("[name=amount]").keyup(function () {
			var  amount_crypto = $("[name=amount]");
			var  amount_usd = $("[name=amount_usd]");
			var  currency = $("[name=wallet_type]").val();
			var  amount_crypto_val = amount_crypto.val();
			var  amount_usd_val = amount_usd.val();
			var  rateResult = $("[name=amount]");
			if(currency !=='' && amount_crypto !==''){
				amount_usd.val('***');
				amount_usd.attr('disabled','');
				$.post('<?php echo $dir; ?>wallet-topup/rate-crypto.php', 
				{
					currency:currency,
					amount_crypto_val:amount_crypto_val,
					amount_usd_val:amount_usd_val,
				},function(data){
					if(data){
					   rateResult.html(data);
						amount_usd.removeAttr('disabled'); 
					}else{
						amount_usd.val('');
						amount_usd.removeAttr('disabled'); 
					}
				});
			}
		});
		
    // img is a generic <img> element that is not rendered to the DOM.
    function onloadImage(num,url){
        var newImg_id = $('#m_imgg'+num+'');
        var hideImg_id = $('#imgg'+num+'');
        var imageUrl = url;

        let img = document.createElement('img');
        img.src = imageUrl;

        img.onload = function() {
            //newImg_id.css("background-image", "url(" + imageUrl + ")");
            newImg_id.html('<img class="img-fluid" src="'+imageUrl+'"/>');
            hideImg_id.remove();
        };

        img.onerror = function() {
          //alert("Error occurred while loading image");
        };
    }
    var numImgs = $('.main-content .c_img').length;
    for(i=1; i<=numImgs; i++){
        var newImg_id = $('#imgg'+i+'');
        var newImg_num = i;//newImg_id.attr('id');
        var newImg_url = newImg_id.attr('url');
        onloadImage(newImg_num,newImg_url);
    }
    
       
    
    $('.currency').click(function(){
        var currency = $(this).text();
        $('#chooseCurrency').html(currency);
    });
        
        
        
    $('.period').click(function(){
        var period = $(this).text();
        var currency = $('#chooseCurrency').text();
        if(period !=='Choose' && currency !==''){
            $("#period_r").html("<i class='anticon anticon-loading'></i> Loading Revenue Report for <b>"+period+"</b>");
            $('#choosePeriod').html(period);
            $.post('revenue.php', 
            {
                period : period,
                currency : currency,
            },function(data){
                if(data){
                    $('#Overall_r').hide();
                    $('#period_r').hide().fadeIn(2000).html(data);
                }else{
                    alert('No Report');
                }
            });
        }else{
            $('#choosePeriod').html('Choose Period');
            $('#period_r').html(''); 
            $('#Overall_r').slideDown();

        }
    });
        
        
    $(document).ready(function(){
        var period = $('#choosePeriod').text();
        var currency = $('#chooseCurrency').text();
        if(period !=='Choose' && currency !==''){
            $("#period_r").html("<i class='anticon anticon-loading'></i> Loading Revenue Report for <b>"+period+"</b>");
            $('#choosePeriod').html(period);
            $.post('revenue.php', 
            {
                period : period,
                currency : currency,
            },function(data){
                if(data){
                    $('#Overall_r').hide();
                    $('#period_r').hide().fadeIn(2000).html(data);
                }else{
                    alert('No Report');
                }
            });
        }else{
            $('#choosePeriod').html('Choose Period');
            $('#period_r').html(''); 
            $('#Overall_r').slideDown();

        }
    });
        
    </script>
   
</body>

</html>