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

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

// Handle Delete
$success = $error = "";
if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $stmt = $conn->prepare("DELETE FROM boq_master WHERE id = ?");
    $stmt->bind_param("i", $id);
    if ($stmt->execute()) {
        $success = "BOQ deleted successfully.";
    } else {
        $error = "Error deleting BOQ.";
    }
}

// Search & Filter
$search = trim($_GET['search'] ?? '');
$type   = $_GET['type'] ?? '';

$where = " WHERE 1=1";
$params = [];
$types = "";

if (!empty($search)) {
    $where .= " AND (bm.boq_code LIKE ? OR p.project_name LIKE ? OR bm.system_description LIKE ?)";
    $like = "%$search%";
    $params = [$like, $like, $like];
    $types = "sss";
}

if (!empty($type)) {
    $where .= " AND bm.type = ?";
    $params[] = $type;
    $types .= "s";
}

$query = "
    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
    ORDER BY bm.created_at DESC
";

$stmt = $conn->prepare($query);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BOQ Management</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: 1350px; 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, 5vw, 42px);
            font-weight: 700;
        }

        .top-bar {
            padding: 25px 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
            background: rgba(255,255,255,0.05);
        }

        .search-box { flex: 1; min-width: 300px; }
        .search-box input {
            width: 100%;
            padding: 14px 20px;
            background: rgba(255,255,255,0.09);
            border: 1px solid rgba(255,255,255,0.25);
            border-radius: 16px;
            color: white;
        }

        .add-btn {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
            padding: 14px 28px;
            border-radius: 16px;
            text-decoration: none;
            font-weight: 700;
            display: inline-flex;
            align-items: center;
            gap: 10px;
        }

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

        tr:hover {
            background: rgba(255,255,255,0.08);
        }

        .amount {
            font-weight: 600;
            color: #c4b5fd;
        }

        .type-badge {
            padding: 6px 14px;
            border-radius: 50px;
            font-size: 0.85rem;
            font-weight: 700;
        }
        .type-SITC { background: #8b5cf6; color: white; }
        .type-S+ITC { background: #ec4899; color: white; }
        .action-btns { display: flex; }
        
        .action-btns a {
            margin: 0 6px;
            padding: 8px 14px;
            border-radius: 10px;
            text-decoration: none;
            font-size: 0.95rem;
        }
        .btn-view  { background: rgba(59,130,246,0.2); color: #60a5fa; }
        .btn-edit  { background: rgba(234,179,8,0.2); color: #fde047; }
        .btn-delete{ background: rgba(239,68,68,0.2); color: #f87171; }


        .no-records {
            text-align: center;
            padding: 100px 20px;
            color: #94a3b8;
        }

        @media (max-width: 768px) {
            .top-bar { flex-direction: column; align-items: stretch; }
            .glass-container { border-radius: 24px; }
        }
    </style>
</head>
<body>

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

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

        <div class="header">
            <h1><i class="fas fa-file-contract"></i> BOQ Management</h1>
            <p style="margin-top:12px; opacity:0.9;">Bill of Quantity Entries</p>
        </div>

        <div class="top-bar">
            <div class="search-box">
                <form method="GET">
                    <input type="text" name="search" 
                           placeholder="Search BOQ Code, Project or Description..." 
                           value="<?= htmlspecialchars($search) ?>">
                </form>
            </div>

            <select name="type" onchange="this.form.submit()" 
                    style="padding:14px 18px; background:rgba(255,255,255,0.09); border:1px solid rgba(255,255,255,0.25); border-radius:16px; color:white;">
                <option value="">All Types</option>
                <option value="SITC" <?= $type === 'SITC' ? 'selected' : '' ?>>SITC</option>
                <option value="S+ITC" <?= $type === 'S+ITC' ? 'selected' : '' ?>>S + ITC</option>
            </select>

            <a href="add_boq.php" class="add-btn">
                <i class="fas fa-plus"></i> New BOQ
            </a>
        </div>

        <?php if ($success): ?>
            <div style="margin:20px 40px; padding:16px 24px; background:rgba(34,197,94,0.25); border:1px solid #4ade80; color:#86efac; border-radius:16px;">
                <?= htmlspecialchars($success) ?>
            </div>
        <?php endif; ?>

        <div style="padding:0 40px 40px;">
            <?php if ($result->num_rows > 0): ?>
                <table>
                    <thead>
                        <tr>
                            <th>BOQ Code</th>
                            <th>Project</th>
                            <th>System Description</th>
                            <th>Quantity</th>
                            <th>UOM</th>
                            <th>Basic Amount</th>
                            <th>GST Amount</th>
                            <th>Grand Total</th>
                            <th style="text-align:center; width:140px;">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td><strong><?= htmlspecialchars($row['boq_code']) ?></strong></td>
                            <td><?= htmlspecialchars($row['project_code']) ?> - <?= htmlspecialchars($row['project_name']) ?></td>
                            <td><?= htmlspecialchars($row['system_description']) ?></td>
                            <td><?= number_format($row['quantity'], 3) ?></td> 
                            <td><?= htmlspecialchars($row['uom_name']) ?></td>
                           
                            <td class="amount">₹ <?= number_format($row['basic_amount'], 2) ?></td>
                            <td class="amount">₹ <?= number_format($row['gst_amount'], 2) ?></td>
                            <td class="amount" style="font-weight:700; color:#c4b5fd;">
                                ₹ <?= number_format($row['grand_total'], 2) ?>
                            </td>
                            <td class="action-btns" style="text-align:center;">
                                <a href="view_boq.php?id=<?= $row['id'] ?>" title="View" class="btn-view"><i class="fas fa-eye"></i></a>
                                <a href="edit_boq.php?id=<?= $row['id'] ?>" title="Edit" class="btn-edit"><i class="fas fa-edit"></i></a>
                                <a href="?delete=<?= $row['id'] ?>" 
                                   onclick="return confirm('Are you sure you want to delete this BOQ?')" 
                                   title="Delete" class="btn-delete">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="no-records">
                    <i class="fas fa-file-contract" style="font-size:5rem; opacity:0.3; margin-bottom:20px;"></i>
                    <h3>No BOQ Records Found</h3>
                    <p>Create your first BOQ entry.</p>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

</body>
</html>

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