<?php
require_once __DIR__.'/../../includes/bootstrap.php'; require_login();
if(session_status()===PHP_SESSION_NONE) session_start();
require_once __DIR__.'/lib.php';
$pageTitle='Communication Centre';
$orderId=(int)($_GET['order_id']??0); $booking=null; $history=[]; $error='';
try { if($orderId>0){ $booking=comm_booking($pdo,$orderId); $s=$pdo->prepare("SELECT * FROM communication_log WHERE order_id=:id ORDER BY created_at DESC,id DESC LIMIT 100");$s->execute([':id'=>$orderId]);$history=$s->fetchAll(PDO::FETCH_ASSOC);} } catch(Throwable $e){$error=$e->getMessage();}
require_once __DIR__.'/../../includes/header.php';
?>
<link rel="stylesheet" href="assets/app.css">
<div class="comm-shell">
  <div class="comm-hero"><div><span class="eyebrow">ROOMZ</span><h2>Communication Centre</h2><p>Send new documents or prepare an Outlook reply attachment.</p></div><a class="btn btn-outline-secondary" href="../calendar/calendar.php">Back to Calendar</a></div>
  <?php if($error): ?><div class="alert alert-danger"><?=comm_h($error)?></div><?php endif; ?>
  <div class="comm-card search-card"><form method="get"><label>Booking ID</label><div class="search-line"><input class="form-control" type="number" name="order_id" min="1" required value="<?=$orderId?:''?>"><button class="btn btn-primary">Load Booking</button></div></form></div>
  <?php if($booking): ?>
  <div class="booking-strip"><div><small>BOOKING</small><strong><?=comm_h($booking['order_no']?:'#'.$booking['id'])?></strong></div><div><small>GUEST</small><strong><?=comm_h($booking['guest_name']?:'Guest')?></strong></div><div><small>STAY</small><strong><?=comm_h($booking['check_in'])?> → <?=comm_h($booking['check_out'])?></strong></div></div>
  <div class="comm-grid">
    <section class="comm-card"><h4>Outlook reply</h4><p>Create a secure one-time handoff for this booking, then attach the PDF from the RoomZ Outlook add-in.</p><button class="btn btn-success w-100" id="outlookBtn" data-order="<?=$orderId?>">Prepare Outlook Reply</button><div id="outlookResult" class="mt-3"></div></section>
    <section class="comm-card"><h4>New email</h4><p>Use the existing SendGrid page when you are starting a new email rather than replying to an existing thread.</p><a class="btn btn-outline-primary w-100" href="../reservations/email_proforma.php?order_id=<?=$orderId?>">Email Proforma</a></section>
  </div>
  <section class="comm-card"><div class="section-head"><h4>Communication history</h4><span><?=count($history)?> entries</span></div><?php if(!$history):?><div class="empty">No communication has been logged for this booking yet.</div><?php else:?><div class="history"><?php foreach($history as $row):?><div class="history-row"><div class="dot"></div><div><strong><?=comm_h(ucwords(str_replace('_',' ',$row['action'])))?></strong><div><?=comm_h(strtoupper($row['channel']))?><?=!empty($row['document_name'])?' • '.comm_h($row['document_name']):''?></div><small><?=comm_h($row['created_at'])?></small></div></div><?php endforeach;?></div><?php endif;?></section>
  <?php endif; ?>
</div>
<script src="assets/app.js"></script>
<?php require_once __DIR__.'/../../includes/footer.php'; ?>