<?php require view('theme/header'); ?>

<style>.support-chat .old-tickets .old-ticket {padding-left:0}</style>
<main class="app-main">
   <div class="page-content">
      <div class="container-fluid">
         <div class="row mt-2">
            <div class="col-lg-12">
               <div class="card mb-5">
                  <div class="px-lg-1 py-lg-0 py-3 px-4">
                     <div class="support-chat">
                        <div class="schat-left">
                           <div class="schat-left-header">
                              <h5 class="schlh-title pb-2">Hizmet Saatleri</h5>
                              <h3 class="schlh-title pb-2">10:00 - 20:00</h3>
							  <p>Sizlere yardımcı olmak için buradayız.. Sorununuz çözülürse lütfen yeni bir destek talebi oluşturunuz.</p>
                           </div>
                           <div class="schat-left-body">
                              <div class="old-tickets">
							  <?php foreach($tickets as $ticket) { ?>	
                              		<div class="old-ticket">
									<a href="<?=site("tickets/$ticket[ticket_id]")?>">
										<div class="card mb-3">
											<div class="py-4 px-lg-3 px-4">
												<div class="row">
													<div class="col-lg-3 col-6">
														<h4 class="op-title">Talep No</h4>
														<div id="ticket-id" class="op-id <?php if($ticket['ticket_status'] == 1) {if($ticket['ticket_ans'] == 1){echo "pending";}elseif($ticket['ticket_ans'] == 2){echo "answered";}else{echo "pending";}} elseif($ticket["ticket_status"] == 0){echo "";}elseif($ticket["ticket_status"] == 2){echo "pending";}?>"><?=$ticket["ticket_id"]?></div>
														
													</div>
													<div class="col-lg-5 col-6">
														<h4 class="op-title">
															<span>Konu Başlığı</span>
														</h4>
														<div class="op-date"><?=$ticket["ticket_title"] ?></div>
													</div>
													<div class="col-lg-4 col-12 text-center">
															<div class="op-status op-<?php if($ticket['ticket_status'] == 1) {if($ticket['ticket_ans'] == 1){echo "waiting";}elseif($ticket['ticket_ans'] == 2){echo "answered";}else{echo "waiting";}} elseif($ticket["ticket_status"] == 0){echo "closed";}elseif($ticket["ticket_status"] == 2){echo "waiting";}?>">
															<?php if($ticket['ticket_status'] == 1) {if($ticket['ticket_ans'] == 1){echo "Beklemede";}elseif($ticket['ticket_ans'] == 2){echo "Yanıtlandı";}else{echo "Beklemede";}} elseif($ticket["ticket_status"] == 0){echo "Kapandı";}elseif($ticket["ticket_status"] == 2){echo "Beklemede";}?>
															</div>
													</div>
												</div>
											</div>
										</div>
									</a>
                                </div>
							   <?php } ?>
                              </div>
                           </div>
                        </div>
                        <!-- schat-left -->
                        <div class="schat-body">
                           <div class="schat-body-header">
                              <div class="row align-items-center">
                                 <div class="col-auto d-lg-none">
                                    <a href="/tickets" class="btn btn-dark btn-square">
                                    <i class="fas fa-chevron-left"></i>
                                    </a>
                                 </div>
                                 <div class="col">
                                    <h2 class="schat-title"><i class="fas fa-bookmark"></i> <?=$title?></h2>
									<h2 class="schat-title" id="ticket-id" style="display: none"></h2>
                                 </div>
                              </div>
                           </div>
                           <!-- schat-body-header -->
                           <div class="schat-chat-body">
                              <div class="schat-body-content">
							  
							<?php foreach($messages as $message) {

					$query = $db->prepare("UPDATE tickets SET client_read = 1 WHERE ticket_id = ?");	
					$query->execute([$id]);	if($message["message_author"] == 1) { ?>
                                 <div class="ticket-message-block ticket-message-right">
                                    <div class="ticket-msg-container">
                                       <div class="ticket-msg"><?=$message["message_text"]?></div>
                                       <div class="date-time">{USER_USERNAME} <?=date("d.m.Y H:i:s", $message["message_time"])?></div>
                                    </div>
                                 </div><br>
                                 <!-- msg-end -->
								<?php } else { ?>
                                 <div class="ticket-message-block ticket-message-left">
                                    <div class="ticket-msg-container">
                                       <div class="ticket-msg"><?=htmlspecialchars_decode($message["message_text"])?></div>
                                       <div class="date-time">Yetkili <?=date("d.m.Y H:i:s", $message["message_time"])?></div>
                                    </div>
                                 </div><br>
                                 <!-- msg-end -->
                                <?php } } ?>
                                 
                              </div>
                           </div>
                           
						   <?php $id = route(1);

			$query = $db->prepare("SELECT * FROM `tickets` WHERE ticket_id = ? AND user_id = ?");

			$query->execute([$id, session("user_id")]);

			$ticket = $query->fetch(PDO::FETCH_ASSOC); if($ticket["ticket_status"] == 1) { ?>
						   <div class="schat-input">
							  <form id="send_message" onsubmit="return false;">
                                 <div class="schat-msg-input">
                                    <input type="text" placeholder="Mesajınızı buraya giriniz..." name="message" id="chat-message">
									<input type="hidden" name="id" value="{TICKET_ID}" />
                                    <button class="send-btn" type="submit"><i class="fas fa-paper-plane"></i><span class="ms-3">Yanıtla</span></button>
                                 </div>
                              </form>
                           </div>
						   <?php } else { ?>
						   <div class="schat-input">
                                 <div class="schat-msg-input ">
                                    <button class="send-btn" style="width:100%; margin-left: 10px;">Destek Talebi Kapalı</button>
                                 </div>
                           </div>
						   
						   <?php } ?>
                        </div>
                     </div>
                  </div>
               </div>
               <!-- card -->
            </div>
            <!-- col -->
         </div>
      </div>
   </div>
</main>


		<!--	<script type="text/javascript">
			  let ticketUrl = window.location.href.split('/');
			  let ticketID = ticketUrl[ticketUrl.length - 1];
			  document.querySelector('#ticket-id').innerHTML = '#' + ticketID;
			  window.onload = function(){

				$('[data-toggle="tooltip"]').tooltip(); 
				
				let ajaxUrl = "/tickets";
				
				$.ajax({
				  url: ajaxUrl,
				}).done(function(data){
				  let html = $.parseHTML(data);
				  let msg_container = $(html).find('.old-tickets .old-ticket');
				  $(msg_container).find('.active').removeClass('active');

				  let ticketId = window.location.href.split('/');
				  ticketId = ticketId[ticketId.length - 1 ];
				  
				  $(msg_container).find('a[data-id="'+ ticketId +'"]').addClass('active');
				  $('.old-tickets').append(msg_container);

				});

			  };
			</script>-->


<?php require view('theme/footer'); ?>
