<?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','Blacklist');
if(userRole()>2) {
	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['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 `blacklist` WHERE `id` = '$inbid'") or die(mysqli_error($server));
		logEvent($userID,'Deleted contact ID '.$inbid.' from blacklist');
	}	
	$_SESSION['message'] = $LANG['The selected number(s) has been deleted from your blacklist'];
	header('location: ablacklist?err=0');
  }  else {
	  header('location: ablacklist');
  }
}
	//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[] = "phone LIKE '%$term%' OR date = '%$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 blacklist 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['blacklist_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">
                    <a class="btn waves-effect waves-light light-green darken-4" href="#" onclick="addPB();"><?=$LANG['Add Contact']?></a>
                </div>
                <div class="col s12 m8 l8">
                  <div class="col s12 m12 l12">
                    <div class="row no-m-t no-m-b">
                     <form action="ablacklist" 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['delete_confirm']?>');"> <i class="fa fa-trash"></i> <?=$LANG['Delete']?></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">
                <thead>
                	<tr>
                    	<th><input type="checkbox" class="filled-in" id="selectall" /><label for="selectall"></label></th>
                        <th data-field="id"><?=$LANG['ID']?></th>
                        <th data-field="date"><?=$LANG['Phone']?></th>
                        <th data-field="by"><?=$LANG['Blacklisted By']?></th>
                        <th data-field="message"><?=$LANG['Blacklisted On']?></th>
                    </tr>
                 </thead>
                 <tbody>
                    <form action="ablacklist" 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 = ''; 
$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" class="filled-in bulk" id="bulk<?=$rows['id']?>" value="<?=$rows['id']?>]" name="bulk[<?=$rows['id']?>]"/> <label for="bulk<?=$rows['id']?>"></label></td>
                             <td><?=$rows['id']?></td>
                             <td><?=$rows['phone']?></td>
                             <td><?=$customer?></td>
                             <td><?=date('j M, Y h:iA',strtotime($rows['date']))?></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"> 
             <h5><?=$LANG['Add Contact']?> <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="row no-m-t no-m-b">
                <div class="col s12 m12 l12">
                    <div class="card blue">
                        <div class="card-content white-text"><?=$LANG['add_blacklist_tip']?></div>
                    </div>
                </div>
             </div>
             
             <div class="card ">
                 <div class="card-content">
                    <form action="blacklist" method="post" id="add_phonebook"  onSubmit="return false;">
                        <div class="row no-m-t no-m-b">
                    	 <div class="input-field col s12">
                           <input id="phone" type="text" class="validate" required >
                           <label for="phone"><?=$LANG['Phone']?></label>
                    	 </div>
                        </div>
                      
                       <div class="card-action">
                            <button id="cont-button" class="btn ight-blue darken-4" type="submit"><i class="fa fa-save"></i> <?=$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');"><i class="fa fa-times"></i> <?=$LANG['Cancel']?></a>
                       </div>
                    </form>
                 </div>
             </div>
           </div>
       <div class="col s1 m2 l3"> </div>
	 </div>
 </div>                             
<?php } ?> 
