<?php
require_once '../includes/auth.php';
require_once '../db.php';

$id = (int)($_GET['id'] ?? 0);
if ($id <= 0) die('Invalid ID');

$po = $conn->query("
    SELECT po.*, v.name AS vendor_name, v.phone AS vendor_phone
    FROM purchase_orders po
    JOIN vendors v ON v.id = po.vendor_id
    WHERE po.id = $id AND po.company_id = $cid
")->fetch_assoc();
if (!$po) die('Purchase order not found.');

$items = $conn->query("
    SELECT pi.*, p.name AS product_name, p.unit
    FROM purchase_items pi
    JOIN products p ON p.id = pi.product_id
    WHERE pi.purchase_order_id = $id
");
$statusColor = $po['status'] === 'final' ? '#10b981' : '#f59e0b';
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: Inter, sans-serif; font-size: 13px; color: #1e293b; padding: 20px; background: #fff; }
.header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 18px; gap: 12px; }
.po-no { font-size: 20px; font-weight: 700; color: #1e293b; }
.muted { color: #64748b; font-size: 12px; margin-top: 3px; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 20px; font-size: 11px; font-weight: 600; }
table { width: 100%; border-collapse: collapse; }
thead th { background: #f1f5f9; text-transform: uppercase; font-size: 11px; letter-spacing: .4px; padding: 8px 10px; text-align: left; color: #475569; }
tbody td { padding: 7px 10px; border-bottom: 1px solid #f1f5f9; }
tbody tr:last-child td { border-bottom: none; }
.totals-table { width: 240px; margin-left: auto; margin-top: 14px; }
.totals-table td { padding: 5px 8px; }
.totals-table .label { color: #64748b; }
.totals-table .grand td { font-weight: 700; font-size: 14px; border-top: 2px solid #e2e8f0; padding-top: 8px; }
</style>
</head>
<body>

<div class="header">
    <div>
        <div class="po-no"><?= htmlspecialchars($po['po_no']) ?></div>
        <div class="muted"><?= date('d M Y', strtotime($po['purchase_date'])) ?></div>
        <?php if ($po['supplier_invoice_no']): ?>
        <div class="muted">Supplier Inv: <?= htmlspecialchars($po['supplier_invoice_no']) ?></div>
        <?php endif; ?>
    </div>
    <div style="text-align:right;">
        <div style="font-weight:600;"><?= htmlspecialchars($po['vendor_name']) ?></div>
        <?php if ($po['vendor_phone']): ?>
        <div class="muted"><?= htmlspecialchars($po['vendor_phone']) ?></div>
        <?php endif; ?>
        <div style="margin-top:6px;">
            <span class="badge" style="background:<?= $statusColor ?>22;color:<?= $statusColor ?>;"><?= ucfirst($po['status']) ?></span>
        </div>
    </div>
</div>

<table>
    <thead>
        <tr>
            <th>#</th>
            <th>Product</th>
            <th>Batch</th>
            <th style="text-align:right;">MRP</th>
            <th style="text-align:right;">Purchase Price</th>
            <th style="text-align:center;">GST%</th>
            <th style="text-align:right;">Qty</th>
            <th style="text-align:right;">Total</th>
        </tr>
    </thead>
    <tbody>
    <?php $i = 1; while ($item = $items->fetch_assoc()): ?>
    <tr>
        <td style="color:#94a3b8;"><?= $i++ ?></td>
        <td>
            <strong><?= htmlspecialchars($item['product_name']) ?></strong>
            <?php if ($item['unit']): ?><span style="font-size:11px;color:#94a3b8;margin-left:4px;"><?= htmlspecialchars($item['unit']) ?></span><?php endif; ?>
        </td>
        <td style="font-size:11px;color:#64748b;"><?= htmlspecialchars($item['batch_no']) ?></td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($item['mrp'],2) ?></td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($item['purchase_price'],2) ?></td>
        <td style="text-align:center;"><?= $item['gst_percent'] ?>%</td>
        <td style="text-align:right;"><?= number_format($item['quantity'], 0) ?></td>
        <td style="text-align:right;font-weight:600;">&#8377;&nbsp;<?= number_format($item['purchase_price'] * $item['quantity'],2) ?></td>
    </tr>
    <?php endwhile; ?>
    </tbody>
</table>

<table class="totals-table">
    <tr>
        <td class="label">Taxable Amount</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($po['total_amount'],2) ?></td>
    </tr>
    <tr>
        <td class="label">GST</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($po['gst_amount'],2) ?></td>
    </tr>
    <tr class="grand">
        <td>Net Amount</td>
        <td style="text-align:right;">&#8377;&nbsp;<?= number_format($po['net_amount'],2) ?></td>
    </tr>
</table>

</body>
</html>
