<?php

require __dir__ . '/init.php';

$_POST = help::sanitize($_POST);

if(isset($_POST['msgs'])) {

	$data = array(
		"message" => $_POST['message'],
		"msgid" => help::keygen(10, false),
		"account" => $__user['acctno']
	);
	
	$SQL = sQuery::insert( 'messageadmin', $data );
	
	$status = (mysqli_query($link, $SQL));
    
	$msg_admin = $status ? "Your message was successfully sent!" : "Cannot send message!";
        
};
    
    
    
if(isset($_POST['delete'])) {
	 
    $sql = "DELETE FROM messageadmin WHERE msgid='{$_POST['msgid']}'";
    
	$status = (mysqli_query($link, $sql));
	
    $msg_delete = $status ? "Message deleted successfully!" : "Message not deleted! ";
    
}


template::get_header();

?>

	<?php template::title("Message Admin"); ?>

    <div class="row">
        <div class="col-lg-12">
            <div class="sparkline12-list shadow-reset mg-t-30">
			
				<div class="sparkline12-graph">
					<div class="basic-login-form-ad">
					
						<div class="row">
							<div class="col-lg-12">
								<div class="all-form-element-inner">
								
									<form action="message.php" method="post">
									
										<?php template::alert( $msg_admin ?? null, $status ?? null ); ?>
										
										<div class="form-group">
											<div class="row">
												<div class="col-lg-3">
													<label class="login2 pull-right pull-right-pro">Message</label>
												</div>
												<div class="col-lg-9">
													<textarea  name="message" placeholder="Type enter your message"  class="form-control" rows="5"/></textarea>
												</div>
											</div>
										</div>

										
										<div class="form-group">
											<div class="row">
												<div class="col-lg-3"></div>
												<div class="col-lg-3">
													<input type="submit" name="msgs" value="Send Message"  class="btn btn-info" />
												</div>
											</div>
										</div>

									</form>
									
										<hr>
									
										<div class='row'>
											<div class="col-md-12 col-sm-12 col-sx-12">

												<div class='card'>
													<div class='card-body'>
													
														<?php template::alert( $msg_delete ?? null, $status ?? null ); ?>
												
														<div class="box-body table-responsive no-padding">
															<table id="table" class="table table-responsive table-striped table-bordered table-hover" >
																<thead>
																	<tr class="info">
																		<th>Account</th>
																		<th>Message</th>
																		<th>Admin-Response</th>
																		<th>Status</th>
																		<th>Date</th>
																		<th>Action</th>
																	</tr>
																</thead>
																<tbody>
																<?php 
																
																	$sql= "
																		SELECT * FROM messageadmin 
																		WHERE account='{$__user['acctno']}' 
																		ORDER BY id DESC LIMIT 100
																	";
																	
																	$result = mysqli_query($link,$sql);
																	
																	if(mysqli_num_rows($result) > 0){
																		while($row = mysqli_fetch_assoc($result)){  
																		
																			if(isset($row['astatus'])  && $row['astatus']==1) {
																				$msg = 'Administrator replied &nbsp;&nbsp;<i class="fa fa-check text-success" ></i>';
																			} else {
																				$msg = 'Waiting for response! &nbsp;&nbsp;<i class="fa fa-envelope text-warning"></i>';
																			}
																	
																?>

																	<tr class="primary">
																		<td><?php echo $row['account'];?></td>
																		<td id="account">  <?php echo $row['message'];?></td>
																		<td id="account"> <?php echo $row['reply'];?></td>
																		<td><?php echo $msg;?></td>
																		<td><?php echo $row['date'];?></td>
																		<td>
																			<form action="message.php" method="post" onsubmit="return confirm('Are you sure?');">
																				<input type="hidden" name="msgid" value="<?php echo $row['msgid'];?>">
																				<button type="submit" name="delete" class="btn btn-danger" <?php if(!empty($row['reply'])) echo 'disabled'; ?>>
																					<span class="glyphicon glyphicon-trash"> Delete</span>
																				</button>
																			</form>
																		</td>
																	</tr>
						
																<?php
																		}
																	}
																?>
																</tbody>	
															</table>
															
														</div>
															
													</div>
												</div>
											
											</div>
										</div>
										
								</div>
							</div>
						</div>
							
					</div>
				</div>
				
			</div>
		</div>
	</div>
	   
	
<?php template::get_footer(); ?>  