<?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");

$error = "";
$material_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Fetch material details
$material = null;
if ($material_id > 0) {
    try {
        $query = "SELECT * FROM material_master WHERE id = ?";
        $stmt = $pdo->prepare($query);
        $stmt->execute([$material_id]);
        $material = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if (!$material) {
            $error = "Material not found.";
        }
    } catch (PDOException $e) {
        $error = "Database error: " . $e->getMessage();
    }
} else {
    $error = "Invalid material ID.";
    header("Location: material-management.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Material Details</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            background: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.6)),
                        url('../background.jpeg') center/cover no-repeat fixed;
            display: flex;
            flex-direction: column;
            color: white;
        }

        /* Header */
        .page-header {
            background: rgba(255, 255, 255, 0.15);
            backdrop-filter: blur(12px);
            padding: 40px 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 20px;
        }

        .page-header h1 {
            font-size: 36px;
            font-weight: 700;
            text-shadow: 0 2px 10px rgba(0,0,0,0.4);
        }

        .back-btn {
            background: white;
            color: #6b21a8;
            padding: 14px 32px;
            border-radius: 14px;
            font-weight: 600;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s ease;
            box-shadow: 0 8px 20px rgba(0,0,0,0.3);
        }

        .back-btn:hover {
            transform: translateY(-3px);
            background: #f3e8ff;
            box-shadow: 0 12px 25px rgba(0,0,0,0.4);
        }

        /* Main Content */
        main {
            flex: 1;
            padding: 40px 20px;
            max-width: 1200px;
            margin: 0 auto;
            width: 100%;
        }

        /* Error Message */
        .error-message {
            padding: 24px;
            border-radius: 16px;
            background: rgba(239, 68, 68, 0.25);
            border: 1px solid rgba(252, 165, 165, 0.4);
            backdrop-filter: blur(8px);
            max-width: 700px;
            margin: 0 auto 40px;
            display: flex;
            align-items: center;
            font-weight: 600;
        }

        .error-message i {
            margin-right: 16px;
            font-size: 28px;
        }

        /* Glassmorphism Details Card */
        .glass-card {
            background: rgba(255, 255, 255, 0.12);
            backdrop-filter: blur(16px);
            -webkit-backdrop-filter: blur(16px);
            border-radius: 24px;
            border: 1px solid rgba(255, 255, 255, 0.25);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.5);
            overflow: hidden;
            max-width: 800px;
            margin: 0 auto;
        }

        .card-header {
            background: rgba(255, 255, 255, 0.15);
            padding: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        .card-header h2 {
            font-size: 28px;
            font-weight: 700;
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .card-header i {
            font-size: 36px;
        }

        .id-badge {
            background: rgba(255, 255, 255, 0.2);
            padding: 10px 20px;
            border-radius: 50px;
            font-size: 14px;
            font-weight: 600;
        }

        .sku-badge {
            background: rgba(139, 92, 246, 0.3);
            padding: 8px 16px;
            border-radius: 50px;
            font-size: 14px;
            font-weight: 600;
            margin-top: 8px;
        }

        .card-body {
            padding: 40px 35px;
        }

        .detail-item {
            display: flex;
            gap: 20px;
            margin-bottom: 40px;
        }

        .detail-icon {
            background: rgba(255, 255, 255, 0.15);
            width: 56px;
            height: 56px;
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            flex-shrink: 0;
        }

        .detail-icon i {
            font-size: 24px;
            color: #c4b5fd;
        }

        .detail-content {
            flex: 1;
        }

        .detail-label {
            font-size: 13px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: rgba(255, 255, 255, 0.7);
            margin-bottom: 8px;
        }

        .detail-value {
            font-size: 20px;
            font-weight: 500;
            color: white;
        }

        .detail-value.small {
            font-size: 16px;
            color: rgba(255, 255, 255, 0.8);
        }

        .no-value {
            color: rgba(255, 255, 255, 0.5);
            font-style: italic;
        }

        .stock-status {
            display: inline-block;
            padding: 6px 12px;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 600;
            margin-top: 8px;
        }

        .stock-low {
            background: rgba(239, 68, 68, 0.3);
            color: #fca5a5;
        }

        .stock-normal {
            background: rgba(34, 197, 94, 0.3);
            color: #86efac;
        }

        /* Action Buttons */
        .card-footer {
            padding: 30px 35px;
            background: rgba(255, 255, 255, 0.05);
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
        }

        .btn {
            flex: 1;
            min-width: 200px;
            padding: 18px;
            border-radius: 14px;
            font-size: 17px;
            font-weight: 700;
            text-align: center;
            text-decoration: none;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 12px;
            transition: all 0.4s ease;
        }

        .btn-primary {
            background: linear-gradient(135deg, #8b5cf6, #a78bfa);
            color: white;
            box-shadow: 0 10px 30px rgba(139, 92, 246, 0.4);
        }

        .btn-primary:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(139, 92, 246, 0.5);
        }

        .btn-secondary {
            background: rgba(255, 255, 255, 0.15);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            backdrop-filter: blur(8px);
        }

        .btn-secondary:hover {
            background: rgba(255, 255, 255, 0.25);
            transform: translateY(-4px);
        }

        /* Responsive */
        @media (max-width: 640px) {
            .page-header h1 {
                font-size: 30px;
            }
            .card-header {
                flex-direction: column;
                text-align: center;
                gap: 16px;
            }
            .card-header h2 {
                font-size: 24px;
            }
            .detail-item {
                flex-direction: column;
            }
            .detail-icon {
                width: 48px;
                height: 48px;
            }
            .detail-icon i {
                font-size: 20px;
            }
            .card-footer {
                flex-direction: column;
            }
            .btn {
                min-width: auto;
            }
        }
    </style>
</head>
<body>
    <!-- Page Header -->
    <header class="page-header">
        <div class="header-content">
            <h1>Material Details</h1>
            <a href="material-management.php" class="back-btn">
                <i class="fas fa-arrow-left"></i> Back to Materials
            </a>
        </div>
    </header>

    <!-- Main Content -->
    <main>
        <?php if (!empty($error)): ?>
            <div class="error-message">
                <i class="fas fa-exclamation-triangle"></i>
                <?= htmlspecialchars($error) ?>
            </div>
        <?php endif; ?>

        <?php if ($material): ?>
            <div class="glass-card">
                <!-- Card Header -->
                <div class="card-header">
                    <div>
                        <h2>
                            <i class="fas fa-box"></i>
                            <?= htmlspecialchars($material['name']) ?>
                        </h2>
                        <div class="sku-badge">SKU: <?= htmlspecialchars($material['sku']) ?></div>
                    </div>
                    <span class="id-badge">ID: #<?= $material['id'] ?></span>
                </div>

                <!-- Card Body -->
                <div class="card-body">
                    <!-- Material Name -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-tag"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">Material Name</div>
                            <div class="detail-value"><?= htmlspecialchars($material['name']) ?></div>
                        </div>
                    </div>

                    <!-- SKU -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-barcode"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">SKU</div>
                            <div class="detail-value"><?= htmlspecialchars($material['sku']) ?></div>
                        </div>
                    </div>

                    <!-- Category -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-folder"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">Category</div>
                            <div class="detail-value">
                                <?= !empty($material['category']) ? htmlspecialchars($material['category']) : '<span class="no-value">Not specified</span>' ?>
                            </div>
                        </div>
                    </div>

                    <!-- Unit -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-ruler"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">Unit</div>
                            <div class="detail-value"><?= htmlspecialchars($material['unit']) ?></div>
                        </div>
                    </div>

                    <!-- Stock & Reorder Level -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-cubes"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">Current Stock</div>
                            <div class="detail-value">
                                <?= number_format($material['stock'], 3) ?>
                                <?php if ($material['reorder_level'] > 0): ?>
                                    <span class="stock-status <?= $material['stock'] <= $material['reorder_level'] ? 'stock-low' : 'stock-normal' ?>">
                                        <?= $material['stock'] <= $material['reorder_level'] ? 'Low Stock' : 'In Stock' ?>
                                    </span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>

                    <!-- Reorder Level -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-exclamation-triangle"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">Reorder Level</div>
                            <div class="detail-value"><?= number_format($material['reorder_level'], 3) ?></div>
                        </div>
                    </div>

                    <!-- Last Cost -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-dollar-sign"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">Last Cost (per unit)</div>
                            <div class="detail-value"><?= number_format($material['last_cost'], 2) ?></div>
                        </div>
                    </div>

                    <!-- Created Date -->
                    <div class="detail-item">
                        <div class="detail-icon">
                            <i class="fas fa-calendar-alt"></i>
                        </div>
                        <div class="detail-content">
                            <div class="detail-label">Created On</div>
                            <div class="detail-value">
                                <?= date('d F Y', strtotime($material['created_at'])) ?>
                                <span class="detail-value small">at <?= date('h:i A', strtotime($material['created_at'])) ?></span>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Action Buttons -->
                <div class="card-footer">
                    <a href="edit-material.php?id=<?= $material['id'] ?>" class="btn btn-primary">
                        <i class="fas fa-edit"></i> Edit Material
                    </a>
                    <a href="material-management.php" class="btn btn-secondary">
                        <i class="fas fa-list"></i> View All Materials
                    </a>
                </div>
            </div>
        <?php endif; ?>
    </main>
</body>
</html>