<?php
require_once __DIR__ . '/../app/init.php';
require_once __DIR__ . '/_lib.php';
require_login();
$pdo = db();
$cid = (int)($_SESSION['company_id'] ?? 0);
hotel_ensure_schema($pdo);

$id = (int)($_GET['id'] ?? 0);
$st = $pdo->prepare("SELECT b.*, c.full_name AS customer_name, c.phone AS customer_phone, h.name AS hotel_name, h.city, h.address, r.room_no, r.room_name, r.room_type
                     FROM hotel_bookings b
                     LEFT JOIN customers c ON c.id=b.customer_id AND c.company_id=b.company_id
                     JOIN hotel_hotels h ON h.id=b.hotel_id AND h.company_id=b.company_id
                     LEFT JOIN hotel_rooms r ON r.id=b.room_id AND r.company_id=b.company_id
                     WHERE b.id=? AND b.company_id=? LIMIT 1");
$st->execute([$id, $cid]);
$r = $st->fetch(PDO::FETCH_ASSOC);
if(!$r){ http_response_code(404); exit('Booking not found'); }
?><!doctype html>
<html lang="ar" dir="rtl">
<head>
<meta charset="utf-8">
<title>Voucher <?= h($r['request_no']) ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.rtl.min.css" rel="stylesheet">
<style>
body{background:#f8f9fb;font-family:Tahoma,Arial,sans-serif}
.sheet{max-width:900px;margin:24px auto;background:#fff;padding:28px;border-radius:18px;box-shadow:0 8px 25px rgba(15,23,42,.08)}
.label{color:#64748b;font-size:13px}
.value{font-weight:700}
@media print{body{background:#fff}.sheet{box-shadow:none;border-radius:0;margin:0;max-width:none}}
</style>
</head>
<body>
<div class="sheet">
  <div class="d-flex justify-content-between align-items-center mb-4">
    <div>
      <div class="fs-3 fw-bold">فاوتشر حجز فندقي</div>
      <div class="text-secondary">رقم الطلب: <?= h($r['request_no']) ?></div>
    </div>
    <button class="btn btn-dark d-print-none" onclick="window.print()">طباعة</button>
  </div>

  <div class="row g-3">
    <div class="col-md-6"><div class="label">العميل</div><div class="value"><?= h($r['customer_name'] ?: '-') ?></div></div>
    <div class="col-md-6"><div class="label">الهاتف</div><div class="value"><?= h($r['customer_phone'] ?: '-') ?></div></div>
    <div class="col-md-6"><div class="label">الفندق</div><div class="value"><?= h($r['hotel_name']) ?></div></div>
    <div class="col-md-6"><div class="label">المدينة</div><div class="value"><?= h($r['city'] ?: '-') ?></div></div>
    <div class="col-md-6"><div class="label">الغرفة</div><div class="value"><?= h(trim(($r['room_no'] ?: '').' '.($r['room_name'] ?: ''))) ?></div></div>
    <div class="col-md-6"><div class="label">نوع الغرفة</div><div class="value"><?= h($r['room_type'] ?: '-') ?></div></div>
    <div class="col-md-4"><div class="label">تاريخ الدخول</div><div class="value"><?= h($r['checkin_date']) ?></div></div>
    <div class="col-md-4"><div class="label">تاريخ الخروج</div><div class="value"><?= h($r['checkout_date']) ?></div></div>
    <div class="col-md-4"><div class="label">عدد الليالي</div><div class="value"><?= (int)$r['nights'] ?></div></div>
    <div class="col-md-4"><div class="label">بالغين</div><div class="value"><?= (int)$r['adults_count'] ?></div></div>
    <div class="col-md-4"><div class="label">أطفال</div><div class="value"><?= (int)$r['children_count'] ?></div></div>
    <div class="col-md-4"><div class="label">نوع السداد</div><div class="value"><?= ($r['payment_type'] ?? 'CREDIT') === 'CASH' ? 'نقدي' : 'آجل' ?></div></div>
    <div class="col-md-6"><div class="label">إجمالي البيع</div><div class="value"><?= nf($r['total_sale']) ?></div></div>
    <div class="col-md-6"><div class="label">الحالة</div><div class="value"><?= h(hotel_status_label($r['status'])) ?></div></div>
    <div class="col-12"><div class="label">ملاحظات الفاوتشر</div><div class="value"><?= nl2br(h($r['voucher_notes'] ?: '-')) ?></div></div>
    <div class="col-12"><div class="label">ملاحظات داخلية</div><div class="value"><?= nl2br(h($r['notes'] ?: '-')) ?></div></div>
  </div>
</div>
</body>
</html>
