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

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

$user_id = $_SESSION['user_id'];
$project_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($project_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Invalid Project ID</h2>");
}

// Fetch Project Details
$stmt = $conn->prepare("
    SELECT p.*, pm.user_id 
    FROM project_master p
    LEFT JOIN project_managers pm ON p.id = pm.project_id
    WHERE p.id = ? AND pm.user_id = ?
");
$stmt->bind_param("ii", $project_id, $user_id);
$stmt->execute();
$project = $stmt->get_result()->fetch_assoc();

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

// ───── Quick Stats ─────
$today = date('Y-m-d');

// Stock Value
$stock_value = $conn->query("SELECT COALESCE(SUM(current_stock * avg_rate), 0) AS total 
    FROM project_inventory WHERE project_id = $project_id")->fetch_assoc()['total'] ?? 0;

// Today's DPR
$dpr_today = $conn->query("SELECT * FROM dpr_master 
    WHERE project_id = $project_id AND report_date = '$today'")->fetch_assoc();
    
// Budget Remaining (Simple Calculation)
$budget_used = $conn->query("SELECT COALESCE(SUM(quantity_used * rate), 0) AS used 
    FROM material_consumption WHERE project_id = $project_id")->fetch_assoc()['used'] ?? 0;
$budget_remaining = max(0, $project['project_budget'] - $budget_used);

// Pending PRs
$pending_pr = $conn->query("SELECT COUNT(*) AS count FROM procurement_request 
    WHERE project_id = $project_id AND status = 'Pending'")->fetch_assoc()['count'] ?? 0;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($project['project_name']) ?> - Dashboard</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;
            --success: #22c55e;
            --warning: #eab308;
            --danger: #ef4444;
            --glass: rgba(255,255,255,0.09);
            --glass-border: rgba(255,255,255,0.18);
        }
        * { 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.85), rgba(0,0,0,0.95)), 
                        url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            min-height: 100vh;
            padding: 20px 15px;
        }
        .container { max-width: 1280px; margin: 0 auto; }

        .header {
            background: linear-gradient(90deg, rgba(168,85,247,0.25), rgba(192,132,252,0.15));
            backdrop-filter: blur(20px);
            border: 1px solid var(--glass-border);
            border-radius: 24px;
            padding: 35px 40px;
            margin-bottom: 30px;
            text-align: center;
        }
        .project-code {
            color: var(--accent);
            font-size: 1.35rem;
            font-weight: 600;
            margin-bottom: 8px;
        }
        h1 {
            font-size: clamp(1.8rem, 5vw, 2.8rem);
            font-weight: 700;
            margin-bottom: 10px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
            gap: 22px;
            margin: 30px 0;
        }
        .stat-card {
            background: var(--glass);
            border: 1px solid var(--glass-border);
            border-radius: 20px;
            padding: 28px 20px;
            text-align: center;
            transition: all 0.4s ease;
            backdrop-filter: blur(16px);
        }
        .stat-card:hover {
            transform: translateY(-12px) scale(1.03);
            box-shadow: 0 20px 40px rgba(168,85,247,0.25);
        }
        .stat-icon {
            font-size: 3rem;
            margin-bottom: 15px;
            display: block;
        }
        .stat-value {
            font-size: 2.4rem;
            font-weight: 800;
            margin: 8px 0;
        }
        .stat-label {
            font-size: 1.05rem;
            opacity: 0.85;
        }

        .actions-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin-top: 50px;
        }
        .action-card {
            background: rgba(30,30,50,0.7);
            border: 1px solid rgba(168,85,247,0.3);
            border-radius: 20px;
            padding: 32px 20px;
            text-align: center;
            text-decoration: none;
            color: white;
            transition: all 0.35s ease;
        }
        .action-card:hover {
            background: rgba(168,85,247,0.15);
            border-color: var(--accent);
            transform: translateY(-8px);
        }
        .action-icon {
            font-size: 3.2rem;
            margin-bottom: 18px;
            color: var(--primary);
        }
        .action-title {
            font-size: 1.25rem;
            font-weight: 600;
        }

        .status-badge {
            padding: 6px 16px;
            border-radius: 30px;
            font-weight: 600;
            display: inline-block;
            margin-top: 10px;
        }

        @media (max-width: 768px) {
            .header { padding: 25px 20px; }
            .stats-grid { grid-template-columns: 1fr 1fr; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>
<div class="container">
    <a href="dashboard_project_manager.php" class="back-btn" style="color:#c4b5fd; text-decoration:none; display:inline-flex; align-items:center; gap:8px; margin-bottom:20px;">
        <i class="fas fa-arrow-left"></i> Back to My Projects
    </a>

    <div class="header">
        <div class="project-code"><?= htmlspecialchars($project['project_code']) ?></div>
        <h1><?= htmlspecialchars($project['project_name']) ?></h1>
        <p style="opacity:0.9; font-size:1.1rem;">
            <?= htmlspecialchars($project['client_name'] ?: 'No Client') ?> • 
            <?= date('d M Y', strtotime($project['start_date'])) ?> — 
            <?= date('d M Y', strtotime($project['end_date'])) ?>
        </p>
        <span class="status-badge" style="background:<?= $project['project_status']=='Active' ? '#22c55e33' : '#eab30833' ?>; color:<?= $project['project_status']=='Active' ? '#86efac' : '#fcd34d' ?>;">
            <?= htmlspecialchars($project['project_status']) ?>
        </span>
    </div>

    <!-- Stats -->
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon" style="color:#60a5fa;"><i class="fas fa-boxes-stacked"></i></div>
            <div class="stat-value">₹ <?= number_format($stock_value, 0) ?></div>
            <div class="stat-label">Current Stock Value</div>
        </div>

        <div class="stat-card">
    <div class="stat-icon" style="color:#eab308;">
        <i class="fas fa-file-alt"></i>
    </div>
    <div class="stat-value">
        <?= $dpr_today ? 'Submitted ✅' : 'Pending ❌' ?>
    </div>
    <div class="stat-label">Today's DPR</div>
</div>

        <div class="stat-card">
            <div class="stat-icon" style="color:#22c55e;"><i class="fas fa-rupee-sign"></i></div>
            <div class="stat-value">₹ <?= number_format($project['project_budget'] ?? 0) ?></div>
            <div class="stat-label">Total Budget</div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="color:#f87171;"><i class="fas fa-wallet"></i></div>
            <div class="stat-value">₹ <?= number_format($budget_remaining) ?></div>
            <div class="stat-label">Budget Remaining</div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="color:#ef4444;"><i class="fas fa-file-import"></i></div>
            <div class="stat-value"><?= $pending_pr ?></div>
            <div class="stat-label">Pending PRs</div>
        </div>
    </div>

    <!-- Quick Actions -->
    <h2 style="text-align:center; margin:50px 0 30px; font-size:2rem; color:#e0e7ff;">
        Quick Actions
    </h2>

    <div class="actions-grid">
       
        <a href="../daily_progress_report/dpr_list.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-list-check"></i></div>
            <div class="action-title">View All DPRs</div>
        </a>

        <a href="../project-inventory/project_inventory.php?id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-warehouse"></i></div>
            <div class="action-title">Project Inventory</div>
        </a>

        <a href="../pr/pr_create.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-file-import"></i></div>
            <div class="action-title">Raise Procurement Request</div>
        </a>
        <a href="../pr/pr_list.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-list"></i></div>
            <div class="action-title">My PR List</div>
        </a>
        

        <a href="../grn/grn_list.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-truck-loading"></i></div>
            <div class="action-title">Goods Received Notes</div>
        </a>
        <a href="../srn/srn_list.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-truck-loading"></i></div>
            <div class="action-title">Supply Return Notes</div>
        </a>
        <a href="../project_delivery_chalan/project-dc-list.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-truck"></i></div>
            <div class="action-title">Delivery Challans</div>
        </a>

        <!-- Optional future links -->
        <a href="../site_photos_documents/site_photos_documents.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-images"></i></div>
            <div class="action-title">Site Photos / Documents</div>
        </a>
    </div>
</div>

</body>
</html>

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