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

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

$boq_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($boq_id <= 0) {
    header("Location: boq-management.php");
    exit();
}

// Fetch BOQ with Project & UOM details
$stmt = $conn->prepare("
    SELECT 
        bm.*,
        p.project_code,
        p.project_name,
        u.uom_name
    FROM boq_master bm
    LEFT JOIN project_master p ON bm.project_id = p.id
    LEFT JOIN uom_tbl u ON bm.uom = u.id
    WHERE bm.id = ?
");
$stmt->bind_param("i", $boq_id);
$stmt->execute();
$boq = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (!$boq) {
    header("Location: boq-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 BOQ - <?= htmlspecialchars($boq['boq_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: 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, 42px);
            font-weight: 700;
        }

        .content { padding: 45px 40px; }

        .boq-header {
            text-align: center;
            margin-bottom: 35px;
        }
        .boq-code {
            font-size: 2.4rem;
            font-weight: 700;
            color: #c4b5fd;
            margin-bottom: 8px;
        }

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

        .info-card {
            background: rgba(255,255,255,0.06);
            border: 1px solid rgba(255,255,255,0.15);
            border-radius: 20px;
            padding: 30px;
        }

        .info-card h3 {
            margin-bottom: 22px;
            font-size: 1.4rem;
            display: flex;
            align-items: center;
            gap: 12px;
            color: #ddd6fe;
        }

        .detail-row {
            margin-bottom: 20px;
        }

        .label {
            font-size: 0.95rem;
            opacity: 0.75;
            margin-bottom: 6px;
        }

        .value {
            font-size: 1.15rem;
            font-weight: 600;
            word-break: break-word;
        }

        .amount {
            font-size: 1.35rem;
            font-weight: 700;
            color: #c4b5fd;
        }

        .type-badge {
            padding: 10px 26px;
            border-radius: 50px;
            font-weight: 700;
            display: inline-block;
            margin-top: 10px;
        }
        .type-SITC { background: #8b5cf6; color: white; }
        .type-S+ITC { background: #ec4899; color: white; }

        .actions {
            margin-top: 40px;
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            justify-content: center;
        }

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

        .btn-back {
            background: rgba(255,255,255,0.15);
            color: white;
        }

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

        @media (max-width: 768px) {
            .glass-container { border-radius: 24px; }
            .header, .content { padding: 35px 25px; }
            .info-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>

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

<div class="container">
    <div class="glass-container">

        <!-- Header -->
        <div class="header">
            <a href="boq-management.php" style="color:#c4b5fd; text-decoration:none; position:absolute; left:40px; top:35px; font-weight:500;">
                ← Back to BOQ List
            </a>
            <h1><i class="fas fa-file-contract"></i> BOQ Details</h1>
        </div>

        <div class="content">

            <div class="boq-header">
                <div class="boq-code"><?= htmlspecialchars($boq['boq_code']) ?></div>
                <h2><?= htmlspecialchars($boq['project_name']) ?></h2>
                <p style="opacity:0.8;"><?= htmlspecialchars($boq['project_code']) ?></p>
            </div>

            <div class="info-grid">

                <!-- Project & Description -->
                <div class="info-card">
                    <h3><i class="fas fa-info-circle"></i> BOQ Information</h3>
                    <div class="detail-row">
                        <div class="label">System Description</div>
                        <div class="value"><?= nl2br(htmlspecialchars($boq['system_description'])) ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Quantity</div>
                        <div class="value"><?= number_format($boq['quantity'], 3) ?></div> 
                        
                    </div>
                    <div class="detail-row">
                        <div class="label">UOM</div>
                        <div><?= htmlspecialchars($boq['uom_name'] ?? '') ?></div>
                        
                    </div>
                    <div class="detail-row">
                        <div class="label">Type</div>
                        <div>
                            <span class="type-badge type-<?= htmlspecialchars($boq['type']) ?>">
                                <?= htmlspecialchars($boq['type']) ?>
                            </span>
                        </div>
                    </div>
                </div>

                <!-- Financial Breakdown -->
                <div class="info-card">
                    <h3><i class="fas fa-calculator"></i> Financial Details</h3>
                    <div class="detail-row">
                        <div class="label">Basic Amount</div>
                        <div class="value amount">₹ <?= number_format($boq['basic_amount'], 2) ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">GST Amount</div>
                        <div class="value amount">₹ <?= number_format($boq['gst_amount'], 2) ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">Grand Total</div>
                        <div class="value amount" style="font-size:1.5rem; color:#e0bbff;">
                            ₹ <?= number_format($boq['grand_total'], 2) ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Rate Details -->
            <div class="info-card" style="margin-top:20px;">
                <h3><i class="fas fa-list"></i> Rate Breakdown</h3>
                <?php if ($boq['type'] === 'SITC'): ?>
                    <div class="detail-row">
                        <div class="label">SITC Rate</div>
                        <div class="value">₹ <?= number_format($boq['sitc_rate'], 2) ?></div>
                    </div>
                <?php else: ?>
                    <div class="detail-row">
                        <div class="label">Supply Rate</div>
                        <div class="value">₹ <?= number_format($boq['supply_rate'], 2) ?></div>
                    </div>
                    <div class="detail-row">
                        <div class="label">ITC Rate</div>
                        <div class="value">₹ <?= number_format($boq['itc_rate'], 2) ?></div>
                    </div>
                <?php endif; ?>
            </div>

            <?php if (!empty($boq['remarks'])): ?>
            <div class="info-card" style="margin-top:20px;">
                <h3><i class="fas fa-comment-dots"></i> Remarks</h3>
                <p style="line-height:1.7; opacity:0.95;"><?= nl2br(htmlspecialchars($boq['remarks'])) ?></p>
            </div>
            <?php endif; ?>

            <!-- Action Buttons -->
            <div class="actions">
                <a href="boq-management.php" class="btn btn-back">
                    <i class="fas fa-arrow-left"></i> Back to List
                </a>
                <a href="edit_boq.php?id=<?= $boq_id ?>" class="btn btn-edit">
                    <i class="fas fa-edit"></i> Edit BOQ
                </a>
            </div>
        </div>
    </div>
</div>

</body>
</html>

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