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

// Security check - only project manager
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';

$project_id = isset($_GET['project_id']) ? (int)$_GET['project_id'] : 0;
if ($project_id <= 0) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>No project selected</h2>");
}

// Verify project belongs to this manager
$check = $conn->prepare("
    SELECT project_code, project_name 
    FROM project_master
    LEFT JOIN project_managers ON project_master.id = project_managers.project_id 
    WHERE  project_managers.project_id = ? AND   project_managers.user_id = ?
");
$check->bind_param("ii", $project_id, $user_id);
$check->execute();
$project = $check->get_result()->fetch_assoc();

if (!$project) {
    die("<h2 style='color:#ef4444;text-align:center;padding:100px;'>Project not found or access denied</h2>");
}

// ───────────────────────────────────────────────
// DPR Stats

$today = date('Y-m-d');
$this_month = date('Y-m-01');

// 1. Today's DPR status
$dpr_today = $conn->query("
    SELECT id, status 
    FROM daily_progress_report 
    WHERE project_id = $project_id 
    AND report_date = '$today'
    LIMIT 1
")->fetch_assoc();
$dpr_today_status = $dpr_today ? $dpr_today['status'] : 'Not submitted';
$dpr_today_id = $dpr_today ? $dpr_today['id'] : 0;

// 2. Total DPRs this month
$total_this_month = $conn->query("
    SELECT COUNT(*) AS count 
    FROM daily_progress_report 
    WHERE project_id = $project_id 
    AND report_date >= '$this_month'
")->fetch_assoc()['count'] ?? 0;

// 3. Pending DPRs (not submitted/approved today or overdue - simple example)
$pending_dpr = $conn->query("
    SELECT COUNT(*) AS count 
    FROM daily_progress_report 
    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>DPR Dashboard - <?= htmlspecialchars($project['project_name']) ?></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;
            --glass: rgba(255,255,255,0.08);
            --border: rgba(255,255,255,0.15);
        }
        body {
            font-family: 'Segoe UI', system-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 15px;
        }
        .container { max-width: 1100px; margin: 0 auto; }
        .back {
            color: #c4b5fd;
            text-decoration: none;
            font-weight: 500;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            margin-bottom: 25px;
        }
        .back:hover { color: white; }

        h1 { margin: 0 0 8px; font-size: 2.2rem; }
        .project-info { opacity: 0.9; margin-bottom: 30px; font-size: 1.1rem; }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
            gap: 20px;
            margin: 30px 0;
        }
        .stat-card {
            background: var(--glass);
            backdrop-filter: blur(12px);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 24px;
            text-align: center;
            transition: all 0.25s;
        }
        .stat-card:hover {
            transform: translateY(-6px);
            box-shadow: 0 15px 35px rgba(0,0,0,0.4);
        }
        .stat-icon { font-size: 2.6rem; margin-bottom: 12px; }
        .stat-value { font-size: 2.5rem; font-weight: 700; margin: 8px 0; }
        .stat-label { font-size: 1.05rem; opacity: 0.85; }

        .actions-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
            gap: 20px;
            margin: 50px 0 30px;
        }
        .action-card {
            background: rgba(30,30,50,0.7);
            border: 1px solid var(--border);
            border-radius: 14px;
            padding: 24px 20px;
            text-align: center;
            text-decoration: none;
            color: white;
            transition: all 0.3s;
        }
        .action-card:hover {
            background: rgba(139,92,246,0.18);
            transform: translateY(-5px);
            border-color: var(--primary);
        }
        .action-icon { font-size: 2.8rem; margin-bottom: 14px; color: var(--primary); }
        .action-title { font-size: 1.2rem; font-weight: 600; }

        .status-today {
            font-size: 1.3rem;
            font-weight: 600;
            margin: 15px 0;
            padding: 12px;
            border-radius: 12px;
        }
        .status-submitted { background: rgba(34,197,94,0.25); color: #86efac; }
        .status-pending   { background: rgba(245,158,11,0.25); color: #fcd34d; }
        .status-missing   { background: rgba(239,68,68,0.25); color: #fca5a5; }
    </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 Project Dashboard
    </a>

    <h1>DPR Management</h1>
    <div class="project-info">
        Project: <strong><?= htmlspecialchars($project['project_name']) ?></strong> 
        (<?= htmlspecialchars($project['project_code']) ?>)
    </div>

    <!-- Today's DPR Status -->
    <div class="stats-grid" style="margin-bottom:50px;">
        <div class="stat-card">
            <div class="stat-icon" style="color: var(--warning);"><i class="fas fa-calendar-day"></i></div>
            <div class="stat-value"><?= date('d M Y') ?></div>
            <div class="stat-label">Today's Date</div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="color: 
                <?php 
                    if ($dpr_today_status === 'Submitted' || $dpr_today_status === 'Approved') echo 'var(--success)';
                    elseif ($dpr_today_status === 'Pending') echo 'var(--warning)';
                    else echo 'var(--danger)';
                ?>">
                <i class="fas fa-file-alt"></i>
            </div>
            <div class="stat-value"><?= htmlspecialchars($dpr_today_status) ?></div>
            <div class="stat-label">Today's DPR Status</div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="color: var(--info);"><i class="fas fa-list"></i></div>
            <div class="stat-value"><?= $total_this_month ?></div>
            <div class="stat-label">DPRs This Month</div>
        </div>

        <div class="stat-card">
            <div class="stat-icon" style="color: var(--danger);"><i class="fas fa-exclamation-triangle"></i></div>
            <div class="stat-value"><?= $pending_dpr ?></div>
            <div class="stat-label">Pending DPRs</div>
        </div>
    </div>

    <!-- Today's DPR Quick Status -->
    <div style="text-align:center; margin: 40px 0;">
        <div class="status-today 
            <?php 
                if ($dpr_today_status === 'Submitted' || $dpr_today_status === 'Approved') echo 'status-submitted';
                elseif ($dpr_today_status === 'Pending') echo 'status-pending';
                else echo 'status-missing';
            ?>">
            <?= $dpr_today_status === 'Not submitted' 
                ? 'No DPR submitted today yet' 
                : "Today's DPR: " . htmlspecialchars($dpr_today_status) ?>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="actions-grid">
        <?php if ($dpr_today_status === 'Not submitted'): ?>
            <a href="dpr_add.php?project_id=<?= $project_id ?>" class="action-card">
                <div class="action-icon"><i class="fas fa-plus-circle"></i></div>
                <div class="action-title">Add Today's DPR</div>
            </a>
        <?php elseif ($dpr_today_status === 'Pending'): ?>
            <a href="dpr_edit.php?id=<?= $dpr_today_id ?>" class="action-card">
                <div class="action-icon"><i class="fas fa-edit"></i></div>
                <div class="action-title">Edit Today's DPR</div>
            </a>
        <?php endif; ?>

        <a href="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="dpr_add.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-calendar-plus"></i></div>
            <div class="action-title">Add DPR for Other Date</div>
        </a>

        <!-- Optional future actions -->
        <a href="dpr_analytics.php?project_id=<?= $project_id ?>" class="action-card">
            <div class="action-icon"><i class="fas fa-chart-line"></i></div>
            <div class="action-title">DPR Analytics</div>
        </a>
    </div>
</div>

</body>
</html>

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