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

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

$user_id   = $_SESSION['user_id'];
$full_name = $_SESSION['full_name'] ?? 'Project Manager';


$query = "
    SELECT 
        pm.user_id, p.id, p.project_code, p.project_name, p.client_name,
        p.start_date, p.end_date, p.project_status,
        p.project_budget
    FROM project_master p
    LEFT JOIN project_managers pm ON p.id = pm.project_id
    WHERE pm.user_id = ?
    ORDER BY 
        FIELD(project_status, 'Active', 'In Progress', 'Planning', 'On Hold', 'Completed'),
        project_name ASC
";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id);
$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>My Projects Dashboard</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        :root {
            --primary: #8b5cf6;
            --success: #22c55e;
            --warning: #f59e0b;
            --danger: #ef4444;
            --info: #60a5fa;
            --glass: rgba(255,255,255,0.07);
            --border: rgba(255,255,255,0.14);
        }
        body {
            font-family: 'Segoe UI', system-ui, sans-serif;
            background: linear-gradient(rgba(0,0,0,0.75), rgba(0,0,0,0.9)),
                        url('../background.jpeg') center/cover fixed;
            color: white;
            margin: 0;
            /* padding: 20px 15px; */
        }
        .container { max-width: 1200px; margin: 0 auto; }
        h1 { margin: 0 0 10px; font-size: 2.3rem; }
        .subtitle { opacity: 0.85; margin-bottom: 30px; }

        .projects-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
            gap: 20px;
        }
        .project-card {
            background: var(--glass);
            backdrop-filter: blur(12px);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 24px;
            cursor: pointer;
            transition: all 0.25s ease;
            box-shadow: 0 8px 25px rgba(0,0,0,0.35);
            text-decoration: none;
            color: inherit;
            display: block;
        }
        .project-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(139,92,246,0.25);
            border-color: var(--primary);
        }
        .project-code {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--primary);
            margin-bottom: 8px;
        }
        .project-name {
            font-size: 1.35rem;
            font-weight: 700;
            margin: 8px 0 12px;
        }
        .client-name {
            opacity: 0.8;
            margin-bottom: 16px;
        }
        .status-badge {
            display: inline-block;
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 0.9rem;
            font-weight: 600;
            margin-top: 12px;
        }
        .status-Active       { background: rgba(34,197,94,0.25); color: #86efac; }
        .status-In-Progress  { background: rgba(59,130,246,0.25); color: #93c5fd; }
        .status-Planning     { background: rgba(245,158,11,0.25); color: #fcd34d; }
        .status-On-Hold      { background: rgba(239,68,68,0.25); color: #fca5a5; }
        .status-Completed    { background: rgba(100,116,139,0.35); color: #cbd5e1; }

        .no-projects {
            text-align: center;
            padding: 100px 20px;
            opacity: 0.8;
        }
        .no-projects i { font-size: 5rem; margin-bottom: 20px; }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>
<div class="container">
    <h1>My Projects</h1>
    <div class="subtitle">Welcome, <?= htmlspecialchars($full_name) ?> • <?= date("d M Y") ?></div>

    <?php if ($result->num_rows > 0): ?>

        <div class="projects-grid">
            <?php while ($row = $result->fetch_assoc()): ?>
                <a href="project_dashboard.php?id=<?= $row['id'] ?>" class="project-card">
                    <div class="project-code"><?= htmlspecialchars($row['project_code']) ?></div>
                    <div class="project-name"><?= htmlspecialchars($row['project_name']) ?></div>
                    <div class="client-name"><?= htmlspecialchars($row['client_name'] ?: 'Client not specified') ?></div>

                    <div style="margin: 12px 0; opacity: 0.9;">
                        <i class="fas fa-calendar-alt" style="margin-right: 6px;"></i>
                        <?= $row['start_date'] ? date('d M y', strtotime($row['start_date'])) : '?' ?>
                        <?= $row['end_date'] ? ' → ' . date('d M y', strtotime($row['end_date'])) : '' ?>
                    </div>

                    <div class="status-badge status-<?= str_replace(' ', '-', $row['project_status']) ?>">
                        <?= htmlspecialchars($row['project_status']) ?>
                    </div>

                    <div style="margin-top: 16px; font-size: 0.95rem; opacity: 0.85;">
                        Budget: ₹ <?= number_format($row['project_budget'] ?: 0) ?>
                    </div>
                </a>
            <?php endwhile; ?>
        </div>

    <?php else: ?>

        <div class="no-projects">
            <i class="fas fa-folder-open"></i>
            <h2>No Projects Assigned</h2>
            <p>Please contact admin to assign projects to you.</p>
        </div>

    <?php endif; ?>
</div>

</body>
</html>

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