<?php
require_once '../includes/functions.php';
requireLogin();

$db = getDB();
$id = (int)($_GET['id'] ?? 0);
if (!$id) { header('Location: purchases.php'); exit; }

try {
    $stmt = $db->prepare("SELECT * FROM purchases WHERE id = ?");
    $stmt->execute([$id]);
    $purchase = $stmt->fetch();
    if (!$purchase) { header('Location: purchases.php'); exit; }

    $items = $db->prepare("SELECT * FROM purchase_items WHERE purchase_id = ?");
    $items->execute([$id]);
    $lineItems = $items->fetchAll();
} catch (PDOException $e) {
    setFlash('error', 'Failed to load purchase.');
    header('Location: purchases.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Purchase <?= sanitize($purchase['purchase_number']) ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@300;400;500;600;700;800&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <script>
        tailwind.config = {
            theme: { extend: { fontFamily: { sans: ['Plus Jakarta Sans','sans-serif'], mono: ['JetBrains Mono','monospace'] }, colors: { brand: { 600:'#4f56ea', 700:'#4143d0', 950:'#1e1f52' } } } }
        }
    </script>
    <style>
        body { font-family: 'Plus Jakarta Sans', sans-serif; background: #f1f5f9; }
        @media print {
            body { background: white !important; }
            .no-print { display: none !important; }
            .purchase-wrapper { box-shadow: none !important; margin: 0 !important; padding: 0 !important; }
            @page { margin: 15mm; size: A4; }
        }
    </style>
</head>
<body>

<!-- Action Bar -->
<div class="no-print bg-brand-950 text-white px-3 sm:px-6 py-2 sm:py-3 flex flex-col sm:flex-row sm:items-center justify-between gap-3 sticky top-0 z-10">
    <div class="flex items-center gap-4">
        <a href="purchases.php" class="text-blue-300 hover:text-white text-sm"><i class="fas fa-arrow-left mr-2"></i>Back to Purchases</a>
        <span class="text-blue-400 text-xs font-mono"><?= sanitize($purchase['purchase_number']) ?></span>
    </div>
    <div class="flex items-center gap-3">
        <button onclick="window.print()" class="bg-white text-brand-700 px-4 py-1.5 rounded-lg text-sm font-bold transition hover:bg-blue-50">
            <i class="fas fa-print mr-1.5"></i>Print / PDF
        </button>
    </div>
</div>

<!-- Purchase Paper -->
<div class="max-w-4xl mx-auto my-8 px-4">
    <div class="purchase-wrapper bg-white rounded-2xl shadow-xl overflow-hidden">

        <!-- Header -->
        <div style="background: linear-gradient(135deg, #1e1f52 0%, #4143d0 100%);" class="px-4 sm:px-10 py-6 sm:py-8">
            <div class="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4">
                <div>
                    <div class="flex items-center gap-3 mb-3">
                        <div class="w-10 h-10 bg-white/20 rounded-xl flex items-center justify-center">
                            <i class="fas fa-shopping-cart text-white"></i>
                        </div>
                        <span class="text-white text-2xl font-bold">Purchase Order</span>
                    </div>
                    <div class="text-blue-200 text-sm">Your Business Address Line 1<br>City, State, PIN 000000<br>GST: 22AAAAA0000A1Z5</div>
                </div>
                <div class="text-right">
                    <div class="text-white text-sm mb-2">Purchase # <span class="font-bold text-lg"><?= sanitize($purchase['purchase_number']) ?></span></div>
                    <div class="text-blue-200 text-sm">Date: <?= date('d M Y', strtotime($purchase['purchase_date'])) ?></div>
                    <div class="text-blue-200 text-sm">Status: <span class="font-semibold text-white"><?= ucfirst($purchase['status']) ?></span></div>
                </div>
            </div>
        </div>

        <!-- Supplier Details -->
        <div class="px-4 sm:px-10 py-6 bg-slate-50">
            <h3 class="font-bold text-slate-800 mb-4">Supplier Information</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <div class="font-semibold text-slate-800 text-lg mb-2"><?= sanitize($purchase['supplier_name']) ?></div>
                    <?php if ($purchase['supplier_gstin']): ?>
                    <div class="text-slate-600">GSTIN: <?= sanitize($purchase['supplier_gstin']) ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Items Table -->
        <div class="px-4 sm:px-10 py-6">
            <h3 class="font-bold text-slate-800 mb-4">Purchased Items</h3>
            <div class="overflow-x-auto">
                <table class="w-full border-collapse">
                    <thead>
                        <tr class="border-b-2 border-slate-200">
                            <th class="text-left py-3 px-2 text-slate-700 font-semibold">Product</th>
                            <th class="text-center py-3 px-2 text-slate-700 font-semibold w-20">Qty</th>
                            <th class="text-right py-3 px-2 text-slate-700 font-semibold w-24">Rate</th>
                            <th class="text-right py-3 px-2 text-slate-700 font-semibold w-20">GST%</th>
                            <th class="text-right py-3 px-2 text-slate-700 font-semibold w-24">GST Amt</th>
                            <th class="text-right py-3 px-2 text-slate-700 font-semibold w-28">Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($lineItems as $item): ?>
                        <tr class="border-b border-slate-100">
                            <td class="py-3 px-2 text-slate-800 font-medium"><?= sanitize($item['product_name']) ?></td>
                            <td class="py-3 px-2 text-center text-slate-600"><?= (int)$item['quantity'] ?></td>
                            <td class="py-3 px-2 text-right text-slate-600">₹<?= number_format($item['unit_price'], 2) ?></td>
                            <td class="py-3 px-2 text-right text-slate-600"><?= number_format($item['gst_percent'], 2) ?>%</td>
                            <td class="py-3 px-2 text-right text-slate-600">₹<?= number_format($item['gst_amount'], 2) ?></td>
                            <td class="py-3 px-2 text-right font-semibold text-slate-800">₹<?= number_format($item['line_total'], 2) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Totals -->
        <div class="px-4 sm:px-10 py-6 bg-slate-50">
            <div class="flex justify-end">
                <div class="w-64">
                    <div class="space-y-2">
                        <div class="flex justify-between text-slate-600">
                            <span>Subtotal:</span>
                            <span class="font-semibold">₹<?= number_format($purchase['subtotal'], 2) ?></span>
                        </div>
                        <div class="flex justify-between text-slate-600">
                            <span>GST:</span>
                            <span class="font-semibold">₹<?= number_format($purchase['gst_amount'], 2) ?></span>
                        </div>
                        <div class="border-t border-slate-300 pt-2 flex justify-between text-lg font-bold text-slate-800">
                            <span>Total:</span>
                            <span>₹<?= number_format($purchase['total_amount'], 2) ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Notes -->
        <?php if ($purchase['notes']): ?>
        <div class="px-4 sm:px-10 py-6">
            <h3 class="font-bold text-slate-800 mb-3">Notes</h3>
            <div class="bg-slate-50 p-4 rounded-lg text-slate-700">
                <?= nl2br(sanitize($purchase['notes'])) ?>
            </div>
        </div>
        <?php endif; ?>

    </div>
</div>

</body>
</html>