<?php
require_once __DIR__ . '/../app/helpers.php';
require_once __DIR__ . '/../app/db.php';
require_once __DIR__ . '/_lib.php';
require_login();

$pdo = db();
$cid = (int)($_SESSION['company_id'] ?? 0);
fleet_ensure_schema($pdo);
fleet_ensure_limo_schema($pdo);

$id = (int)($_GET['id'] ?? 0);
$st = $pdo->prepare("SELECT t.*, s.serial_no, lp.name AS pickup_name, ld.name AS drop_name
  FROM fleet_limo_trips t
  JOIN fleet_limo_serials s ON s.id=t.serial_id
  LEFT JOIN fleet_locations lp ON lp.id=t.pickup_location_id
  LEFT JOIN fleet_locations ld ON ld.id=t.dropoff_location_id
  WHERE t.id=? AND t.company_id=? LIMIT 1");
$st->execute([$id, $cid]);
$r = $st->fetch(PDO::FETCH_ASSOC);

$customerName = '-';
if (!empty($r['customer_id']) && fleet_table_exists($pdo, 'customers')) {
  $nameCol = fleet_col_exists($pdo, 'customers', 'full_name') ? 'full_name' : (fleet_col_exists($pdo, 'customers', 'name') ? 'name' : null);
  if ($nameCol) {
    $s = $pdo->prepare("SELECT {$nameCol} FROM customers WHERE id=? LIMIT 1");
    $s->execute([(int)$r['customer_id']]);
    $customerName = (string)($s->fetchColumn() ?: '-');
  }
}

$vehicleLabel = '#'.(int)($r['vehicle_id'] ?? 0);
if (!empty($r['vehicle_id']) && fleet_table_exists($pdo, 'vehicles')) {
  $cols = [];
  foreach ($pdo->query("SHOW COLUMNS FROM vehicles")->fetchAll(PDO::FETCH_ASSOC) as $c) $cols[] = $c['Field'];
  $selectCols = ['id'];
  foreach (['plate_no_current','plate_no','brand','model'] as $c) { if (in_array($c, $cols, true)) $selectCols[] = $c; }
  $s = $pdo->prepare("SELECT " . implode(',', $selectCols) . " FROM vehicles WHERE id=? LIMIT 1");
  $s->execute([(int)$r['vehicle_id']]);
  $vv = $s->fetch(PDO::FETCH_ASSOC) ?: ['id' => (int)$r['vehicle_id']];
  $parts = [];
  if (!empty($vv['plate_no_current'])) $parts[] = $vv['plate_no_current'];
  elseif (!empty($vv['plate_no'])) $parts[] = $vv['plate_no'];
  if (!empty($vv['brand'])) $parts[] = $vv['brand'];
  if (!empty($vv['model'])) $parts[] = $vv['model'];
  $vehicleLabel = trim(implode(' - ', array_filter($parts))) ?: ('#' . (int)($vv['id'] ?? 0));
}
if (!$r) die('الرحلة غير موجودة');
?><!doctype html>
<html lang="ar" dir="rtl"><head><meta charset="utf-8"><title>طباعة رحلة ليموزين <?= h($r['display_no']) ?></title>
<style>@page{size:A4;margin:14mm}body{font-family:Arial,sans-serif;color:#111}table{width:100%;border-collapse:collapse}th,td{border:1px solid #222;padding:8px}h1{margin:0 0 10px 0} .box{border:1px solid #222;padding:12px;border-radius:8px;margin-bottom:14px}</style></head>
<body>
<h1>تشغيل ليموزين - <?= h($r['display_no']) ?></h1>
<div class="box">
<table>
<tr><th>السريال الرئيسي</th><td><?= h($r['serial_no']) ?></td><th>رقم الرحلة</th><td><?= h($r['display_no']) ?></td></tr>
<tr><th>التاريخ</th><td><?= h($r['trip_date']) ?></td><th>الوقت</th><td><?= h($r['pickup_time'] ?: '-') ?></td></tr>
<tr><th>السيارة</th><td><?= h($vehicleLabel) ?></td><th>العميل</th><td><?= h($customerName) ?></td></tr>
<tr><th>من</th><td><?= h($r['pickup_name'] ?: '-') ?></td><th>إلى</th><td><?= h($r['drop_name'] ?: '-') ?></td></tr>
<tr><th>كم الخروج</th><td><?= h($r['km_out']) ?></td><th>كم الدخول</th><td><?= h($r['km_in'] ?: '-') ?></td></tr>
<tr><th>كم مستخدم</th><td><?= h($r['used_km']) ?></td><th>القيمة</th><td><?= h($r['trip_amount']) ?></td></tr>
<tr><th>السداد</th><td><?= h(($r['payment_type'] ?? 'CASH')==='CASH' ? 'نقدي' : 'أجل') ?></td><th>تاريخ التحصيل</th><td><?= h($r['expected_collection_date'] ?: '-') ?></td></tr>
</table>
</div>
<div class="box"><strong>ملاحظات:</strong><br><?= nl2br(h($r['notes'] ?: '-')) ?></div>
<script>window.print()</script>
</body></html>