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

if (!isset($_SESSION['user_id'])) {
    header("Location: ../login.php");
    exit();
}

$warehouse_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($warehouse_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>No Warehouse Selected</h2>");
}

// Fetch Warehouse Details
$stmt = $conn->prepare("SELECT * FROM warehouse_master WHERE id = ?");
$stmt->bind_param("i", $warehouse_id);
$stmt->execute();
$warehouse = $stmt->get_result()->fetch_assoc();

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Warehouse Details - <?= htmlspecialchars($warehouse['warehouse_name']) ?></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;
        }
        .header h1 { 
            font-size: clamp(28px, 5.5vw, 40px); 
            font-weight: 700; 
            margin-bottom: 8px;
        }

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

        .info-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 40px;
        }
        .info-card {
            background: rgba(255,255,255,0.08);
            border: 1px solid rgba(255,255,255,0.18);
            border-radius: 20px;
            padding: 25px;
        }
        .info-label {
            font-size: 0.95rem;
            color: #c4b5fd;
            margin-bottom: 6px;
        }
        .info-value {
            font-size: 1.15rem;
            color: #e0e7ff;
            word-break: break-word;
        }

        .status-badge {
            display: inline-block;
            padding: 10px 24px;
            border-radius: 50px;
            font-weight: 700;
            font-size: 1.05rem;
        }
        .status-Active { background: #22c55e; color: white; }
        .status-Inactive { background: #64748b; color: white; }
        .status-UnderMaintenance { background: #f59e0b; color: white; }

        .section-title {
            font-size: 1.45rem;
            margin: 45px 0 20px;
            color: #e0e7ff;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .detail-box {
            background: rgba(255,255,255,0.06);
            border-radius: 16px;
            padding: 25px;
            margin-bottom: 30px;
        }

        .btn {
            padding: 14px 28px;
            border-radius: 14px;
            text-decoration: none;
            font-weight: 700;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            margin-right: 12px;
        }
        .btn-edit {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }
        .btn-back {
            background: rgba(255,255,255,0.1);
            color: #e0e7ff;
            border: 1px solid rgba(255,255,255,0.3);
        }

        @media (max-width: 768px) {
            .form-area { padding: 30px 20px; }
            .info-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>

<div class="container">
    <div class="glass-container">
        <div class="header">
            <a href="warehouse-management.php" style="color:#c4b5fd; text-decoration:none; position:absolute; left:40px;">
                ← Back to Warehouses
            </a>
            <h1><i class="fas fa-warehouse"></i> Warehouse Details</h1>
            <p style="margin-top:10px; opacity:0.9;"><?= htmlspecialchars($warehouse['warehouse_code']) ?></p>
        </div>

        <div class="form-area">

            <!-- Status -->
            <div style="text-align:center; margin-bottom:35px;">
                <span class="status-badge status-<?= str_replace(' ', '', htmlspecialchars($warehouse['status'])) ?>">
                    <?= htmlspecialchars($warehouse['status']) ?>
                </span>
            </div>

            <!-- Main Info -->
            <div class="info-grid">
                <div class="info-card">
                    <div class="info-label">Warehouse Name</div>
                    <div class="info-value"><?= htmlspecialchars($warehouse['warehouse_name']) ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Location</div>
                    <div class="info-value"><?= htmlspecialchars($warehouse['location']) ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">City</div>
                    <div class="info-value"><?= htmlspecialchars($warehouse['city'] ?? '—') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">State</div>
                    <div class="info-value"><?= htmlspecialchars($warehouse['state'] ?? '—') ?></div>
                </div>
            </div>

            <!-- Address -->
            <?php if (!empty($warehouse['address'])): ?>
            <div class="detail-box">
                <div class="info-label">Full Address</div>
                <div class="info-value"><?= nl2br(htmlspecialchars($warehouse['address'])) ?></div>
            </div>
            <?php endif; ?>

            <!-- Additional Details -->
            <div class="info-grid">
                <div class="info-card">
                    <div class="info-label">Pincode</div>
                    <div class="info-value"><?= htmlspecialchars($warehouse['pincode'] ?? '—') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Storage Capacity</div>
                    <div class="info-value"><?= htmlspecialchars($warehouse['capacity'] ?? '—') ?></div>
                </div>
                <div class="info-card">
                    <div class="info-label">Created On</div>
                    <div class="info-value">
                        <?= $warehouse['created_at'] ? date('d M Y, h:i A', strtotime($warehouse['created_at'])) : '—' ?>
                    </div>
                </div>
            </div>

            <!-- Remarks -->
            <?php if (!empty($warehouse['remarks'])): ?>
            <div class="detail-box">
                <div class="info-label">Remarks / Special Notes</div>
                <div class="info-value"><?= nl2br(htmlspecialchars($warehouse['remarks'])) ?></div>
            </div>
            <?php endif; ?>

            <!-- Action Buttons -->
            <div style="text-align:center; margin-top:50px;">
                <a href="edit_warehouse.php?id=<?= $warehouse_id ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i> Edit Warehouse
                </a>
                <a href="warehouse-management.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to Warehouse List
                </a>
            </div>
        </div>
    </div>
</div>

</body>
</html>

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