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

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

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header("Location: grn_warehouse_list.php");
    exit();
}

$grn_id = (int)$_GET['id'];

// Fetch GRN Header
$grn = $conn->query("
    SELECT g.*, 
           u.full_name as received_by_name,
           p.project_name, p.project_code,
           rc.rc_code as return_challan_code
    FROM goods_received_note_warehouse g
    LEFT JOIN users u ON g.received_by = u.id
    LEFT JOIN project_master p ON g.project_id = p.id
    LEFT JOIN return_challan rc ON g.source_id = rc.id 
    WHERE g.id = $grn_id
")->fetch_assoc();

if (!$grn) {
    die("<h2 style='color:red; text-align:center; margin-top:50px;'>GRN Not Found!</h2>");
}

// Fetch GRN Items
$items = $conn->query("
    SELECT gi.*, im.name, im.model_no 
    FROM goods_received_note_items_warehouse gi
    JOIN inventory_master im ON gi.item_id = im.id
    WHERE gi.grn_id = $grn_id
    ORDER BY im.name ASC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View GRN - <?= htmlspecialchars($grn['grn_code']) ?></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: 25px 15px;
        }
        .container { max-width: 1200px; 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: 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: 18px 22px;
            border-radius: 16px;
            border: 1px solid rgba(255,255,255,0.15);
        }
        .info-box label {
            display: block;
            font-size: 0.9rem;
            opacity: 0.8;
            margin-bottom: 4px;
        }
        .info-box strong { font-size: 1.1rem; }

        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(0,0,0,0.25);
            border-radius: 16px;
            overflow: hidden;
        }
        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);
        }
        .total-row {
            background: rgba(168,85,247,0.2);
            font-weight: bold;
        }

        .btn {
            padding: 12px 24px;
            border-radius: 12px;
            text-decoration: none;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            margin-right: 10px;
        }
        .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; }
            .content { padding: 25px 20px; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>
<div class="container">
    <div class="glass-container">
        <div class="header">
            <h1><i class="fas fa-receipt"></i> Warehouse GRN Details</h1>
        </div>

        <div class="content">
            <div style="margin-bottom: 25px;">
                <a href="grn_warehouse_list.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to List
                </a>
                <!-- <button onclick="window.print()" class="btn btn-print">
                    <i class="fas fa-print"></i> Print GRN
                </button> -->
            </div>

            <div class="info-grid">
                <div class="info-box">
                    <label>GRN Code</label>
                    <strong><?= htmlspecialchars($grn['grn_code']) ?></strong>
                </div>
                <div class="info-box">
                    <label>GRN Date</label>
                    <strong><?= date('d M Y', strtotime($grn['grn_date'])) ?></strong>
                </div>
                <div class="info-box">
                    <label>Return Challan</label>
                    <strong><?= htmlspecialchars($grn['return_challan_code'] ?? 'N/A') ?></strong>
                </div>
                <div class="info-box">
                    <label>Project</label>
                    <strong><?= htmlspecialchars($grn['project_code'] ?? '') ?> - <?= htmlspecialchars($grn['project_name'] ?? 'Central Warehouse') ?></strong>
                </div>
                <div class="info-box">
                    <label>Received By</label>
                    <strong><?= htmlspecialchars($grn['received_by_name'] ?? 'Unknown') ?></strong>
                </div>
            </div>

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

            <h3 style="margin: 25px 0 15px; color:#c4b5fd;">Received Items</h3>
            <table>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Item Name</th>
                        <th>Model No</th>
                        <th>Serial No</th>
                        <th style="text-align:right;">Received Qty</th>
                        <th style="text-align:right;">Rate (₹)</th>
                        <th style="text-align:right;">Amount (₹)</th>
                        <th>UOM</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $total_amount = 0;
                    $i = 1;
                    while ($item = $items->fetch_assoc()): 
                        $amount = $item['received_qty'] * $item['rate'];
                        $total_amount += $amount;
                    ?>
                        <tr>
                            <td><?= $i++ ?></td>
                            <td><strong><?= htmlspecialchars($item['name']) ?></strong></td>
                            <td><?= htmlspecialchars($item['model_no'] ?? '-') ?></td>
                            <td><?= htmlspecialchars($item['serial_number'] ?? '-') ?></td>
                            <td style="text-align:right;"><?= number_format($item['received_qty'], 3) ?></td>
                            <td style="text-align:right;"><?= number_format($item['rate'], 2) ?></td>
                            <td style="text-align:right;"><?= number_format($amount, 2) ?></td>
                            <td><?= htmlspecialchars($item['unit']) ?></td>
                        </tr>
                    <?php endwhile; ?>
                    
                    <tr class="total-row">
                        <td colspan="6" style="text-align:right; font-size:1.1rem;">Grand Total</td>
                        <td style="text-align:right; font-size:1.2rem;">₹ <?= number_format($total_amount, 2) ?></td>
                        <td></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

</body>
</html>

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