<?php
session_start();
require_once 'class.admin.php';
include_once ('session.php');
if(!isset($_SESSION['email'])){
	
header("Location: login.php");

exit(); 
}
$reg_user = new USER();


$account = $reg_user->runQuery("SELECT * FROM account");
$account->execute();
$stmt = $reg_user->runQuery("SELECT * FROM message ORDER BY id DESC LIMIT 200");
$stmt->execute();

if(isset($_POST['message']))
{
	
	$sender_name = trim($_POST['sender_name']);
	$sender_name = strip_tags($sender_name);
	$sender_name = htmlspecialchars($sender_name);
	
	$reci_name = trim($_POST['reci_name']);
	$reci_name = strip_tags($reci_name);
	$reci_name = htmlspecialchars($reci_name);
	
	$subject = trim($_POST['subject']);
	$subject = strip_tags($subject);
	$subject = htmlspecialchars($subject);
	
	$msg = trim($_POST['msg']);
	$msg = strip_tags($msg);
	$msg = htmlspecialchars($msg);
	
	
	
	
		if($reg_user->message($sender_name,$reci_name,$subject,$msg))
		{			
			$id = $reg_user->lasdID();	
			
			$msg = "
					<div class='alert alert-success'>
						<button class='close' data-dismiss='alert'>&times;</button>
						<strong>Sent!</strong>.
                     
			  		</div>
					";
		}
		else
		{
			echo "Sorry, Message was not sent";
		}		
}

?>

<?php include 'headeradmin.php'; ?>
 

 <!---begin of Mobile View Here   only from Digital Forest Team-->
 
       <?php include 'menu.php'; ?>
  
  <!---End of Mobile View Here   only from Digital Forest Team-->
  
 
      <script type="text/javascript" src="paginator.js"></script>
        <script type="text/javascript" src="table.js"></script>
     
     
  
    <div class="container-fluid mt--7">
     <!-- Table -->
      <div class="row">
        <div class="col">
          <div class="card shadow" >
            <div class="card-header border-0">
              <h3 class="mb-0"><a href=""   class="btn btn-danger">COMPOSE MESSAGES</a></h3>
            </div>
          
           <div class="card-body">
           
               
<div class="container-fluid">
  
			<?php if(isset($msg)) echo $msg;  ?>
                
               
               
               
               
               
               
               
               
               <style>
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myTable {
  border-collapse: collapse;
  width: 100%;
  border: 1px solid #ddd;
  font-size: 18px;
}

#myTable th, #myTable td {
  text-align: left;
  padding: 12px;
}

#myTable tr {
  border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
  background-color: #f1f1f1;
}
</style>



<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for sender names.." title="Type in a sender name">

<table id="myTable">
  <tr class="header">
    <th style="width:60%;">Sender Name</th>
    <th style="width:40%;">To</th>
    <th style="width:40%;">Date</th>
    <th style="width:40%;">Message</th>
    <td>Action</td>
  </tr>
  
  <?php while($row = $stmt->fetch(PDO::FETCH_ASSOC)){?>
  <tr>    
    <td><?php echo $row['sender_name']; ?></td>
   <td><?php echo $row['reci_name']; ?></td>
   <td><?php echo $row['date']; ?></td>
   <td><?php echo $row['msg']; ?></td>
   <td><a class="btn btn-danger" href="decmessage.php?id=<?php echo $row['id']; ?>" >DELETE</a></td>
  </tr>
   <?php }?>
  
</table>
    
<script>
function myFunction() {
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("myTable");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }       
  }
}
</script>

               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               
               <h2>COMPOSE  NEW MESSAGE</h2>
               
                
						<form class="form-group" method="POST">
                            <div class="modal-header">
							
                              
                            </div>
                            <select class="form-control primary" name="sender_name">
                                <option class="form-control" >Sender</option>
                                <option class="form-control" style="background-color:#000" value="Customer Care">Customer Care</option>
                                <option class="form-control" style="background-color:#000" value="Account Officer">Account Officer</option>
                                <option class="form-control" style="background-color:#000" value="Service Department">Service Department</option>
                            </select><br>
							<select class="form-control primary"name="reci_name">
                                <option class="form-control" >To</option>
								<?php while($show = $account->fetch(PDO::FETCH_ASSOC)){?>
                                <option class="form-control" style="background-color:#000" value="<?php echo $show['uname']; ?>"><?php echo $show['fname']; ?> <?php echo $show['lname']; ?></option>
                                <?php }?>
                            </select><br>
                            <div class=" form-control  primary">
                                <input type="text" class="form-control" name="subject" placeholder="Subject...">
                            </div><br>
                            <div class="p-relative">
                                <textarea class="message-editor form-control" name="msg" placeholder="Message..."></textarea>
                            </div><br>
                            <div class="modal-footer m-0">
                                <button class="btn" data-dismiss="modal">Cancel</button>
								<button class="btn" type="subit" name="message">Send</button>
                            </div>
						</form>
                        
        


</div>
               
               
               
          </div>
        </div>
      </div>
     </div>
 
    
       <?php include 'foot.php'; ?>



