<?php session_start(); ob_start(); ?>

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

<?php

if(empty($_SESSION['LOGINID']) && empty($_SESSION['LOGINNM']) ){

	header("Location:index.php");
}


include("user.php");


$t_id=intval($_GET["id"]);


$selectkt = "SELECT * FROM `tickets` WHERE id= '$t_id' ";
$selectkt = mysqli_query($connection, $selectkt);
$selectkt = mysqli_fetch_object($selectkt);

$subject = $selectkt->subject;

if (isset($_POST['add_reply'])) {
	       $reply = $_POST['reply'];
		   $reply = filter_var($reply, FILTER_SANITIZE_STRING);
		  $reply = mysqli_real_escape_string($connection, $reply);
		   
		   $new_ip = $_SERVER['REMOTE_ADDR'];
		 $sqlinsert = "INSERT INTO `ticket_replies` (`id`, `ticket_id`, `details`, `type`, `added_by`, `added_at`, `updated_at`, `ip`) VALUES (NULL, '$t_id', '$reply', 'Client', '0', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '$new_ip');";
$sqlinsert = mysqli_query($connection, $sqlinsert); 


 $sqlUpdate = "UPDATE tickets SET
  last_reply_by = 'Client',
  updated_at = CURRENT_TIMESTAMP
												  											  
 WHERE
 id = '$t_id'           
";
$sqlUpdate = mysqli_query($connection, $sqlUpdate); 

		 
}


?>



<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Tickets</title>
    <!-- ================= Favicon ================== -->
    <!-- Standard -->
    <link rel="shortcut icon" href="http://placehold.it/64.png/000/fff">
    <!-- Retina iPad Touch Icon-->
    <link rel="apple-touch-icon" sizes="144x144" href="http://placehold.it/144.png/000/fff">
    <!-- Retina iPhone Touch Icon-->
    <link rel="apple-touch-icon" sizes="114x114" href="http://placehold.it/114.png/000/fff">
    <!-- Standard iPad Touch Icon-->
    <link rel="apple-touch-icon" sizes="72x72" href="http://placehold.it/72.png/000/fff">
    <!-- Standard iPhone Touch Icon-->
    <link rel="apple-touch-icon" sizes="57x57" href="http://placehold.it/57.png/000/fff">
    <!-- Styles -->
     <link href="assets/css/lib/data-table/buttons.bootstrap.min.css" rel="stylesheet" />
    <link href="assets/css/lib/font-awesome.min.css" rel="stylesheet">
    <link href="assets/css/lib/themify-icons.css" rel="stylesheet">
    <link href="assets/css/lib/owl.carousel.min.css" rel="stylesheet" />
    <link href="assets/css/lib/owl.theme.default.min.css" rel="stylesheet" />
    <link href="assets/css/lib/weather-icons.css" rel="stylesheet" />
    <link href="assets/css/lib/menubar/sidebar.css" rel="stylesheet">
    <link href="assets/css/lib/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/lib/unix.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
</head>

<body>

   
   
   
     <?php include("sidebar.php"); ?>
	
	
    <!-- /# sidebar -->
     <?php include("top.php"); ?>
	<!-- /# topebar --> 
	
	
	
    <div class="content-wrap">
        <div class="main">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-8 p-r-0 title-margin-right">
                        <div class="page-header">
                            <div class="page-title">
                                <h1>Hello, <span>Welcome Here</span></h1>
                            </div>
                        </div>
                    </div>
                    <!-- /# column -->
                    <div class="col-lg-4 p-l-0 title-margin-left">
                        <div class="page-header">
                            <div class="page-title">
                                <ol class="breadcrumb text-right">
                                    <li><a href="#">Dashboard</a></li>
                                    <li class="active">Home</li>
                                </ol>
                            </div>
                        </div>
                    </div>
                    <!-- /# column -->
                </div>
                <!-- /# row -->
                <section id="main-content">
				
				
			
				
				
				
				
				
				<div class="row">
				
				        <div class="col-lg-12">
						
						<div class="panel panel-info m-t-15">
                                            <div class="panel-heading">#<?php echo $t_id; ?> -   <?php echo $subject; ?> </div>
                                            <div class="panel-body">
					
                                  
 

                                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card alert">
							
							<form action="ticket_details.php?id=<?php echo $t_id; ?>" method="post">
                               <div class="form-group">
                                                <label class="col-sm-2 control-label">Add New Reply</label>
                                                <div class="col-sm-10">
                                                    <textarea name="reply" class="form-control" rows="6" placeholder="Type your reply" required></textarea>
                                                </div>
												
                               </div>
							   
							 
								 <div class="form-group">
                                             
                                                <div class="col-sm-12">
                                                   &nbsp;&nbsp;&nbsp;
                                                </div>
												
                               </div>
								
								  <div class="form-group">
								               <label class="col-sm-2 control-label">   &nbsp;&nbsp;&nbsp; </label>
                                              <div class="col-sm-10">
                                                  <button class="btn btn-success btn-md" type="submit" name="add_reply"> Add Reply </button> 
                                               	
								 
												
                                    </div>
					
							</form>
								 
								 <div class="form-group">
                                             
                                                <div class="col-sm-12">
                                                   &nbsp;&nbsp;&nbsp;
                                                </div>
												
                               </div>
							   
							   
							   
							   
							   
							   
							   <?php
							    $sqlSelectedLG = "SELECT f1.*,f2.name as client_name from (SELECT t1.*,t2.booking_temp_id, t3.name as staff_name FROM `ticket_replies` as t1 left join tickets as t2 on t1.ticket_id = t2.id left join users as t3 on t3.id=t1.added_by) as f1 left join bookings as f2 on f1.booking_temp_id=f2.id where ticket_id= '$t_id' order by f1.id DESC ";
								$sqlSelectedLG = mysqli_query($connection, $sqlSelectedLG);
							   
							   
							     while ($list = mysqli_fetch_object($sqlSelectedLG)) {
                                      $details = $list->details;
									  $type = $list->type;
									  $ip = $list->ip;
									    $client_name = $list->client_name;
									  $staff_name = $list->staff_name;
									  
									  	if( $type=="Client"){ $reply_name=$client_name;   $color="#03A9F4"; }
								     	if( $type=="Staff"){ $reply_name=$staff_name; $ip=""; $color="#F39C12";}
									  
									  
									  
									  
									
									
                                    $added_at = $list->added_at;
								    $added_at = date('h:i A d/m/Y', strtotime($added_at));
									
									 $updated_at = $list->updated_at;
									$updated_at = date('h:i A d/m/Y', strtotime($updated_at));
									 
									
                                    
                                   
                                ?>
							   
							   
							    <table border='1' BORDERCOLOR='<?php echo $color;  ?>' width='100%'> <tr> <td align='left' width='30%'>  
								
								 </br>   </br>
							
								 <h4>&nbsp;&nbsp;&nbsp;<b><?php echo $reply_name;  ?></b></h4>
								 <p>&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $type;  ?></p>
								 <p>&nbsp;&nbsp;&nbsp;&nbsp; </p>
                                   	
								
								
								
								</td>
								<td align='left' width='70%'>  
								
								 
								 
								  <table  border='5' align='left' BORDERCOLOR='white' width='90%'> <tr> <td align='left' width='20%'>  
								  </br>   
								  <p class="text-left">Added at: <?php echo $added_at;  ?> </p>
								  </br>
								  	
									<p class="text-left"><?php echo $details;  ?></p>
								  </br>
								    <p class="text-left">IP: <?php echo $ip;  ?> </p>
							     </td> </tr> 	</table>
								 
								 
						
								 
								
								
								
								</td> </tr> 	</table>
							   
							   
							   
							   </br>
							   
								 <?php } ?>
							   
							   
							   
							   
							   
							   
							   
							   
							   
							   
							   
							   
							   
							
							
							
                            <!-- /# card -->
                        </div>
                        <!-- /# column -->
                    </div>

                                   


							 </div> </div>
							
                        </div>
				
				
                        <!-- /# column -->
                        
                    </div>
				
				
				
				
				
                    <!-- /# row -->
                     <?php include("footer.php"); ?>
                </section>
            </div>
        </div>
    </div>

    <div id="search">
        <button type="button" class="close">×</button>
        <form>
            <input type="search" value="" placeholder="type keyword(s) here" />
            <button type="submit" class="btn btn-primary">Search</button>
        </form>
    </div>


    </div>
    <script src="assets/js/lib/jquery.min.js"></script>
    <!-- jquery vendor -->
    <script src="assets/js/lib/jquery.nanoscroller.min.js"></script>
    <!-- nano scroller -->
    <script src="assets/js/lib/menubar/sidebar.js"></script>
    <script src="assets/js/lib/preloader/pace.min.js"></script>
    <!-- sidebar -->
    <script src="assets/js/lib/bootstrap.min.js"></script>
    <!-- bootstrap -->
   
    <script src="assets/js/scripts.js"></script>
	
	 <script src="assets/js/lib/data-table/datatables.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.dataTables.min.js"></script>
    <script src="assets/js/lib/data-table/dataTables.buttons.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.flash.min.js"></script>
    <script src="assets/js/lib/data-table/jszip.min.js"></script>
    <script src="assets/js/lib/data-table/pdfmake.min.js"></script>
    <script src="assets/js/lib/data-table/vfs_fonts.js"></script>
    <script src="assets/js/lib/data-table/buttons.html5.min.js"></script>
    <script src="assets/js/lib/data-table/buttons.print.min.js"></script>
    <script src="assets/js/lib/data-table/datatables-init.js"></script>
</body>

</html>