<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; $resellerID = getReseller();
//=== List page stuffs =============
$pag = $page = 1; $setLimit = 20;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $setLimit) - $setLimit; 
if(isset($_GET["pag"])) $pag = (int)$_GET["pag"];
$pageLimit2 = ($pag * $setLimit) - $setLimit; 
$pageurl=$_SERVER['REQUEST_URI']."?";
if(count($_GET)) { $pageurl = $_SERVER['REQUEST_URI'].'&'; }
$trans = array("s=d_o" => "", "s=d_n" => "", "s=r_h" => "", "s=r_l" => "", "s=s_r" => "", "s=s_u" => "", "s=s_q" => "", "s=s_c" => "", "s=s_f" => "", "s=s_p" => "");
$pageurl = strtr($pageurl,$trans);
$pageurl = str_replace('&&','&',$pageurl);
define('TITLE','Gateways');
//====End ==============
if(userRole()>1) {
	header('location: index.php');	
}

$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
?>
<?php
if(isset($_POST['action'])) {
  if($_POST['action']=='dvouch') {
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"DELETE FROM `vouchers` WHERE `id` = '$inbid' AND customer_id = '0'") or die(mysqli_error($server));
	}	
	logEvent($userID,'Deleted sms vouchers');
	$_SESSION['message'] = $LANG['The selected SMS Voucher(s) has been deleted'];
	header('location: setting?err=0&vouc');
  }
  if($_POST['action']=='delplan') {
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"DELETE FROM `sms_plans` WHERE `id` = '$inbid' AND customer_id = '0'") or die(mysqli_error($server));
	}	
	logEvent($userID,'Deleted sms plans');
	$_SESSION['message'] = $LANG['The selected SMS Plan(s) has been deleted'];
	header('location: setting?err=0&pack');
  }
  
}


if(isset($_REQUEST['dgt'])) {
	$tdelete = mysqli_real_escape_string($server,$_REQUEST['dgt']);
	mysqli_query($server,"DELETE FROM `paymentgateways` WHERE id = '$tdelete' AND customer_id = '0'") or die(mysqli_error($server));
	$_SESSION['message'] = $LANG['The selected payment gateway has been deleted'];
	header('location: setting?err=0&pay');
 }
if(isset($_REQUEST['dst'])) {
	$tdelete = mysqli_real_escape_string($server,$_REQUEST['dst']);
	mysqli_query($server,"DELETE FROM `sms_gateways` WHERE id = '$tdelete' AND customer_id = '0'") or die(mysqli_error($server));
	$_SESSION['message'] = $LANG['The selected SMS gateway has been deleted'];
	header('location: setting?err=0&sms');
 } 
 if(isset($_REQUEST['drt'])) {
	$tdelete = mysqli_real_escape_string($server,$_REQUEST['drt']);
	mysqli_query($server,"DELETE FROM `rates` WHERE id = '$tdelete' AND customer_id = '0'") or die(mysqli_error($server));
	$_SESSION['message'] = $LANG['The selected custom rate has been deleted'];
	header('location: setting?err=0&rate');
 }	
if(isset($_GET['set'])) {
?>
<div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card red">
            <div class="card-content white-text"><?=$LANG['You have not completed initial setup yet!']?></div>
        </div>
    </div>
 </div>
<?php } ?> 
 
<div class="row no-m-t no-m-b">
	<div class="col s12 m12 l12 ">

      
      <h5><?=$LANG['Payment Gateways']?></h5>
      <div class="card card-white <?php if(isset($_GET['pay'])) echo ' responsive-tabs__panel--active'?>">
            <div class="card-content">
            	<span class="card-title"><?=$LANG['Payment Gateways']?></span>
                
                 <div class="row no-m-t no-m-b right"> 
                	<a class='waves-effect waves-light btn teal accent-4 white-text' href="#" onclick="$('#add_gateway_pop').show('slow');"><i class="fa fa-plus-circle"></i> <?=$LANG['Create New Gateway']?></a>
                </div>             	
                <div class="row no-m-t no-m-b">   
                     <div class="col s12 m12 l12">
                    <?php
                    $query="SELECT * FROM paymentgateways WHERE customer_id = '0' ORDER BY id DESC limit $pageLimit,$setLimit";
					$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 have not created any payment gateways! Customers will not be able to make payments untill you do so']?></h5>
                                </div>
                            </div>
                        </div>

                       </table> 
                      <?php } else { ?>  
                      <table class="responsive-table bordered">
                          <thead>
                            <tr>
                                <th data-field="ref"><?=$LANG['ID']?></th>
                                <th data-field="date"><?=$LANG['Title']?></th>
                                <th data-field="credit"><?=$LANG['Type']?></th>
                                <th data-field="valid"><?=$LANG['Status']?></th>
                                <th data-field="act"></th>
                             </tr>
                          </thead>
                          <tbody>
                       <?php
                        while ($rows = mysqli_fetch_assoc($result)) { 
						$status = $LANG['Enabled'];
						if($rows['enabled'] < 1)	$status =$LANG['Disabled'];
						$charge_type = 'fixed';
						if($rows['charges_perc'] > 0) $charge_type = 'percent';
						$params = "'$rows[type]','$rows[title]','$rows[param1]','$rows[param2]','$rows[param3]','$rows[minimum_order]','$charge_type','$rows[charges_perc]','$rows[charges_fix]','".str_replace("\n", '\n',$rows['text'])."','$rows[enabled]','$rows[id]'";
						?>
                             <tr>
                               	 <td><?=$rows['id']?></td>
                                 <td><?=$rows['title']?></td>
                                 <td><?=ucfirst($rows['type'])?></td>
                                 <td><?=$status?> </td>
                                 <td><a class="hoverable" href="#" onclick="editPayG(<?=$params?>);"><i class="fa fa-pencil"></i> <?=$LANG['Edit']?></a> &nbsp;
                                 <a class="hoverable red-text" onclick="return confirm('<?=$LANG['delete_confirm']?>');" href="paysetting?dgt=<?=$rows['id']?>"><i class="fa fa-trash"></i> <?=$LANG['Delete']?></a></td>
                             </tr>
                       <?php } ?>   
                       		</form>
                          </tbody>
                      </table>
                      <?php } ?>
                      <div class="col s12 m12 l12" style="margin-top: 5px;">
                          <?php displayPagination($setLimit,$page,$query,'vouc');  ?>
                      </div>
                   </div>
                </div>   
            </div>
      </div> 
 
	</div>
</div>

<!--  //Popups  ---->

 <!--add payment gateway -->
<div id="add_gateway_pop" style="display: none; overflow-x: hidden; width: 99.99%; height: 99.99%; z-index: 1000; position: fixed; top: 0; left: 0; background: rgba(0,0,0,0.7);">
   	 <div class="row"><br><br>
       	<div class="col s1 m2 l3"> </div>
        <div class="col s10 m8 l6 white"> 
             <h5><?=$LANG['Add New Payment Gateway']?> <span class="right"><a href="#" class="red-text" onclick="$('#add_gateway_form')[0].reset();$('#add_gateway_pop').hide('slow');"><i class="fa fa-times"></i></a></span></h5>
            
             <div class="card ">
                 <div class="card-content">
                 	<form action="" method="post" id="add_gateway_form" onSubmit="return false;">
	                    	<div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                   <select id="gtype" required class="js-states browser-default"  style="width: 100%">
                                        <option value="" disabled selected><?=$LANG['Select a gateway template']?></option>
                                        <?php $query="SELECT * FROM paymentgateway_templates WHERE status = 1";
                                        $result = mysqli_query($server,$query) or die(mysqli_error($server));
                                        while ($rows = mysqli_fetch_assoc($result)) { 
                                            echo '<option value="'.$rows['alias'].'">'.$rows['title'].'</option>';
                                        }
                                       ?> 
                                    </select>
                                    <label class="active"><?=$LANG['Gateway Type']?></label>
                                </div>
                            </div>
                        	<div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="gtitle" type="text" class="validate" required value="" >
                                       <label for="gtitle"><?=$LANG['Display Name']?></label>
                                 </div>
                            </div>                    
                           <div class="row no-m-t no-m-b" id="show_p1" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param1" type="text" class="validate" value="" >
                                       <label for="param1" id="label_p1"></label>
                                 </div>
                            </div>
                           <div class="row no-m-t no-m-b" id="show_p2" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param2" type="text" class="validate" value="" >
                                       <label for="param2" id="label_p2"></label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b" id="show_p3" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param3" type="text" class="validate" value="" >
                                       <label for="param3" id="label_p3"></label>
                                 </div>
                            </div>                           
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="minimum_order" type="text" class="validate" value="0.00" >
                                       <label for="minimum_order"><?=$LANG['Minimum Order']?> (<?=userCurrencyCode($userID)?>)</label>
                                 </div>
                            </div>
                           <div class="row no-m-t no-m-b">
                           	  <div class="input-field col s12">
                                   <select id="charge_type" required class="validate">
                                        <option value="0"><?=$LANG['None']?></option>
                                        <option value="fixed"><?=$LANG['Fixed Amount']?></option>
                                        <option value="percent"><?=$LANG['Percentage of Order Total']?></option>
                                   </select>
                                   <label><?=$LANG['Gateway Fee Type']?></label>
                           	  </div>
                            </div>
                            <div class="row no-m-t no-m-b" id="hide_fix" style="display: none;">
                                 <div class="input-field col s12">
                                       <input id="charges_perc" type="text" class="validate" value="0.00">
                                       <label for="charges_perc"><?=$LANG['Gateway Fee Percentage']?> (%)</label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b"  id="hide_pec" style="display: none;">
                                 <div class="input-field col s12">
                                       <input id="charges_fix" type="text" class="validate" value="0.00">
                                       <label for="charges_fix"><?=$LANG['Gateway Fee Ammount']?> (<?=userCurrencyCode($userID)?>)</label>
                                 </div>
                            </div>

                            <div class="row no-m-t no-m-b">
                                <div class="input-field col s12">
                                      <textarea id="text" class="materialize-textarea" length="960"></textarea>
                                      <label for="text"><?=$LANG['Gateway Notice']?></label>
                                </div>
                           </div>
                          <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <select id="genabled" required class="validate">
                                        <option value="1"><?=$LANG['Yes']?></option>
                                        <option value="0"><?=$LANG['No']?></option>
                                   </select>
                                   <label><?=$LANG['Enable Gateway']?></label>
                             </div>
                           </div>  
                            
                           <div class="card-action">
                           		<input type="hidden" id="gateway_id" value="0">
                                <button id="gate-button" class="btn green darken-4" type="submit"><?=$LANG['Save Gateway']?> </button>
                                <span id="gate-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
                            <a class="btn waves-effect waves-light red" href="#" onclick="$('#add_gateway_form')[0].reset();$('#add_gateway_pop').hide('slow');"><?=$LANG['Cancel']?></a>
                           </div>
                        </form>
                 </div>
              </div>
        </div>      
        <div class="col s1 m2 l3"> </div>
     </div>
</div>   

