<?php
declare(strict_types=1);
session_start();
$isAdmin = isset($_SESSION['role']) && $_SESSION['role'] === 'Admin' && isset($_SESSION['user_id']) && (int)$_SESSION['user_id'] > 0;
$reqId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($isAdmin && $reqId > 0) {
  header('Location: admin-order-view.php?id='.$reqId);
  exit;
}
$dbHost = getenv('DB_HOST') ?: 'localhost';
$dbUser = getenv('DB_USER') ?: 'root';
$dbPass = getenv('DB_PASSWORD') ?: '';
$dbName = 'listbnb';
try {
  $pdo = new PDO("mysql:host={$dbHost};dbname={$dbName};charset=utf8mb4", $dbUser, $dbPass, [
    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC,
  ]);
} catch (Throwable $e) {
  echo "<!doctype html><html><head><meta charset='utf-8'></head><body><script>alert('Database connection failed');window.location.href='index-2.php';</script></body></html>";
  exit;
}
$id = $reqId;
if ($id <= 0) { echo "<!doctype html><html><head><meta charset='utf-8'></head><body><script>alert('Invalid order');window.location.href='index-2.php';</script></body></html>"; exit; }
$stmt = $pdo->prepare('SELECT * FROM order_requests WHERE id = :id');
$stmt->execute([':id'=>$id]);
$order = $stmt->fetch();
if (!$order) { echo "<!doctype html><html><head><meta charset='utf-8'></head><body><script>alert('Order not found');window.location.href='index-2.php';</script></body></html>"; exit; }
$items = [];
if (!empty($order['items_json'])) { $items = json_decode((string)$order['items_json'], true) ?: []; }
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Order Request - ListBnb</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="assets/css/bootstrap.css">
  <link rel="stylesheet" href="assets/css/main.css">
  <link rel="stylesheet" href="assets/css/font-awesome-pro.css">
  <style>
    :root{--primary:#4f46e5;--primary-light:#eef2ff;--border:#e5e7eb;--text:#374151;--text-light:#6b7280}
    .hero{background:linear-gradient(135deg,#4f46e5 0%,#4338ca 100%);padding:16px 0}
    .hero-wrap{display:flex;align-items:center;gap:14px}
    .hero-title{color:#fff;font-size:28px;font-weight:800;margin:0}
    .badge{display:inline-block;padding:6px 10px;border-radius:999px;font-size:12px;border:1px solid rgba(255,255,255,.5);color:#fff}
    .status-pending{background:rgba(255,255,255,.15)}
    .status-approved{background:rgba(16,185,129,.25)}
    .status-rejected{background:rgba(239,68,68,.25)}
    .container-narrow{max-width:960px;margin:0 auto}
    .grid-2{display:grid;grid-template-columns:1fr 1fr;gap:20px}
    @media(max-width:991px){.grid-2{grid-template-columns:1fr}}
    .card{background:#fff;border:1px solid var(--border);border-radius:16px;box-shadow:0 4px 10px rgba(0,0,0,.05);margin-bottom:20px}
    .card .box{padding:18px}
    .muted{color:var(--text-light);font-size:13px}
    .item{display:grid;grid-template-columns:56px 1fr auto;gap:12px;align-items:center}
    .thumb{width:56px;height:56px;border-radius:10px;object-fit:cover;border:1px solid var(--border)}
    .item-card{border:1px solid var(--border);border-radius:14px;padding:12px;background:#fff;box-shadow:0 2px 8px rgba(0,0,0,.04)}
    .item-title{font-weight:600;color:var(--text)}
    .item-meta{display:flex;gap:10px;flex-wrap:wrap;margin-top:6px}
    .meta-badge{display:inline-flex;align-items:center;gap:6px;padding:6px 10px;border:1px solid var(--border);border-radius:999px;background:#f8fafc;color:var(--text-light);font-size:12px}
    .price-col{min-width:110px;text-align:right;font-weight:700;color:var(--text)}
    .payshot{max-width:240px;border:1px solid var(--border);border-radius:12px}
    .details-list{display:grid;grid-template-columns:140px 1fr;gap:8px 12px}
    .details-row{display:flex;flex-wrap:wrap;gap:12px}
    .details-pair{display:flex;align-items:center;gap:6px;padding:8px 12px;border:1px solid var(--border);border-radius:999px;background:#fff}
    .details-label{color:var(--text-light);font-weight:600}
    .details-value{color:var(--text);font-weight:600}
    .btn-hero{display:inline-flex;align-items:center;gap:8px;padding:8px 12px;border:1px solid rgba(255,255,255,.6);border-radius:12px;color:#fff;text-decoration:none;background:transparent}
    .timeline{display:flex;gap:12px;align-items:center;margin-top:12px}
    .tl-step{display:flex;align-items:center;gap:8px}
    .tl-dot{width:10px;height:10px;border-radius:999px;background:rgba(255,255,255,.6)}
    .tl-label{color:#fff;font-size:12px}
    .tl-active .tl-dot{background:#fff}
    .tl-sep{width:24px;height:2px;background:rgba(255,255,255,.5)}
    .actions{display:flex;gap:8px;margin-top:12px}
    .btn-print{display:inline-flex;align-items:center;gap:8px;padding:8px 12px;border:1px solid var(--border);border-radius:12px;background:#fff;color:var(--text);text-decoration:none}
    .meta{display:flex;flex-wrap:wrap;gap:12px;margin-top:12px}
    .chip{display:inline-flex;align-items:center;gap:8px;padding:8px 12px;border:1px solid var(--border);border-radius:999px;background:#fff;color:var(--text)}
    .chip i{color:#6b7280}
    .totals{margin-top:12px;border-top:1px dashed var(--border);padding-top:12px}
    .totals-grid{display:grid;grid-template-columns:1fr auto;gap:8px 12px}
    .totals-label{color:var(--text-light)}
    .badge-small{display:inline-block;padding:4px 8px;border-radius:999px;border:1px solid var(--border);font-size:12px;color:var(--text-light)}
    .card-head{display:flex;align-items:center;justify-content:space-between;padding:16px;border-bottom:1px solid var(--border)}
    .card-title{display:flex;align-items:center;gap:8px;font-weight:700;color:var(--text)}
    .card .content{padding:16px}
    .btn-outline{display:inline-flex;align-items:center;gap:8px;padding:8px 12px;border:1px solid var(--border);border-radius:12px;background:#fff;color:var(--text);text-decoration:none}
    .card-head .actions{display:flex;gap:8px;margin-top:0}
  </style>
</head>
<body>
  <main>
    <section class="hero">
      <div class="container container-narrow hero-wrap">
        <a href="index-2.php" class="btn-hero"><i class="fa fa-arrow-left"></i><span>Back</span></a>
        <h1 class="hero-title">Order Request</h1>
        <?php $st = strtolower((string)($order['status'] ?? 'pending')); ?>
        <span class="badge status-<?php echo htmlspecialchars($st, ENT_QUOTES); ?>" style="margin-left:auto">Status: <?php echo htmlspecialchars($st, ENT_QUOTES); ?></span>
      </div>
      <div class="container container-narrow">
        <div class="meta">
          <span class="chip"><i class="fa-regular fa-receipt"></i><span>#<?php echo (int)$order['id']; ?></span></span>
          <?php if (!empty($order['created_at'])): ?><span class="chip"><i class="fa-regular fa-calendar"></i><span><?php echo htmlspecialchars((string)$order['created_at'], ENT_QUOTES); ?></span></span><?php endif; ?>
          <?php if (!empty($order['payment_method'])): ?><span class="chip"><i class="fa-regular fa-credit-card"></i><span><?php echo htmlspecialchars((string)$order['payment_method'], ENT_QUOTES); ?></span></span><?php endif; ?>
          <?php if (!empty($order['shipping_method'])): ?><span class="chip"><i class="fa-regular fa-truck"></i><span><?php echo htmlspecialchars((string)$order['shipping_method'], ENT_QUOTES); ?></span></span><?php endif; ?>
          <?php if ($items): ?><span class="chip"><i class="fa-regular fa-bag-shopping"></i><span><?php echo count($items); ?> items</span></span><?php endif; ?>
        </div>
        <div class="timeline">
          <?php $isPending = ($st==='pending'); $isApproved = ($st==='approved'); $isRejected = ($st==='rejected'); ?>
          <div class="tl-step <?php echo $isPending?'tl-active':''; ?>"><span class="tl-dot"></span><span class="tl-label">Submitted</span></div>
          <div class="tl-sep"></div>
          <div class="tl-step <?php echo ($isApproved||$isRejected)?'tl-active':''; ?>"><span class="tl-dot"></span><span class="tl-label">Reviewed</span></div>
          <div class="tl-sep"></div>
          <div class="tl-step <?php echo $isApproved?'tl-active':''; ?>"><span class="tl-dot"></span><span class="tl-label">Approved</span></div>
          <div class="tl-sep"></div>
          <div class="tl-step <?php echo $isRejected?'tl-active':''; ?>"><span class="tl-dot"></span><span class="tl-label">Rejected</span></div>
        </div>
      </div>
    </section>
    <section class="pb-120 pt-20">
      <div class="container container-narrow">
        <div class="grid-2">
          <div>
            <div class="card mb-20">
              <div class="card-head"><div class="card-title"><i class="fa-regular fa-clipboard-list"></i><span>Summary</span></div><div class="actions"><a href="#" class="btn-outline" onclick="window.print();return false;"><i class="fa-regular fa-print"></i><span>Print Receipt</span></a></div></div>
              <div class="content">
              <div class="details-row mt-12">
                <div class="details-pair"><span class="details-label">Order</span><span class="details-value">#<?php echo (int)$order['id']; ?></span></div>
                <div class="details-pair"><span class="details-label">Payment</span><span class="details-value"><?php echo htmlspecialchars((string)$order['payment_method'], ENT_QUOTES); ?></span></div>
                <?php if (!empty($order['shipping_method'])): ?><div class="details-pair"><span class="details-label">Shipping</span><span class="details-value"><?php echo htmlspecialchars((string)$order['shipping_method'], ENT_QUOTES); ?></span></div><?php endif; ?>
                <div class="details-pair"><span class="details-label">Total</span><span class="details-value">Rs <?php echo number_format((float)$order['total'],2); ?></span></div>
                <div class="details-pair"><span class="details-label">Shipping Fee</span><span class="details-value">Rs <?php echo number_format((float)$order['shipping_charge'],2); ?></span></div>
                <div class="details-pair"><span class="details-label">Grand</span><span class="details-value">Rs <?php echo number_format((float)$order['grand_total'],2); ?></span></div>
              </div>
              <?php if (!empty($order['payment_image_path'])): ?>
                <div class="mt-12"><img class="payshot" src="<?php echo htmlspecialchars((string)$order['payment_image_path'], ENT_QUOTES); ?>" alt="payment"></div>
              <?php endif; ?>
              <div class="totals">
                <div class="totals-grid">
                  <?php if (!empty($order['shipping_method'])): ?><div class="totals-label">Shipping Method</div><div><?php echo htmlspecialchars((string)$order['shipping_method'], ENT_QUOTES); ?></div><?php endif; ?>
                  <?php if (!empty($order['discount_code'])): ?><div class="totals-label">Discount</div><div><span class="meta-badge"><i class="fa-regular fa-ticket"></i><span><?php echo htmlspecialchars((string)$order['discount_code'], ENT_QUOTES); ?></span></span><span class="meta-badge"><i class="fa-regular fa-percent"></i><span>Rs <?php echo number_format((float)$order['discount_amount'],2); ?></span></span></div><?php endif; ?>
                </div>
              </div>
              </div>
            </div>
            <div class="card mb-20">
              <div class="card-head"><div class="card-title"><i class="fa-regular fa-envelope"></i><span>Contact</span></div></div>
              <div class="details-row mt-12">
                <div class="details-pair"><span class="details-label">Email</span><span class="details-value"><?php echo htmlspecialchars((string)$order['contact'], ENT_QUOTES); ?></span></div>
              </div>
            </div>
            <div class="card mb-20">
              <div class="card-head"><div class="card-title"><i class="fa-regular fa-map-location-dot"></i><span>Shipping Address</span></div></div>
              <div class="details-row mt-12">
                <div class="details-pair"><span class="details-label">Name</span><span class="details-value"><?php echo htmlspecialchars((string)$order['first_name'].' '.$order['last_name'], ENT_QUOTES); ?></span></div>
                <div class="details-pair"><span class="details-label">Address</span><span class="details-value"><?php echo htmlspecialchars((string)$order['address'], ENT_QUOTES); ?></span></div>
                <?php if (!empty($order['apartment'])): ?><div class="details-pair"><span class="details-label">Apartment</span><span class="details-value"><?php echo htmlspecialchars((string)$order['apartment'], ENT_QUOTES); ?></span></div><?php endif; ?>
                <div class="details-pair"><span class="details-label">City / Postal</span><span class="details-value"><?php echo htmlspecialchars((string)$order['city'].' '.$order['postal_code'], ENT_QUOTES); ?></span></div>
                <div class="details-pair"><span class="details-label">Area</span><span class="details-value"><?php echo htmlspecialchars((string)$order['area'], ENT_QUOTES); ?></span></div>
                <div class="details-pair"><span class="details-label">Phone</span><span class="details-value"><?php echo htmlspecialchars((string)$order['phone'], ENT_QUOTES); ?></span></div>
              </div>
            </div>
          </div>
          <div>
            <div class="card">
              <div class="card-head"><div class="card-title"><i class="fa-regular fa-bag-shopping"></i><span>Items</span></div></div>
              <?php if (!$items): ?>
                <div class="muted">No items listed</div>
              <?php else: foreach ($items as $it): ?>
                <div class="item-card mb-12">
                  <div class="item">
                    <img class="thumb" src="<?php echo htmlspecialchars((string)($it['image1'] ?? 'assets/img/listing/listing-1-1.png'), ENT_QUOTES); ?>" alt="thumb">
                    <div>
                      <div class="item-title"><?php echo htmlspecialchars((string)($it['product_title'] ?? ''), ENT_QUOTES); ?></div>
                      <div class="item-meta">
                        <?php if (!empty($it['size'])): ?><span class="meta-badge"><i class="fa-regular fa-ruler"></i><span><?php echo htmlspecialchars((string)$it['size'], ENT_QUOTES); ?></span></span><?php endif; ?>
                        <?php if (!empty($it['brand'])): ?><span class="meta-badge"><i class="fa-regular fa-tags"></i><span><?php echo htmlspecialchars((string)$it['brand'], ENT_QUOTES); ?></span></span><?php endif; ?>
                        <?php if (!empty($it['color'])): ?><span class="meta-badge"><i class="fa-regular fa-palette"></i><span><?php echo htmlspecialchars((string)$it['color'], ENT_QUOTES); ?></span></span><?php endif; ?>
                        <?php if (!empty($it['quantity'])): ?><span class="meta-badge"><i class="fa-regular fa-hashtag"></i><span>Qty: <?php echo (int)$it['quantity']; ?></span></span><?php endif; ?>
                      </div>
                    </div>
                    <div class="price-col">Rs <?php echo number_format((float)($it['price'] ?? 0), 2); ?></div>
                  </div>
                </div>
              <?php endforeach; endif; ?>
            </div>
          </div>
        </div>
      </div>
    </section>
  </main>
</body>
</html>
