<?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','Email');
if(userRole()>3) {
	header('location: index.php');	
	exit;
}
if(userRole()<3) {
hasModulePermission('send email');	
}
$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
if(!isset($_GET['ti'])) {
?>
<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['em_intro']?></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='waves-effect waves-light btn green white-text' href='#' onclick="addPB();"><i class="fa fa-plus-circle"></i> <?=$LANG['Send New Message']?></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="sendemail" 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">
       
<?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[] = "t.message LIKE '%$term%' OR t.id LIKE '%$term%' OR t.subject LIKE '%$term%' OR t.id LIKE '%$term%' OR t.status LIKE '%$term%'";
			} else {
				 $clauses[] = " t.id LIKE '%%' ";
			}
		}
		$added .= ' AND ('.implode(' OR ', $clauses).') ';		
	}
	if(isset($_GET['s'])) {
		switch($_GET['s']) {
			case 'd_o': $added .= " AND status LIKE '%open%' order by t.id DESC ";
			break;
			case 'd_n': $added .= " AND status LIKE '%answered%' order by t.id DESC ";
			break;
			case 's_r': $added .= " AND status LIKE '%customer-reply%' order by t.id DESC ";
			break;
			case 's_u': $added .= " AND status LIKE '%closed%' order by t.id DESC ";
			break;
		}
	} else {
		$added .= " order by t.id DESC ";	
	}

    $query="SELECT t.* FROM emails t WHERE t.customer_id = '$userID' $added limit $pageLimit,$setLimit";
	if(userRole()<3) 
	$query="SELECT t.* FROM emails t WHERE t.customer_id = 0 $added limit $pageLimit,$setLimit";
	$result = mysqli_query($server,$query) or die(mysqli_error($server));
	$x = mysqli_num_rows($result);
?>     
     <div class="col s12 m12 l12">
         <div class="card card-white">
         	<div class="card-content">
            	<div class="card-options">
                    
                </div>
            </div>
            <div class="card-content">
            	<div class="card-options">
                </div>
                <table class="highlight responsive-table">
                <?php if($x>0) { ?>
                <thead>
                	<tr>
                        <th data-field="date"><?=$LANG['Date']?></th>
                        <th data-field="to"><?=$LANG['Subject']?></th>
                        <th data-field="credit"><?=$LANG['Status']?></th>
                        <th></th>
                    </tr>
                 </thead>
                 <?php } ?>
                 <tbody>
<?php $xf=0; while ($rows = mysqli_fetch_assoc($result)) { $xf+=1;
?>
						<tr class="">
                             <td class=""><?=strtotime($rows['date'])>0?date('M j, Y h:iA',strtotime($rows['date'])):'Unknown'?></td>
                             <td class=""><?=$rows['subject']?></td>
                             <td class=""><?=ucfirst($rows['status'])?></td>
                             <td></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>     
 <?php
 $none = 'none';
 $added = '';
 if(isset($_GET['e'])) {
	$none = 'block'; 
	$added = " AND id = '".mysqli_real_escape_string($server,$_GET['e'])."'";
 }
 ?>
 
 <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['Compose Email Message']?> <span class="right"><a href="#" class="red-text" onclick="$('#add_new').hide('slow');"><i class="fa fa-times"></i></a></span></h5>
             <div class="card ">
                 <div class="card-content">
                 	<div class="row no-m-t no-m-b">
                    	<div class="input-field col s12">
                            <select id="recipients" required class="js-states browser-default"  style="width: 100%">
                               <?php if($_GET['e']<1) { ?>
                                <option value="all" selected><?=$LANG['All Registered Customers']?></option>
                                <option value="allA" selected><?=$LANG['Only Verified Customers']?></option>
                                <option value="allU" selected><?=$LANG['Only Unverified Customers']?></option>
                                <option value="zb" selected><?=$LANG['Customers With Zero Balance']?></option>
                                <?php if(userRole()<3) { ?>
                                <option value="resel" ><?=$LANG['All Resellers Only']?></option>
                                <?php } ?>
								<?php 
							   }
								$query="SELECT * FROM users WHERE reseller = '$userID' $added";
								if(userRole()<3) $query="SELECT * FROM users WHERE reseller = '0' $added";
                                $result = mysqli_query($server,$query) or die(mysqli_error($server));
                                while ($rws = mysqli_fetch_assoc($result)) { 
                                    echo '<option value="'.$rws['id'].'">'.$rws['first_name'].' '.$rws['last_name'].' ('.$rws['email'].')</option>';
                                }
                               ?> 
                            </select>
                            <label class="active"><?=$LANG['Recipient']?></label>
                        </div>
                     </div>  
                     <div class="row no-m-t no-m-b">
                         <div class="input-field col s12">
                               <input id="emailTo" autofocus type="text" class="validate" >
                               <label for="emailTo"><?=$LANG['Type Email Addresses']?></label>
                         </div>
                       </div> 
                     <div class="row no-m-t no-m-b">
                         <div class="input-field col s12">
                               <input id="subject" autofocus type="text" class="validate" required >
                               <label for="subject"><?=$LANG['Subject']?></label>
                         </div>
                       </div>
                     <div class="row no-m-t no-m-b">
                        <div class="input-field col s12">
                             <textarea id="message"></textarea>
                        </div>			
					</div>
                     <div class="row no-m-t no-m-b">       
                        <div class="file-field input-field col s12">
                            <div class="btn light-blue darken-4">
                                <span><i class="fa fa-paperclip"></i></span>
                                <input id="files" type="file" name="files">
                            </div>
                            <div class="file-path-wrapper">
                               <input class="file-path validate" type="text" placeholder="<?=$LANG['Attach File']?>">
                               <input type="hidden" id="files_holder">
                            </div>
                        </div> 
                    </div>
                   <div class="card-action">
                    	<a class="btn waves-effect waves-light red" href="sendemail"><?=$LANG['Cancel']?></a>
                    	<button id="send-button" class="btn green" type="submit"><?=$LANG['Send Email']?> </button>
                    	<span id="send-spin" class="btn-flat" style="display: none;"><i class="fa fa-spinner fa-spin"></i> <strong><?=$LANG['Please wait']?>...</strong></span>
                	</div>   
                 </div>
             </div>
           </div>
       <div class="col s1 m2 l3"> </div>
	 </div>
 </div>                
  
<?php } ?>	