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

// Allowed Roles
$allowed_roles = ['store manager', 'admin', 'project manager'];
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], $allowed_roles)) {
    header("Location: ../login.php");
    exit();
}
$project_id = $_GET['project_id'] ?? '';
// Fetch All DPR Records
$dprs = $conn->query("
    SELECT d.id, d.report_date, d.created_at,
           p.project_code, p.project_name,
           u.full_name as created_by_name,
           COUNT(DISTINCT prog.id) as progress_count,
           COUNT(DISTINCT mat.id) as material_count
    FROM dpr_master d
    LEFT JOIN project_master p ON d.project_id = p.id
    LEFT JOIN users u ON d.created_by = u.id
    LEFT JOIN dpr_progress prog ON prog.dpr_id = d.id
    LEFT JOIN dpr_major_material mat ON mat.dpr_id = d.id
    GROUP BY d.id
    ORDER BY d.report_date DESC, d.created_at DESC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DPR List - Daily Progress Report</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: 25px 15px;
        }
        .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: 45px 40px;
            text-align: center;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        .header h1 { font-size: 2.4rem; font-weight: 700; }

        .form-area { padding: 40px; }

        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(0,0,0,0.25);
            border-radius: 16px;
            overflow: hidden;
        }
        th {
            background: rgba(168,85,247,0.3);
            padding: 16px 12px;
            text-align: left;
        }
        td {
            padding: 16px 12px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        tr:hover { background: rgba(168,85,247,0.1); }

        .btn {
            padding: 10px 18px;
            border-radius: 10px;
            text-decoration: none;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: 0.3s;
        }
        .btn-new {
            background: linear-gradient(135deg, var(--primary), var(--accent));
            color: white;
        }
        .btn-view {
            background: #22c55e;
            color: white;
        }
        .btn-view:hover { background: #16a34a; }

        .message {
            padding: 16px 24px;
            border-radius: 16px;
            margin-bottom: 25px;
        }
        .success { background: rgba(34,197,94,0.25); border:1px solid #4ade80; color:#86efac; }

        @media (max-width: 768px) {
            .header h1 { font-size: 2rem; }
            table { font-size: 0.95rem; }
        }
    </style>
</head>
<body>
<?php include('../config/header.php'); ?>
<div class="container">
    <div class="glass-container">
        <div class="header">
            <h1><i class="fas fa-list-check"></i> Daily Progress Reports (DPR)</h1>
            <a href="add_dpr.php?project_id=<?=$project_id?>" class="btn btn-new">
                <i class="fas fa-plus"></i> New DPR
            </a>
        </div>

        <div class="form-area">
            <?php if (isset($_GET['success'])): ?>
                <div class="message success">✅ DPR Created Successfully!</div>
            <?php endif; ?>

            <table>
                <thead>
                    <tr>
                        <th>Report Date</th>
                        <th>Project</th>
                        <th>Created By</th>
                        <th>Progress Items</th>
                        <th>Materials</th>
                        <th>Created At</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($dprs && $dprs->num_rows > 0): ?>
                        <?php while ($row = $dprs->fetch_assoc()): ?>
                            <tr>
                                <td><strong><?= date('d M Y', strtotime($row['report_date'])) ?></strong></td>
                                <td>
                                    <?= htmlspecialchars($row['project_code'] ?? 'N/A') ?> 
                                    <?= htmlspecialchars($row['project_name'] ?? 'No Project') ?>
                                </td>
                                <td><?= htmlspecialchars($row['created_by_name'] ?? 'Unknown') ?></td>
                                <td style="text-align:center;"><?= $row['progress_count'] ?></td>
                                <td style="text-align:center;"><?= $row['material_count'] ?></td>
                                <td><?= date('d M Y H:i', strtotime($row['created_at'])) ?></td>
                                <td>
                                    <a href="view_dpr.php?id=<?= $row['id'] ?>" class="btn btn-view">
                                        <i class="fas fa-eye"></i> View
                                    </a>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" style="text-align:center; padding:60px; color:#94a3b8;">
                                <i class="fas fa-folder-open" style="font-size:3rem; opacity:0.3; display:block; margin-bottom:15px;"></i>
                                No Daily Progress Reports found.<br>
                                <a href="add_dpr.php" style="color:var(--primary);">Create your first DPR</a>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

</body>
</html>

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