<?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 = (int)($_GET['project_id'] ?? 0);

$where = "WHERE d.project_manager_id = ?";
$params = [$user_id];
$types = "i";

if ($project_id > 0) {
    $where .= " AND d.project_id = ?";
    $params[] = $project_id;
    $types .= "i";
}

$query = "
    SELECT d.id, d.report_date, d.status, d.work_description,
           p.project_code, p.project_name
    FROM daily_progress_report d
    JOIN project_master p ON d.project_id = p.id
    $where
    ORDER BY d.report_date DESC
    LIMIT 100
";
$stmt = $conn->prepare($query);
$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>DPR List</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <style>
        /* Paste same CSS as previous pages */
               :root {
            --primary: #8b5cf6;
            --success: #22c55e;
            --danger: #ef4444;
            --glass: rgba(255,255,255,0.08);
            --border: rgba(255,255,255,0.15);
        }
        body {
            font-family: 'Segoe UI', sans-serif;
            background: linear-gradient(rgba(0,0,0,0.8), rgba(0,0,0,0.95)),
                        url('../background.jpeg') center/cover fixed;
            color: white;
            margin: 0;
            padding: 20px;
        }
        .container { max-width: 900px; margin: 0 auto; }
        .back { color: #c4b5fd; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; margin-bottom: 20px; }
        .back:hover { color: white; }
        h1 { margin: 0 0 10px; }
        .form-area {
            background: var(--glass);
            backdrop-filter: blur(12px);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.4);
        }
        .form-group { margin-bottom: 24px; }
        label { display: block; margin-bottom: 8px; font-weight: 600; }
        input, select, textarea {
            width: 100%;
            padding: 14px;
            background: rgba(255,255,255,0.08);
            border: 1px solid rgba(255,255,255,0.2);
            border-radius: 10px;
            color: white;
        }
        textarea { min-height: 140px; }
        .submit-btn {
            background: linear-gradient(135deg, var(--primary), #a78bfa);
            color: white;
            border: none;
            padding: 16px;
            border-radius: 12px;
            font-size: 1.1rem;
            font-weight: 600;
            cursor: pointer;
            width: 100%;
        }
        .message { padding: 16px; border-radius: 10px; margin-bottom: 20px; }
        .success { background: rgba(34,197,94,0.25); color: #86efac; }
        .error { background: rgba(239,68,68,0.25); color: #fca5a5; }
        .status-Pending { background:#f59e0b66; }
        .status-Submitted { background:#22c55e66; }
        .status-Approved { background:#16a34a66; }
    </style>
</head>
<body>
    <div class="container">
        <a href="project_dashboard.php?id=<?= $project_id ?>" class="back">
            <i class="fas fa-arrow-left"></i> Back to Dashboard
        </a>

        <h1>DPR List</h1>

        <?php if ($result->num_rows > 0): ?>
            <table style="width:100%;border-collapse:collapse;background:var(--glass);">
                <thead>
                    <tr style="background:rgba(255,255,255,0.1);">
                        <th>Date</th>
                        <th>Project</th>
                        <th>Status</th>
                        <th>Work Summary</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php while ($row = $result->fetch_assoc()): ?>
                    <tr style="border-bottom:1px solid var(--border);">
                        <td><?= date('d M Y', strtotime($row['report_date'])) ?></td>
                        <td><?= htmlspecialchars($row['project_code']) ?> - <?= htmlspecialchars($row['project_name']) ?></td>
                        <td><span class="status status-<?= $row['status'] ?>"><?= htmlspecialchars($row['status']) ?></span></td>
                        <td style="max-width:300px;"><?= htmlspecialchars(substr($row['work_description'], 0, 100)) ?>...</td>
                        <td>
                            <a href="dpr_view.php?id=<?= $row['id'] ?>" style="color:#60a5fa;margin-right:12px;">
                                <i class="fas fa-eye"></i> View
                            </a>
                            <?php if ($row['status'] === 'Pending'): ?>
                                <a href="dpr_edit.php?id=<?= $row['id'] ?>" style="color:#f59e0b;">
                                    <i class="fas fa-edit"></i> Edit
                                </a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endwhile; ?>
                </tbody>
            </table>
        <?php else: ?>
            <div style="text-align:center;padding:80px;">
                <i class="fas fa-folder-open" style="font-size:4rem;opacity:0.7;"></i><br><br>
                No DPRs found for this project yet.
            </div>
        <?php endif; ?>
    </div>
</body>
</html>