<?php
/**
 * Professional Printable Invoice
 */

require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';
require_once 'includes/auth.php';

// Check if user is logged in
if (!function_exists('is_logged_in') || !is_logged_in()) {
    redirect(SITE_URL . '/login.php');
    exit;
}

$order_id = isset($_GET['order_id']) ? (int)$_GET['order_id'] : 0;
$user_id = $_SESSION['user_id'];

if (!$order_id) {
    die("Invalid Invoice ID.");
}

// Fetch Order, Product, and User Details
try {
    $stmt = $pdo->prepare("
        SELECT o.*, p.title as product_title, p.version, u.name as user_name, u.email as user_email
        FROM orders o
        JOIN products p ON o.product_id = p.id
        JOIN users u ON o.user_id = u.id
        WHERE o.id = :oid AND o.user_id = :uid AND o.status = 'approved'
        LIMIT 1
    ");
    $stmt->execute([':oid' => $order_id, ':uid' => $user_id]);
    $invoice = $stmt->fetch();

    if (!$invoice) {
        die("Invoice not found or you do not have permission to view it.");
    }
} catch (PDOException $e) {
    die("Database error while fetching invoice.");
}

$settings = get_site_settings($pdo);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice - <?php echo htmlspecialchars($invoice['invoice_no']); ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-blue: #2563eb;
            --text-primary: #1e293b;
            --text-secondary: #475569;
            --border-color: #e2e8f0;
        }
        body {
            font-family: 'Inter', sans-serif;
            background-color: #f4f7f6;
            color: var(--text-primary);
            margin: 0;
            padding: 40px 20px;
        }
        .invoice-container {
            max-width: 800px;
            margin: 0 auto;
            background: #ffffff;
            padding: 50px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
            border: 1px solid var(--border-color);
        }
        .invoice-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            border-bottom: 2px solid var(--border-color);
            padding-bottom: 30px;
            margin-bottom: 30px;
        }
        .brand-name {
            font-size: 2rem;
            font-weight: 800;
            color: var(--primary-blue);
            margin: 0 0 8px 0;
            letter-spacing: -0.5px;
        }
        .invoice-title {
            font-size: 2.5rem;
            font-weight: 800;
            color: var(--text-primary);
            margin: 0;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        .invoice-meta {
            text-align: right;
            color: var(--text-secondary);
            font-size: 0.95rem;
        }
        .invoice-meta strong {
            color: var(--text-primary);
        }
        .billing-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 40px;
        }
        .billing-col h3 {
            font-size: 1rem;
            color: var(--text-secondary);
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 12px;
            border-bottom: 1px solid var(--border-color);
            padding-bottom: 8px;
            display: inline-block;
        }
        .billing-col p {
            margin: 4px 0;
            font-size: 1.05rem;
            font-weight: 500;
        }
        .invoice-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 40px;
        }
        .invoice-table th, .invoice-table td {
            padding: 16px;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }
        .invoice-table th {
            background-color: #f8fafc;
            color: var(--text-secondary);
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.85rem;
            letter-spacing: 0.5px;
        }
        .invoice-table td {
            font-size: 1rem;
        }
        .text-right {
            text-align: right !important;
        }
        .totals-row td {
            font-weight: 600;
            color: var(--text-primary);
        }
        .grand-total {
            font-size: 1.25rem;
            color: var(--primary-blue) !important;
            font-weight: 800 !important;
        }
        .invoice-footer {
            text-align: center;
            border-top: 1px solid var(--border-color);
            padding-top: 30px;
            color: var(--text-secondary);
            font-size: 0.95rem;
        }
        
        /* Print Styles */
        .print-btn {
            display: block;
            width: 200px;
            margin: 0 auto 30px;
            padding: 12px 24px;
            background-color: var(--primary-blue);
            color: white;
            text-align: center;
            text-decoration: none;
            border-radius: 6px;
            font-weight: 600;
            cursor: pointer;
            border: none;
            font-size: 1rem;
            transition: background 0.3s;
        }
        .print-btn:hover {
            background-color: #1d4ed8;
        }

        @media print {
            body { background: white; padding: 0; }
            .invoice-container { box-shadow: none; border: none; padding: 0; max-width: 100%; }
            .print-btn { display: none; }
        }
    </style>
</head>
<body>

    <button onclick="window.print()" class="print-btn">
        <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2" style="vertical-align: middle; margin-right: 6px; margin-top: -2px;">
            <path stroke-linecap="round" stroke-linejoin="round" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
        </svg>
        Print Invoice
    </button>

    <div class="invoice-container">
        
        <div class="invoice-header">
            <div>
                <h1 class="brand-name"><?php echo htmlspecialchars($settings['site_name'] ?? 'DevStore Pro'); ?></h1>
                <p style="margin: 0; color: var(--text-secondary); font-size: 0.95rem;"><?php echo htmlspecialchars($settings['contact_email'] ?? 'support@example.com'); ?></p>
            </div>
            <div class="invoice-meta">
                <h2 class="invoice-title">INVOICE</h2>
                <p style="margin: 8px 0 4px;"><strong>Invoice No:</strong> <?php echo htmlspecialchars($invoice['invoice_no']); ?></p>
                <p style="margin: 0;"><strong>Date:</strong> <?php echo date('F d, Y', strtotime($invoice['approved_at'])); ?></p>
                <p style="margin: 4px 0 0;"><strong>TrxID:</strong> <?php echo htmlspecialchars($invoice['transaction_id']); ?></p>
            </div>
        </div>

        <div class="billing-info">
            <div class="billing-col">
                <h3>Billed To</h3>
                <p><?php echo htmlspecialchars($invoice['user_name']); ?></p>
                <p style="color: var(--text-secondary); font-weight: 400;"><?php echo htmlspecialchars($invoice['user_email']); ?></p>
            </div>
            <div class="billing-col" style="text-align: right;">
                <h3>Payment Status</h3>
                <p style="color: #10b981; font-weight: 700; display: inline-flex; align-items: center; justify-content: flex-end; gap: 6px;">
                    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>
                    PAID / APPROVED
                </p>
            </div>
        </div>

        <table class="invoice-table">
            <thead>
                <tr>
                    <th>Item Description</th>
                    <th>Version</th>
                    <th class="text-right">Amount</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <strong><?php echo htmlspecialchars($invoice['product_title']); ?></strong><br>
                        <span style="font-size: 0.85rem; color: var(--text-secondary);">Digital Source Code License</span>
                    </td>
                    <td>v<?php echo htmlspecialchars($invoice['version']); ?></td>
                    <td class="text-right"><?php echo CURRENCY_SYMBOL . ' ' . number_format($invoice['amount'] + $invoice['discount_amount'], 2); ?></td>
                </tr>
                
                <?php if ($invoice['discount_amount'] > 0): ?>
                <tr class="totals-row">
                    <td colspan="2" class="text-right" style="color: var(--text-secondary);">Discount (Coupon: <?php echo htmlspecialchars($invoice['coupon_used']); ?>)</td>
                    <td class="text-right" style="color: #ef4444;">- <?php echo CURRENCY_SYMBOL . ' ' . number_format($invoice['discount_amount'], 2); ?></td>
                </tr>
                <?php endif; ?>

                <tr class="totals-row">
                    <td colspan="2" class="text-right" style="padding-top: 24px;">Total Paid</td>
                    <td class="text-right grand-total" style="padding-top: 24px; border-top: 2px solid var(--border-color);">
                        <?php echo CURRENCY_SYMBOL . ' ' . number_format($invoice['amount'], 2); ?>
                    </td>
                </tr>
            </tbody>
        </table>

        <div class="invoice-footer">
            <p style="margin: 0 0 8px; font-weight: 600; color: var(--text-primary);">Thank you for your business!</p>
            <p style="margin: 0;">If you have any questions concerning this invoice, please contact support.</p>
        </div>

    </div>

</body>
</html>