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

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

$user_id = $_SESSION['user_id'];

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

// Fetch PR Details with Project Info
$stmt = $conn->prepare("
    SELECT pr.*, p.project_name, p.project_code, u.full_name as requested_by_name
    FROM procurement_request pr
    JOIN project_master p ON pr.project_id = p.id
    LEFT JOIN users u ON pr.requested_by = u.id
    WHERE pr.id = ? 
");
$stmt->bind_param("i", $pr_id);
$stmt->execute();
$pr = $stmt->get_result()->fetch_assoc();

if (!$pr) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Procurement Request Not Found or Access Denied</h2>");
}

// Fetch Items
$items_stmt = $conn->prepare("
    SELECT pri.*, im.name, im.uom 
    FROM procurement_request_items pri
    JOIN inventory_master im ON pri.item_id = im.id
    WHERE pri.pr_id = ?
    ORDER BY pri.id ASC
");
$items_stmt->bind_param("i", $pr_id);
$items_stmt->execute();
$items = $items_stmt->get_result()->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Procurement Request - PR-<?= str_pad($pr_id, 5, '0', STR_PAD_LEFT) ?></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: 1280px; 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.3rem; 
            font-weight: 700; 
            margin-bottom: 8px;
        }
        .form-area { padding: 40px; }

        .info-card {
            background: rgba(255,255,255,0.08);
            border: 1px solid rgba(255,255,255,0.18);
            border-radius: 20px;
            padding: 25px;
            margin-bottom: 30px;
        }

        .status-badge {
            display: inline-block;
            padding: 8px 20px;
            border-radius: 50px;
            font-weight: 700;
            font-size: 1rem;
        }
        .status-Pending  { background: #f59e0b; color: #fff; }
        .status-Approved { background: #22c55e; color: #fff; }
        .status-Rejected { background: #ef4444; color: #fff; }

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

        .detail-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-bottom: 25px;
        }
        .detail-label {
            font-weight: 600;
            color: #c4b5fd;
            margin-bottom: 4px;
        }
        .detail-value {
            font-size: 1.1rem;
            color: #e0e7ff;
        }

        .btn {
            padding: 14px 28px;
            border-radius: 12px;
            font-weight: 700;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 10px;
            margin-right: 12px;
            transition: all 0.3s;
        }
        .btn-primary {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }
        .btn-secondary {
            background: rgba(255,255,255,0.1);
            color: #e0e7ff;
            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) {
            .header h1 { font-size: 1.9rem; }
            .form-area { padding: 25px 20px; }
            table th, table td { padding: 12px 10px; font-size: 0.95rem; }
            .detail-row { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>

<div class="container">
    <div class="glass-container">
        <div class="header">
            <a href="view_procurement_requests.php?project_id=<?= $pr['project_id'] ?>" 
               style="color:#c4b5fd; text-decoration:none; font-size:1.05rem;">
                ← Back to All Requests
            </a>
            <h1><i class="fas fa-file-invoice"></i> Procurement Request Details</h1>
            <p style="margin-top:8px; opacity:0.9;">
                PR-<?= str_pad($pr_id, 5, '0', STR_PAD_LEFT) ?>
            </p>
        </div>

        <div class="form-area">

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

            <div class="info-card">
                <div class="detail-row">
                    <div>
                        <div class="detail-label">Project</div>
                        <div class="detail-value">
                            <?= htmlspecialchars($pr['project_name']) ?> 
                            <small>(<?= htmlspecialchars($pr['project_code']) ?>)</small>
                        </div>
                    </div>
                    <div>
                        <div class="detail-label">Requested By</div>
                        <div class="detail-value"><?= htmlspecialchars($pr['requested_by_name'] ?? 'Project Manager') ?></div>
                    </div>
                    <div>
                        <div class="detail-label">PR Date</div>
                        <div class="detail-value"><?= date('d M Y', strtotime($pr['pr_date'])) ?></div>
                    </div>
                    <div>
                        <div class="detail-label">Required By</div>
                        <div class="detail-value">
                            <?= $pr['required_by_date'] ? date('d M Y', strtotime($pr['required_by_date'])) : '—' ?>
                        </div>
                    </div>
                </div>

                <?php if (!empty($pr['remarks'])): ?>
                <div style="margin-top:20px;">
                    <div class="detail-label">Remarks / Special Instructions</div>
                    <div class="detail-value" style="background:rgba(0,0,0,0.2); padding:15px; border-radius:12px; white-space:pre-wrap;">
                        <?= nl2br(htmlspecialchars($pr['remarks'])) ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>

            <h3 style="margin: 35px 0 15px; color:#e0e7ff;">
                <i class="fas fa-list"></i> Requested Items
            </h3>

            <table>
                <thead>
                    <tr>
                        <th style="width:60px;">Sr.No</th>
                        <th>Item Name</th>
                        <th>Unit</th>
                        <th style="text-align:right; width:140px;">Quantity</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($items)): ?>
                    <tr>
                        <td colspan="4" style="text-align:center; padding:40px; color:#94a3b8;">
                            No items found in this request.
                        </td>
                    </tr>
                    <?php else: ?>
                        <?php foreach ($items as $index => $item): ?>
                        <tr>
                            <td><?= $index + 1 ?></td>
                            <td><?= htmlspecialchars($item['name']) ?></td>
                            <td><?= htmlspecialchars($item['uom']) ?></td>
                            <td style="text-align:right; font-weight:600;">
                                <?= number_format($item['requested_qty'], 3) ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>

            <!-- Action Buttons -->
            <div style="margin-top:40px; text-align:center;">
                <?php if ($pr['status'] === 'Pending'): ?>
                    <a href="pr_edit.php?pr_id=<?= $pr_id ?>" class="btn btn-primary">
                        <i class="fas fa-edit"></i> Edit Request
                    </a>
                <?php endif; ?>

                <a href="pr_list.php?project_id=<?= $pr['project_id'] ?>" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Back to List
                </a>
            </div>
        </div>
    </div>
</div>

</body>
</html>

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