<?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 =============
$page = 1; $setLimit = 30;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $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);
//====End ==============
define('TITLE','Senders');
if(userRole()>2) {
	header('location: index.php');	
}
hasModulePermission('view senderID');
$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['view'])&& $_REQUEST['view']>0) {
} else {
if(isset($_POST['bulk'])) { 
	$userID = getUser();
  if($_POST['action']=='delete') {
	  hasModulePermission('delete senderID');
	foreach($_POST['bulk'] as $inbid) { 
		$inbid = mysqli_real_escape_string($server,$inbid);
		mysqli_query($server,"DELETE FROM `sender_id` WHERE `id` = '$inbid'") or die(mysqli_error($server));
		logEvent($userID,'Removed sender ID '.$inbid.' from his account');
	}	
	$_SESSION['message'] = $LANG['The selected sender ID(s) have been removed'];
	header('location: asenderid?err=0');
  } elseif($_POST['action']=='reject') {
	  hasModulePermission('approve senderID');
	foreach($_POST['bulk'] as $inbid) { 
		$inbid = mysqli_real_escape_string($server,$inbid);
		mysqli_query($server,"UPDATE `sender_id` SET status = 'rejected' WHERE `id` = '$inbid'") or die(mysqli_error($server));
		logEvent($userID,'Rejected sender ID '.$inbid.'');
	}	
	$_SESSION['message'] = $LANG['The selected sender ID(s) have been rejected'];
	header('location: asenderid?err=0');
  } elseif($_POST['action']=='approve') {
	  hasModulePermission('approve senderID');
	foreach($_POST['bulk'] as $inbid) { 
		$inbid = mysqli_real_escape_string($server,$inbid);
		mysqli_query($server,"UPDATE `sender_id` SET status = 'approved' WHERE `id` = '$inbid'") or die(mysqli_error($server));
		logEvent($userID,'Rejected sender ID '.$inbid.'');
	}	
	$_SESSION['message'] = $LANG['The selected sender ID(s) have been approved'];
	header('location: asenderid?err=0');
  } else {
	  header('location: asenderid');
  }
}

if(isset($_GET['approve'])) {
	hasModulePermission('approve senderID');
	$inbid = mysqli_real_escape_string($server,$_GET['approve']);
	mysqli_query($server,"UPDATE `sender_id` SET status = 'approved' WHERE `id` = '$inbid'") or die(mysqli_error($server));
	logEvent($userID,'Rejected sender ID '.$inbid.'');
	$_SESSION['message'] = $LANG['The selected sender ID(s) have been approved'];
	header('location: asenderid?err=0');
}

if(isset($_GET['reject'])) {
	hasModulePermission('approve senderID');
	$inbid = mysqli_real_escape_string($server,$_GET['reject']);
	mysqli_query($server,"UPDATE `sender_id` SET status = 'rejected' WHERE `id` = '$inbid'") or die(mysqli_error($server));
	logEvent($userID,'Rejected sender ID '.$inbid.'');
	$_SESSION['message'] = $LANG['The selected sender ID(s) have been rejected'];
	header('location: asenderid?err=0');
}
	//show list
	$added = '';	
	if(isset($_GET['sc'])) {
		$keywords = mysqli_real_escape_string($server,$_GET['sc']);
		$ser = explode(' ', $keywords);
		$clauses = array();
		foreach($ser as $look){
			$term = trim( $look);   
			if (!empty($term)){
				 $clauses[] = "sender_id LIKE '%$term%' OR type = '$term'  OR customer_id = '$term' ";
			} else {
				 $clauses[] = " id LIKE '%%' ";
			}
		}
		$added .= ' AND ('.implode(' OR ', $clauses).') ';		
	}
	if(isset($_GET['s'])) {
		if($_GET['s']=="p") {
			$added .= " AND status = 'pending' order by id DESC ";	
		}
		if($_GET['s']=="a") {
			$added .= " AND status = 'approved' order by id DESC ";	
		}
		if($_GET['s']=="r") {
			$added .= " AND status = 'rejected' order by id DESC ";	
		}
	} else {
		$added .= " order by id DESC ";	
	}

    $query="SELECT * FROM sender_id WHERE 1 $added limit $pageLimit,$setLimit";
	$result = mysqli_query($server,$query) or die(mysqli_error($server));
	$x = mysqli_num_rows($result);
?>
<!-- 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['asenderid_tip']?></div>
        </div>
    </div>
 </div>

<div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card card-transparent">
            <div class="card-content">
            	<div class="col s12 m4 l4">
                <?php if(userData($userID,'reseller') < 1) { ?>
                    <a class="btn waves-effect waves-light light-green darken-4" href="#" onclick="addPB();"><?=$LANG['Add Sender ID']?></a>
                <?php } ?>    
                </div>
                <div class="col s12 m8 l8">
                  <div class="col s12 m12 l12">
                    <div class="row no-m-t no-m-b">
                     <form action="asenderid" method="get">
                     <div class="input-field col s12">
                        <i class="material-icons prefix">search</i>
                        <input type="text" name="sc" value="<?=@$_GET['sc']?>" placeholder="<?=$LANG['Search...']?>">
                     </div>
                     </form>  
                    </div>
                  </div> 
                </div>
            </div>
        </div>
    </div>
 </div>

<div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
         <div class="card card-white">   
            <div class="card-content">
           	 	<div class="card-title" style="float: right;">
                    <a class='dropdown-button btn btn-flat' href='#' data-activates='filter' data-beloworigin='true' data-constrainwidth='false'><?=$LANG['Filter']?></a></li>
                    <ul id='filter' class='dropdown-content'>
                        <li><a href="asenderid" ><?=$LANG['All']?></a></li>
                        <li><a href="asenderid?s=p" ><?=$LANG['Pending']?></a></li>
                        <li><a href="asenderid?s=a" ><?=$LANG['Approved']?></a></li>
                        <li><a href="asenderid?s=r" ><?=$LANG['Rejected']?></a></li>
                    </ul>
                </div>
            <div class="card-options">   
                <ul id='bulka' class='dropdown-content'>
                        <li><a href="#" onclick="bulkaction('delete','inbox-form','<?=$LANG['removesid_confirm']?>');"> <i class="fa fa-trash"></i> <?=$LANG['Remove']?></a></li>
                        <li><a href="#" onclick="bulkaction('reject','inbox-form','<?=$LANG['reject_confirm']?>');"> <i class="fa fa-times"></i> <?=$LANG['Reject']?></a></li>
                        <li><a href="#" onclick="bulkaction('approve','inbox-form','<?=$LANG['approve_confirm']?>');"> <i class="fa fa-check"></i> <?=$LANG['Approve']?></a></li>
                </ul>                
            </div>
            <span class="card-title"><a class='dropdown-button btn light-blue darken-4' href='#' data-activates='bulka' data-beloworigin='true' data-constrainwidth='false'><?=$LANG['With Selected']?></a></span>
       		<table class="highlight responsive-table">
            <?php if($x >0) { ?>
                <thead>
                	<tr>
                    	<th><input type="checkbox" class="filled-in" id="selectall" /><label for="selectall"></label></th>
                        <th data-field="number"><?=$LANG['Sender ID']?></th>
                        <th data-field="type"><?=$LANG['Type']?></th>
                        <th data-field="cost"><?=$LANG['Owner']?></th>
                        <th data-field="cost"><?=$LANG['Status']?></th>
                        <th data-field="act"><?=$LANG['Options']?></th>
                    </tr>
                 </thead>
            <?php } ?>     
                 <tbody>
                    <form action="asenderid" method="post" id="inbox-form">
                    	<input type="hidden" id="bulkaction" name="action" value=""/>
<?php $xf=0; while ($rows = mysqli_fetch_assoc($result)) { $xf+=1; $stats = ''; 
$bill = 'One-time';
$next = '-';
$disabled = '';
$type = $LANG['Dedicated'];
$customer = '<a href="acustomer?v='.$rows['customer_id'].'">'.userData($rows['customer_id'],'first_name').' '.userData($rows['customer_id'],'last_name').'</a>';
if($rows['customer_id']==0) $customer = 'Administrator';
?>
						<tr class="">
                             <td><input type="checkbox" <?=$disabled?> class="filled-in bulk" id="bulk<?=$rows['id']?>" value="<?=$rows['id']?>" name="bulk[<?=$rows['id']?>]"/> <label for="bulk<?=$rows['id']?>"></label></td>
                             <td><?=$rows['sender_id']?></td>
                             <td><?=strtoupper($rows['type'])?></td>
                             <td><?=$customer?></td>
                             <td><?=strtoupper($rows['status'])?></td>
                             <td>
							 <?php if(($rows['status']!='approved')) { ?><a class="hoverable green-text" href="asenderid?approve=<?=$rows['id']?>"><?=$LANG['Approve']?></a> &nbsp;&nbsp;<?php } ?>
                             <?php if(($rows['status']!='rejected')) { ?><a class="hoverable red-text" href="asenderid?reject=<?=$rows['id']?>"><?=$LANG['Reject']?></a><?php } ?> 
							 </td>
                        </tr>
<?php } ?>
<?php if($xf<1) echo '<tr class="orange lighten-5"><td colspan=10><h5 class="center-align">'.$LANG['No records found!'].'</h5></td></tr>' ?>	
					</form>
                 </tbody>
            </table>  
          </div>
        </div>          
     </div>
  </div>

 
 <div class="row">
 	<div class="col s12 m12 l12" style="margin-top: 5px;">
        <?php displayPagination($setLimit,$page,$query);  ?>
    </div>
 </div>     
 
 <div id="add_new" 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">  <?php hasModulePermission('add senderID');?>
             <h5><?=$LANG['Add Sender ID']?> <span class="right"><a href="#" class="red-text" onclick="$('#add_phonebook')[0].reset();$('#add_new').hide('slow');"><i class="fa fa-times"></i></a></span></h5>
          
             <div class="card ">
             <form action="asenderid" method="post" id="add_phonebook"  onSubmit="return false;">
                <div class="card-content">
						<div class="row no-m-t no-m-b">
                         <div class="input-field col s12">
                               <select id="type" required 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['Sender ID for']?></label>
                         </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" disabled selected><?=$LANG['Choose a Customer']?></option>
                                <?php $query="SELECT * FROM users WHERE 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'].'</option>';
                                }
                               ?> 
                            </select>
                            <label class="active"><?=$LANG['Assign To']?></label>
                    	 </div>
                        </div> 
                       <div class="row no-m-t no-m-b">
                    	 <div class="input-field col s12">
                           <input type="text" id="sender" class="validate" value="">
                           <label for="sender"><?=$LANG['Sender ID']?></label>
                    	 </div>
                        </div>
                </div>  
                      
                <div class="card-action">
                            <button id="cont-button" class="btn ight-blue darken-4" type="submit"><?=$LANG['Save']?> </button>
                            <span id="cont-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_phonebook')[0].reset();$('#add_new').hide('slow');"><?=$LANG['Cancel']?></a>
                </div>
             </form>
             </div>
        </div>
       <div class="col s1 m2 l3"> </div>
	 </div>
 </div>                             
 

<?php } ?> 
