<div class="ticket-header">
  <h2 class="mono mb-0"><?= e($ticket['ticket_no']) ?></h2>
  <?= priority_badge(['code' => $priority['code'], 'color' => $priority['color']]) ?>
  <span class="<?= status_badge_class($ticket['status']) ?>"><?= status_label($ticket['status']) ?></span>
</div>
<p class="text-faint" style="margin-bottom:18px"><?= e($ticket['subject']) ?></p>

<div class="ticket-grid" style="grid-template-columns:1fr 300px">
  <div>
    <div class="thread">
      <?php if ($openingMessage) : ?>
        <div class="msg msg-public">
          <div class="msg-head"><span class="msg-author">You</span><span class="msg-time"><?= fmt_dt($openingMessage['created_at']) ?></span></div>
          <div class="msg-body"><?= nl2br(e($openingMessage['body'])) ?></div>
          <?php if (!empty($attachmentsByMsg[$openingMessage['id']])) : ?>
            <div class="msg-attachments"><?php foreach ($attachmentsByMsg[$openingMessage['id']] as $a) : ?><a class="attachment-chip" href="<?= url('/portal/tickets/' . $ticket['id'] . '/attachment/' . $a['id']) ?>">📎 <?= e($a['original_name']) ?></a><?php endforeach; ?></div>
          <?php endif; ?>
        </div>
      <?php endif; ?>
      <?php foreach ($messages as $m) : ?>
        <div class="msg msg-public">
          <div class="msg-head">
            <span class="msg-author"><?= e($m['user_name'] ?? ($m['contact_name'] ?? 'You')) ?><?= $m['user_id'] ? ' <span class="text-faint">(Support)</span>' : '' ?></span>
            <span class="msg-time"><?= fmt_dt($m['created_at']) ?></span>
          </div>
          <div class="msg-body"><?= nl2br(e($m['body'])) ?></div>
          <?php if (!empty($attachmentsByMsg[$m['id']])) : ?>
            <div class="msg-attachments"><?php foreach ($attachmentsByMsg[$m['id']] as $a) : ?><a class="attachment-chip" href="<?= url('/portal/tickets/' . $ticket['id'] . '/attachment/' . $a['id']) ?>">📎 <?= e($a['original_name']) ?></a><?php endforeach; ?></div>
          <?php endif; ?>
        </div>
      <?php endforeach; ?>
    </div>

    <?php if (!in_array($ticket['status'], ['CLOSED', 'REJECTED'])) : ?>
    <div class="reply-box">
      <form method="post" action="<?= url('/portal/tickets/' . $ticket['id'] . '/reply') ?>" enctype="multipart/form-data">
        <?= csrf_field() ?>
        <textarea name="body" rows="4" placeholder="Add a reply…" required></textarea>
        <div class="reply-actions">
          <button type="submit" class="btn btn-primary btn-sm">Send reply</button>
          <label class="btn btn-sm btn-ghost" style="cursor:pointer">
            📎 Attach<input type="file" name="attachments[]" multiple data-file-preview="#att-preview-portal-reply" style="display:none">
          </label>
        </div>
        <div id="att-preview-portal-reply" class="help-text mt-2"></div>
      </form>
    </div>
    <?php endif; ?>

    <?php if ($ticket['status'] === 'RESOLVED') : ?>
      <div class="card mt-3" style="background:var(--green-bg);border-color:#bbf7d0">
        <div class="card-body">
          <strong>We believe this is resolved.</strong>
          <p class="text-sm mt-2 mb-2">Please confirm so we can close the ticket, or reopen it if you're still seeing the issue.</p>
          <div class="flex gap-2">
            <form method="post" action="<?= url('/portal/tickets/' . $ticket['id'] . '/accept') ?>"><?= csrf_field() ?><button class="btn btn-primary btn-sm">Confirm — close ticket</button></form>
            <form method="post" action="<?= url('/portal/tickets/' . $ticket['id'] . '/reopen') ?>"><?= csrf_field() ?><input type="hidden" name="reason" value="Issue still occurring after proposed fix"><button class="btn btn-sm">Still not fixed — reopen</button></form>
          </div>
        </div>
      </div>
    <?php endif; ?>

    <?php if ($ticket['status'] === 'CLOSED' && !$csat) : ?>
      <div class="card mt-3">
        <div class="card-body">
          <strong>How did we do?</strong>
          <form method="post" action="<?= url('/portal/tickets/' . $ticket['id'] . '/csat') ?>" class="mt-2">
            <?= csrf_field() ?>
            <input type="hidden" name="rating" id="csat-rating-input" value="0">
            <div class="csat-stars" data-csat-stars data-csat-input="#csat-rating-input">
              <button type="button">★</button><button type="button">★</button><button type="button">★</button><button type="button">★</button><button type="button">★</button>
            </div>
            <textarea name="comment" rows="2" placeholder="Optional comment" class="mt-2"></textarea>
            <button type="submit" class="btn btn-primary btn-sm mt-2">Submit rating</button>
          </form>
        </div>
      </div>
    <?php elseif ($csat) : ?>
      <div class="card mt-3"><div class="card-body">You rated this ticket <?= str_repeat('★', $csat['rating']) ?>. Thanks for the feedback!</div></div>
    <?php endif; ?>

    <?php if ($ticket['status'] === 'CLOSED') : ?>
      <form method="post" action="<?= url('/portal/tickets/' . $ticket['id'] . '/reopen') ?>" class="mt-3">
        <?= csrf_field() ?>
        <textarea name="reason" rows="2" placeholder="Describe why you need to reopen this…"></textarea>
        <button class="btn btn-sm mt-2">Reopen this ticket</button>
      </form>
    <?php endif; ?>
  </div>

  <div class="side-panel">
    <div class="card">
      <div class="card-header"><h3>Ticket</h3></div>
      <div class="card-body">
        <div class="kv"><span class="k">Priority</span><span class="v"><?= e($priority['name']) ?></span></div>
        <div class="kv"><span class="k">Status</span><span class="v"><?= status_label($ticket['status']) ?></span></div>
        <div class="kv"><span class="k">Promised response</span><span class="v"><?= fmt_dt($ticket['response_due_at']) ?></span></div>
        <div class="kv"><span class="k">Created</span><span class="v"><?= fmt_dt($ticket['created_at']) ?></span></div>
      </div>
    </div>
  </div>
</div>
