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

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

<?php

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

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


include("user.php");




if (isset($_POST['add_ticket'])) {
	       $reply = $_POST['reply'];
		   $reply = filter_var($reply, FILTER_SANITIZE_STRING);
		   $reply = mysqli_real_escape_string($connection, $reply);
		   
		   $subject = $_POST['subject'];
		   $subject = filter_var($subject, FILTER_SANITIZE_STRING);
		   $subject = mysqli_real_escape_string($connection, $subject);
		   
		   
		   $dept_id = intval($_POST['dept_id']);
		   $dept_id = mysqli_real_escape_string($connection, $dept_id);
		   
		    $sqlinsert = "INSERT INTO `tickets` (`id`, `booking_temp_id`, `subject`, `department`, `last_reply_by`, `user_id`, `status`, `added_at`, `updated_at`) VALUES (NULL, '$temp_id', '$subject', '$dept_id', 'Client', NULL, 'open', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);";
            $sqlinsert = mysqli_query($connection, $sqlinsert); 
		   
		   
		   $resultta = "SELECT * FROM tickets ORDER BY id DESC LIMIT 0,1 ";
           $resultta = mysqli_query($connection, $resultta);
           $resultta = mysqli_fetch_object($resultta);

           $t_id = $resultta->id;
		   
		   $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); 

         

		 
}





?>



<!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">Support Tickets</div>
                                            <div class="panel-body">
						
						
                         
						<a href="tickets.php?status=2"> <button class="btn btn-default btn-md" type="button"> Closed</button> </a>
						 <a href="tickets.php?status=1">  <button class="btn btn-warning btn-md" type="submit"> Active</button> </a>
                            <a href="ticket_add.php">  <button class="btn btn-info btn-md" type="submit"> Add New</button> </a>       


                                    <div class="row">
                        <div class="col-lg-12">
                            <div class="card alert">
                                <div class="bootstrap-data-table-panel">
                                    <div class="table-responsive">
                                           <table class="table table-hover ">
                                            <thead>
                                                <tr>
                                                    <th>Ticket ID</th>
                                                    <th>Client Name</th>
                                                    <th>Subject</th>
													<th>Department</th>
													<th>Status</th>
													<th>Last Update</th>
													<th>Last Update By</th>
                                                </tr>
                                            </thead>
                                            <tbody>
											
											<?php
											$tstatus=intval($_GET["status"]);
											if($tstatus==1){$tstatus="open";} if($tstatus==2){$tstatus="closed";}
											
											 $sqlSelectedLG = "SELECT t1.*, t2.name,t4.name as staff_name, t3.dept_name FROM `tickets` as t1 left join bookings as t2 on t1.booking_temp_id=t2.id left join ticket_departments as t3 on t1.department=t3.id left join users as t4 on t1.user_id=t4.id where t1.booking_temp_id='$temp_id' AND t1.status='$tstatus' order by t1.id desc";
											$sqlSelectedLG = mysqli_query($connection, $sqlSelectedLG);


                               while ($list = mysqli_fetch_object($sqlSelectedLG)) {
                                      $tid = $list->id;
									   $subject = $list->subject;
									  $dept_name = $list->dept_name;
									  $client_name = $list->name;
									  $staff_name = $list->staff_name;
									  
									   $last_reply_by = $list->last_reply_by;
									
									if( $last_reply_by=="Client"){ $last_reply_by=$client_name; $status="Wating for staff reply";  $color="danger"; }
									if( $last_reply_by=="Staff"){ $last_reply_by=$staff_name; $status="Wating for your reply"; $color="success";}
									
									$sts = $list->status; 
									if ($sts=="closed") { $status="Closed";  $color="default"; }
                                    $added_at = $list->added_at;
								    $added_at = date('d/m/Y h:i A', strtotime($added_at));
									
									 $updated_at = $list->updated_at;
									$updated_at = date('d/m/Y h:i A', strtotime($updated_at));
									 
									
                                    
                                   
                                ?>
											
											
											
											
											
                                                <tr>
                                                    <td><?php echo $tid;  ?> </td>
                                                    <td><?php echo $client_name;  ?></td>
                                                    <td><a href="ticket_details.php?id=<?php echo $tid;  ?>"><b><?php echo $subject;  ?></b></a></td>
													 <td><?php echo $dept_name;  ?></td>
													  <td><span class="badge badge-<?php echo $color;  ?>"><?php echo $status;  ?></span></td>
													  <td><?php echo $updated_at;  ?></td>
													   <td><?php echo  $last_reply_by;  ?></td>
													
                                                </tr>
							   <?php } ?>
												
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <!-- /# 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>