<?php
session_start();
require_once("../config/config.php");

if (!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], ['admin', 'project manager', 'store manager'])) {
    header("Location: ../login.php");
    exit();
}

$po_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($po_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Invalid Purchase Order ID</h2>");
}

// Fetch PO Header
$po = $conn->query("
    SELECT po.*, p.project_code, p.project_name, u.full_name as created_by_name
    FROM purchase_order_from_client po
    LEFT JOIN project_master p ON po.project_id = p.id
    LEFT JOIN users u ON po.created_by = u.id
    WHERE po.id = $po_id
")->fetch_assoc();

if (!$po) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Purchase Order Not Found</h2>");
}

// Fetch PO Items
$items = $conn->query("
    SELECT * FROM purchase_order_from_client_items 
    WHERE purchase_order_from_client_id = $po_id 
    ORDER BY id ASC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PO View - <?= htmlspecialchars($po['order_number'] ?? 'PO-'.$po_id) ?></title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    
    <style>
        :root {
            --primary: #a855f7;
            --accent: #c084fc;
            --glass: rgba(255, 255, 255, 0.10);
            --glass-border: rgba(255, 255, 255, 0.20);
        }
        * { margin:0; padding:0; box-sizing:border-box; }
        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: linear-gradient(135deg, rgba(0,0,0,0.78), rgba(0,0,0,0.92)), url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            padding: 20px 15px;
        }
        .container { max-width: 1350px; margin: auto; }
        .glass-container {
            background: var(--glass);
            backdrop-filter: blur(24px);
            border-radius: 32px;
            border: 1px solid var(--glass-border);
            box-shadow: 0 25px 80px rgba(0,0,0,0.65);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(90deg, rgba(168,85,247,0.35), rgba(147,51,234,0.25));
            padding: 45px 40px;
            text-align: center;
        }
        .header h1 { font-size: 2.4rem; font-weight: 700; }

        .content { padding: 40px; }
        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 35px;
        }
        .info-box {
            background: rgba(255,255,255,0.08);
            padding: 20px;
            border-radius: 16px;
            border: 1px solid rgba(255,255,255,0.15);
        }
        .info-box label { display: block; opacity: 0.8; font-size: 0.95rem; margin-bottom: 4px; }

        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(0,0,0,0.25);
            border-radius: 16px;
            overflow: hidden;
            margin: 30px 0;
        }
        th {
            background: rgba(168,85,247,0.3);
            padding: 16px;
            text-align: left;
        }
        td {
            padding: 16px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        tr:hover { background: rgba(168,85,247,0.08); }

        .totals {
            background: linear-gradient(135deg, rgba(168,85,247,0.15), rgba(192,132,252,0.1));
            border: 1px solid var(--accent);
            border-radius: 20px;
            padding: 25px;
            margin: 30px 0;
        }
        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 12px 0;
            font-size: 1.1rem;
        }
        .grand-total {
            font-size: 1.6rem;
            font-weight: 700;
            color: #ddd6fe;
            border-top: 2px solid rgba(255,255,255,0.2);
            padding-top: 15px;
            margin-top: 10px;
        }

        .btn {
            padding: 12px 28px;
            border-radius: 12px;
            text-decoration: none;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-back { background: rgba(255,255,255,0.15); color: white; }
        .btn-print { background: linear-gradient(135deg, #22c55e, #86efac); color: black; }

        @media (max-width: 768px) {
            .header h1 { font-size: 2rem; }
            table { font-size: 0.95rem; }
        }
    </style>
</head>
<body>
<?php require '../config/header.php'; ?>
<div class="container">
    <div class="glass-container">
        <div class="header">
            <a href="po_from_client_list.php" class="btn btn-back" style="float:left;">
                ← Back to PO List
            </a>
            <h1><i class="fas fa-file-invoice"></i> Purchase Order View</h1>
        </div>

        <div class="content">
            <!-- <div style="margin-bottom:25px;">
                <button onclick="window.print()" class="btn btn-print">
                    <i class="fas fa-print"></i> Print / Save as PDF
                </button>
            </div> -->

            <!-- PO Info -->
            <div class="info-grid">
                <div class="info-box">
                    <label>Order Number</label>
                    <strong><?= htmlspecialchars($po['order_number'] ?? 'PO-'.$po_id) ?></strong>
                </div>
                <div class="info-box">
                    <label>Project</label>
                    <strong><?= htmlspecialchars($po['project_code']) ?> - <?= htmlspecialchars($po['project_name']) ?></strong>
                </div>
                <div class="info-box">
                    <label>PO Date</label>
                    <strong><?= date('d M Y', strtotime($po['created_at'])) ?></strong>
                </div>
                <div class="info-box">
                    <label>Created By</label>
                    <strong><?= htmlspecialchars($po['created_by_name'] ?? 'System') ?></strong>
                </div>
            </div>

            <!-- Items Table -->
            <h2 style="color:#c4b5fd; margin:30px 0 15px;">Ordered Items</h2>
            <table>
                <thead>
                    <tr>
                        <th>Sr.</th>
                        <th>BBU Item No</th>
                        <th>Material Name</th>
                        <th>Qty</th>
                        <th>UOM</th>
                        <th>Type</th>
                        <th>Rate</th>
                        <th>Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $basic_total = 0;
                    $i = 1;
                    while ($item = $items->fetch_assoc()): 
                        $amount = $item['sitc_amount'] ?: ($item['supply_amount'] + $item['itc_amount']);
                        $basic_total += $amount;
                    ?>
                        <tr>
                            <td><?= $i++ ?></td>
                            <td><?= htmlspecialchars($item['bbu_item_no'] ?: '—') ?></td>
                            <td><strong><?= htmlspecialchars($item['name']) ?></strong></td>
                            <td><?= number_format($item['quantity'], 3) ?></td>
                            <td><?= htmlspecialchars($item['uom']) ?></td>
                            <td><?= htmlspecialchars($item['type']) ?></td>
                            <td>₹ <?= number_format($item['sitc_rate'] ?: $item['supply_rate'] ?: $item['itc_rate'], 2) ?></td>
                            <td>₹ <?= number_format($amount, 2) ?></td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>

            <!-- Totals -->
            <div class="totals">
                <div class="total-row"><span>Basic Total</span> <span>₹ <?= number_format($basic_total, 2) ?></span></div>
                <div class="total-row"><span>GST (18%)</span> <span>₹ <?= number_format($basic_total * 0.18, 2) ?></span></div>
                <div class="total-row grand-total">
                    <span>Grand Total</span> 
                    <span>₹ <?= number_format($basic_total * 1.18, 2) ?></span>
                </div>
            </div>

            <?php if (!empty($po['remarks'])): ?>
            <div class="info-box" style="margin-top:30px;">
                <label>Remarks</label>
                <p><?= nl2br(htmlspecialchars($po['remarks'])) ?></p>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

</body>
</html>

<?php $conn->close(); ?>