<?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','Routing');
//====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($_REQUEST['dsg'])) {
	$tdelete = mysqli_real_escape_string($server,$_REQUEST['dsg']);
	mysqli_query($server,"DELETE FROM `sms_gateways` WHERE id = '$tdelete'") or die(mysqli_error($server));
	mysqli_query($server,"DELETE FROM `routes` WHERE gateway_id = '$tdelete'") or die(mysqli_error($server));
	$_SESSION['message'] = $LANG['The selected SMS gateway with all assigned routing rules has been deleted'];
	header('location: smsetting?err=0&sms');
 } 
 if(isset($_REQUEST['drt'])) {
	$tdelete = mysqli_real_escape_string($server,$_REQUEST['drt']);
	mysqli_query($server,"DELETE FROM `routes` WHERE id = '$tdelete'") or die(mysqli_error($server));
	$_SESSION['message'] = $LANG['The selected routing rule has been deleted'];
	header('location: smsetting?err=0&rout');
 }	
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 responsive-tabs">

       <h5><?=$LANG['SMS Gateways']?></h5>
      <div class="card card-white <?php if(isset($_GET['sms'])) echo ' responsive-tabs__panel--active'?>">
            <div class="card-content">
            	<span class="card-title"><?=$LANG['SMS 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_sms_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 sms_gateways WHERE 1 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 SMS gateways! Customers will not be able to send messages 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="temo"><?=$LANG['Template']?></th>
                                <th data-field="valid"><?=$LANG['Balance']?></th>
                                <th data-field="act"></th>
                             </tr>
                          </thead>
                          <tbody>
                       <?php
                        while ($rows = mysqli_fetch_assoc($result)) { 
						$params = $rows['id'];
						?>
                             <tr>
                               	 <td><?=$rows['id']?></td>
                                 <td><?=$rows['title']?></td>
                                 <td><?=strtoupper($rows['type'])?></td>
                                 <td><?=ucfirst($rows['alias'])?></td>
                                 <td><?=gateAPIBalance($rows['balance_url'])?> </td>
                                 <td><a class="hoverable" href="?gwid=<?=$params?>" ><i class="fa fa-pencil"></i> <?=$LANG['Edit']?></a> &nbsp;
                                 <a class="hoverable red-text" onclick="return confirm('<?=$LANG['delete_confirm']?>');" href="smsetting?sms&dsg=<?=$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> 
      
      	<h5><?=$LANG['SMS Routing Rules']?></h5>
      <div class="card invoices-card <?php if(isset($_GET['rout'])) echo ' responsive-tabs__panel--active'?>">
            <div class="card-content">
            	<div class="row no-m-t no-m-b">   
                
                     <div class="col s12 m6 l7">
                          <h5><?=$LANG['SMS Routing Rules']?></h5> 
                     <?php
                    $query="SELECT * FROM routes WHERE 1 ORDER BY id DESC";
					$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 routing rules yet']?></h5>
                                </div>
                            </div>
                        </div>
                       </table> 
                      <?php } else { ?>  
                      <table class="responsive-table bordered">
                          <thead>
                            <tr>
                                <th data-field="ref"><?=$LANG['Type']?></th>
                                <th data-field="status"><?=$LANG['Customer']?></th>
                                <th data-field="status"><?=$LANG['Destination']?></th>
                                <th data-field="credit"><?=$LANG['Operator Prefix']?></th>
                                <th data-field="date"><?=$LANG['Gateway']?></th>
                                <th data-field="act"></th>
                             </tr>
                          </thead>
                          <tbody>
                          <?php
                        while ($rows = mysqli_fetch_assoc($result)) { 
						$link = '';
						$customer = userData($rows['customer_id'],'first_name').' '.userData($rows['customer_id'],'last_name');
						$operator = $rows['operator_code'];
						$country = countryName(getCountryByCode($rows['country_code']));
						if($rows['customer_id'] < 1)	$customer = $LANG['All'];
						if($rows['operator_code'] < 1)	$operator = $LANG['Any Operator'];
						if($rows['country_code'] < 1)	$country = $LANG['All'];
						if($rows['type']=='sms') $rows['type'] = 'text'
						?>
                             <tr>
                               	 <td><?=strtoupper($rows['type'])?></td>
                                 <td><?=$customer?> </td>
                                 <td><?=$country?> </td>
                                 <td><?=$operator?></td>
                                 <td><?=smsGatewayData($rows['gateway_id'],'title')?></td>
                                  <td><a class="hoverable red-text" onclick="return confirm('<?=$LANG['delete_confirm']?>');" href="smsetting?drt=<?=$rows['id']?>"><?=$LANG['Delete']?></a></td>
                             </tr>
                       <?php } ?>   
                          </tbody>
                      </table>
                      <?php } ?>
                      <br><br>
                     </div>
                
                     <div class="col s12 m6 l5 yellow lighten-5" style="border:1px solid black">
                          <form action="" method="post" id="credit-form" onSubmit="return false;">
                          	<h5><?=$LANG['Add New Routing Rule']?></h5> 
                          	  <div class="col s12 m12 l12">
                                <div class="card blue lighten-5">
                                    <div class="card-content" ><?=$LANG['route_tip']?></div>
                                </div>
                            </div>
                                      
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                      <select id="customer_id" required class="js-states browser-default"  style="width: 100%" >
                                        <option value="0" selected><?=$LANG['All Customers']?></option>
                                        <?php $query="SELECT * FROM users WHERE reseller = 0 AND role_id > 2";
                                        $result = mysqli_query($server,$query) or die(mysqli_error($server));
                                        while ($rows = mysqli_fetch_assoc($result)) { 
                                            echo '<option value="'.$rows['id'].'">'.$rows['first_name'].' '.$rows['last_name'].' ('.$rows['email'].')</option>';
                                        }
                                       ?> 
                                    </select>
                                    <label class="active"><?=$LANG['Customer']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <select id="country_code" required class="js-states browser-default"  style="width: 100%" >
                                       		<option value="0" ><?=$LANG['All Countries']?></option>
                                            <?=getCountryList('') ?>                            
                                       </select>
                                       <label class="active"><?=$LANG['Country']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="operator_code" type="number" class="validate" value="" placeholder="Eg. 803 for MTN Nigeria, 82 for Vodaconn South-Africa" >
                                       <label for="operator_code"><?=$LANG['Operator Prefix']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                     <select id="gateway_id" required class="js-states browser-default"  style="width: 100%" >
                                        <option value="" disabled selected><?=$LANG['Assign a gateway']?></option>
                                        <?php $query="SELECT * FROM sms_gateways ";
                                        $result = mysqli_query($server,$query) or die(mysqli_error($server));
                                        while ($rows = mysqli_fetch_assoc($result)) { 
                                            echo '<option value="'.$rows['id'].'">'.$rows['title'].' ('.strtoupper($rows['type']).') </option>';
                                        }
                                       ?> 
                                    </select>
                                    <label class="active"><?=$LANG['Gateway']?></label>
                                 </div>
                            </div>
                           <div class="card-action">
                           		<input type="hidden" id="group_id" value="1" >
                                <button id="route-button" class="btn green darken-4" type="submit"><i class="fa fa-save"></i> <?=$LANG['Add Rule']?> </button>
                                <span id="route-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
                           </div>
                        </form>
                     </div>
                   </div>  
				</div>                
            </div>
      </div>        
      </div>  
      
	</div>
</div>

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

<?php
 if(isset($_GET['gwid'])) {
	$gwid = mysqli_real_escape_string($server,$_GET['gwid']);
	$result = mysqli_query($server,"SELECT * FROM `sms_gateways` WHERE id = '$gwid'") or die(mysqli_error($server));
	$rows = mysqli_fetch_assoc($result);
	if($rows['id']<1) {
		header('locatioon: smsetting');	
	} ?>
<!--add sms gateway -->
<div id="add_sms_pop" style="display: block; overflow-x: hidden; overflow-y: auto; 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><?=$rows['title']?> <span class="right"><a href="smsetting" class="red-text" ><i class="fa fa-times"></i></a></span></h5>
			<?php if(!empty(smsGatewayTemplateData($rows['alias'],'setup_text'))) { ?>
            	<div class="row no-m-t no-m-b show-text" style="display: none;">
                    <div class="col s12 m12 l12">
                        <div class="card black">
                           <div class="card-content white-text"><?=smsGatewayTemplateData($rows['alias'],'setup_text')?></div>
                        </div>
                    </div>
                 </div>
            <?php } ?>
             <div class="card ">
                 <div class="card-content">
                 	<form action="" method="post" id="add_sms_form" onSubmit="return false;">
	                    <div class="row no-m-t no-m-b">
                              <div class="input-field col s12">
                                   <select id="alias" required class="js-states browser-default" style="width: 100%">
                                        <?php $query="SELECT * FROM api_templates WHERE alias = '".$rows['alias']."'";
                                        $result = mysqli_query($server,$query) or die(mysqli_error($server));
                                        while ($rows2 = mysqli_fetch_assoc($result)) { 
                                            echo '<option selected value="'.$rows2['id'].'">'.$rows2['title'].'</option>';
                                        }
                                       ?> 
                                    </select>
                                    <label class="active"><?=$LANG['Gateway Template']?></label>
                                </div>
                            </div>
                         <?php if($rows['type']=='2way') { ?>	
                          <div class="row no-m-t no-m-b show-2way">
                                 <div class="input-field col s12">
                                       <input type="text" readonly value="<?=home_base_url().'incomingCallback'?>" >
                                       <label ><?=$LANG['Incoming SMS Callback URL']?></label>
                                 </div>
                           </div>
                         <?php } ?>  
                           
                            <div class="row no-m-t no-m-b show-custom hide-2way" >
                            	<div class="input-field col s12">
                                   <select id="cusType" class="validate">
                                    <?php if($rows['alias']=="custom") { ?>  
                                        <option <?php if($rows['type']=='sms'){echo 'selected';} ?> value="sms"><?=$LANG['Text Message']?></option>
                                        <?php if(getSetting('unicodeEnabled',0)>0){?>
                                        <option <?php if($rows['type']=='unicode'){echo 'selected';} ?> value="unicode" ><?=$LANG['Unicode Message']?></option>
                                        <?php }?>
                                        <?php if(getSetting('voiceEnabled',0)>0){?>
                                        <option <?php if($rows['type']=='voice'){echo 'selected';} ?> value="voice" ><?=$LANG['Voice Message']?></option>
                                        <?php } ?>
                                        <?php if(getSetting('watsappEnabled',0)>0){?>
                                        <option <?php if($rows['type']=='whatsapp'){echo 'selected';} ?> value="whatsapp" ><?=$LANG['WhatsApp Message']?></option>
                                        <?php } ?>
                                        <?php if(getSetting('mmsEnabled',0)>0){?>
                                        <option <?php if($rows['type']=='mms'){echo 'selected';} ?> value="mms" ><?=$LANG['Multimedia Message']?></option>
                                        <?php } ?>
                                        <?php if(getSetting('flashEnabled',0)>0){?>
                                        <option <?php if($rows['type']=='flash'){echo 'selected';} ?> value="flash" ><?=$LANG['Flash Message']?></option>
                                        <?php } ?>
                                      <?php } else {?>  
                                      	<option value="<?=$rows['type']?>"><?=strtoupper($rows['type'])?></option>
                                      <?php } ?>
                                   </select>
                                   <label><?=$LANG['Type']?></label>
                             </div>
                            </div>
                            
                        	<div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="sgtitle" type="text" class="validate" required value="<?=$rows['title']?>">
                                       <label for="sgtitle"><?=$LANG['Display Name']?></label>
                                 </div>
                           </div>
                          
                           <?php if($rows['alias']=="custom") { ?>  	 
                        	<div class="row no-m-t no-m-b show-custom hide-2way">
                              <div class="input-field col s12" >
                                  <select id="request_type" required class="validate">
                                       <option <?php if($rows['request_type']=='GET'){echo 'selected';} ?> value="GET"><?=$LANG['GET']?></option>
                                       <option <?php if($rows['request_type']=='POST'){echo 'selected';} ?> value="POST"><?=$LANG['POST']?></option>
                                  </select>
                                  <label><?=$LANG['HTTP Request Method']?></label>
                              </div>
							</div>
                            
                          <div class="row no-m-t no-m-b show-post hide-2way">                          
                              <div class="input-field col s12" >
                                  <select id="json_encode" required class="validate">
                                       <option <?php if($rows['json_encode']=='0'){echo 'selected';} ?> value="0"><?=$LANG['No']?></option>
                                       <option <?php if($rows['json_encode']=='1'){echo 'selected';} ?> value="1"><?=$LANG['Yes']?></option>
                                  </select>
                                  <label><?=$LANG['Enable JSON Encoded post']?></label>
                              </div>
                          </div>
                          <?php } ?>
                            
                          <div class="row no-m-t no-m-b show-custom  hide-2way">
                                 <div class="input-field col s12">
                                       <input id="base_url" type="text" class="validate" required value="<?=$rows['base_url']?>">
                                       <label for="base_url"><?=$LANG['Base URL']?></label>
                                 </div>
                          </div>
                          
                          
						 <?php if($rows['alias']=="custom") { ?>  	
                          <div class="row no-m-t no-m-b show-custom  hide-2way">
                                 <div class="input-field col s12">
                                       <input id="success_word" type="text" class="validate" required value="<?=$rows['success_word']?>" >
                                       <label for="success_word"><?=$LANG['Success Word']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b show-custom  hide-2way">
                                 <div class="input-field col s12">
                                       <select id="success_logic" required class="validate">
                                            <option <?php if($rows['success_logic']=='contain'){echo 'selected';} ?> value="contain"><?=$LANG['Contains the success word']?></option>
                                            <option <?php if($rows['success_logic']=='notcontain'){echo 'selected';} ?> value="notcontain"><?=$LANG['Does not contain the success word']?></option>    
                                       </select>
                                       <label><?=$LANG['Success Logic']?></label>
                                  </div>
                         </div>
                       <?php } ?>  
                         
                       <?php if(($rows['alias']=="custom") || smsGatewayTemplateData($rows['alias'],'authentication')>0) { ?>  
                         <div class="input-field col s12 show-custom hide-2way" >
                              <select id="authentication" required class="validate">
                                   <option <?php if($rows['authentication']=='0'){echo 'selected';} ?> value="0"><?=$LANG['Disabled']?></option>
                                   <option <?php if($rows['authentication']=='1'){echo 'selected';} ?> value="1"><?=$LANG['Enabled']?></option>
                              </select>
                              <label><?=$LANG['HTTP Basic authentication']?></label>
                         </div>
                       <?php } ?>
                       <?php if(($rows['alias']=="custom") || smsGatewayTemplateData($rows['alias'],'authentication')>0) { ?>
                         <div class="row no-m-t no-m-b show-auth hide-2way" >
                                 <div class="input-field col s12">
                                       <input id="username" type="text" class="validate" required value="<?=$rows['username']?>">
                                       <label for="username"><?=$LANG['Username/Key']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b show-auth hide-2way" >
                                 <div class="input-field col s12">
                                       <input id="password" type="text" class="validate" required value="<?=$rows['password']?>">
                                       <label for="password"><?=$LANG['Password/Token']?></label>
                                 </div>
                           </div>
                          
                          <?php if(($rows['alias']=="custom")) { ?> 
                           <div class="row no-m-t no-m-b show-auth hide-2way">                          
                              <div class="input-field col s12" >
                                  <select id="base64_encode" required class="validate">
                                       <option <?php if($rows['base64_encode']=='1'){echo 'selected';} ?> value="1"><?=$LANG['Yes']?></option>
                                       <option <?php if($rows['base64_encode']=='0'){echo 'selected';} ?> value="0"><?=$LANG['No']?></option>
                                  </select>
                                  <label><?=$LANG['Use Base64 Encoding']?></label>
                              </div>
                          </div>                          
                          <?php } ?>
                          <?php } ?>
                          
                         <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="sender_field" type="text" class="validate" value="<?=$rows['sender_field']?>" >
                                       <label for="sender_field" ><?=$LANG['Sender Parameter Name']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="recipient_field" type="text" class="validate" value="<?=$rows['recipient_field']?>" >
                                       <label for="recipient_field" ><?=$LANG['To Parameter Name']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="message_field" type="text" class="validate" value="<?=$rows['message_field']?>" >
                                       <label for="message_field" ><?=$LANG['Message Parameter Name']?></label>
                                 </div>
                           </div>
                          <?php if(($rows['alias']=="custom")) { ?>
                            <div class="row no-m-t no-m-b show-voice show-mms">
                                 <div class="input-field col s12">
                                       <input id="audio_field" type="text" class="validate" value="<?=$rows['id']?>" >
                                       <label for="audio_field" ><?=$LANG['Audio File Parameter Name']?></label>
                                 </div>
                           </div>
                           <?php } ?>
                          <?php if(($rows['alias']=="custom")) { ?>
                            <div class="row no-m-t no-m-b show-voice" >
                                 <div class="input-field col s12">
                                       <input id="timeout_field" type="text" class="validate" value="<?=$rows['timeout_field']?>" >
                                       <label for="timeout_field" ><?=$LANG['Call Duration Parameter Name']?></label>
                                 </div>
                           </div>
                          <?php } ?>
                           
                          <?php if(($rows['alias']=="custom") || !empty(smsGatewayTemplateData($rows['alias'],'param1_field'))) { ?>
                          <?php if(($rows['alias']=="custom")) { ?>
                           <div class="row no-m-t no-m-b show-custom">
                                 <div class="input-field col s12">
                                       <input id="param1_field" type="text" class="validate" value="<?=$rows['param1_field']?>" >
                                       <label for="param1_field"><?=$LANG['Parameter 1 Name']?></label>
                                 </div>
                           </div>
                           <?php } ?>
                           <div class="row no-m-t no-m-b show_p1" >
                                 <div class="input-field col s12">
                                       <input id="param1_value" type="text" class="validate" value="<?=$rows['param1_value']?>" >
                                       <label for="param1_value" id="label_p1"><?=smsGatewayTemplateData($rows['alias'],'param1_label')?></label>
                                 </div>
                           </div>
                         <?php } ?>
                         
                        <?php if(($rows['alias']=="custom") || !empty(smsGatewayTemplateData($rows['alias'],'param2_field'))) { ?>
                        <?php if(($rows['alias']=="custom")) { ?>
                           <div class="row no-m-t no-m-b show-custom" >
                                 <div class="input-field col s12">
                                       <input id="param2_field" type="text" class="validate" value="<?=$rows['param2_field']?>" >
                                       <label for="param2_field"><?=$LANG['Parameter 2 Name']?></label>
                                 </div>
                            </div>
                         <?php } ?>   
                           <div class="row no-m-t no-m-b show_p2"  >
                                 <div class="input-field col s12">
                                       <input id="param2_value" type="text" class="validate" value="<?=$rows['param2_value']?>" >
                                       <label for="param2_value" id="label_p2"><?=smsGatewayTemplateData($rows['alias'],'param2_label')?></label>
                                 </div>
                            </div>
                        <?php } ?> 
                            
                         <?php if(($rows['alias']=="custom") || !empty(smsGatewayTemplateData($rows['alias'],'param3_field'))) { ?>
                         <?php if(($rows['alias']=="custom")) { ?>
                            <div class="row no-m-t no-m-b show-custom" >
                                 <div class="input-field col s12">
                                       <input id="param3_field" type="text" class="validate" value="<?=$rows['param3_field']?>" >
                                       <label for="param3_field" ><?=$LANG['Parameter 3 Name']?></label>
                                 </div>
                            </div> 
                         <?php } ?>   
                            <div class="row no-m-t no-m-b show_p3" >
                                 <div class="input-field col s12">
                                       <input id="param3_value" type="text" class="validate" value="<?=$rows['param3_value']?>" >
                                       <label for="param3_value" id="label_p3"><?=smsGatewayTemplateData($rows['alias'],'param3_label')?></label>
                                 </div>
                            </div> 
                        <?php } ?>
                           
                         <?php if(($rows['alias']=="custom") || !empty(smsGatewayTemplateData($rows['alias'],'param4_field'))) { ?>
                         <?php if(($rows['alias']=="custom")) { ?>
                            <div class="row no-m-t no-m-b show-custom"  >
                                 <div class="input-field col s12">
                                       <input id="param4_field" type="text" class="validate" value="<?=$rows['param4_field']?>" >
                                       <label for="param4_field" ><?=$LANG['Parameter 4 Name']?></label>
                                 </div>
                            </div>
                         <?php } ?>   
                            <div class="row no-m-t no-m-b show_p4"  >
                                 <div class="input-field col s12">
                                       <input id="param4_value" type="text" class="validate" value="<?=$rows['param4_value']?>" >
                                       <label for="param4_value" id="label_p4"><?=smsGatewayTemplateData($rows['alias'],'param4_label')?></label>
                                 </div>
                            </div>   
                          <?php } ?>                 
                                     
                           <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="cutting_limit" type="text" class="validate" placeholder="<?=$LANG['Leave empty or set to Zero to disable']?>" value="<?=$rows['cutting_limit']?>" >
                                       <label for="cutting_limit"><?=$LANG['Cutting Limit']?> </label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="cutting_percent" type="text" class="validate" value="<?=$rows['cutting_percent']?>">
                                       <label for="cutting_percent"><?=$LANG['Percentage to Send']?> (%)</label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="hour_limit" type="text" class="validate" value="<?=$rows['hour_limit']?>">
                                       <label for="hour_limit"><?=$LANG['Max. Traffic per Hour']?> </label>
                                 </div>
                            </div>
                            
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="balance_url" type="url" class="validate" value="<?=$rows['balance_url']?>">
                                       <label for="balance_url"><?=$LANG['Balance Enquiry URL']?> </label>
                                 </div>
                            </div>
                            
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <select id="group_id2" required class="validate">
                                            <option <?php if($rows['group_id']=='1'){echo 'selected';} ?> value="1"><?=$LANG['No']?></option>
                                            <option <?php if($rows['group_id']=='2'){echo 'selected';} ?> value="2"><?=$LANG['Yes']?></option>    
                                       </select>
                                       <label><?=$LANG['Make available for selection on SMS Composer?']?></label>
                                  </div>
                         	</div>
                         	<div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="d_rate" type="text" class="validate" value="<?=$rows['d_rate']?>">
                                       <label for="d_rate"><?=$LANG['Percentage SMS Rate Adjustment (+/-)']?> </label>
                                 </div>
                            </div>

                           <div class="card-action">
                           		<input type="hidden" id="sgateway_id" value="<?=$rows['id']?>">
                                <input type="hidden" id="sgtype" value="<?=$rows['type']?>">
                                <button id="sgate-button" class="btn green darken-4" type="submit"><i class="fa fa-save"></i> <?=$LANG['Save Gateway']?> </button>
                                <span id="sgate-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="smsetting"><i class="fa fa-times"></i> <?=$LANG['Cancel']?></a>
                           </div>
                        </form>
                 </div>
              </div>
        </div>      
        <div class="col s1 m2 l3"> </div>
     </div>
</div>   	
<?php }	 else {
?>

 <!--add sms gateway -->
<div id="add_sms_pop" style="display: none; overflow-x: hidden; overflow-y: auto; 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 SMS Gateway']?> <span class="right"><a href="#" class="red-text" onclick="$('#add_sms_form')[0].reset();$('#add_sms_pop').hide('slow');"><i class="fa fa-times"></i></a></span></h5>
            	<div class="row no-m-t no-m-b show-text" style="display: none;">
                    <div class="col s12 m12 l12">
                        <div class="card black">
                            <div class="card-content white-text" id="setup_text"></div>
                        </div>
                    </div>
                 </div>
                 
             <div class="card ">
                 <div class="card-content">
                 	<form action="" method="post" id="add_sms_form" onSubmit="return false;">
	                    <div class="row no-m-t no-m-b">
                              <div class="input-field col s12">
                                   <select id="alias" required class="js-states browser-default" style="width: 100%">
                                        <option value="" disabled selected><?=$LANG['Select a gateway template']?></option>
                                        <?php $query="SELECT * FROM api_templates WHERE status = 1";
                                        $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 class="active"><?=$LANG['Gateway Template']?></label>
                                </div>
                            </div>
                         	
                            <div class="row no-m-t no-m-b show-2way" style="display: none;">
                                 <div class="input-field col s12">
                                       <input type="text" readonly value="<?=home_base_url().'incomingCallback'?>" >
                                       <label ><?=$LANG['Incoming SMS Callback URL']?></label>
                                 </div>
                           </div>
                           
                            <div class="row no-m-t no-m-b show-custom hide-2way"  style="display: none;" >
                            	<div class="input-field col s12">
                                   <select id="cusType" class="validate">
                                        <option value="sms"><?=$LANG['Text Message']?></option>
                                        <?php if(getSetting('unicodeEnabled',0)>0){?>
                                        <option value="unicode" ><?=$LANG['Unicode Message']?></option>
                                        <?php }?>
                                        <?php if(getSetting('voiceEnabled',0)>0){?>
                                        <option value="voice" ><?=$LANG['Voice Message']?></option>
                                        <?php } ?>
                                        <?php if(getSetting('watsappEnabled',0)>0){?>
                                        <option value="whatsapp" ><?=$LANG['WhatsApp Message']?></option>
                                        <?php } ?>
                                        <?php if(getSetting('mmsEnabled',0)>0){?>
                                        <option value="mms" ><?=$LANG['Multimedia Message']?></option>
                                        <?php } ?>
                                        <?php if(getSetting('flashEnabled',0)>0){?>
                                        <option value="flash" ><?=$LANG['Flash Message']?></option>
                                        <?php } ?>
                                   </select>
                                   <label><?=$LANG['Type']?></label>
                             </div>
                            </div>
                            
                        	<div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="sgtitle" type="text" class="validate" required  >
                                       <label for="sgtitle"><?=$LANG['Display Name']?></label>
                                 </div>
                           </div>
                           
                        	<div class="row no-m-t no-m-b show-custom hide-2way no_change" style="display: none;">
                              <div class="input-field col s12" >
                                  <select id="request_type" required class="validate">
                                       <option value="GET"><?=$LANG['GET']?></option>
                                       <option value="POST"><?=$LANG['POST']?></option>
                                  </select>
                                  <label><?=$LANG['HTTP Request Method']?></label>
                              </div>
							</div>
                          <div class="row no-m-t no-m-b show-post hide-2way no_change">                          
                              <div class="input-field col s12" >
                                  <select id="json_encode" required class="validate">
                                       <option value="0"><?=$LANG['No']?></option>
                                       <option value="1"><?=$LANG['Yes']?></option>
                                  </select>
                                  <label><?=$LANG['Enable JSON Encoded post']?></label>
                              </div>
                          </div>
                            
                          <div class="row no-m-t no-m-b show-custom hide-2way">
                                 <div class="input-field col s12">
                                       <input id="base_url" type="text" class="validate" required >
                                       <label for="base_url"><?=$LANG['Base URL']?></label>
                                 </div>
                          </div>

                          <div class="row no-m-t no-m-b show-custom no_change hide-2way">
                                 <div class="input-field col s12">
                                       <input id="success_word" type="text" class="validate" required >
                                       <label for="success_word"><?=$LANG['Success Word']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b show-custom no_change hide-2way">
                                 <div class="input-field col s12">
                                       <select id="success_logic" required class="validate">
                                            <option value="contain"><?=$LANG['Contains the success word']?></option>
                                            <option value="notcontain"><?=$LANG['Does not contain the success word']?></option>                                       </select>
                                       <label><?=$LANG['Success Logic']?></label>
                                  </div>
                         </div>
                         <div class="input-field col s12 show-custom no_change hide-2way" >
                              <select id="authentication" required class="validate">
                                   <option value="0"><?=$LANG['Disabled']?></option>
                                   <option value="1"><?=$LANG['Enabled']?></option>
                              </select>
                              <label><?=$LANG['HTTP Basic authentication']?></label>
                         </div>
                         <div class="row no-m-t no-m-b show-auth hide-2way"  style="display: none;">
                                 <div class="input-field col s12">
                                       <input id="username" type="text" class="validate" required >
                                       <label for="username"><?=$LANG['Username/Key']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b show-auth hide-2way" style="display: none;">
                                 <div class="input-field col s12">
                                       <input id="password" type="text" class="validate" required >
                                       <label for="password"><?=$LANG['Password/Token']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b show-auth no_change hide-2way">                          
                              <div class="input-field col s12" >
                                  <select id="base64_encode" required class="validate">
                                       <option value="1"><?=$LANG['Yes']?></option>
                                       <option value="0"><?=$LANG['No']?></option>
                                  </select>
                                  <label><?=$LANG['Use Base64 Encoding']?></label>
                              </div>
                          </div>                          
                         <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="sender_field" type="text" class="validate" value="" >
                                       <label for="sender_field" ><?=$LANG['Sender Parameter Name']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="recipient_field" type="text" class="validate" value="" >
                                       <label for="recipient_field" ><?=$LANG['To Parameter Name']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b">
                                 <div class="input-field col s12">
                                       <input id="message_field" type="text" class="validate" value="" >
                                       <label for="message_field" ><?=$LANG['Message Parameter Name']?></label>
                                 </div>
                           </div>
                           
                            <div class="row no-m-t no-m-b show-voice show-mms" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="audio_field" type="text" class="validate" value="" >
                                       <label for="audio_field" ><?=$LANG['Media File Parameter Name']?></label>
                                 </div>
                           </div>
                            <div class="row no-m-t no-m-b show-voice" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="timeout_field" type="text" class="validate" value="" >
                                       <label for="timeout_field" ><?=$LANG['Call Duration Parameter Name']?></label>
                                 </div>
                           </div>
                           
                           <div class="row no-m-t no-m-b show-custom" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param1_field" type="text" class="validate" value="" >
                                       <label for="param1_field"><?=$LANG['Parameter 1 Name']?></label>
                                 </div>
                           </div>
                           <div class="row no-m-t no-m-b show_p1" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param1_value" type="text" class="validate" value="" >
                                       <label for="param1_value" id="label_p1"><?=$LANG['Parameter 1 Value']?></label>
                                 </div>
                           </div>
                           
                           <div class="row no-m-t no-m-b show-custom" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param2_field" type="text" class="validate" value="" >
                                       <label for="param2_field"><?=$LANG['Parameter 2 Name']?></label>
                                 </div>
                            </div>
                           <div class="row no-m-t no-m-b show_p2" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param2_value" type="text" class="validate" value="" >
                                       <label for="param2_value" id="label_p2"><?=$LANG['Parameter 2 Value']?></label>
                                 </div>
                            </div>
                            
                            <div class="row no-m-t no-m-b show-custom" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param3_field" type="text" class="validate" value="" >
                                       <label for="param3_field" ><?=$LANG['Parameter 3 Name']?></label>
                                 </div>
                            </div> 
                            <div class="row no-m-t no-m-b show_p3" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param3_value" type="text" class="validate" value="" >
                                       <label for="param3_value" id="label_p3"><?=$LANG['Parameter 3 Value']?></label>
                                 </div>
                            </div> 
                            
                            <div class="row no-m-t no-m-b show-custom" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param4_field" type="text" class="validate" value="" >
                                       <label for="param4_field" ><?=$LANG['Parameter 4 Name']?></label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b show_p4" style="display:none;">
                                 <div class="input-field col s12">
                                       <input id="param4_value" type="text" class="validate" value="" >
                                       <label for="param4_value" id="label_p4"><?=$LANG['Parameter 4 Value']?></label>
                                 </div>
                            </div>                  
                                     
                           <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="cutting_limit" type="text" class="validate" placeholder="<?=$LANG['Leave empty or set to Zero to disable']?>" >
                                       <label for="cutting_limit"><?=$LANG['Cutting Limit']?> </label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="cutting_percent" type="text" class="validate" value="0">
                                       <label for="cutting_percent"><?=$LANG['Percentage to Send']?> (%)</label>
                                 </div>
                            </div>
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="hour_limit" type="text" class="validate" value="0">
                                       <label for="hour_limit"><?=$LANG['Max. Traffic per Hour']?> </label>
                                 </div>
                            </div>
                            
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="balance_url" type="url" class="validate" value="">
                                       <label for="balance_url"><?=$LANG['Balance Enquiry URL']?> </label>
                                 </div>
                            </div>
                            
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <select id="group_id2" required class="validate">
                                            <option value="1"><?=$LANG['No']?></option>
                                            <option value="2"><?=$LANG['Yes']?></option>    
                                       </select>
                                       <label><?=$LANG['Make available for selection on SMS Composer?']?></label>
                                  </div>
                            </div>
                            
                            <div class="row no-m-t no-m-b hide-2way">
                                 <div class="input-field col s12">
                                       <input id="d_rate" type="text" class="validate" value="0.00">
                                       <label for="d_rate"><?=$LANG['Percentage SMS Rate Adjustment (+/-)']?> </label>
                                 </div>
                            </div>

                           <div class="card-action">
                           		<input type="hidden" id="sgateway_id" value="0">
                                <input type="hidden" id="sgtype" value="sms">
                                <button id="sgate-button" class="btn green darken-4" type="submit"><i class="fa fa-save"></i> <?=$LANG['Save Gateway']?> </button>
                                <span id="sgate-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_sms_form')[0].reset();$('#add_sms_pop').hide('slow');"><i class="fa fa-times"></i> <?=$LANG['Cancel']?></a>
                           </div>
                        </form>
                 </div>
              </div>
        </div>      
        <div class="col s1 m2 l3"> </div>
     </div>
</div>   
<?php } ?>