<?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(!isAdmin(getUser()) && (userData(getUser(),'phone') == '' || userData(getUser(),'first_name') == '') ) {
	header('location: profile?setting'); exit();
}
if(!isAdmin(getUser()) && userData($userID,'email_verified') != 0) {
	showVerify();
}
$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') {
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"DELETE FROM `sender_id` WHERE `id` = '$inbid' AND customer_id = '$userID'") or die(mysqli_error($server));
		logEvent($userID,'Removed sender ID '.$inbid.' from his account');
	}	
	$_SESSION['message'] = $LANG['The selected sender ID(s) has been removed from your account'];
	header('location: senderid?err=0');
  } elseif($_POST['action']=='unassign') {
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"UPDATE `sender_id` SET resold = 0 WHERE `id` = '$inbid' AND customer_id = '$userID'") or die(mysqli_error($server));
		logEvent($userID,'Unassigned sender ID '.$inbid.'');
	}	
	$_SESSION['message'] = $LANG['The selected sender ID(s) has been unassigned'];
	header('location: senderid?err=0');
  }  else {
	  header('location: senderid');
  }
}
	//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(preg_replace('/[^a-z0-9]/i', '', $look));   
			if (!empty($term)){
				 $clauses[] = "sender_id LIKE '%$term%' OR type = '%$term%'  OR id = '%$term%' ";
			} else {
				 $clauses[] = " id LIKE '%%' ";
			}
		}
		$added .= ' AND ('.implode(' OR ', $clauses).') ';		
	}
	if(isset($_GET['s'])) {
	} else {
		$added .= " order by id DESC ";	
	}

    $query="SELECT * FROM sender_id WHERE customer_id = '$userID' OR resold = '$userID' $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['senderid_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="senderid" 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-options">
            	<ul>
                </ul>    
                <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>
                        <?php if(isReseller($userID)) { ?>
                        <li><a href="#" onclick="bulkaction('unassign','inbox-form','<?=$LANG['unassignsid_confirm']?>');"> <i class="fa fa-times"></i> <?=$LANG['Unassign']?></a></li>
                        <?php } ?>
                </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['Status']?></th>
                        <?php if(isReseller($userID)) { ?><th data-field="act"><?=$LANG['Options']?></th><?php } ?>
                    </tr>
                 </thead>
                <?php } ?> 
                 <tbody>
                    <form action="senderid" 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'];
if($rows['status']=='pending') $disabled = 'disabled';;
?>
						<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><?=strtoupper($rows['status'])?></td>
                             <?php if(isReseller($userID)) { ?>
                             <td><?php if(($rows['status']=='approved') && ($rows['resold']<1)) { ?><a href="#" onclick="resCode('<?=$rows['id']?>');"><?=$LANG['Share']?></a><?php } ?> <?php if($rows['resold']>0) { ?><?=$LANG['Assigned to'].' '.userData($rows['resold'],'first_name').' '.userData($rows['resold'],'last_name')?><?php } ?></td>
                             <?php } ?>
                        </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"> 
             <h5><?=$LANG['Request 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 ">
                 <div class="card-content">
                    <form action="senderid" method="post" id="add_phonebook"  onSubmit="return false;">
						<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">
                           <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['Submit']?> </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>
       <div class="col s1 m2 l3"> </div>
	 </div>
 </div>                             
 
 <?php if(isReseller($userID)) { ?>
 <!-- Share code for reseller -->
 <div id="add_new2" 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['Assign Sender ID']?> <span class="right"><a href="#" class="red-text" onclick="$('#add_phonebook2')[0].reset();$('#add_new2').hide('slow');"><i class="fa fa-times"></i></a></span></h5>
                
                 <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['share_sender_tip']?></div>
                        </div>
                    </div>
                 </div>    
               <form action="senderid" method="post" id="add_phonebook2"  onSubmit="return false;">                 
               	<div class="card ">           
                 	<div class="card-content">
                    	<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 reseller = '$userID'";
                                $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['Choose a Customer']?></label>
                    	 </div>
                        </div> 
                      </div>   
                     <div class="card-action">
                      	<input id="senderid2" value="0" type="hidden" >
                        <button id="sha-button" class="btn ight-blue darken-4" type="submit"><?=$LANG['Assign']?> </button>
                        <span id="sha-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_phonebook2')[0].reset();$('#add_new2').hide('slow');"><?=$LANG['Cancel']?></a>
                     </div>
                 </div>
             </div>
            </form>
           	</div>
       		<div class="col s1 m2 l3"> </div>
	 </div>
 </div>                              
<?php } ?>
 
<?php } ?> 
