<?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 = 30;
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);
//====End ==============
define('TITLE','Phonebook');
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) {
	$did = mysqli_real_escape_string($server,$_REQUEST['view']);	
	if(isset($_POST['bulk'])) { 
	  if($_POST['action']=='delete') {
		foreach($_POST['bulk'] as $inbid) { 
			mysqli_query($server,"DELETE FROM `contacts` WHERE `id` = '$inbid'") or die(mysqli_error($server));
			logEvent($userID,'Deleted contact ID '.$inbid.' from contact lists');
		}	
		$_SESSION['message'] = $LANG['The selected contacts(s) has been deleted from your phonebook'];
		header('location: aphonebook?view='.$did.'&err=0');
	  } elseif($_POST['action']=='deleteshare') {
		foreach($_POST['bulk'] as $inbid) { 
			mysqli_query($server,"DELETE FROM `phonebook_owners` WHERE `id` = '$inbid'") or die(mysqli_error($server));
			logEvent($userID,'Removed phonebook owner ID '.$inbid);
		}	
		$_SESSION['message'] = $LANG['The selected phonebook owner(s) has been removed'];
		header('location: aphonebook?view='.$did.'&err=0&own');
	  }  else {
		  header('location: aphonebook?view='.$did);
	  }
	}	
	
	if(isset($_POST['unshare'])) {
		$inbid = mysqli_real_escape_string($server,$_POST['unshare']);
		mysqli_query($server,"DELETE FROM `phonebook_owners` WHERE `id` = '$inbid' AND is_owner = 0") or die(mysqli_error($server));
		logEvent($userID,'Removed phonebook owner ID '.$inbid);
		$_SESSION['message'] = $LANG['The selected phonebook owner(s) has been removed'];
		header('location: aphonebook?err=0&own');
	}
	
	if(isset($_REQUEST['exportc'])) { 
		exportToCSV('Contacts_','contacts','id,first_name,last_name,phone,birthday'," WHERE phonebook_id = '".$did."'");
		exit;
	}		
	$query="SELECT * FROM phonebooks WHERE id = '$did'"; 
	$result = mysqli_query($server,$query) or die(mysqli_error($server));  
	$row = mysqli_fetch_assoc($result); 
	if($row['id'] > 0) {
		$id = $row['id'];	
		$type='Private';
		if(countListUsers($did,'phonebook') > 1) $type = 'Shared';
		$customer = '<a href="acustomer?v='.$row['customer_id'].'">'.userData($row['customer_id'],'first_name').' '.userData($row['customer_id'],'last_name').'</a>';
if($row['customer_id']==0) $customer = 'Administrator';
	} else {
		header('location: aphonebook');	
	}
?>
<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-blue darken-5" href="aphonebook"><?=$LANG['Back to Phonebooks']?></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="aphonebook?view=<?=$id?>" 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 Contacts...']?>">
                     </div>
                     </form>  
                    </div>
                  </div> 
                </div>
            </div>
        </div>
    </div>
 </div>

 <div class="row no-m-t no-m-b">
     <div class="col s12 m6 l5">
  
       <ul class="collapsible" data-collapsible="accordion">
        <li>
        <div class="collapsible-header <?php if(!isset($_GET['pag']) && !isset($_GET['own'])) { echo 'active'; } ?>"><i class="material-icons">info</i> <?=$LANG['Phonebook Details']?></div>
         <div class="card card-white collapsible-body">   
            <div class="card-content">
            	<table class="highlight responsive-table">
                	<tbody>
                    	<tr class="">
                			<td><b><?=$LANG['Title']?></b></td>
                            <td><?=ucfirst($row['title'])?></td>        	
                        </tr>
                        <tr class="">
                            <td><b><?=$LANG['Type']?></b></td>
                            <td><?=$LANG[$type]?></td>
                        </tr>
                        <tr class="">
                            <td><b><?=$LANG['Owner']?></b></td>
                            <td><?=$customer?></td>
                        </tr>
                        <tr class="">
                        	<td><b><?=$LANG['Total Contacts']?></b></td>
                            <td><?=number_format(countContacts($id))?> <?=$LANG['Contact(s)']?></td>
                        </tr>
                    </tbody>
                </table>
            </div>
         </div> 
         </li>
         
        <li>
        <div class="collapsible-header"><i class="material-icons">mode_edit</i> <?=$LANG['Update Details']?></div>
         <div class="card card-white collapsible-body">
             <div class="card-content">
                <form action="aphonebook?view=<?=$row['id']?>" method="post" id="update-ph"  onSubmit="return false;">
                	<input type="hidden" id="phonebook_id" value="<?=$row['id']?>"/>
                	<div class="row no-m-t no-m-b">
                    	 <div class="input-field col s12">
                           <input id="title" type="text" class="validate" required value="<?=$row['title']?>">
                           <label for="title"><?=$LANG['Title']?></label>
                    	 </div>
                   	</div>
                    <div class="row no-m-t no-m-b">
                    	 <div class="input-field col s12">
                           <input id="description" type="text" class="validate" required value="<?=$row['description']?>">
                           <label for="description"><?=$LANG['Description']?></label>
                    	 </div>
                   	</div>
                   <div class="row no-m-t no-m-b">
                        <div class="input-field col s12">
                        	<h6 class="m-t-md black-text darken-4"><?=$LANG['bdmesg_notice']?></h6>
                              <textarea id="message" placeholder="<?=$LANG['Leave empty to disable birthday messaging']?>" class="materialize-textarea" length="960"><?=$row['birthday_sms']?></textarea>
                              <label id="mess_lab" for="message"><?=$LANG['Birthday Message']?></label>
                        </div>
                   </div>
                   <div class="card-action">
                        <button id="updateP-button" class="btn ight-blue darken-4" type="submit"><i class="fa fa-save"></i> <?=$LANG['Save']?> </button>
                        <span id="save-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>  
         </li>
         
        <li>
        <div class="collapsible-header"><i class="material-icons">screen_share</i> <?=$LANG['Share Phonebook']?></div> 
         <div class="card card-white collapsible-body">
             <div class="card-content">
                <form action="aphonebook?view=<?=$row['id']?>" method="post" id="share_ph"  onSubmit="return false;">
	                <input type="hidden" id="phonebookid" value="<?=$row['id']?>">
                	<div class="row no-m-t no-m-b">
                     <div class="input-field col s12">
                          <select id="member" required class="js-states browser-default"  style="width: 100%">
                                <option value="" disabled selected><?=$LANG['Select Customer']?></option>
                                <?php $query="SELECT * FROM users WHERE reseller = '0' AND role_id > 2";
                                $result = mysqli_query($server,$query) or die(mysqli_error($server));
                                while ($rws = mysqli_fetch_assoc($result)) { 
                                    echo '<option value="'.$rws['email'].'">'.$rws['first_name'].' '.$rws['last_name'].'</option>';
                                }
                               ?> 
                            </select>
                            <label class="active"><?=$LANG['Customer']?></label>
                           
                     </div>
                   </div>
                   <div class="card-action">
                        <button id="share-button" class="btn ight-blue darken-4" type="submit"><i class="fa fa-share"></i> <?=$LANG['Share']?> </button>
                        <span id="share-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>  
         </li>     
         
         <li>
         <div class="collapsible-header <?php if(isset($_GET['pag']) || isset($_GET['own'])) { echo 'active'; } ?>"><i class="material-icons">toc</i> <?=$LANG['Phonebook Owners']?></div> 
         	<div class="card card-white collapsible-body">
         		<div class="card-content">
                   <div class="col s12 m12 l12">
                        <a class="btn waves-effect waves-light red darken-4" onclick="bulkaction2('deleteshare','inbox-form2','<?=$LANG['delete_confirm']?>');"><?=$LANG['Remove Selected']?></a>
                        
                    </div>         
                </div>    
                <div class="card-content">
                    <table class="highlight responsive-table">
                    <thead>
                        <tr>
                        <th><input type="checkbox" class="filled-in" id="selectall2" /><label for="selectall2"></label></th>
                            <th data-field="to"><?=$LANG['Owner']?></th>
                            <th data-field="role"><?=$LANG['Privilege']?></th>
                            <th data-field="act"></th>
                        </tr>
                     </thead>
                     <tbody>
                        <form action="aphonebook?view=<?=$id?>" method="post" id="inbox-form2">
                            <input type="hidden" id="bulkaction2" name="action" value=""/>
    <?php
 $query="SELECT * FROM phonebook_owners  WHERE phonebook_id = '$id' limit $pageLimit2,$setLimit";
	$result = mysqli_query($server,$query) or die(mysqli_error($server));
	$x = mysqli_num_rows($result);	
	 $xf=0; while ($rows = mysqli_fetch_assoc($result)) { $xf+=1;
    $stats = ''; 
    $disabled = 'disabled';
	$access = $LANG['Full Access'];
	if($rows['is_owner']<1) { $access = $LANG['Read-only'];  $disabled = '';}
    $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 bulk2" id="bulk2<?=$rows['id']?>" value="<?=$rows['id']?>" name="bulk[<?=$rows['id']?>]"/> <label for="bulk2<?=$rows['id']?>"></label></td> 
                                 <td><?=$customer?></td>
                                 <td><?=$access?></td>
                                 <td>
                                 <?php if(empty($disabled)) { ?>
                                 <a class="hoverable red-text" href="aphonebook?view=<?=$id?>&unshare=<?=$rows['id']?>"><i class="fa fa-trash"></i> <?=$LANG['Remove']?><?php } ?></a> 
                                 </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 class="row">
                <div class="col s12 m12 l12" style="margin-top: 5px;">
                    <?php displayPagination2($setLimit,$pag,$query);  ?>
                </div>
            </div>    
         	</div>
         </li>   
       </ul>
     </div>           

<?php
	$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[] = "first_name LIKE '%$term%' OR phone = '%$term%' OR last_name = '%$term%' ";
			} else {
				 $clauses[] = " id LIKE '%%' ";
			}
		}
		$added .= ' AND ('.implode(' OR ', $clauses).') ';		
	}
	if(isset($_GET['s'])) {

	} else {
		$added .= " order by id DESC ";	
	}

    $query="SELECT * FROM contacts WHERE phonebook_id = '$id' $added limit $pageLimit,$setLimit";
	$result = mysqli_query($server,$query) or die(mysqli_error($server));
	$x = mysqli_num_rows($result);
?>     
     <div class="col s12 m6 l7">
         <div class="card card-white">
            <div class="card-content">
             	<span class="card-title"><?=$LANG['Contacts']?></span>
               <div class="col s12 m12 l12">
                    <a class="btn waves-effect waves-light red darken-4" onclick="bulkaction('delete','inbox-form','<?=$LANG['delete_confirm']?>');"><?=$LANG['Delete Selected']?></a>
            	<a class="btn waves-effect waves-light light-blue darken-4" href="#" onclick="ediCon('','','','',0);"><?=$LANG['Add Contact']?></a>
                    
           		</div>         
            </div>    
            <div class="card-content">
            	<div class="card-title">
                	<?php if($row['customer_id']==$userID) { ?>
                      <a class='waves-effect waves-light btn green white-text' href='aphonebook?view=<?=$row['id']?>&exportc=1'>Export</a>
                    <?php } ?>  
                </div>
                <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="to"><?=$LANG['First Name']?></th>
                        <th data-field="message"><?=$LANG['Last Name']?></th>
                        <th data-field="message"><?=$LANG['Phone']?></th>
                        <th data-field="act"></th>
                    </tr>
                 </thead>
                 <?php } ?>
                 <tbody>
                    <form action="aphonebook?view=<?=$id?>" 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 = ''; 
$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['first_name']?></td>
                             <td><?=$rows['last_name']?></td>
                             <td><?=$rows['phone']?> </td>
                             <td>
                             <a class="hoverable green-text" href="acompose?coid=<?=$rows['id']?>"><i class="fa fa-envelope"></i> <?=$LANG['SMS']?></a> &nbsp; <?php if($row['customer_id']==$userID) { ?><a class="hoverable" href="#" onclick="ediCon('<?=$rows['first_name']?>','<?=$rows['last_name']?>','<?=$rows['phone']?>','<?=$rows['birthday']?>','<?=$rows['id']?>');"><i class="fa fa-pencil"></i> <?=$LANG['Edit']?></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 class="row">
        <div class="col s12 m12 l12" style="margin-top: 5px;">
            <?php displayPagination($setLimit,$page,$query);  ?>
        </div>
    </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 Contacts']?> <span class="right"><a href="#" class="red-text" onclick="$('#add_contact')[0].reset();$('#add_new').hide('slow');"><i class="fa fa-times"></i></a></span></h5>
             <!--Tips disabled to create space. you can enable it if you want 
             <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_contact_tip']?></div>
                    </div>
                </div>
             </div>				
              -->
             <div class="card ">
                 <div class="card-content">
                    <form action="aphonebook?view=<?=$row['id']?>" method="post" id="add_contact"  onSubmit="return false;">
                    <div class="row">
                      <div class="col s12">
                          <ul class="tabs tab-demo z-depth-1" style="width: 100%;">
                             <li class="tab col s6"><a id="sc5" href="#singlec" class=""><?=$LANG['Single Contact']?></a></li>
                             <li id="bulk_cont" class="tab col s6"><a href="#bulkc"><?=$LANG['Upload File']?></a></li>
                          </ul>
                      </div>
                      <div id="singlec">
                        <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <input id="fname" type="text" class="validate" required value="">
                                   <label for="fname"><?=$LANG['First Name']?></label>
                             </div>
                       	</div>
                        <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <input id="lname" type="text" class="validate" required value="">
                                   <label for="lname"><?=$LANG['Last Name']?></label>
                             </div>
                       	</div>
                        <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <input id="phone" type="text" class="validate" required value="">
                                   <label for="phone"><?=$LANG['Phone']?></label>
                             </div>
                       	</div>
                        <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                                   <input id="dob" type="date" placeholder="" value="">
                                   <label class="active" for="dob"><?=$LANG['Date of Birth']?></label>
                             </div>
                       	</div>
                      </div>
                      
                      <div id="bulkc">
                      	<div class="row no-m-t no-m-b">
                            <div class="col s12 m12 l12">
                                <div class="card grey lighten-5 ">
                                    <div class="card-content black-text"><?=$LANG['upload_contact_tip']?><br>
                                    <a href="files/sample1.xls" class="btn cyan darken-4"><?=$LANG['download_sample']?></a></div>
                                </div>
                            </div>
                         </div>	
                         <div class="file-field input-field col s12">
                            <div class="btn light-blue darken-4">
                                <span><i class="fa fa-cloud-upload"></i></span>
                                <input id="files" type="file" name="files" accept=".csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                            </div>
                            <div class="file-path-wrapper">
                                <input class="file-path validate" type="text" placeholder="<?=$LANG['Upload an Excel or CSV file']?>">
                                <input type="hidden" id="files_holder">
                           </div>
                         </div> 
                      </div>  
                   </div>   
                       <div class="card-action">
                       		<input id="updateCon" value="0" type="hidden" >
                            <input id="phonebook_id2" value="<?=$row['id']?>" type="hidden" >
                            <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>
                             <span id="upload-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Uploading your files']?> <span id="progress-wrp">0%</span>...</strong></span>
                            <a class="btn waves-effect waves-light red" href="#" onclick="$('#add_contact')[0].reset();$('#add_new').hide('slow');"><?=$LANG['Cancel']?></a>
                       </div>
                    </form>
                 </div>
             </div>
           </div>
       <div class="col s1 m2 l3"> </div>
	 </div>
 </div>          
       
<?php	
} else {
if(isset($_POST['bulk'])) { 
	$userID = getUser();
  if($_POST['action']=='delete') {
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"DELETE FROM `phonebook_owners` WHERE `phonebook_id` = '$inbid'") or die(mysqli_error($server));
		mysqli_query($server,"DELETE FROM `phonebooks` WHERE `id` = '$inbid'") or die(mysqli_error($server));
		logEvent($userID,'Deleted phonebook ID '.$inbid.' from phonebooks');
	}	
	$_SESSION['message'] = $LANG['The selected phonebooks(s) has been deleted from your phonebook list'];
	header('location: aphonebook?err=0');
  }  else {
	  header('location: aphonebook');
  }
}
	//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[] = "p.title LIKE '%$term%' OR p.description = '%$term%'  OR p.id = '%$term%' ";
			} else {
				 $clauses[] = " p.id LIKE '%%' ";
			}
		}
		$added .= ' AND ('.implode(' OR ', $clauses).') ';		
	}
	if(isset($_GET['s'])) {
	} else {
		$added .= " order by p.id DESC ";	
	}

    $query="SELECT * FROM phonebooks p 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['aphonebook_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['Create Phonebook']?></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="aphonebook" 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">
            <?php if($x>0) { ?>
                <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['Title']?></th>
                        <th data-field="owner"><?=$LANG['Owner']?></th>
                        <th data-field="message"><?=$LANG['Contacts']?></th>
                        <th data-field="act"></th>
                    </tr>
                 </thead>
               <?php } ?>  
                 <tbody>
                    <form action="aphonebook" 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 = ''; 
$disabled = '';

$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 = 'System';
?>
						<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['id']?></td>
                             <td><?=$rows['title']?></td>
                             <td><?=$customer?></td>
                             <td><?=number_format(countContacts($rows['id']))?> <?=$LANG['Contact(s)']?></td>
                             <td><a class="hoverable" href="aphonebook?view=<?=$rows['id']?>"><i class="fa fa-circle"></i> <?=$LANG['Manage']?></a> &nbsp; <a class="hoverable green-text" href="acompose?pbid=<?=$rows['id']?>"><i class="fa fa-envelope"></i> <?=$LANG['SMS']?></a></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['Create Phonebook']?> <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="phonebook" 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="title" type="text" class="validate" required >
                           <label for="title"><?=$LANG['Title']?></label>
                    	 </div>
                        </div>
                        <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                               <input id="description" type="text" class="validate" required >
                               <label for="description"><?=$LANG['Description']?></label>
                             </div>
                        </div>
                       <div class="row no-m-t no-m-b">
                            <div class="input-field col s12">
                                <h6 class="m-t-md black-text darken-4"><?=$LANG['bdmesg_notice']?></h6>
                                  <textarea id="message" placeholder="<?=$LANG['Leave empty to disable birthday messaging']?>" class="materialize-textarea" length="960"></textarea>
                                  <label id="mess_lab" for="message"><?=$LANG['Birthday Message']?></label>
                            </div>
                       </div>
                      
                       <div class="card-action">
                            <input id="phonebook_id" value="0" type="hidden" >
                            <button id="phon-button" class="btn ight-blue darken-4" type="submit"><?=$LANG['Save']?> </button>
                            <span id="phon-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 } ?> 
