<?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','Recharge');
if(!isAdmin(getUser()) && (userData(getUser(),'phone') == '' || userData(getUser(),'first_name') == '') ) {
	header('location: profile?setting'); exit();
}
if(!isAdmin(getUser()) && (userData($userID,'email_verified') != 0 || userData($userID,'phone_verified') != 0)) {
	showVerify();
}
if(userRole()<= 2) {
	header('location: index.php');	
	exit();
}
$resellerID = getReseller();
$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
$billingModel = checkBillingModel($userID);
if($billingModel == 'unit') { }
if(!isset($_GET['confirm'])) {
?>
<!-- Quick Navs ::::: You can customize this -->
 <div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card blue">
            <div class="card-content white-text"><?=$LANG['buy_credit_tip']?></div>
        </div>
    </div>
 </div>
 
 <div class="row">
     <div class="col s12">
         <ul class="tabs tab-demo z-depth-1" style="width: 100%;">
             <li class="tab col s4"><a href="#credit" class=""><?=$billingModel=='unit'?$LANG['Buy_Unitss']:$LANG['Buy_Credits']?></a></li>
             <li class="tab col s4 <?php if(getSetting('useSMSPackage')<1) echo 'disabled';?>"><a class="" href="#plan"><?=$LANG['Purchase a Package']?></a></li>
             <li class="tab col s4 <?php if(getSetting('useSMSVoucher')<1) echo 'disabled';?>"><a href="#vourcher"><?=$LANG['Load a Voucher']?></a></li>
         </ul>
     </div>
     
     <!-- credit -->
     <div id="credit" class="col s12 m6 l7">
      <form action="" method="post" id="credit-form">
     	<div class="card white darken-1"><?php if(getSetting('buycreditDisabled')>0) { ?>
         <div class="row" style="z-index:999;position:absolute;top:0;left:0;width: 100%; height: 100%;background: rgba(0,0,0,0.8);color:black;">
      		<div class="col s1 m2 l3"> </div>
            <div class="col s10 m8 l6 white" style="padding: 15px;text-align:center;margin-top:100px"><?=$LANG['funding_opt_not_available']?></div>
            <div class="col s1 m2 l3"> </div>
          </div> <?php } ?>
            <div class="card-content">
                <span class="card-title"><i class="fa fa-calculator"></i> <?=$billingModel=='unit'?$LANG['Buy SMS Units']:$LANG['Buy SMS Credits']?></span>
                <p><?=$billingModel=='unit'?$LANG['Enter the quantity of SMS units you want to buy']:$LANG['Enter the amount of SMS credits you want to buy in']?> <?=$billingModel=='unit'?'':userCurrencyCode($userID)?>.</p>
                <div class="row">
                    <div class="input-field col s12">
                        <i class="material-icons prefix">shopping_cart</i>
                        <input id="credits" type="text" class="validate" required>
                        <label for="credits"><?=$LANG['How much credits do you need?']?> <?=$billingModel=='unit'?'':'('.userCurrencyCode($userID).')'?></label>
                    </div>
                    <div class="input-field col s12">
                    	<i class="material-icons prefix">credit_card</i>
                       <select id="payment_opt" required class="validate">
                            <option value="" disabled selected><?=$LANG['How do you want to pay?']?></option>
                            <?php $query="SELECT * FROM paymentgateways WHERE customer_id = '$resellerID' AND enabled > 0";
                            $result = mysqli_query($server,$query) or die(mysqli_error($server));
                            while ($rows = mysqli_fetch_assoc($result)) { 
                            	echo '<option value="'.$rows['id'].'">'.$rows['title'].'</option>';
                            }
                           ?> 
                        </select>
                        <label><?=$LANG['Payment Method']?></label>
                    </div>
                </div> 
                <p><?=$LANG['payment_footer_1']?></p>   
            </div>
            <div class="card-action">
                <a class="btn waves-effect waves-light red" href="dashboard"><i class="fa fa-times"></i> <?=$LANG['Cancel Order']?></a>
                <button id="credit-button" class="btn green" type="submit"><i class="fa fa-check"></i> <?=$LANG['Proceed']?> </button>
                <span id="credit-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
            </div>
        </div>
       </form> 
     </div>
     <!-- plans -->
     <div id="plan" class="col s12 s12 m6 l7">
     	<form action="" method="post" id="plan-form">
     	<div class="card white darken-1">
            <div class="card-content">
                <span class="card-title"><i class="fa fa-shopping-cart"></i> <?=$LANG['Purchase SMS Package']?></span>
                <p><?=$LANG['Select the SMS package you want to buy']?>.</p>
                <div class="row">
                    <div class="input-field col s12">
                        <select id="sms_pack" required class="js-states browser-default">
                            <option value="" disabled selected><?=$LANG['Choose an SMS Package']?></option>
                            <?php $query="SELECT * FROM sms_plans WHERE customer_id = '$resellerID' ORDER BY price ASC";
                            $result = mysqli_query($server,$query) or die(mysqli_error($server));
                            while ($rows = mysqli_fetch_assoc($result)) { 
                            	echo '<option value="'.$rows['id'].':'.$rows['price'].':'.$rows['credits'].'">'.$rows['title'].'</option>';
                            }
                           ?> 
                        </select>
                        <label class="active"><?=$LANG['SMS Package']?></label>
                    </div>
                    <div class="input-field col s12">
                    	<i class="material-icons prefix">credit_card</i>
                       <select id="payment_opt2" required>
                            <option value="" disabled selected><?=$LANG['How do you want to pay?']?></option>
                            <?php $query="SELECT * FROM paymentgateways WHERE customer_id = '$resellerID' AND enabled > 0";
                            $result = mysqli_query($server,$query) or die(mysqli_error($server));
                            while ($rows = mysqli_fetch_assoc($result)) { 
                            	echo '<option value="'.$rows['id'].'">'.$rows['title'].'</option>';
                            }
                           ?> 
                        </select>
                        <label><?=$LANG['Payment Method']?></label>
                    </div>
                </div> 
                <p><?=$LANG['payment_footer_1']?></p>   
            </div>
            <div class="card-action">
                <a class="btn waves-effect waves-light red" href="dashboard"><i class="fa fa-times"></i> <?=$LANG['Cancel Order']?></a>
                <button class="btn green" id="plan-button" type="submit"><i class="fa fa-check"></i> <?=$LANG['Proceed']?> </button>
                <span id="plan-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
            </div>
        </div>
       </form> 
     </div>
     <!-- voucher -->
     <div id="vourcher" class="col s12 s12 m6 l7">
     	<form action="" method="post" id="voucher-form">
     	<div class="card white darken-1">
            <div class="card-content">
                <span class="card-title"><i class="fa fa-credit-card"></i> <?=$LANG['Purchase SMS Package']?></span>
                <p><?=$LANG['voucher_intro']?>.</p>
                <div class="row">
                    <div class="input-field col s12">
                        <i class="material-icons prefix">credit_card</i>
                        <input id="voucher" type="text" class="validate" required>
                        <label for="voucher"><?=$LANG['Voucher PIN']?> </label>
                    </div>
                </div> 
                <p><?=$LANG['payment_footer_3']?></p>   
            </div>
            <div class="card-action">
                <a class="btn waves-effect waves-light red" href="dashboard"><i class="fa fa-times"></i> <?=$LANG['Cancel Order']?></a>
                <button class="btn green" id="voucher-button" type="submit"><i class="fa fa-money"></i> <?=$LANG['Recharge']?> </button>
                <span id="voucher-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
            </div>
        </div>
       </form> 
     </div>
     
     <div id="sumary" class="col s12 s12 m6 l5">
     	<div class="card blue darken-4 white-text">
            <div class="card-content">
                <span class="card-title"><i class="fa fa-shopping-basket"></i> <?=$LANG['Order Summery']?></span>
                <ul class="collection">
                    <li class="collection-item blue  darken-4 white-text"><div><h5><?=$billingModel=='unit'?$LANG['SMS Units']:$LANG['SMS Credits']?> </h5><h6><?=$billingModel=='unit'?'':userCurrencySymbol($userID)?><span  id="credit_val">0.00</span></h6></div></li>
                    <li class="collection-item blue  darken-4 white-text"><div><h5><?=$LANG['Total Amount']?> </h5><h6><?=userCurrencySymbol($userID)?><span id="total_val">0.00</span></h6></div></li>
                    <?php
					$vars = array(  "id"=>$userID   );
					$_SESSION['EventVals'] = $vars;
					global $hooks; $hooks->do_action('CustomerOrderSummary');
					?>
         			<li class="collection-item blue  darken-4 white-text"><div><h5><?=$LANG['Payment Method']?> </h5><h6 id="gateway_val">-</h6></div></li>           
                </ul>
            </div>
        </div>
     </div>
     
</div>
<?php } else { 
if(!isset($_SESSION['reference'])) { header('location: fund');	} 
//get trans data
if(transactionData($_SESSION['reference'],'customer_id')<1) { header('location: fund');	} 
$total = transactionData($_SESSION['reference'],'cost');
?>
<div id="printable">
	<?php if(strtolower(transactionData($_SESSION['reference'],'status'))=='completed') { $status = $LANG['Paid']?>
		<div class="green darken-4">
    <?php } elseif(strtolower(transactionData($_SESSION['reference'],'status'))=='canceled') { $status = $LANG['Canceled'] ?>
    	<div class="grey darken-1">
     <?php } else { $status = $LANG['Unpaid'] ?>
    	<div class="red darken-4">
    <?php } ?>    
                    <div class="container">
                        <div class="row no-m-b">
                            <div class="col s12 m12 l12">
                                <div class="card card-transparent no-m">
                                    <div class="card-content  white-text">
                                        <div class="row">
                                            <div class="col s12 m6 l6">
                                                <h4><?=getSetting('businessName')?></h4>
                                                <address>
                                                    <?=getSetting('companyAddress')?><br>
                                                    <i class="fa fa-phone"></i>: <?=getSetting('companyPhone')?>
                                                </address>
                                            </div>
                                            <div class="col s12 m6 l6 right-align">
                                                <h4><?=$status?></h4>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="container">
                    <div class="row">
                        <div class="col s12 m12 l12">
                            <div class="card card-white no-m">
                                <div class="card-content invoice-relative-content">
                                    <div class="row">
                                        <div class="col s12 12 l12 center-align">
                                            <h4>
                                                <?=$LANG['Invoice']?> #<?=$_SESSION['reference']?></b><br>
                                            </h4>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col s12 m6 l4">
                                            <p>
                                                <span class="light-blue-text"><?=$LANG['Customer']?></span><br>
                                                <b><?=userData($userID,'first_name').' '.userData($userID,'last_name')?></b><br>
                                                <b><?=userData($userID,'phone')?></b><br>
                                            </p>
                                        </div> 
                                        <div class="col s12 m6 l3">
                                            <p>
                                                <span class="light-blue-text"><?=$LANG['Invoice Date']?></span><br>
                                                <b><?=date('M j, Y',strtotime(transactionData($_SESSION['reference'],'date')))?></b><br>
                                            </p>
                                        </div> 
                                        <div class="col s12 m6 l3">
                                            <p>
                                            <?php if($status==$LANG['Paid']) {?>
                                               <span class="light-blue-text"><?=$LANG['Paid On']?></span><br>
                                                <b><?=date('M j, Y',strtotime(transactionData($_SESSION['reference'],'paid_on')))?></b><br>
                                            <?php } else {?>
                                            	<span class="light-blue-text"><?=$LANG['Due Date']?></span><br>
                                                <b><?=date('M j, Y',strtotime(transactionData($_SESSION['reference'],'date')))?></b><br>
                                            <?php }?>    
                                            </p>
                                        </div>
                                        <div class="col s12 m6 l6 right-align">
    	                                        <a onclick="Print('printable');" class="btn-floating btn-large waves-effect waves-light indigo darken-4 invoice-edit-btn"><i class="material-icons">print</i></a>
	                                    </div>
                                    </div>
                                    <div class="row">
                                        <div class="col s12 m12 l12">
                                            <table class="table responsive2-table">
                                                <thead>
                                                    <tr>
                                                        <th>S/N</th>
                                                        <th><?=$LANG['Description']?></th>
                                                        <th class="right-align"><?=$LANG['Quantity']?></th>
                                                        <th class="right-align"><?=$LANG['Cost']?></th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <tr>
                                                    	<th>1</th>
                                                        <td><?=transactionData($_SESSION['reference'],'description')?></td>
                                                        <td class="right-align"><?=number_format(transactionData($_SESSION['reference'],'credits'),3)?></td>
                                 						<td class="right-align"><?=userCurrencySymbol($userID)?><?=number_format(transactionData($_SESSION['reference'],'cost'),3)?></td>                       
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col s12 m6 l7">
                                            <span class="heading-title"><?=$LANG['Thank you']?> !</span>
                                         <?php if($status == $LANG['Unpaid']) { ?>   
                                            <?=paymentGatewayData(transactionData($_SESSION['reference'],'method'),'text')?>
                                          <?php } ?>  
                                       <?php if(!empty(getSetting('invoiceSignature'))) { ?>     
                                            <img src="files/temp/<?=getSetting('invoiceSignature')?>" class="invoice-signature m-t-md" alt="">
                                       <?php } ?>
                                       <?php if($status == $LANG['Unpaid']) { ?>
                                       		<br>
                                       		<a id="cancel_inv" class="waves-effect waves-light btn deep-orange darken-2"><?=$LANG['Cancel Invoice']?></a>     
                                             <span id="cancin-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>

                                       <?php } ?>     
                                        </div>
                                        <div class="col s12 m6 l5 right-align">
                                            <div class="text-right">
                                                <h6 class="m-t-sm light-blue-text"><b><?=$LANG['Subtotal']?></b></h6>
                                                <h5 class=""><?=userCurrencySymbol($userID)?><?=number_format(transactionData($_SESSION['reference'],'cost'),3)?></h5>
                                                <div class="divider"></div>
                                        <?php if(gatewayCharge(getUser(),transactionData($_SESSION['reference'],'method'),transactionData($_SESSION['reference'],'cost')) > 0) { 
											$total += gatewayCharge(getUser(),transactionData($_SESSION['reference'],'method'),transactionData($_SESSION['reference'],'cost'))
										?>
                                                <h6 class="m-t-sm light-blue-text"><b><?=$LANG['Gateway Charges']?></b></h6>
                                                <h5 class=""><?=userCurrencySymbol($userID)?><?=number_format(gatewayCharge(getUser(),transactionData($_SESSION['reference'],'method'),transactionData($_SESSION['reference'],'cost')),3)?></h5>
                                                <div class="divider"></div>
                                        <?php } ?>
                                                
                                        <?php if(getUserVat(getUser(),transactionData($_SESSION['reference'],'cost')) >0) { 
											$total += getUserVat(getUser(),transactionData($_SESSION['reference'],'cost'));
										?>
                                                <h6 class="m-t-sm light-blue-text"><b><?=$LANG['VAT']?> (<?=getSetting('vatAmount')?>%)</b></h6>
                                                <h5 class=""><?=userCurrencySymbol($userID)?><?=number_format(getUserVat(getUser(),transactionData($_SESSION['reference'],'cost')),3)?></h5>
                                                <div class="divider"></div>
                                        <?php } ?>
                                                
                                        <?php if(getUserTax(getUser(),transactionData($_SESSION['reference'],'cost')) >0) { 
											$total += getUserTax(getUser(),transactionData($_SESSION['reference'],'cost'));
										?>
                                                <h6 class="m-t-sm light-blue-text"><b><?=$LANG['TAX']?> (<?=getSetting('taxAmount')?>%)</b></h6>
                                                <h5 class=""><?=userCurrencySymbol($userID)?><?=number_format(getUserTax(getUser(),transactionData($_SESSION['reference'],'cost')),3)?></h5>
                                                <div class="divider"></div>
                                        <?php } ?>
                                                <h6 class="m-t-md text-success light-blue-text"><b><?=$LANG['Total']?></b></h6>
                                                <h4 class="text-success"><?=userCurrencySymbol($userID)?><?=number_format($total,3)?></h4>
                                                <div class="divider"></div>
                                                <h6 class="m-t-md text-success light-blue-text"><b><?=$LANG['Payment Method']?></b></h6>
                                                <h5 class="text-success"><?=paymentMethod(transactionData($_SESSION['reference'],'method'))?></h5>
                                                <br>
                                     <?php if($status == $LANG['Unpaid']) { ?>           
                                        <?php if(paymentGatewayData(transactionData($_SESSION['reference'],'method'),'type')=='custom') { ?>        
                                               <!-- <button type="submit" class="waves-effect waves-light btn green"><?=$LANG['Payment Completed']?></button>-->
                                        <?php } else { 
											loadPaymentGateway(transactionData($_SESSION['reference'],'method'),$_SESSION['reference'],$total,getUser());
										} ?>  
                                    <?php } ?>          
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
</div>                
<?php } ?>