<?php
ini_set('display_errors', 0);
error_reporting(E_ALL);
ini_set('log_errors', 1);
ini_set('error_log', 'php_errors.log');

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

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

$inventory_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($inventory_id <= 0) {
    header("Location: inventory-management.php");
    exit();
}

// Fetch Material Details
$stmt = $conn->prepare("
    SELECT im.*, 
           w.warehouse_name, w.location,
           c.category,
           v.vendor_name,
           u.uom_name
    FROM inventory_master im
    LEFT JOIN warehouse_master w ON im.warehouse_id = w.id
    LEFT JOIN category_tbl c ON im.category = c.id
    LEFT JOIN vendor_master v ON im.supplier_vendor = v.id
    LEFT JOIN uom_tbl u ON im.uom = u.id
    WHERE im.id = ?
");
$stmt->bind_param("i", $inventory_id);
$stmt->execute();
$material = $stmt->get_result()->fetch_assoc();

if (!$material) {
    $error = "Material not found.";
    $material = [];
}

// Fetch Serial Numbers
$serials = [];
$serial_result = $conn->query("SELECT serial_number FROM inventory_serials WHERE inventory_id = $inventory_id ORDER BY serial_number ASC");
while ($row = $serial_result->fetch_assoc()) {
    $serials[] = $row['serial_number'];
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Material - <?= htmlspecialchars($material['name'] ?? 'Not Found') ?></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;
            min-height: 100vh;
        }
        .container { max-width: 1100px; 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: 50px 40px;
            text-align: center;
            position: relative;
        }
        .header h1 { 
            font-size: clamp(28px, 5vw, 40px); 
            font-weight: 700; 
        }

        .form-area { padding: 45px 40px; }

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 25px;
            margin-bottom: 35px;
        }

        .info-card {
            background: rgba(255,255,255,0.07);
            border: 1px solid rgba(255,255,255,0.18);
            border-radius: 20px;
            padding: 28px;
        }

        .info-label {
            font-size: 0.95rem;
            color: #c4b5fd;
            margin-bottom: 6px;
            font-weight: 500;
        }

        .info-value {
            font-size: 1.25rem;
            font-weight: 600;
            color: #f1f5f9;
            word-break: break-word;
        }

        .serial-box {
            background: rgba(0,0,0,0.25);
            border: 1px solid rgba(255,255,255,0.15);
            border-radius: 16px;
            padding: 20px;
            min-height: 180px;
        }

        .serial-list {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }

        .serial-tag {
            background: rgba(168,85,247,0.25);
            color: #e0e7ff;
            padding: 8px 16px;
            border-radius: 50px;
            font-size: 0.95rem;
            border: 1px solid rgba(168,85,247,0.4);
        }

        .empty-state {
            color: #94a3b8;
            font-style: italic;
            text-align: center;
            padding: 40px 20px;
        }

        .action-buttons {
            display: flex;
            gap: 15px;
            margin-top: 40px;
            flex-wrap: wrap;
        }

        .btn {
            padding: 16px 28px;
            border-radius: 16px;
            font-weight: 700;
            font-size: 1.05rem;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
        }

        .btn-edit {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }

        .btn-back {
            background: rgba(255,255,255,0.1);
            color: white;
            border: 1px solid rgba(255,255,255,0.3);
        }

        .btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.4);
        }

        @media (max-width: 768px) {
            .form-area { padding: 30px 20px; }
            .info-grid { grid-template-columns: 1fr; }
            .action-buttons { flex-direction: column; }
        }
    </style>
</head>
<body>

<?php include '../config/header.php'; ?>

<div class="container">
    <div class="glass-container">
        <div class="header">
            <a href="inventory-management.php" style="color:#c4b5fd; text-decoration:none; position:absolute; left:40px; top:40px;">
                ← Back to Inventory
            </a>
            <h1><i class="fas fa-eye"></i> Material Details</h1>
            <p style="margin-top:8px; opacity:0.9; font-size:1.1rem;">
                SKU: <strong><?= htmlspecialchars($material['sku'] ?? 'N/A') ?></strong>
            </p>
        </div>

        <div class="form-area">
            <?php if (isset($error)): ?>
                <div style="padding:20px; background:rgba(239,68,68,0.3); border-radius:16px; color:#fca5a5; text-align:center;">
                    <?= htmlspecialchars($error) ?>
                </div>
            <?php else: ?>

            <div class="info-grid">

                <!-- Basic Information -->
                <div class="info-card">
                    <div class="info-label">Material Name</div>
                    <div class="info-value"><?= htmlspecialchars($material['name'] ?? 'N/A') ?></div>
                </div>

                <div class="info-card">
                    <div class="info-label">Model Number</div>
                    <div class="info-value"><?= htmlspecialchars($material['model_no'] ?? 'N/A') ?></div>
                </div>

                <div class="info-card">
                    <div class="info-label">Warehouse</div>
                    <div class="info-value">
                        <?= htmlspecialchars($material['warehouse_name'] ?? 'N/A') ?><br>
                        <small style="color:#94a3b8;"><?= htmlspecialchars($material['location'] ?? '') ?></small>
                    </div>
                </div>

                <div class="info-card">
                    <div class="info-label">Category</div>
                    <div class="info-value"><?= htmlspecialchars($material['category'] ?? 'Not Assigned') ?></div>
                </div>

                <div class="info-card">
                    <div class="info-label">Vendor / Supplier</div>
                    <div class="info-value"><?= htmlspecialchars($material['vendor_name'] ?? 'Not Assigned') ?></div>
                </div>

                <div class="info-card">
                    <div class="info-label">Unit of Measurement (UOM)</div>
                    <div class="info-value"><?= htmlspecialchars($material['uom_name'] ?? 'N/A') ?></div>
                </div>

                <div class="info-card">
                    <div class="info-label">HSN / SAC Code</div>
                    <div class="info-value"><?= htmlspecialchars($material['hsn_sac'] ?? 'N/A') ?></div>
                </div>

                <div class="info-card">
                    <div class="info-label">Current Stock</div>
                    <div class="info-value" style="color:#4ade80; font-size:1.5rem;">
                        <?= number_format($material['current_stock'] ?? 0, 2) ?>
                    </div>
                </div>

                <div class="info-card">
                    <div class="info-label">Reorder Level</div>
                    <div class="info-value" style="color:#fb923c;">
                        <?= number_format($material['reorder_level'] ?? 0, 2) ?>
                    </div>
                </div>
            </div>

            <!-- Description -->
            <?php if (!empty($material['description'])): ?>
            <div class="info-card" style="margin-bottom:35px;">
                <div class="info-label">Description</div>
                <div style="line-height:1.7; color:#e0e7ff;">
                    <?= nl2br(htmlspecialchars($material['description'])) ?>
                </div>
            </div>
            <?php endif; ?>

            <!-- Serial Numbers -->
            <div class="info-card">
                <div class="info-label" style="margin-bottom:15px;">Serial Numbers 
                    <span style="font-size:0.9rem; font-weight:normal; color:#94a3b8;">(<?= count($serials) ?> found)</span>
                </div>
                
                <div class="serial-box">
                    <?php if (!empty($serials)): ?>
                        <div class="serial-list">
                            <?php foreach ($serials as $serial): ?>
                                <div class="serial-tag">
                                    <i class="fas fa-barcode"></i> <?= htmlspecialchars($serial) ?>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    <?php else: ?>
                        <div class="empty-state">
                            <i class="fas fa-info-circle"></i><br><br>
                            No serial numbers added for this material.
                        </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Action Buttons -->
            <div class="action-buttons">
                <a href="edit-materials.php?id=<?= $inventory_id ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i> Edit Material
                </a>
                <a href="inventory-management.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to Inventory List
                </a>
            </div>

            <?php endif; ?>
        </div>
    </div>
</div>

</body>
</html>

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