<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
$tracking = isset($tracking) && is_array($tracking) ? $tracking : ['order' => [], 'details' => []];
$order = isset($tracking['order']) && is_array($tracking['order']) ? $tracking['order'] : [];
$details = isset($tracking['details']) && is_array($tracking['details']) ? $tracking['details'] : [];
$payment = isset($order['payment']) && is_array($order['payment']) ? $order['payment'] : [];

if (!function_exists('trk_rp')) {
    function trk_rp($number)
    {
        return 'Rp ' . number_format((int) $number, 0, ',', '.');
    }
}

$statusAngka = isset($order['status_angka']) ? (int) $order['status_angka'] : 0;
$statusMeta = job_status_meta($statusAngka);
$timelineRank = job_status_rank($statusAngka);
$finalStepLabel = $statusAngka === 5 ? 'Dikirim' : 'Diambil';
$stepLabels = [];
foreach (job_statuses(true) as $configuredStatus) {
    $code = (int)$configuredStatus['code'];
    if ($code !== 0 && $code !== 4 && $code !== 5) $stepLabels[$code] = $configuredStatus['label'];
}
$finalCode = $statusAngka === 5 ? 5 : 4;
$finalMeta = job_status_meta($finalCode);
if (!empty($finalMeta['active'])) $stepLabels[$finalCode] = $finalStepLabel;
$stepCodes = array_keys($stepLabels);
$currentStepPosition = array_search($statusAngka, $stepCodes, true);
$currentStepPosition = $currentStepPosition === false ? 0 : $currentStepPosition + 1;
$progressPercent = $stepCodes ? min(100, (int)round(($currentStepPosition / count($stepCodes)) * 100)) : 0;
$proofGroups = [];
$proofCount = 0;
foreach ($details as $detailIndex => $detail) {
    $proofs = isset($detail['bukti_progres']) && is_array($detail['bukti_progres']) ? $detail['bukti_progres'] : [];
    if (!$proofs) continue;
    $proofGroups[] = [
        'item' => (string)($detail['nama_produk'] ?? ('Item '.($detailIndex + 1))),
        'proofs' => $proofs,
    ];
    $proofCount += count($proofs);
}
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') ?></title>
<style>
  :root{
    --bg:#f4f7fb;
    --card:#ffffff;
    --line:#dbe4f0;
    --text:#122033;
    --muted:#5f7189;
    --brand:#1e4ed8;
    --baru:#667085;
    --desain:#7c3aed;
    --cetak:#2563eb;
    --selesai:#16a34a;
    --diambil:#d97706;
    --dikirim:#ea580c;
  }
  *{box-sizing:border-box}
  body{
    margin:0;
    font-family:Segoe UI,Arial,sans-serif;
    color:var(--text);
    background:
      radial-gradient(circle at top left, rgba(37,99,235,.12), transparent 30%),
      linear-gradient(180deg,#f8fbff 0%, var(--bg) 100%);
  }
  .wrap{
    max-width:1180px;
    margin:0 auto;
    padding:22px 16px 40px;
  }
  .hero{
    background:linear-gradient(135deg,#0f172a 0%, #1d4ed8 100%);
    color:#fff;
    border-radius:18px;
    padding:28px;
    box-shadow:0 20px 40px rgba(15,23,42,.16);
  }
  .hero-top{
    display:flex;
    flex-wrap:wrap;
    gap:12px;
    justify-content:space-between;
    align-items:flex-start;
  }
  .hero-actions{display:flex;flex-direction:column;align-items:flex-end;gap:10px}
  .refresh-button{appearance:none;border:1px solid rgba(255,255,255,.28);background:rgba(255,255,255,.1);color:#fff;border-radius:12px;padding:9px 12px;font-weight:700;cursor:pointer;transition:.2s ease}
  .refresh-button:hover{background:rgba(255,255,255,.2);transform:translateY(-1px)}
  .updated-at{font-size:12px;color:rgba(255,255,255,.72)}
  .eyebrow{
    font-size:12px;
    letter-spacing:.14em;
    text-transform:uppercase;
    opacity:.82;
    margin-bottom:8px;
  }
  h1{
    margin:0;
    font-size:clamp(26px,4vw,40px);
    line-height:1.1;
  }
  .hero-sub{
    margin-top:8px;
    opacity:.9;
    font-size:15px;
  }
  .badge{
    display:inline-flex;
    align-items:center;
    gap:8px;
    border-radius:999px;
    padding:10px 14px;
    font-weight:700;
    background:rgba(255,255,255,.14);
    border:1px solid rgba(255,255,255,.18);
  }
  .badge-dot{
    width:10px;
    height:10px;
    border-radius:999px;
    background:#fff;
  }
  .summary{
    display:grid;
    grid-template-columns:repeat(auto-fit, minmax(220px, 1fr));
    gap:14px;
    margin-top:18px;
  }
  .card{
    background:var(--card);
    border:1px solid var(--line);
    border-radius:16px;
    padding:18px;
    box-shadow:0 8px 22px rgba(17,24,39,.05);
    transition:transform .2s ease,box-shadow .2s ease,border-color .2s ease;
  }
  .card:hover{transform:translateY(-2px);border-color:#c8d8f2;box-shadow:0 14px 28px rgba(17,24,39,.08)}
  .label{
    color:var(--muted);
    font-size:12px;
    letter-spacing:.08em;
    text-transform:uppercase;
    margin-bottom:6px;
  }
  .value{
    font-size:22px;
    font-weight:700;
    line-height:1.2;
  }
  .subvalue{
    margin-top:8px;
    color:var(--muted);
    font-size:14px;
  }
  .section{
    margin-top:18px;
  }
  .progress-panel,.detail-panel{background:rgba(255,255,255,.72);border:1px solid rgba(219,228,240,.9);border-radius:20px;padding:20px;box-shadow:0 12px 34px rgba(30,64,175,.06);backdrop-filter:blur(10px)}
  .section-heading-row{display:flex;align-items:flex-end;justify-content:space-between;gap:16px;margin-bottom:14px}
  .section-heading-row h2{margin:0}
  .progress-caption{color:var(--muted);font-size:13px;font-weight:600}
  .progress-track{height:8px;background:#e6edf7;border-radius:999px;overflow:hidden;margin-bottom:18px}
  .progress-fill{height:100%;border-radius:inherit;background:linear-gradient(90deg,#2563eb,<?=htmlspecialchars($statusMeta['color'], ENT_QUOTES, 'UTF-8');?>);transition:width .5s ease}
  .section h2{
    margin:0 0 12px;
    font-size:20px;
  }
  .timeline{
    display:none;
  }
  .timeline::before{
    display:none;
  }
  .timeline::after{
    display:none;
  }
  .step{
    position:relative;
    background:#fff;
    border:1px solid var(--line);
    border-radius:18px;
    padding:24px 18px 18px;
    text-align:left;
    overflow:hidden;
    transition:transform .22s ease, box-shadow .22s ease, border-color .22s ease, background .22s ease;
    min-height:128px;
    display:flex;
    align-items:flex-start;
    justify-content:flex-start;
    flex-direction:column;
    z-index:1;
    box-shadow:0 10px 24px rgba(17,24,39,.05);
  }
  .step:not(:last-child)::after{
    content:"";
    position:absolute;
    top:30px;
    left:calc(100% - 1px);
    width:18px;
    height:3px;
    border-radius:999px;
    background:#dce6f6;
  }
  .step.done:not(:last-child)::after{
    background:linear-gradient(90deg, #3b82f6 0%, #1d4ed8 100%);
  }
  .step:hover{
    transform:translateY(-2px);
    box-shadow:0 12px 24px rgba(30,78,216,.08);
  }
  .step::before{
    content:"";
    position:absolute;
    top:21px;
    left:18px;
    width:16px;
    height:16px;
    border-radius:999px;
    background:#cfd8e3;
    border:4px solid #fff;
    box-shadow:0 0 0 6px rgba(207,216,227,.42);
  }
  .step.active{
    color:#fff;
    border-color:transparent;
  }
  .step.active::before{
    background:#fff;
    box-shadow:0 0 0 6px rgba(255,255,255,.28);
  }
  .step.done{
    border-color:rgba(30,78,216,.18);
    background:linear-gradient(180deg, #f7faff 0%, #eef5ff 100%);
  }
  .step.done::before{
    background:var(--brand);
    box-shadow:0 0 0 6px rgba(30,78,216,.14);
  }
  .step.is-desain.active{ background:var(--desain); }
  .step.is-cetak.active{ background:var(--cetak); }
  .step.is-selesai.active{ background:var(--selesai); }
  .step.is-diambil.active{ background:var(--diambil); }
  .step.is-dikirim.active{ background:var(--dikirim); }
  .step-head{
    width:100%;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:10px;
    margin-bottom:10px;
    padding-left:28px;
  }
  .step-title{
    font-size:16px;
    font-weight:700;
    line-height:1.3;
  }
  .step-state{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    min-width:86px;
    border-radius:999px;
    padding:7px 10px;
    font-size:11px;
    font-weight:700;
    text-transform:uppercase;
    letter-spacing:.05em;
    white-space:nowrap;
    background:#eef4ff;
    color:var(--brand);
  }
  .step-note{
    font-size:13px;
    line-height:1.55;
    color:var(--muted);
    padding-left:28px;
  }
  .step.done .step-state{
    background:#edf4ff;
    color:var(--brand);
  }
  .step.active .step-state{
    background:rgba(255,255,255,.18);
    color:#fff;
  }
  .step.active .step-note{
    color:rgba(255,255,255,.88);
  }
  .step.pending .step-state{
    background:#f3f6fa;
    color:#6b7280;
  }
  .timeline-mobile{
    display:block;
    max-width:760px;
  }
  .timeline-mobile-line{
    position:relative;
    margin-left:15px;
    padding-left:26px;
  }
  .timeline-mobile-line::before{
    content:"";
    position:absolute;
    left:0;
    top:10px;
    bottom:10px;
    width:3px;
    border-radius:999px;
    background:linear-gradient(180deg, #d7e4fb 0%, #c8d7f3 100%);
  }
  .timeline-mobile-item{
    position:relative;
    margin-bottom:14px;
  }
  .timeline-mobile-item:last-child{
    margin-bottom:0;
  }
  .timeline-mobile-dot{
    position:absolute;
    left:-26px;
    top:18px;
    width:16px;
    height:16px;
    border-radius:999px;
    background:#d4dde8;
    border:3px solid #fff;
    box-shadow:0 0 0 4px rgba(207,216,227,.45);
    z-index:1;
  }
  .timeline-mobile-index{position:absolute;left:-34px;top:10px;width:32px;height:32px;border-radius:50%;display:flex;align-items:center;justify-content:center;background:#fff;border:2px solid #d5e0ef;color:#64748b;font-size:12px;font-weight:800;z-index:2;box-shadow:0 5px 14px rgba(15,23,42,.08)}
  .timeline-mobile-item.is-done .timeline-mobile-index{background:#2563eb;border-color:#2563eb;color:#fff}
  .timeline-mobile-item.is-active .timeline-mobile-index{background:var(--active-color,#2563eb);border-color:#fff;color:#fff;box-shadow:0 0 0 5px rgba(37,99,235,.16)}
  .timeline-mobile-card{
    position:relative;
    background:#fff;
    border:1px solid var(--line);
    border-radius:18px;
    padding:15px 16px;
    box-shadow:0 10px 24px rgba(17,24,39,.05);
    transition:transform .22s ease, box-shadow .22s ease, border-color .22s ease;
  }
  .timeline-mobile-card:hover{transform:translateX(3px);box-shadow:0 14px 28px rgba(17,24,39,.09)}
  .timeline-mobile-card:active{
    transform:scale(.985);
  }
  .timeline-mobile-item.is-done .timeline-mobile-card{
    background:linear-gradient(180deg, #f6faff 0%, #eef5ff 100%);
    border-color:#bfd3f8;
  }
  .timeline-mobile-item.is-active .timeline-mobile-card{
    color:#fff;
    border-color:transparent;
    box-shadow:0 18px 30px rgba(30,78,216,.2);
  }
  .timeline-mobile-item.is-active.is-desain .timeline-mobile-card{ background:linear-gradient(135deg, #6d28d9 0%, #8b5cf6 100%); }
  .timeline-mobile-item.is-active.is-cetak .timeline-mobile-card{ background:linear-gradient(135deg, #1d4ed8 0%, #3b82f6 100%); }
  .timeline-mobile-item.is-active.is-selesai .timeline-mobile-card{ background:linear-gradient(135deg, #15803d 0%, #22c55e 100%); }
  .timeline-mobile-item.is-active.is-diambil .timeline-mobile-card{ background:linear-gradient(135deg, #b45309 0%, #f59e0b 100%); }
  .timeline-mobile-item.is-active.is-dikirim .timeline-mobile-card{ background:linear-gradient(135deg, #c2410c 0%, #f97316 100%); }
  .timeline-mobile-item.is-done .timeline-mobile-dot{
    background:var(--brand);
    box-shadow:0 0 0 4px rgba(30,78,216,.16);
  }
  .timeline-mobile-item.is-active .timeline-mobile-dot{
    background:#fff;
    box-shadow:0 0 0 5px rgba(30,78,216,.22);
  }
  .timeline-mobile-head{
    display:flex;
    align-items:flex-start;
    justify-content:space-between;
    gap:12px;
  }
  .timeline-mobile-title{
    margin:0;
    font-size:16px;
    font-weight:700;
  }
  .timeline-mobile-note{
    margin-top:5px;
    font-size:13px;
    color:var(--muted);
  }
  .timeline-mobile-item.is-active .timeline-mobile-note{
    color:rgba(255,255,255,.85);
  }
  .timeline-mobile-state{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    min-width:88px;
    border-radius:999px;
    padding:7px 10px;
    font-size:11px;
    font-weight:700;
    text-transform:uppercase;
    letter-spacing:.05em;
    background:#eef4ff;
    color:var(--brand);
    white-space:nowrap;
  }
  .timeline-mobile-item.is-active .timeline-mobile-state{
    background:rgba(255,255,255,.18);
    color:#fff;
  }
  .timeline-mobile-item.is-pending .timeline-mobile-state{
    background:#f3f6fa;
    color:#6b7280;
  }
  table{
    width:100%;
    border-collapse:collapse;
    background:#fff;
    border-radius:16px;
    overflow:hidden;
    border:1px solid var(--line);
  }
  th,td{
    padding:13px 12px;
    border-bottom:1px solid #ebf0f6;
    text-align:left;
    vertical-align:top;
    font-size:14px;
  }
  th{
    background:#f8fbff;
    color:#4c5f78;
    font-size:12px;
    text-transform:uppercase;
    letter-spacing:.06em;
  }
  tr:last-child td{ border-bottom:0; }
  .pill{
    display:inline-flex;
    align-items:center;
    border-radius:999px;
    padding:7px 11px;
    font-size:12px;
    font-weight:700;
    color:#fff;
    white-space:nowrap;
  }
  .pill.is-baru{ background:var(--baru); }
  .pill.is-desain{ background:var(--desain); }
  .pill.is-cetak{ background:var(--cetak); }
  .pill.is-selesai{ background:var(--selesai); }
  .pill.is-diambil{ background:var(--diambil); }
  .pill.is-dikirim{ background:var(--dikirim); }
  .muted{ color:var(--muted); }
  .meta-grid{
    display:grid;
    grid-template-columns:repeat(auto-fit, minmax(220px, 1fr));
    gap:12px;
  }
  .meta-box{
    background:#fff;
    border:1px solid var(--line);
    border-radius:14px;
    padding:14px;
    transition:transform .2s ease,border-color .2s ease;
  }
  .meta-box:hover{transform:translateY(-2px);border-color:#bfd1ec}
  .proof-group + .proof-group{margin-top:18px;padding-top:18px;border-top:1px solid var(--line)}
  .proof-group-title{margin:0 0 10px;font-size:14px;color:#334155}
  .proof-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(150px,1fr));gap:12px}
  .proof-card{appearance:none;padding:0;border:1px solid var(--line);border-radius:15px;overflow:hidden;background:#fff;text-align:left;cursor:pointer;box-shadow:0 7px 18px rgba(15,23,42,.06);transition:transform .2s ease,box-shadow .2s ease,border-color .2s ease}
  .proof-card:hover,.proof-card:focus{transform:translateY(-3px);box-shadow:0 14px 28px rgba(15,23,42,.12);border-color:#93b4ee;outline:0}
  .proof-image{display:block;width:100%;height:128px;object-fit:cover;background:#edf2f7}
  .proof-copy{display:block;padding:10px 11px}
  .proof-caption{display:block;color:#172033;font-size:13px;font-weight:700;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
  .proof-time{display:block;margin-top:4px;color:var(--muted);font-size:11px}
  .proof-lightbox{position:fixed;inset:0;z-index:9999;display:none;align-items:center;justify-content:center;padding:20px;background:rgba(7,15,31,.88);backdrop-filter:blur(8px)}
  .proof-lightbox.is-open{display:flex}
  .proof-lightbox-dialog{position:relative;width:min(960px,100%);max-height:92vh;display:flex;flex-direction:column;align-items:center}
  .proof-lightbox-image{display:block;max-width:100%;max-height:76vh;object-fit:contain;border-radius:16px;background:#fff;box-shadow:0 30px 70px rgba(0,0,0,.4)}
  .proof-lightbox-caption{margin-top:12px;color:#fff;text-align:center;font-size:14px;line-height:1.5}
  .proof-lightbox-close{position:absolute;right:0;top:-48px;width:38px;height:38px;border:1px solid rgba(255,255,255,.35);border-radius:50%;background:rgba(255,255,255,.14);color:#fff;font-size:24px;cursor:pointer}
  body.proof-lightbox-open{overflow:hidden}
  .foot{
    margin-top:18px;
    text-align:center;
    color:var(--muted);
    font-size:13px;
  }
  @media (max-width: 780px){
    body{
      background:
        radial-gradient(circle at top center, rgba(37,99,235,.1), transparent 34%),
        linear-gradient(180deg,#f8fbff 0%, var(--bg) 100%);
    }
    .wrap{
      padding:12px 12px 28px;
    }
    .hero{
      border-radius:22px;
      padding:18px 16px;
    }
    .hero-top{
      gap:16px;
    }
    .hero-actions{width:100%;align-items:stretch}
    .refresh-button{width:100%}
    .hero-sub{
      line-height:1.5;
      font-size:14px;
    }
    .badge{
      width:100%;
      justify-content:center;
      padding:11px 14px;
    }
    .summary{
      grid-template-columns:1fr;
      gap:12px;
      margin-top:14px;
    }
    .card,
    .meta-box{
      border-radius:18px;
      padding:16px;
    }
    .value{
      font-size:18px;
    }
    .section{
      margin-top:16px;
    }
    .progress-panel,.detail-panel{padding:15px;border-radius:18px}
    .section-heading-row{align-items:flex-start;flex-direction:column;gap:5px}
    .section h2{
      font-size:17px;
      margin-bottom:10px;
    }
    .timeline-mobile{
      max-width:none;
    }
    .meta-grid{
      grid-template-columns:1fr;
      gap:10px;
    }
    .proof-grid{grid-template-columns:repeat(2,minmax(0,1fr));gap:9px}
    .proof-image{height:112px}
    .proof-lightbox{padding:14px}
    table, thead, tbody, th, td, tr{ display:block; }
    thead{ display:none; }
    table{
      background:transparent;
      border:0;
    }
    tr{
      border:1px solid var(--line);
      border-radius:18px;
      background:#fff;
      margin-bottom:12px;
      padding:6px 0;
      box-shadow:0 10px 24px rgba(17,24,39,.05);
    }
    td{
      border:0;
      padding:9px 14px;
    }
    td::before{
      content:attr(data-label);
      display:block;
      font-size:11px;
      text-transform:uppercase;
      letter-spacing:.06em;
      color:var(--muted);
      margin-bottom:4px;
    }
    td[data-label="Status"]{
      padding-top:4px;
    }
    .foot{
      padding:0 6px;
      line-height:1.5;
    }
  }
</style>
</head>
<body>
  <div class="wrap">
    <section class="hero">
      <div class="hero-top">
        <div>
          <div class="eyebrow">Tracking Pesanan</div>
          <h1><?= htmlspecialchars((string) ($order['id_transaksi'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></h1>
          <div class="hero-sub">
            <?= htmlspecialchars((string) ($order['nama_pelanggan'] ?? 'Pelanggan'), ENT_QUOTES, 'UTF-8') ?>
          </div>
        </div>
        <div class="hero-actions">
          <div class="badge" style="background-color:<?=htmlspecialchars($statusMeta['color'], ENT_QUOTES, 'UTF-8');?>">
            <span class="badge-dot"></span>
            <?= htmlspecialchars((string) ($order['status_label'] ?? 'Baru'), ENT_QUOTES, 'UTF-8') ?>
          </div>
          <button type="button" class="refresh-button" onclick="window.location.reload()">&#8635; Perbarui status</button>
          <span class="updated-at">Terakhir diperiksa: <?=date('H:i');?> <?=htmlspecialchars(date_default_timezone_get() === 'Asia/Jakarta' ? 'WIB' : '', ENT_QUOTES, 'UTF-8');?></span>
        </div>
      </div>
    </section>

    <section class="summary">
      
      <div class="card">
        <div class="label">Status Pembayaran</div>
        <div class="value"><?= htmlspecialchars((string) ($payment['payment_label'] ?? 'Belum Lunas'), ENT_QUOTES, 'UTF-8') ?></div>
        <div class="subvalue">Sisa pembayaran: <?= htmlspecialchars(trk_rp($payment['piutang'] ?? 0), ENT_QUOTES, 'UTF-8') ?></div>
      </div>
      <div class="card">
        <div class="label">Tanggal Order</div>
        <div class="value" style="font-size:18px">
          <?= !empty($order['tgl_trx']) ? htmlspecialchars(dtimes($order['tgl_trx'], false, false), ENT_QUOTES, 'UTF-8') : '-' ?>
        </div>
        
      </div>
    </section>

    <section class="section progress-panel">
      <div class="section-heading-row"><h2>Alur Progres</h2><span class="progress-caption">Tahap <?=$currentStepPosition;?> dari <?=count($stepCodes);?> &middot; <?=$progressPercent;?>%</span></div>
      <div class="progress-track" aria-label="Progres pesanan <?=$progressPercent;?> persen"><div class="progress-fill" style="width:<?=$progressPercent;?>%"></div></div>
      <div class="timeline">
        <?php foreach ($stepLabels as $stepCode => $stepLabel): ?>
          <?php
		  $stepRank = job_status_rank($stepCode);
          $stepClass = 'pending';
          $desktopNote = 'Menunggu proses berikutnya';
          $desktopBadge = 'Belum';
          if ($timelineRank > $stepRank) {
              $stepClass = 'done';
              $desktopNote = 'Tahap ini sudah selesai';
              $desktopBadge = 'Selesai';
          } elseif ($statusAngka === (int)$stepCode) {
              $stepClass = 'active';
              $desktopNote = $stepCode === 4
                  ? 'Pesanan sudah diambil atau dikirim'
                  : 'Pesanan sedang berada di tahap ini';
              $desktopBadge = 'Aktif';
          }
          ?>
          <div class="step <?= $stepClass; ?>"<?=$statusAngka === (int)$stepCode ? ' style="background:'.htmlspecialchars($statusMeta['color'], ENT_QUOTES, 'UTF-8').'" aria-current="step"' : '';?>>
            <div class="step-head">
              <div class="step-title"><?= htmlspecialchars($stepLabel, ENT_QUOTES, 'UTF-8') ?></div>
              <span class="step-state"><?= htmlspecialchars($desktopBadge, ENT_QUOTES, 'UTF-8') ?></span>
            </div>
            <div class="step-note"><?= htmlspecialchars($desktopNote, ENT_QUOTES, 'UTF-8') ?></div>
          </div>
        <?php endforeach; ?>
      </div>
      <div class="timeline-mobile">
        <div class="timeline-mobile-line">
          <?php $stepNumber = 0; foreach ($stepLabels as $stepCode => $stepLabel): $stepNumber++; ?>
            <?php
			$stepRank = job_status_rank($stepCode);
            $mobileState = 'is-pending';
            $mobileClass = '';
            $mobileNote = 'Menunggu proses berikutnya';
            $mobileBadge = 'Belum';
            if ($timelineRank > $stepRank) {
                $mobileState = 'is-done';
                $mobileNote = 'Tahap ini sudah selesai';
                $mobileBadge = 'Selesai';
            } elseif ($statusAngka === (int)$stepCode) {
                $mobileState = 'is-active';
                $mobileNote = $stepCode === 4
                    ? 'Pesanan sudah diambil atau dikirim'
                    : 'Pesanan sedang berada di tahap ini';
                $mobileBadge = 'Aktif';
            }
            ?>
            <div class="timeline-mobile-item <?= $mobileState . $mobileClass; ?>" style="--active-color:<?=htmlspecialchars($statusMeta['color'], ENT_QUOTES, 'UTF-8');?>">
              <span class="timeline-mobile-index"><?=$mobileState === 'is-done' ? '&#10003;' : $stepNumber;?></span>
              <div class="timeline-mobile-card"<?=$statusAngka === (int)$stepCode ? ' style="background:'.htmlspecialchars($statusMeta['color'], ENT_QUOTES, 'UTF-8').'" aria-current="step"' : '';?>>
                <div class="timeline-mobile-head">
                  <h3 class="timeline-mobile-title"><?= htmlspecialchars($stepLabel, ENT_QUOTES, 'UTF-8') ?></h3>
                  <span class="timeline-mobile-state"><?= htmlspecialchars($mobileBadge, ENT_QUOTES, 'UTF-8') ?></span>
                </div>
                <div class="timeline-mobile-note"><?= htmlspecialchars($mobileNote, ENT_QUOTES, 'UTF-8') ?></div>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
      </div>
    </section>

	<?php if ($proofCount > 0): ?>
	<section class="section detail-panel proof-panel" aria-labelledby="proof-heading">
	  <div class="section-heading-row"><h2 id="proof-heading">Bukti Progres</h2><span class="progress-caption"><?=$proofCount;?> gambar</span></div>
	  <?php foreach ($proofGroups as $group): ?>
		<div class="proof-group">
		  <h3 class="proof-group-title"><?=htmlspecialchars($group['item'], ENT_QUOTES, 'UTF-8');?></h3>
		  <div class="proof-grid">
			<?php foreach ($group['proofs'] as $proof): ?>
			  <button type="button" class="proof-card" data-proof-url="<?=htmlspecialchars($proof['url'], ENT_QUOTES, 'UTF-8');?>" data-proof-caption="<?=htmlspecialchars($proof['keterangan'], ENT_QUOTES, 'UTF-8');?>" data-proof-time="<?=htmlspecialchars($proof['uploaded_at_label'], ENT_QUOTES, 'UTF-8');?>">
				<img class="proof-image" src="<?=htmlspecialchars($proof['url'], ENT_QUOTES, 'UTF-8');?>" alt="<?=htmlspecialchars($proof['keterangan'], ENT_QUOTES, 'UTF-8');?>" loading="lazy">
				<span class="proof-copy"><span class="proof-caption"><?=htmlspecialchars($proof['keterangan'], ENT_QUOTES, 'UTF-8');?></span><?php if ($proof['uploaded_at_label'] !== ''): ?><span class="proof-time"><?=htmlspecialchars($proof['uploaded_at_label'], ENT_QUOTES, 'UTF-8');?></span><?php endif; ?></span>
			  </button>
			<?php endforeach; ?>
		  </div>
		</div>
	  <?php endforeach; ?>
	</section>
	<?php endif; ?>

    <section class="section detail-panel">
      <div class="section-heading-row"><h2>Detail Pesanan</h2><span class="progress-caption"><?=count($details);?> item pesanan</span></div>
      <div class="meta-grid">
        <div class="meta-box">
          <div class="label">Pelanggan</div>
          <div><strong><?= htmlspecialchars((string) ($order['nama_pelanggan'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></strong></div>
          <div class="muted"><?= htmlspecialchars((string) ($order['no_hp'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></div>
        </div>
        <div class="meta-box">
          <div class="label">Kasir</div>
          <div><strong><?= htmlspecialchars((string) ($order['kasir'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></strong></div>
        </div>
        <div class="meta-box">
          <div class="label">Pembayaran</div>
          <div><strong><?= htmlspecialchars(trk_rp($payment['bayar'] ?? 0), ENT_QUOTES, 'UTF-8') ?></strong> dibayar</div>
          <div class="muted">Total <?= htmlspecialchars(trk_rp($payment['total'] ?? 0), ENT_QUOTES, 'UTF-8') ?></div>
        </div>
      </div>
    </section>

    <section class="section">
      <table>
        <thead>
          <tr>
            <th>Qty</th>
            <th>Produk</th>
            <th>Bahan</th>
            <th>Ukuran</th>
            <th>Keterangan</th>
            <th>Operator</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          <?php if (!empty($details)): ?>
            <?php foreach ($details as $detail): ?>
              <?php
              $itemStatus = isset($detail['effective_status']) ? (int) $detail['effective_status'] : 0;
			  $itemMeta = job_status_meta($itemStatus);
              ?>
              <tr>
                <td data-label="Qty"><?= (int) ($detail['jumlah'] ?? 0) ?></td>
                <td data-label="Produk"><?= htmlspecialchars((string) ($detail['nama_produk'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></td>
                <td data-label="Bahan"><?= htmlspecialchars((string) ($detail['nama_bahan'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></td>
                <td data-label="Ukuran"><?= htmlspecialchars((string) ($detail['ukuran'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></td>
                <td data-label="Keterangan"><?= nl2br(htmlspecialchars(trim((string) ($detail['keterangan'] ?? '-')) !== '' ? (string) $detail['keterangan'] : '-', ENT_QUOTES, 'UTF-8')) ?></td>
                <td data-label="Operator"><?= htmlspecialchars((string) ($detail['operator'] ?? '-'), ENT_QUOTES, 'UTF-8') ?></td>
                <td data-label="Status">
                  <span class="pill" style="background-color:<?=htmlspecialchars($itemMeta['color'], ENT_QUOTES, 'UTF-8');?>;color:#fff"><?= htmlspecialchars((string) ($detail['status_label'] ?? $itemMeta['label']), ENT_QUOTES, 'UTF-8') ?></span>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php else: ?>
            <tr>
              <td colspan="7">Belum ada detail item untuk pesanan ini.</td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </section>

    <div class="foot">Halaman ini menampilkan progres pesanan secara otomatis berdasarkan status pekerjaan di sistem.</div>
  </div>

	<?php if ($proofCount > 0): ?>
	<div class="proof-lightbox" id="proof-lightbox" role="dialog" aria-modal="true" aria-hidden="true" aria-label="Pratinjau bukti progres">
	  <div class="proof-lightbox-dialog">
		<button type="button" class="proof-lightbox-close" aria-label="Tutup pratinjau">&times;</button>
		<img class="proof-lightbox-image" alt="Bukti progres">
		<div class="proof-lightbox-caption"></div>
	  </div>
	</div>
	<script>
	(function(){
	  var lightbox=document.getElementById('proof-lightbox');
	  if(!lightbox)return;
	  var image=lightbox.querySelector('.proof-lightbox-image');
	  var caption=lightbox.querySelector('.proof-lightbox-caption');
	  var lastTrigger=null;
	  function closeProof(){lightbox.classList.remove('is-open');lightbox.setAttribute('aria-hidden','true');document.body.classList.remove('proof-lightbox-open');image.removeAttribute('src');if(lastTrigger)lastTrigger.focus();}
	  document.querySelectorAll('.proof-card').forEach(function(button){button.addEventListener('click',function(){lastTrigger=button;image.src=button.getAttribute('data-proof-url')||'';image.alt=button.getAttribute('data-proof-caption')||'Bukti progres';var text=button.getAttribute('data-proof-caption')||'Bukti progres';var time=button.getAttribute('data-proof-time')||'';caption.textContent=time?text+' · '+time:text;lightbox.classList.add('is-open');lightbox.setAttribute('aria-hidden','false');document.body.classList.add('proof-lightbox-open');lightbox.querySelector('.proof-lightbox-close').focus();});});
	  lightbox.querySelector('.proof-lightbox-close').addEventListener('click',closeProof);
	  lightbox.addEventListener('click',function(event){if(event.target===lightbox)closeProof();});
	  document.addEventListener('keydown',function(event){if(event.key==='Escape'&&lightbox.classList.contains('is-open'))closeProof();});
	})();
	</script>
	<?php endif; ?>
</body>
</html>
