<?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','Lists');
if(userRole()>2) {
	header('location: index.php');	
}
hasModulePermission('view marketinglist');
$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 `marketingcontacts` WHERE `id` = '$inbid'") or die(mysqli_error($server));
			logEvent($userID,'Deleted contact ID '.$inbid.' from marketing list');
		}	
		$_SESSION['message'] = $LANG['The selected contacts(s) has been deleted from your list'];
		header('location: amarketinglist?view='.$did.'&err=0');
	  }  elseif($_POST['action']=='deleteshare') {
		foreach($_POST['bulk'] as $inbid) { 
			mysqli_query($server,"DELETE FROM `marketinglist_owners` WHERE `id` = '$inbid'") or die(mysqli_error($server));
			logEvent($userID,'Removed markeing list owner ID '.$inbid);
		}	
		$_SESSION['message'] = $LANG['The selected marketing list owner(s) has been removed'];
		header('location: amarketinglist?view='.$did.'&err=0&own');
	  }   else {
		  header('location: amarketinglist?view='.$did);
	  }
	}	
	
	if(isset($_POST['unshare'])) {
		$inbid = mysqli_real_escape_string($server,$_POST['unshare']);
		mysqli_query($server,"DELETE FROM `marketinglist_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 marketing list owner(s) has been removed'];
		header('location: amarketinglist?err=0&own');
	}
	
	if(isset($_REQUEST['exportc'])) { 
		exportToCSV('List_','marketingcontacts','id,name,phone'," WHERE marketinglist = '".$did."'");
		exit;
	}		
	$query="SELECT * FROM marketinglist 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($row['id'],'marketing') > 1) $type = 'Shared';
		if($row['is_public']==1) $type = 'Commercial';
		$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';
		if(countListUsers($did,'marketing') > 1) $type = 'Shared';
	} else {
		header('location: amarketinglist');	
	}
?>
<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="amarketinglist"><?=$LANG['Back to List']?></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="amarketinglist?view=<?=$id?>" method="post">
                     <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 active"><i class="material-icons">info</i> <?=$LANG['Marketing List 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>
                        <?php if($row['cost']>0) { ?>
                        <tr class="">
                            <td><b><?=$LANG['Price']?></b></td>
                            <td><?=userCurrencySymbol($userID).number_format($row['cost'],3)?></td>
                        </tr>
                        <?php } ?>
                        <tr class="">
                            <td><b><?=$LANG['Owner']?></b></td>
                            <td><?=$customer?></td>
                        </tr>
                        <tr class="">
                            <td><b><?=$LANG['Sender ID']?></b></td>
                            <td><?=$row['default_sender_id']?></td>
                        </tr>
                         <tr class="">
                            <td><b><?=$LANG['Assigned Shortcode']?></b></td>
                            <td><?=$row['shortcode']?></td>
                        </tr>
                        <tr class="">
                            <td><b><?=$LANG['Opt-in Keyword']?></b></td>
                            <td><?=$row['optin_keyword']?></td>
                        </tr>
                        <tr class="">
                            <td><b><?=$LANG['Opt-out Keyword']?></b></td>
                            <td><?=$row['optout_keyword']?></td>
                        </tr>
                        <tr class="">
                        	<td><b><?=$LANG['Total Contacts']?></b></td>
                            <td><?=number_format(countContacts($id,'marketing'))?> <?=$LANG['Contact(s)']?></td>
                        </tr>
                    </tbody>
                </table>
            </div>
         </div> 
         </li>
         
        <li>
        <div class="collapsible-header"><i class="material-icons">mode_edit</i> <?=$LANG['Update List']?></div>
         <div class="card card-white collapsible-body">
             <div class="card-content">
                <form action="amarketinglist?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">
                               <select id="is_public" required class="validate">
                                    <option <?php if($row['is_public']=='0') echo 'selected';?> value="0" ><?=$LANG['Private']?></option>
                                    <option <?php if($row['is_public']=='1') echo 'selected';?> value="1" ><?=$LANG['Commercial']?></option>
                               </select>
                               <label><?=$LANG['Type']?></label>
                         </div>
                       </div>
                    <div class="row no-m-t no-m-b">
                    	 <div class="input-field col s12">
                           <input id="cost" type="text" class="validate" required value="<?=number_format($row['cost'])?>">
                           <label for="cost"><?=$LANG['Price']?> (<?=userCurrencySymbol($userID)?>)</label>
                    	 </div>
                   	</div>
                    <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">
                         	<input id="default_sender_id" type="text" class="validate"  value="<?=$row['default_sender_id']?>">
                           <label for="default_sender_id"><?=$LANG['Sender ID']?> </label>
                         </div>
                       </div>
                       
                       <div class="row no-m-t no-m-b">
                         <div class="input-field col s12">
                         	<input id="shortcode" type="text" class="validate"  value="<?=$row['shortcode']?>">
                           <label for="shortcode"><?=$LANG['Assign Shortcode']?> (<?=$LANG['Include country code without + sign']?>)</label>
                         </div>
                       </div>

                       <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                               <input id="optin_key" type="text" class="validate" value="<?=$row['optin_keyword']?>"  >
                               <label for="optin_key"><?=$LANG['Opt-in Keyword']?></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['opt_notice']?></h6>
                                  <textarea id="optin_sms" placeholder="<?=$LANG['Leave empty to disable optin messaging']?>" class="materialize-textarea" length="960"><?=$row['optin_message']?></textarea>
                                  <label id="mess_lab2" for="optin_sms"><?=$LANG['Opt-in SMS']?></label>
                            </div>
                       </div>
                       <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                               <input id="optout_key" type="text" class="validate" value="<?=$row['optout_keyword']?>"  >
                               <label for="optout_key"><?=$LANG['Opt-out Keyword']?></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['opt_notice']?></h6>
                                  <textarea id="optout_sms" placeholder="<?=$LANG['Leave empty to disable optout messaging']?>" class="materialize-textarea" length="960"><?=$row['optout_message']?></textarea>
                                  <label id="mess_lab" for="optout_sms"><?=$LANG['Opt-out SMS']?></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 List']?></div> 
         <div class="card card-white collapsible-body">
             <div class="card-content">
                <form action="amarketinglist?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['Marketing List 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="amarketinglist?view=<?=$id?>" method="post" id="inbox-form2">
                            <input type="hidden" id="bulkaction2" name="action" value=""/>
    <?php
 $query="SELECT * FROM marketinglist_owners  WHERE marketinglist_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="amarketinglist?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($_REQUEST['sc'])) {
		$keywords = mysqli_real_escape_string($server,$_REQUEST['sc']);
		$ser = explode(' ', $keywords);
		$clauses = array();
		foreach($ser as $look){
			$term = trim(preg_replace('/[^a-z0-9]/i', '', $look));   
			if (!empty($term)){
				 $clauses[] = "name LIKE '%$term%' OR phone LIKE '%$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 marketingcontacts WHERE marketinglist = '$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-options">
                      <a class='waves-effect waves-light btn green white-text' href='amarketinglist?view=<?=$row['id']?>&exportc=1'>Export</a>
                </div>
                <table class="highlight responsive-table">
                <thead>
                	<tr>
                    <th><input type="checkbox" class="filled-in" id="selectall" /><label for="selectall"></label></th>
                        <th data-field="to"><?=$LANG['Name']?></th>
                        <th data-field="message"><?=$LANG['Phone']?></th>
                        <th data-field="act"></th>
                    </tr>
                 </thead>
                 <tbody>
                    <form action="amarketinglist?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 = '';

$phone = $rows['phone'];
if( ($rows['marketinglist']>0) && (marketinglistData($rows['marketinglist'],'customer_id') != $userID) ) {
	//$phone = substr($rows['phone'],0,-5) . '*****'; no point hiding from admin
}
?>
						<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['name']?></td>
                             <td><?=$phone?> </td>
                             <td>
                             <a class="hoverable green-text" href="acompose?mkcoid=<?=$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['name']?>','','<?=$rows['phone']?>','','<?=$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="amarketinglist?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"  value="">
                                   <label for="fname"><?=$LANG['Full 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>
                      
                      <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 class="input-field col s12 m6 l6">
                           <div class="switch m-b-md">
                             <label id="sch_upload" class="black-text"><?=$LANG['Upload Now']?> <input type="checkbox" id="upload_now" value="1"><span class="lever"></span><?=$LANG['Upload in Background']?></label>
                           </div>
                           <br>
                         </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') {
	  hasModulePermission('delete marketinglist');
	foreach($_POST['bulk'] as $inbid) { 
		mysqli_query($server,"DELETE FROM `marketinglist_owners` WHERE `marketinglist_id` = '$inbid'") or die(mysqli_error($server));
		mysqli_query($server,"DELETE FROM `marketinglist` WHERE `id` = '$inbid'") or die(mysqli_error($server));
		logEvent($userID,'Deleted list ID '.$inbid.' from marketing list');
	}	
	$_SESSION['message'] = $LANG['The selected marketing list(s) has been deleted from your list'];
	header('location: amarketinglist?err=0');
  }  else {
	  header('location: amarketinglist');
  }
}
	//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 LIKE '%$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 marketinglist 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['alist_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 List']?></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="amarketinglist" 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="uss"><?=$LANG['Type']?></th>
                        <th data-field="type"><?=$LANG['Owner']?></th>
                        <th data-field="message"><?=$LANG['Contacts']?></th>
                        <th data-field="act"></th>
                    </tr>
                 </thead>
               <?php } ?>  
                 <tbody>
                    <form action="amarketinglist" 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 = '';
$type='Private';
if(countListUsers($rows['id'],'marketing') > 1) $type = 'Shared';
if($rows['is_public']==1) $type = 'Commercial';
$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';
$vars = array( "reference"=>$rows['id']	 );
$_SESSION['EventVals'] = $vars;
?>
						<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><?=$LANG[$type]?></td>
                             <td><?=$customer?></td>
                             <td><?=number_format(countContacts($rows['id'],'marketing'))?> <?=$LANG['Contact(s)']?></td>
                             <td><a class="hoverable" href="amarketinglist?view=<?=$rows['id']?>"><i class="fa fa-circle"></i> <?=$LANG['Manage']?></a> &nbsp; <a class="hoverable green-text" href="acompose?mktid=<?=$rows['id']?>"><i class="fa fa-envelope"></i> <?=$LANG['SMS']?></a>
                             <?php global $hooks; $hooks->do_action('MarketingListTableAction'); ?>
                             </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 marketinglist'); ?>
             <h5><?=$LANG['Create Marketing List']?> <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="marketinglist" onSubmit="return false;" method="post" id="add_phonebook">
                    	<div class="row no-m-t no-m-b">
                         <div class="input-field col s12">
                               <select id="is_public" required class="validate">
                                    <option  value="0" ><?=$LANG['Private']?></option>
                                    <option  value="1" ><?=$LANG['Commercial']?></option>
                               </select>
                               <label><?=$LANG['Type']?></label>
                         </div>
                       </div>
                    <div class="row no-m-t no-m-b">
                    	 <div class="input-field col s12">
                           <input id="cost" type="text" class="validate" required value="0.00">
                           <label for="cost"><?=$LANG['Price']?> (<?=userCurrencySymbol($userID)?>)</label>
                    	 </div>
                   	</div>
                        <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">
                               <input id="shortcode" type="text" class="validate"  value="">
                        	   <label for="shortcode"><?=$LANG['Assign Shortcode']?> (<?=$LANG['Include country code without + sign']?>)</label>
                         </div>
                       </div>
                       
                        <div class="row no-m-t no-m-b">
                         <div class="input-field col s12">
                               <input id="default_sender_id" type="text" class="validate"  value="">
                        	   <label for="default_sender_id"><?=$LANG['Sender ID']?></label>
                         </div>
                       </div>

                       <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                               <input id="optin_key" type="text" class="validate" required >
                               <label for="optin_key"><?=$LANG['Opt-in Keyword']?></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['opt_notice']?></h6>
                                  <textarea id="optin_sms" placeholder="<?=$LANG['Leave empty to disable optin messaging']?>" class="materialize-textarea" length="960"></textarea>
                                  <label id="mess_lab" for="optin_sms"><?=$LANG['Opt-in SMS']?></label>
                            </div>
                       </div>
                       <div class="row no-m-t no-m-b">
                             <div class="input-field col s12">
                               <input id="optout_key" type="text" class="validate" required >
                               <label for="optout_key"><?=$LANG['Opt-out Keyword']?></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['opt_notice']?></h6>
                                  <textarea id="optout_sms" placeholder="<?=$LANG['Leave empty to disable optout messaging']?>" class="materialize-textarea" length="960"></textarea>
                                  <label id="mess_lab" for="optout_sms"><?=$LANG['Opt-out SMS']?></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 } ?> 
