<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; global $resellerID;
define('TITLE','Dashboard');
if(userRole()>2) {
	header('location: index.php');	
}
if(getSetting('businessName',$userID)=='' || getSetting('companyEmail',$userID)=='' ) { if(userRole()<2) header('location: setting?set'); exit();}
if(countSMSGates(0)< 1) {if(userRole()<2)header('location: smsetting?set'); exit();}
if(v3updates20200401_countDeRoute(0)< 1) {if(userRole()<2)header('location: smsetting?rset&rout'); exit();}
if(countPayGates(0)< 1) {if(userRole()<2)header('location: paysetting?set'); exit();}

$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);

$vars = array("customer_id"=>getUser());
$_SESSION['EventVals'] = $vars;
global $hooks; $hooks->do_action('PreDashboardEvents');
?>
<!-- Important Facts ::::: You can customize this --> 
 <div class="row no-m-t no-m-b">
 	<div class="col s12 m4 l3">
    	<a href="acustomer">
         <div class="card stats-card  light-blue darken-3 hoverable" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Customers']?></span>
                 <span class="stats-counter" style="color: white;"><span class="counter" style="color: white;"><?=number_format(countCustomers())?></span></span>
             </div>
          </div>
         </a> 
    </div>
    <div class="col s12 m4 l3">
    	<a href="acustomer?s=res">
         <div class="card stats-card teal darken-1 hoverable" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Resellers']?></span>
                 <span class="stats-counter" style="color: white;"><span class="counter" style="color: white;"><?=number_format(countReseller())?></span></span>
             </div>
          </div>
        </a>  
    </div>
    <div class="col s12 m4 l3">
        <a href="aticket">
         <div class="card stats-card blue-grey darken-4 hoverable" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['New Tickets']?></span>
                 <span class="stats-counter" style="color: white;"><span class="counter aticket" style="color: white;"><?=number_format(countNewTicket())?></span></span>
             </div>
          </div>
        </a>  
    </div>
    <div class="col s12 m4 l3">
    	<a href="asenderid">
         <div class="card stats-card orange darken-4 hoverable" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Sender ID Requests']?></span>
                 <span class="stats-counter" style="color: white;"><span class="counter" style="color: white;"><?=number_format(countNewSender())?></span></span>
             </div>
          </div>
        </a>  
    </div>   

<?php if(userRole()<3) {?>
<div class="col s12 m4 l3">
         <div class="card stats-card  red darken-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Total Sales']?></span>
                 <span class="stats-counter" style="color: white;"><?=userCurrencySymbol($userID)?><span class="counter" style="color: white;"><?=number_format(sunTransactions(),userCurrencyZero($userID))?></span></span>
             </div>
          </div>
    </div>
    <div class="col s12 m4 l3">
         <div class="card stats-card  pink accent-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['This Months Sales']?></span>
                 <span class="stats-counter" style="color: white;"><?=userCurrencySymbol($userID)?><span class="counter" style="color: white;"><?=number_format(sunTransactions(date('Y-m')),userCurrencyZero($userID))?></span></span>
             </div>
          </div>
    </div>
    <div class="col s12 m4 l3">
         <div class="card stats-card  green accent-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['This Year Sales']?></span>
                 <span class="stats-counter" style="color: white;"><?=userCurrencySymbol($userID)?><span class="counter" style="color: white;"><?=number_format(sunTransactions(date('Y')),userCurrencyZero($userID))?></span></span>
             </div>
          </div>
    </div>
    <div class="col s12 m4 l3">
         <div class="card stats-card  blue darken-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Customers Balance']?></span>
                 <span class="stats-counter" style="color: white;"><?=userCurrencySymbol($userID)?><span class="counter" style="color: white;"><?=number_format(sunBalance(),userCurrencyZero($userID))?></span></span>
             </div>
          </div>
    </div>         
 </div>
<?php } ?>
 
 <!-- SMS Trafic Chat -->
<div class="row no-m-t no-m-b">
    <div class="col s12 m12 l12">
        <div class="card visitors-card">
            <div class="card-content">
                <div class="card-options">
                    <ul>
	                    <li><a class='dropdown-button waves-effect waves-teal btn-flat' href='#' data-constrainwidth='false' data-activates='sms_chart_filt'><i class="fa fa-calendar"></i> <?=$LANG['Filter']?></a></li>
                    </ul>
                    <ul id='sms_chart_filt' class='dropdown-content'>
                        <li><a href="javascript:void(0);" onclick="loadDashboardSMSGraph('day');">Today</a></li>
                        <li><a href="javascript:void(0);" onclick="loadDashboardSMSGraph('week');">Past 7 Days</a></li>
                        <li><a href="javascript:void(0);" onclick="loadDashboardSMSGraph('month');">This Month</a></li>
                        <li><a href="javascript:void(0);" onclick="loadDashboardSMSGraph('months');">Past 30 Days</a></li>
                        <li><a href="javascript:void(0);" onclick="loadDashboardSMSGraph('quater');">Past 3 Months</a></li>
                        <li><a href="javascript:void(0);" onclick="loadDashboardSMSGraph('half');">Past 6 Months</a></li>
                        <li><a href="javascript:void(0);" onclick="loadDashboardSMSGraph('year');">This Year</a></li> 
                    </ul>
                </div>
    	        <span class="card-title"><?=$LANG['SMS Traffic By Status']?>
                	<span class="secondary-title" id="sms_chart_desc"></span>
                    <span class="secondary-title right">
                    	<a class="green-text"><i class="fa fa-square"></i> <?=$LANG['Sent SMS']?></a>&nbsp;&nbsp;&nbsp;
                        <a class="red-text"><i class="fa fa-square"></i> <?=$LANG['Failed SMS']?></a>
                    </span>
            	</span>
               	<div id="chart1"></div>
            </div>    
        </div>
    </div>
</div>
<div class="row no-m-t no-m-b">
	<div class="col s12 m6 l6">
    <!-- resect tickets -->
       <div class="card invoices-card">
             <div class="card-content">
                  <span class="card-title"><?=$LANG['Recent Tickets']?></span>
               <?php
			    $query="SELECT t.* FROM tickets t JOIN users u ON t.customer_id = u.id WHERE t.parent_id = 0 AND t.customer_reply = 1 AND u.reseller = '0' AND (t.status = 'open' OR t.status = 'customer-reply') ORDER BY t.last_updated DESC LIMIT 6";
				$result = mysqli_query($server,$query) or die(mysqli_error($server));
				$x = mysqli_num_rows($result);
				if($x < 1) {
				?><table>	   
                  <div class="col s12 m12 l12">
                        <div class="card blue lighten-5">
                            <div class="card-content">
            	                 <h3 class="center-align"><i class="fa fa-exclamation-circle"></i></h3>
			                     <h5 class="center-align"><?=$LANG['You do not have any unanswered tickets']?></h5>
                            </div>
                        </div>
                    </div>
                   </table> 
                  <?php } else { ?>  
                  <table class="responsive-table bordered">
                      <thead>
                        <tr>
                            <th data-field="date"><?=$LANG['Date']?></th>
                            <th data-field="customer"><?=$LANG['Customer']?></th>
                            <th data-field="progress"><?=$LANG['Subject']?></th>
                            <th></th>
                         </tr>
                      </thead>
                      <tbody>
                      <?php
					while ($rows = mysqli_fetch_assoc($result)) {
$customer = '<a href="acustomer?v='.$rows['customer_id'].'">'.userData($rows['customer_id'],'first_name').' '.userData($rows['customer_id'],'last_name').'</a>';
if($rows['customer_id']==0) $customer = 'Administrator';
						 ?>
                         <tr>
                             <td><?=timeElapsed($rows['last_updated'])?></td>
                             <td class=""><?=$customer?></td>
                             <td class="">#<?=$rows['id'].' - '.$rows['subject']?></td>
                           <td><a class="hoverable" href="aticket?ti=<?=$rows['id']?>"><?=$LANG['Details']?></a></td>
                         </tr>
                   <?php } ?>   
                      </tbody>
                  </table>
                  <?php } ?>
             </div>
        </div>
        
         <!-- Recent sender ID -->
		<div class="card invoices-card">
             <div class="card-content">
                  <span class="card-title"><?=$LANG['Recent Sender ID Requests']?></span>
                 <?php
			    $query="SELECT * FROM sender_id WHERE status = 'pending' ORDER BY id DESC LIMIT 6";
				$result = mysqli_query($server,$query) or die(mysqli_error($server));
				$x = mysqli_num_rows($result);
				if($x < 1) {
				?><table>	   
                  <div class="col s12 m12 l12">
                        <div class="card blue lighten-5">
                            <div class="card-content">
            	                 <h3 class="center-align"><i class="fa fa-exclamation-circle"></i></h3>
			                     <h5 class="center-align"><?=$LANG['You do not have any pending sender ID requests']?></h5>
                            </div>
                        </div>
                    </div>
                   </table> 
                  <?php } else { ?>  
                  <table class="responsive-table bordered">
                      <thead>
                        <tr>
                            <th data-field="date"><?=$LANG['Type']?></th>
                            <th data-field="customer"><?=$LANG['Customer']?></th>
                            <th data-field="credits"><?=$LANG['Sender ID']?></th>
                            <th></th
                         ></tr>
                      </thead>
                      <tbody>
                      <?php
					while ($rows = mysqli_fetch_assoc($result)) { 
					$customer = '<a href="acustomer?v='.$rows['customer_id'].'">'.userData($rows['customer_id'],'first_name').' '.userData($rows['customer_id'],'last_name').'</a>';
if($rows['customer_id']==0) $customer = 'Administrator';
					?>
                         <tr>
                           <td><?=strtoupper($rows['type'])?></td>
                            <td class=""><?=$customer?></td>
                           <td><?=$rows['sender_id']?></td>
                           <td><?php if(userRole()<3) {?><a class="hoverable green-text" href="asenderid?app=<?=$rows['id']?>"><?=$LANG['Approve']?></a><?php } ?></td>
                         </tr>
                   <?php } ?>   
                      </tbody>
                  </table>
                  <?php } ?>
             </div>
         </div>  
    </div>

    <div class="col s12 m6 l6">
    <!-- Recent transactions -->
		<div class="card invoices-card">
             <div class="card-content">
                  <span class="card-title"><?=$LANG['Recent Transactions']?></span>
                   <?php
			    $query="SELECT t.* FROM transactions t JOIN users u ON t.customer_id = u.id WHERE u.reseller = '0' ORDER BY t.date DESC LIMIT 6";
				$result = mysqli_query($server,$query) or die(mysqli_error($server));
				$x = mysqli_num_rows($result);
				if($x < 1) {
				?><table>	   
                  <div class="col s12 m12 l12">
                        <div class="card blue lighten-5">
                            <div class="card-content">
            	                 <h3 class="center-align"><i class="fa fa-exclamation-circle"></i></h3>
			                     <h5 class="center-align"><?=$LANG['There are no transaction records yet']?></h5>
                            </div>
                        </div>
                    </div>
                   </table> 
                  <?php } else { ?>  
                  <table class="responsive-table bordered">
                      <thead>
                        <tr>
                            <th data-field="id"><?=$LANG['ID']?></th>
                            <th data-field="date"><?=$LANG['Date']?></th>
                            <th data-field="customer"><?=$LANG['Customer']?></th>
                            <th data-field="progress"><?=$LANG['Status']?></th>
                            <th data-field="credits"><?=$LANG['Credits']?></th>
                            <th></th
                         ></tr>
                      </thead>
                      <tbody>
                      <?php
					while ($rows = mysqli_fetch_assoc($result)) { 
					$link = '';
					if( ($rows['is_earned'] < 1) && ($rows['is_shared'] < 1) && ($rows['credits'] > 0) ) {
					$link = '<a class="hoverable" href="atransaction?view='.$rows['transaction_reference'].'">'.$LANG['Details'].'</a>';	
					if(strtolower($rows['status']) != 'completed')
					$link .= ' &nbsp;<a onclick="return confirm(\''.$LANG['order_confirm_lan'].'\');" class="hoverable green-text" href="atransaction?appr='.$rows['transaction_reference'].'">'.$LANG['Approve'].'</a>';	
					}
					$customer = '<a href="acustomer?v='.$rows['customer_id'].'">'.userData($rows['customer_id'],'first_name').' '.userData($rows['customer_id'],'last_name').'</a>';
if($rows['customer_id']==0) $customer = 'Administrator';
					?>
                         <tr>
                          <td><?=$rows['id']?></td>
                           <td><?=timeElapsed($rows['date'])?></td>
                            <td class=""><?=$customer?></td>
                           <td><?=ucfirst($rows['status'])?></td>
                           <td><?=userCurrencySymbol($userID)?><?=number_format($rows['credits']/userExchangeRate($rows['customer_id']),userCurrencyZero($userID))?></td>
                           <td><?php if(userRole()<3) {?><?=$link?><?php } ?></td>
                         </tr>
                   <?php } ?>   
                      </tbody>
                  </table>
                  <?php } ?>
             </div>
         </div>         
    
    <!-- Recent mesages -->
		<div class="card invoices-card">
             <div class="card-content">
                  <span class="card-title"><?=$LANG['Recent Traffic']?></span>
               <?php
			    $query="SELECT * FROM sentmessages ORDER BY date DESC LIMIT 6";
				$result = mysqli_query($server,$query) or die(mysqli_error($server));
				$x = mysqli_num_rows($result);
				if($x < 1) {
				?><table>	   
                  <div class="col s12 m12 l12">
                        <div class="card blue lighten-5">
                            <div class="card-content">
            	                 <h3 class="center-align"><i class="fa fa-exclamation-circle"></i></h3>
			                     <h5 class="center-align"><?=$LANG['There are no sent messages yet']?></h5>
                            </div>
                        </div>
                    </div>
                   </table> 
                  <?php } else { 
				  ?>  
                  <table class="responsive-table bordered">
                      <thead>
                        <tr>
                            <th data-field="id"><?=$LANG['ID']?></th>
                            <th data-field="date"><?=$LANG['Date']?></th>
                            <th data-field="customer"><?=$LANG['Customer']?></th>
                            <th data-field="progress"><?=$LANG['Status']?></th>
                            <th data-field="credits"><?=$LANG['Credits']?></th>
                            <th></th
                         ></tr>
                      </thead>
                      <tbody>
                      <?php
					while ($rows = mysqli_fetch_assoc($result)) { 
					$customer = '<a href="acustomer?v='.$rows['customer_id'].'">'.userData($rows['customer_id'],'first_name').' '.userData($rows['customer_id'],'last_name').'</a>';
if($rows['customer_id']==0) $customer = 'Administrator';
					?>
                         <tr>
                          <td><?=$rows['id']?></td>
                           <td><?=timeElapsed($rows['date'])?></td>
                            <td class=""><?=$customer?></td>
                           <td><?=ucfirst($rows['status'])?></td>
                           <td><?=userCurrencySymbol($userID)?><?=number_format(messageCost($rows['id'])/userExchangeRate($rows['customer_id']),userCurrencyZero($userID))?></td>
                           <td><a class="hoverable" href="asent?view=<?=$rows['id']?>"><?=$LANG['Details']?></a></td>
                         </tr>
                   <?php } ?>   
                      </tbody>
                  </table>
                  <?php } ?>
             </div>
        </div>
                
    </div>                         
</div>
<?php
$vars = array("customer_id"=>getUser());
$_SESSION['EventVals'] = $vars;
global $hooks; $hooks->do_action('PostDashboardEvents');
?>