<?php
// Invoice Generation Page
// Include functions to get access to BASE_URL and USER_URL
require_once '../includes/functions.php';

if (!isLoggedIn() || isAdmin()) {
    redirect(BASE_URL . 'login.php');
}

$transaction_id = $_GET['id'] ?? 0;

$db = getDBConnection();
$stmt = $db->prepare("
    SELECT t.*, b.booking_id, b.travel_date, b.num_people, b.final_amount as booking_amount,
           p.title as package_title, p.slug as package_slug,
           u.name as user_name, u.email as user_email, u.phone as user_phone
    FROM transactions t
    JOIN bookings b ON t.booking_id = b.id
    JOIN packages p ON b.package_id = p.id
    JOIN users u ON b.user_id = u.id
    WHERE t.id = ? AND b.user_id = ?
");
$stmt->execute([$transaction_id, $_SESSION['user_id']]);
$invoice = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$invoice) {
    setFlash('danger', 'Invoice not found.');
    redirect(USER_URL . 'transactions.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice - <?= $invoice['transaction_id'] ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&family=Playfair+Display:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Poppins', sans-serif;
            background: #f5f5f5;
            padding: 20px;
        }
        .invoice-container {
            max-width: 800px;
            margin: 0 auto;
            background: #fff;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .invoice-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 40px;
            padding-bottom: 20px;
            border-bottom: 2px solid #f0f0f0;
        }
        .invoice-brand {
            font-family: 'Playfair Display', serif;
            font-size: 1.8rem;
            font-weight: 700;
            color: #004E89;
        }
        .invoice-brand span {
            color: #FF6B35;
        }
        .invoice-title {
            text-align: right;
        }
        .invoice-title h1 {
            font-size: 2rem;
            color: #1F2937;
            margin: 0;
        }
        .invoice-title p {
            color: #6B7280;
            margin: 5px 0 0;
        }
        .invoice-details {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 40px;
            margin-bottom: 40px;
        }
        .invoice-detail-box h3 {
            font-size: 0.85rem;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: #6B7280;
            margin-bottom: 15px;
        }
        .invoice-detail-box p {
            margin: 5px 0;
            color: #1F2937;
        }
        .invoice-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
        }
        .invoice-table th {
            background: #f9fafb;
            padding: 12px;
            text-align: left;
            font-weight: 600;
            color: #6B7280;
            font-size: 0.85rem;
            text-transform: uppercase;
        }
        .invoice-table td {
            padding: 12px;
            border-bottom: 1px solid #f0f0f0;
        }
        .invoice-total {
            text-align: right;
            padding: 20px;
            background: #f9fafb;
            border-radius: 8px;
        }
        .invoice-total .total-label {
            color: #6B7280;
            margin-right: 15px;
        }
        .invoice-total .total-amount {
            font-size: 1.5rem;
            font-weight: 700;
            color: #FF6B35;
        }
        .invoice-footer {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 1px solid #f0f0f0;
            text-align: center;
            color: #9CA3AF;
            font-size: 0.85rem;
        }
        .invoice-actions {
            margin-top: 30px;
            text-align: center;
        }
        .btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            padding: 12px 28px;
            border-radius: 8px;
            font-weight: 600;
            font-size: 0.95rem;
            text-decoration: none;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        .btn-primary {
            background: #FF6B35;
            color: #fff;
            border: none;
        }
        .btn-primary:hover {
            background: #E55A2B;
        }
        .btn-outline {
            background: transparent;
            color: #FF6B35;
            border: 2px solid #FF6B35;
        }
        .btn-outline:hover {
            background: #FF6B35;
            color: #fff;
        }
        @media print {
            body { padding: 0; }
            .invoice-container { box-shadow: none; }
            .invoice-actions { display: none; }
        }
    </style>
</head>
<body>
    <div class="invoice-container">
        <div class="invoice-header">
            <div>
                <div class="invoice-brand">Travel<span>er</span></div>
                <p style="color: #6B7280; margin-top: 10px;">Booking Invoice</p>
            </div>
            <div class="invoice-title">
                <h1>INVOICE</h1>
                <p>#<?= $invoice['transaction_id'] ?></p>
            </div>
        </div>
        
        <div class="invoice-details">
            <div class="invoice-detail-box">
                <h3>Bill To</h3>
                <p><strong><?= sanitize($invoice['user_name']) ?></strong></p>
                <p><?= sanitize($invoice['user_email']) ?></p>
                <p><?= sanitize($invoice['user_phone'] ?? 'N/A') ?></p>
            </div>
            <div class="invoice-detail-box">
                <h3>Invoice Details</h3>
                <p><strong>Booking ID:</strong> <?= $invoice['booking_id'] ?></p>
                <p><strong>Date:</strong> <?= formatDate($invoice['created_at'], 'd M Y') ?></p>
                <p><strong>Travel Date:</strong> <?= formatDate($invoice['travel_date']) ?></p>
                <p><strong>Guests:</strong> <?= $invoice['num_people'] ?></p>
            </div>
        </div>
        
        <table class="invoice-table">
            <thead>
                <tr>
                    <th>Description</th>
                    <th>Package</th>
                    <th>Travel Date</th>
                    <th style="text-align: right;">Amount</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Tour Package Booking</td>
                    <td><?= sanitize($invoice['package_title']) ?></td>
                    <td><?= formatDate($invoice['travel_date']) ?></td>
                    <td style="text-align: right;"><?= formatCurrency($invoice['amount']) ?></td>
                </tr>
            </tbody>
        </table>
        
        <div class="invoice-total">
            <span class="total-label">Total Paid:</span>
            <span class="total-amount"><?= formatCurrency($invoice['amount']) ?></span>
        </div>
        
        <div class="invoice-footer">
            <p>Thank you for booking with Traveler!</p>
            <p>This is a computer-generated invoice and does not require a signature.</p>
        </div>
        
        <div class="invoice-actions">
            <button onclick="window.print()" class="btn btn-primary">
                <i class="fas fa-print"></i> Print
            </button>
            <a href="<?= USER_URL ?>transactions.php" class="btn btn-outline">
                <i class="fas fa-arrow-left"></i> Back to Transactions
            </a>
        </div>
    </div>
</body>
</html>
