<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Reported Issues</title>
  <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500&family=IBM+Plex+Sans:wght@400;500;600&display=swap" rel="stylesheet">
  <link rel="stylesheet" href="/css/report.css">
  <style>
    /* ── Status & Messaging Feature Styles ──────────────────────── */

    /* Status Badge */
    .status-badge {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      padding: 4px 12px;
      border-radius: 20px;
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.72rem;
      font-weight: 500;
      letter-spacing: 0.04em;
      text-transform: uppercase;
    }
    .status-badge::before {
      content: '';
      width: 7px;
      height: 7px;
      border-radius: 50%;
    }
    .status-badge.pending    { background: #fff8e6; color: #b07d00; }
    .status-badge.pending::before { background: #f0a500; }
    .status-badge.reviewing  { background: #e8f0fe; color: #1a56b0; }
    .status-badge.reviewing::before { background: #1a73e8; animation: pulse 1.4s infinite; }
    .status-badge.resolved   { background: #e6f4ea; color: #1a7a3c; }
    .status-badge.resolved::before { background: #1e8a3c; }
    .status-badge.rejected   { background: #fce8e6; color: #c5221f; }
    .status-badge.rejected::before { background: #d93025; }

    @keyframes pulse {
      0%, 100% { opacity: 1; }
      50%       { opacity: 0.3; }
    }

    /* Report Status Panel (shown after submission / on my-reports view) */
    .report-status-panel {
      background: #fff;
      border: 1px solid #e8eaed;
      border-radius: 12px;
      overflow: hidden;
      margin: 16px 16px 0;
      box-shadow: 0 1px 4px rgba(0,0,0,.06);
    }

    .rsp-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 14px 16px;
      border-bottom: 1px solid #f1f3f4;
      background: #fafafa;
    }
    .rsp-title {
      font-family: 'IBM Plex Sans', sans-serif;
      font-weight: 600;
      font-size: 0.9rem;
      color: #202124;
    }
    .rsp-meta {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.72rem;
      color: #80868b;
    }

    /* Progress Steps */
    .status-steps {
      display: flex;
      align-items: center;
      padding: 16px 20px;
      gap: 0;
      border-bottom: 1px solid #f1f3f4;
      overflow-x: auto;
    }
    .step {
      display: flex;
      flex-direction: column;
      align-items: center;
      flex: 1;
      min-width: 60px;
      position: relative;
    }
    .step:not(:last-child)::after {
      content: '';
      position: absolute;
      top: 14px;
      left: 50%;
      width: 100%;
      height: 2px;
      background: #e8eaed;
      z-index: 0;
    }
    .step.done:not(:last-child)::after  { background: #1e8a3c; }
    .step.active:not(:last-child)::after { background: linear-gradient(90deg, #1a73e8 0%, #e8eaed 100%); }
    .step-dot {
      width: 28px;
      height: 28px;
      border-radius: 50%;
      border: 2px solid #e8eaed;
      background: #fff;
      z-index: 1;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 0.75rem;
      transition: all .25s;
    }
    .step.done   .step-dot { border-color: #1e8a3c; background: #1e8a3c; color: #fff; }
    .step.active .step-dot { border-color: #1a73e8; background: #1a73e8; color: #fff; box-shadow: 0 0 0 4px rgba(26,115,232,.15); }
    .step-label {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.65rem;
      color: #80868b;
      margin-top: 6px;
      text-align: center;
      white-space: nowrap;
    }
    .step.done   .step-label { color: #1a7a3c; }
    .step.active .step-label { color: #1a56b0; font-weight: 500; }

    /* ── Messaging Thread ───────────────────────────────────────── */
    .messaging-section {
      padding: 0;
    }
    .msg-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 12px 16px 10px;
      border-bottom: 1px solid #f1f3f4;
    }
    .msg-header-title {
      font-family: 'IBM Plex Sans', sans-serif;
      font-weight: 600;
      font-size: 0.82rem;
      color: #202124;
    }
    .msg-unread-badge {
      background: #1a73e8;
      color: #fff;
      border-radius: 10px;
      font-size: 0.65rem;
      font-family: 'IBM Plex Mono', monospace;
      padding: 2px 7px;
      font-weight: 500;
    }

    .msg-thread {
      max-height: 280px;
      overflow-y: auto;
      padding: 12px 16px;
      display: flex;
      flex-direction: column;
      gap: 10px;
      scroll-behavior: smooth;
    }
    .msg-thread::-webkit-scrollbar { width: 4px; }
    .msg-thread::-webkit-scrollbar-thumb { background: #dadce0; border-radius: 4px; }

    /* Message bubbles */
    .msg-bubble {
      display: flex;
      gap: 8px;
      max-width: 90%;
      animation: msgIn .2s ease;
    }
    @keyframes msgIn {
      from { opacity: 0; transform: translateY(6px); }
      to   { opacity: 1; transform: translateY(0); }
    }
    .msg-bubble.from-admin  { align-self: flex-start; }
    .msg-bubble.from-user   { align-self: flex-end; flex-direction: row-reverse; }

    .msg-avatar {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 0.8rem;
      flex-shrink: 0;
      font-family: 'IBM Plex Sans', sans-serif;
      font-weight: 600;
    }
    .from-admin .msg-avatar { background: #e8f0fe; color: #1a56b0; }
    .from-user  .msg-avatar { background: #fce8e6; color: #c5221f; }

    .msg-content-wrap { display: flex; flex-direction: column; gap: 2px; }
    .from-user  .msg-content-wrap { align-items: flex-end; }

    .msg-sender {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.65rem;
      color: #80868b;
      margin: 0 4px;
    }

    .msg-text {
      padding: 9px 13px;
      border-radius: 14px;
      font-family: 'IBM Plex Sans', sans-serif;
      font-size: 0.84rem;
      line-height: 1.45;
      color: #202124;
    }
    .from-admin .msg-text {
      background: #f1f3f4;
      border-radius: 4px 14px 14px 14px;
    }
    .from-user .msg-text {
      background: #1a73e8;
      color: #fff;
      border-radius: 14px 4px 14px 14px;
    }

    .msg-time {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.6rem;
      color: #9aa0a6;
      margin: 0 4px;
    }

    /* Status change system messages */
    .msg-system {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 6px 10px;
      background: #f8f9fa;
      border-radius: 20px;
      align-self: center;
      max-width: 80%;
    }
    .msg-system span {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.67rem;
      color: #5f6368;
    }
    .msg-system-icon { font-size: 0.8rem; }

    /* Input row */
    .msg-input-row {
      display: flex;
      align-items: flex-end;
      gap: 8px;
      padding: 10px 12px 12px;
      border-top: 1px solid #f1f3f4;
      background: #fafafa;
    }
    .msg-textarea {
      flex: 1;
      border: 1px solid #dadce0;
      border-radius: 20px;
      padding: 9px 14px;
      font-family: 'IBM Plex Sans', sans-serif;
      font-size: 0.84rem;
      resize: none;
      outline: none;
      line-height: 1.4;
      background: #fff;
      max-height: 100px;
      transition: border-color .2s;
      color: #202124;
    }
    .msg-textarea:focus { border-color: #1a73e8; }
    .msg-send-btn {
      width: 38px;
      height: 38px;
      border-radius: 50%;
      background: #1a73e8;
      border: none;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: background .2s, transform .1s;
      flex-shrink: 0;
      color: #fff;
    }
    .msg-send-btn:hover   { background: #1557b0; }
    .msg-send-btn:active  { transform: scale(.92); }
    .msg-send-btn:disabled { background: #dadce0; cursor: not-allowed; }
    .msg-send-btn svg { width: 16px; height: 16px; fill: #fff; }

    /* ── My Reports Tab ─────────────────────────────────────────── */
    .page-tabs {
      display: flex;
      background: #fff;
      border-bottom: 1px solid #e8eaed;
      padding: 0 16px;
      gap: 0;
      position: sticky;
      top: 0;
      z-index: 10;
    }
    .page-tab {
      padding: 14px 18px 12px;
      font-family: 'IBM Plex Sans', sans-serif;
      font-size: 0.84rem;
      font-weight: 500;
      color: #5f6368;
      border: none;
      background: none;
      cursor: pointer;
      border-bottom: 2px solid transparent;
      transition: color .15s, border-color .15s;
      white-space: nowrap;
    }
    .page-tab.active { color: #1a73e8; border-bottom-color: #1a73e8; }

    .tab-pane { display: none; }
    .tab-pane.active { display: block; }

    /* My Reports list */
    .my-reports-list {
      padding: 12px 16px;
      display: flex;
      flex-direction: column;
      gap: 10px;
    }
    .my-report-card {
      border: 1px solid #e8eaed;
      border-radius: 10px;
      padding: 14px;
      background: #fff;
      cursor: pointer;
      transition: box-shadow .2s, border-color .2s;
    }
    .my-report-card:hover { box-shadow: 0 2px 8px rgba(0,0,0,.08); border-color: #c5cace; }
    .mrc-row1 {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 6px;
    }
    .mrc-id {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.72rem;
      color: #80868b;
    }
    .mrc-issues {
      font-family: 'IBM Plex Sans', sans-serif;
      font-size: 0.84rem;
      color: #202124;
      font-weight: 500;
      margin-bottom: 4px;
    }
    .mrc-meta {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.68rem;
      color: #9aa0a6;
      display: flex;
      align-items: center;
      gap: 10px;
    }
    .mrc-unread-dot {
      width: 8px;
      height: 8px;
      background: #1a73e8;
      border-radius: 50%;
      display: inline-block;
    }

    /* Empty state */
    .empty-state {
      text-align: center;
      padding: 48px 24px;
      font-family: 'IBM Plex Sans', sans-serif;
      color: #80868b;
    }
    .empty-state-icon { font-size: 2.4rem; margin-bottom: 12px; }
    .empty-state-title { font-size: 0.95rem; font-weight: 600; color: #202124; margin-bottom: 6px; }
    .empty-state-desc  { font-size: 0.82rem; line-height: 1.5; }

    /* Detail overlay */
    .detail-overlay {
      position: fixed;
      inset: 0;
      background: rgba(0,0,0,.45);
      z-index: 100;
      display: flex;
      align-items: flex-end;
      justify-content: center;
      opacity: 0;
      pointer-events: none;
      transition: opacity .25s;
    }
    .detail-overlay.open { opacity: 1; pointer-events: all; }
    .detail-sheet {
      background: #fff;
      width: 100%;
      max-width: 520px;
      border-radius: 16px 16px 0 0;
      max-height: 90vh;
      overflow-y: auto;
      transform: translateY(30px);
      transition: transform .3s cubic-bezier(.2,.8,.4,1);
    }
    .detail-overlay.open .detail-sheet { transform: translateY(0); }
    .detail-sheet-handle {
      width: 36px; height: 4px;
      background: #dadce0;
      border-radius: 2px;
      margin: 12px auto 0;
    }
    .detail-sheet-close {
      position: absolute;
      top: 12px; right: 16px;
      background: none; border: none;
      font-size: 1.2rem;
      cursor: pointer;
      color: #5f6368;
      line-height: 1;
    }
    .detail-sheet-header {
      padding: 8px 16px 14px;
      border-bottom: 1px solid #f1f3f4;
      position: relative;
    }
    .detail-sheet-title {
      font-family: 'IBM Plex Sans', sans-serif;
      font-size: 1rem;
      font-weight: 600;
      color: #202124;
      margin-bottom: 4px;
    }

    /* Admin-only status controls */
    .admin-controls {
      padding: 12px 16px;
      background: #fffde7;
      border-bottom: 1px solid #fff9c4;
      display: none; /* shown when IS_ADMIN = true */
    }
    .admin-controls.visible { display: block; }
    .admin-controls-label {
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.7rem;
      color: #f57f17;
      text-transform: uppercase;
      letter-spacing: .05em;
      margin-bottom: 8px;
    }
    .status-select-row { display: flex; gap: 8px; flex-wrap: wrap; }
    .status-btn {
      padding: 6px 14px;
      border-radius: 20px;
      border: 1px solid transparent;
      font-family: 'IBM Plex Mono', monospace;
      font-size: 0.7rem;
      cursor: pointer;
      transition: all .15s;
      text-transform: uppercase;
      letter-spacing: .04em;
    }
    .status-btn.s-pending   { background: #fff8e6; color: #b07d00; border-color: #f0a500; }
    .status-btn.s-reviewing { background: #e8f0fe; color: #1a56b0; border-color: #1a73e8; }
    .status-btn.s-resolved  { background: #e6f4ea; color: #1a7a3c; border-color: #1e8a3c; }
    .status-btn.s-rejected  { background: #fce8e6; color: #c5221f; border-color: #d93025; }
    .status-btn:hover { opacity: .8; }
    .status-btn.active { font-weight: 600; box-shadow: 0 0 0 2px currentColor; }
.mrc-label {
    font-weight: 700;
    color: #111;
  }

  .mrc-value-light {
    font-weight: 300;
    color: #888;
  }
.mrc-section {
  margin: 8px 0;
}

.mrc-section-title {
  font-size: 11px;
  font-weight: 700;
  color: #111;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 4px;
}

.mrc-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2px 0;
}

.mrc-row .mrc-label {
  font-weight: 600;
  color: #111;
  font-size: 13px;
}

.mrc-row .mrc-value-light {
  font-weight: 300;
  color: #888;
  font-size: 13px;
  text-align: right;
  max-width: 60%;
}

.mrc-divider {
  border-top: 1px solid #f0f0f0;
  margin: 8px 0;
}

.mrc-people {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
}

.mrc-person {
  flex: 1;
}

.mrc-person-divider {
  width: 1px;
  background: #f0f0f0;
  align-self: stretch;
}

.mrc-phone {
  font-size: 12px;
  margin-top: 2px;
}

.my-report-card {
  width: 100%;
  max-width: 480px;
  box-sizing: border-box;
  padding: 12px 16px;
}

/* Trip rows — label left, value right, wrap on small screens */
.mrc-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px 0;
}

.mrc-row .mrc-label {
  font-weight: 600;
  color: #111;
  font-size: 13px;
  flex-shrink: 0;
}

.mrc-row .mrc-value-light {
  font-weight: 300;
  color: #888;
  font-size: 13px;
  text-align: right;
  word-break: break-word;
  max-width: 60%;
}

/* Rider/Driver side-by-side, stacks on very small screens */
.mrc-people {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  flex-wrap: wrap;
}

.mrc-person {
  flex: 1;
  min-width: 120px; /* stacks when card is too narrow */
}

.mrc-person-divider {
  width: 1px;
  background: #f0f0f0;
  align-self: stretch;
  flex-shrink: 0;
}

/* Hide the vertical divider when people section wraps */
@media (max-width: 280px) {
  .mrc-person-divider {
    display: none;
  }

  .mrc-person {
    width: 100%;
    padding-bottom: 8px;
    border-bottom: 1px solid #f0f0f0;
  }

  .mrc-person:last-child {
    border-bottom: none;
    padding-bottom: 0;
  }
}

.mrc-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  font-size: 12px;
  color: #888;
  margin-top: 6px;
}

.mrc-row1 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
}
.mrc-issue-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 6px;
  padding: 4px 0;
}

.mrc-issue-category {
  font-size: 12px;
  color: #888;
  font-weight: 400;
}

.mrc-issue-tag {
  font-size: 12px;
  font-weight: 400;
  color: #000000;
  background: #D3D3D3;
  padding: 2px 8px;
  border-radius: 12px;
}
.mrc-issue-group {
  margin-bottom: 6px;
}

.mrc-issue-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}
.mrc-trip-link {
  font-size: 13px;
  font-weight: 400;
  color: #1a73e8;
  text-decoration: none;
}

.mrc-trip-link:hover {
  text-decoration: underline;
}
  </style>
</head>
<body>

<%# ── Toast & Loader ─────────────────────────────────────────── %>
<div class="toast" id="toast"></div>
<div class="loader" id="loader"><div class="spinner"></div></div>

<%# ── Header ──────────────────────────────────────────────────── %>
<div class="header">
  <div class="header-inner">
    <div class="header-icon">🚨</div>
    <div>
      <h1>Reported Issues</h1>
    </div>
  </div>
</div>
 

<%# ══════════════════════════════════════════════════════════════ %>
<%# TAB 2 — My Reports                                            %>
<%# ══════════════════════════════════════════════════════════════ %>
<div class="tab-pane active" id="tab-my-reports">
  <div class="my-reports-list" id="myReportsList">
    <%# Rendered dynamically by JS %>
    <div class="empty-state" id="myReportsEmpty" style="display:none">
      <div class="empty-state-icon">📋</div>
      <div class="empty-state-title">No reports yet</div>
      <div class="empty-state-desc">Once you submit a report, you can track its status and chat with our support team here.</div>
    </div>
  </div>
</div><%# /tab-my-reports %>


<%# ══════════════════════════════════════════════════════════════ %>
<%# Report Detail Bottom-Sheet (shared for both tabs)             %>
<%# ══════════════════════════════════════════════════════════════ %>
<div class="detail-overlay" id="detailOverlay" onclick="closeDetail(event)">
  <div class="detail-sheet" id="detailSheet">
    <div class="detail-sheet-handle"></div>
    <button class="detail-sheet-close" onclick="closeDetailBtn()">✕</button>

    <div class="detail-sheet-header">
      <div class="detail-sheet-title" id="ds-title">Report Details</div>
      <div style="display:flex;align-items:center;gap:8px;flex-wrap:wrap;margin-top:6px">
        <span class="status-badge pending" id="ds-status-badge">Pending</span>
        <span class="rsp-meta" id="ds-meta"></span>
      </div>
    </div>

    <%# Status progress steps %>
    <div class="status-steps" id="ds-steps">
      <div class="step done"   id="step-submitted"><div class="step-dot">✓</div><div class="step-label">Submitted</div></div>
      <div class="step"        id="step-reviewing"><div class="step-dot">👁</div><div class="step-label">Reviewing</div></div>
      <div class="step"        id="step-resolved"><div class="step-dot">✓</div><div class="step-label">Resolved</div></div>
    </div>

    <%# Admin controls (visible only for admins) %>
    <div class="admin-controls" id="adminControls">
      <div class="admin-controls-label">⚙ Admin — Change Status</div>
      <div class="status-select-row">
        <button class="status-btn s-pending"   onclick="adminSetStatus('pending')">Pending</button>
        <button class="status-btn s-reviewing" onclick="adminSetStatus('reviewing')">Reviewing</button>
        <button class="status-btn s-resolved"  onclick="adminSetStatus('resolved')">Resolved</button>
        <button class="status-btn s-rejected"  onclick="adminSetStatus('rejected')">Rejected</button>
      </div>
    </div>

    <%# Messaging thread %>
    <div class="report-status-panel" style="margin:0;border-radius:0;box-shadow:none;border-left:none;border-right:none">
      <div class="messaging-section">
        <div class="msg-header">
          <span class="msg-header-title">💬 Messages</span>
          <span class="msg-unread-badge" id="ds-unread-badge" style="display:none">New</span>
        </div>
        <div class="msg-thread" id="msgThread">
          <%# Populated by JS %>
        </div>
        <div class="msg-input-row">
          <textarea
            class="msg-textarea"
            id="msgInput"
            rows="1"
            placeholder="Write a message…"
            oninput="autoResizeTextarea(this)"
            onkeydown="msgKeydown(event)"
          ></textarea>
          <button class="msg-send-btn" id="msgSendBtn" onclick="sendMessage()" disabled>
            <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
              <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z"/>
            </svg>
          </button>
        </div>
      </div>
    </div>

  </div><%# /detail-sheet %>
</div><%# /detail-overlay %>

<script src="/js/report.js"></script>
<script src="/js/report-status.js"></script>
<%# ── Bootstrap data passed from server ──────────────────────── %>
<script>
  const TRIP_ID  = '<%= trip_id %>';
  const REF_ID   = '<%= ref_id %>';
  const REF_TYPE   = '<%= ref_type %>';
  const COLOR    = '<%= color %>'; 
  const IS_ADMIN = <%- is_admin ? 'true' : 'false' %>;

  const CATEGORY_TITLES = <%- JSON.stringify(categories.map(c => c.title)) %>;
   window.onload = function () {
    loadMyReports();
  };
</script>

</body>
</html>
